@@ -612,8 +612,10 @@ def test_new_repr_complex(self):
612
612
self .assertEqual (repr (typing .Mapping [T , TS ][TS , T ]), 'typing.Mapping[~TS, ~T]' )
613
613
self .assertEqual (repr (List [Tuple [T , TS ]][int , T ]),
614
614
'typing.List[typing.Tuple[int, ~T]]' )
615
- self .assertEqual (repr (List [Tuple [T , T ]][List [int ]]),
616
- 'typing.List[typing.Tuple[typing.List[int], typing.List[int]]]' )
615
+ self .assertEqual (
616
+ repr (List [Tuple [T , T ]][List [int ]]),
617
+ 'typing.List[typing.Tuple[typing.List[int], typing.List[int]]]'
618
+ )
617
619
618
620
def test_new_repr_bare (self ):
619
621
T = TypeVar ('T' )
@@ -684,8 +686,10 @@ def naive_dict_check(obj, tp):
684
686
raise NotImplementedError
685
687
if tp .__args__ :
686
688
KT , VT = tp .__args__
687
- return all (isinstance (k , KT ) and isinstance (v , VT )
688
- for k , v in obj .items ())
689
+ return all (
690
+ isinstance (k , KT ) and isinstance (v , VT )
691
+ for k , v in obj .items ()
692
+ )
689
693
self .assertTrue (naive_dict_check ({'x' : 1 }, typing .Dict [str , int ]))
690
694
self .assertFalse (naive_dict_check ({1 : 'x' }, typing .Dict [str , int ]))
691
695
with self .assertRaises (NotImplementedError ):
@@ -1409,6 +1413,16 @@ class CoolEmployee(NamedTuple):
1409
1413
class CoolEmployeeWithDefault(NamedTuple):
1410
1414
name: str
1411
1415
cool: int = 0
1416
+
1417
+ class XMeth(NamedTuple):
1418
+ x: int
1419
+ def double(self):
1420
+ return 2 * self.x
1421
+
1422
+ class XMethBad(NamedTuple):
1423
+ x: int
1424
+ def _fields(self):
1425
+ return 'no chance for this'
1412
1426
"""
1413
1427
1414
1428
if PY36 :
@@ -1417,6 +1431,7 @@ class CoolEmployeeWithDefault(NamedTuple):
1417
1431
# fake names for the sake of static analysis
1418
1432
ann_module = ann_module2 = ann_module3 = None
1419
1433
A = B = CSub = G = CoolEmployee = CoolEmployeeWithDefault = object
1434
+ XMeth = XMethBad = object
1420
1435
1421
1436
gth = get_type_hints
1422
1437
@@ -1750,7 +1765,7 @@ def test_no_generator_instantiation(self):
1750
1765
def test_async_generator (self ):
1751
1766
ns = {}
1752
1767
exec ("async def f():\n "
1753
- " yield 42\n " , globals (), ns )
1768
+ " yield 42\n " , globals (), ns )
1754
1769
g = ns ['f' ]()
1755
1770
self .assertIsSubclass (type (g ), typing .AsyncGenerator )
1756
1771
@@ -2038,6 +2053,13 @@ class NonDefaultAfterDefault(NamedTuple):
2038
2053
y: int
2039
2054
""" )
2040
2055
2056
+ @skipUnless (PY36 , 'Python 3.6 required' )
2057
+ def test_annotation_usage_with_methods (self ):
2058
+ self .assertEquals (XMeth (1 ).double (), 2 )
2059
+ self .assertEquals (XMeth (42 ).x , XMeth (42 )[0 ])
2060
+ self .assertEquals (XMethBad (1 )._fields , ('x' ,))
2061
+ self .assertEquals (XMethBad (1 ).__annotations__ , {'x' : int })
2062
+
2041
2063
@skipUnless (PY36 , 'Python 3.6 required' )
2042
2064
def test_namedtuple_keyword_usage (self ):
2043
2065
LocalEmployee = NamedTuple ("LocalEmployee" , name = str , age = int )
0 commit comments