@@ -1310,6 +1310,74 @@ class D(C):
13101310 with self .assertRaises (Exception ):
13111311 D [T ]
13121312
1313+ def test_new_with_args (self ):
1314+
1315+ class A (Generic [T ]):
1316+ pass
1317+
1318+ class B :
1319+ def __new__ (cls , arg ):
1320+ # call object
1321+ obj = super ().__new__ (cls )
1322+ obj .arg = arg
1323+ return obj
1324+
1325+ # mro: C, A, Generic, B, object
1326+ class C (A , B ):
1327+ pass
1328+
1329+ c = C ('foo' )
1330+ self .assertEqual (c .arg , 'foo' )
1331+
1332+ def test_new_with_args2 (self ):
1333+
1334+ class A :
1335+ def __init__ (self , arg ):
1336+ self .from_a = arg
1337+ # call object
1338+ super ().__init__ ()
1339+
1340+ # mro: C, Generic, A, object
1341+ class C (Generic [T ], A ):
1342+ def __init__ (self , arg ):
1343+ self .from_c = arg
1344+ # call Generic
1345+ super ().__init__ (arg )
1346+
1347+ c = C ('foo' )
1348+ self .assertEqual (c .from_a , 'foo' )
1349+ self .assertEqual (c .from_c , 'foo' )
1350+
1351+ def test_new_no_args (self ):
1352+
1353+ class A (Generic [T ]):
1354+ pass
1355+
1356+ with self .assertRaises (TypeError ):
1357+ A ('foo' )
1358+
1359+ class B :
1360+ def __new__ (cls ):
1361+ # call object
1362+ obj = super ().__new__ (cls )
1363+ obj .from_b = 'b'
1364+ return obj
1365+
1366+ # mro: C, A, Generic, B, object
1367+ class C (A , B ):
1368+ def __init__ (self , arg ):
1369+ self .arg = arg
1370+
1371+ def __new__ (cls , arg ):
1372+ # call A
1373+ obj = super ().__new__ (cls )
1374+ obj .from_c = 'c'
1375+ return obj
1376+
1377+ c = C ('foo' )
1378+ self .assertEqual (c .arg , 'foo' )
1379+ self .assertEqual (c .from_b , 'b' )
1380+ self .assertEqual (c .from_c , 'c' )
13131381
13141382class ClassVarTests (BaseTestCase ):
13151383
@@ -1739,6 +1807,8 @@ def test_get_type_hints_classes(self):
17391807 self .assertEqual (gth (HasForeignBaseClass ),
17401808 {'some_xrepr' : XRepr , 'other_a' : mod_generics_cache .A ,
17411809 'some_b' : mod_generics_cache .B })
1810+ self .assertEqual (gth (XRepr .__new__ ),
1811+ {'x' : int , 'y' : int })
17421812 self .assertEqual (gth (mod_generics_cache .B ),
17431813 {'my_inner_a1' : mod_generics_cache .B .A ,
17441814 'my_inner_a2' : mod_generics_cache .B .A ,
0 commit comments