bpo-28556: Minor fixes for typing module (GH-6732)

This also fixes https://bugs.python.org/issue33420
This commit is contained in:
Ivan Levkivskyi 2018-05-09 02:23:46 +01:00 committed by GitHub
parent 0904f766e1
commit 43d12a6bd8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 79 additions and 2 deletions

View file

@ -1324,6 +1324,72 @@ class GenericTests(BaseTestCase):
with self.assertRaises(Exception):
D[T]
def test_new_with_args(self):
class A(Generic[T]):
pass
class B:
def __new__(cls, arg):
# call object
obj = super().__new__(cls)
obj.arg = arg
return obj
# mro: C, A, Generic, B, object
class C(A, B):
pass
c = C('foo')
self.assertEqual(c.arg, 'foo')
def test_new_with_args2(self):
class A:
def __init__(self, arg):
self.from_a = arg
# call object
super().__init__()
# mro: C, Generic, A, object
class C(Generic[T], A):
def __init__(self, arg):
self.from_c = arg
# call Generic
super().__init__(arg)
c = C('foo')
self.assertEqual(c.from_a, 'foo')
self.assertEqual(c.from_c, 'foo')
def test_new_no_args(self):
class A(Generic[T]):
pass
class B:
def __new__(cls):
# call object
obj = super().__new__(cls)
obj.from_b = 'b'
return obj
# mro: C, A, Generic, B, object
class C(A, B):
def __init__(self, arg):
self.arg = arg
def __new__(cls, arg):
# call A
obj = super().__new__(cls)
obj.from_c = 'c'
return obj
c = C('foo')
self.assertEqual(c.arg, 'foo')
self.assertEqual(c.from_b, 'b')
self.assertEqual(c.from_c, 'c')
class ClassVarTests(BaseTestCase):
@ -1737,6 +1803,8 @@ class GetTypeHintTests(BaseTestCase):
self.assertEqual(gth(HasForeignBaseClass),
{'some_xrepr': XRepr, 'other_a': mod_generics_cache.A,
'some_b': mod_generics_cache.B})
self.assertEqual(gth(XRepr.__new__),
{'x': int, 'y': int})
self.assertEqual(gth(mod_generics_cache.B),
{'my_inner_a1': mod_generics_cache.B.A,
'my_inner_a2': mod_generics_cache.B.A,