@@ -1352,6 +1352,58 @@ def test_nested_tuple(self):
13521352 "argument 1 must be sequence of length 1, not 0" ):
13531353 parse (((),), {}, '(' + f + ')' , ['a' ])
13541354
1355+ def test_specific_type_errors (self ):
1356+ parse = _testcapi .parse_tuple_and_keywords
1357+
1358+ def check (format , arg , expected , got = 'list' ):
1359+ errmsg = f'must be { expected } , not { got } '
1360+ with self .assertRaisesRegex (TypeError , errmsg ):
1361+ parse ((arg ,), {}, format , ['a' ])
1362+
1363+ check ('k' , [], 'int' )
1364+ check ('?k' , [], 'int or None' )
1365+ check ('K' , [], 'int' )
1366+ check ('?K' , [], 'int or None' )
1367+ check ('c' , [], 'a byte string of length 1' )
1368+ check ('?c' , [], 'a byte string of length 1 or None' )
1369+ check ('c' , b'abc' , 'a byte string of length 1' ,
1370+ 'a bytes object of length 3' )
1371+ check ('?c' , b'abc' , 'a byte string of length 1 or None' ,
1372+ 'a bytes object of length 3' )
1373+ check ('c' , bytearray (b'abc' ), 'a byte string of length 1' ,
1374+ 'a bytearray object of length 3' )
1375+ check ('?c' , bytearray (b'abc' ), 'a byte string of length 1 or None' ,
1376+ 'a bytearray object of length 3' )
1377+ check ('C' , [], 'a unicode character' )
1378+ check ('?C' , [], 'a unicode character or None' )
1379+ check ('C' , 'abc' , 'a unicode character' ,
1380+ 'a string of length 3' )
1381+ check ('?C' , 'abc' , 'a unicode character or None' ,
1382+ 'a string of length 3' )
1383+ check ('s' , [], 'str' )
1384+ check ('?s' , [], 'str or None' )
1385+ check ('z' , [], 'str or None' )
1386+ check ('?z' , [], 'str or None' )
1387+ check ('es' , [], 'str' )
1388+ check ('?es' , [], 'str or None' )
1389+ check ('es#' , [], 'str' )
1390+ check ('?es#' , [], 'str or None' )
1391+ check ('et' , [], 'str, bytes or bytearray' )
1392+ check ('?et' , [], 'str, bytes or bytearray or None' )
1393+ check ('et#' , [], 'str, bytes or bytearray' )
1394+ check ('?et#' , [], 'str, bytes or bytearray or None' )
1395+ check ('w*' , [], 'read-write bytes-like object' )
1396+ check ('?w*' , [], 'read-write bytes-like object or None' )
1397+ check ('S' , [], 'bytes' )
1398+ check ('?S' , [], 'bytes or None' )
1399+ check ('U' , [], 'str' )
1400+ check ('?U' , [], 'str or None' )
1401+ check ('Y' , [], 'bytearray' )
1402+ check ('?Y' , [], 'bytearray or None' )
1403+ check ('(OO)' , 42 , '2-item sequence' , 'int' )
1404+ check ('?(OO)' , 42 , '2-item sequence or None' , 'int' )
1405+ check ('(OO)' , (1 , 2 , 3 ), 'sequence of length 2' , '3' )
1406+
13551407 def test_nullable (self ):
13561408 parse = _testcapi .parse_tuple_and_keywords
13571409
0 commit comments