Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions python2/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,73 @@ class D(C):
with self.assertRaises(Exception):
D[T]

def test_new_with_args(self):

class A(Generic[T]):
pass

class B(object):
def __new__(cls, arg):
# call object.__new__
obj = super(B, cls).__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(object):
def __init__(self, arg):
self.from_a = arg
# call object
super(A, self).__init__()

# mro: C, Generic, A, object
class C(Generic[T], A):
def __init__(self, arg):
self.from_c = arg
# call Generic
super(C, self).__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(object):
def __new__(cls):
# call object
obj = super(B, cls).__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(C, cls).__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):

Expand Down
10 changes: 8 additions & 2 deletions python2/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1287,10 +1287,16 @@ def _generic_new(base_cls, cls, *args, **kwds):
# Assure type is erased on instantiation,
# but attempt to store it in __orig_class__
if cls.__origin__ is None:
return base_cls.__new__(cls)
if base_cls.__new__ is object.__new__:
return base_cls.__new__(cls)
else:
return base_cls.__new__(cls, *args, **kwds)
else:
origin = cls._gorg
obj = base_cls.__new__(origin)
if base_cls.__new__ is object.__new__:
obj = base_cls.__new__(origin)
else:
obj = base_cls.__new__(origin, *args, **kwds)
try:
obj.__orig_class__ = cls
except AttributeError:
Expand Down
66 changes: 66 additions & 0 deletions src/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,72 @@ class D(C):
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):

Expand Down
10 changes: 8 additions & 2 deletions src/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1181,10 +1181,16 @@ def _generic_new(base_cls, cls, *args, **kwds):
# Assure type is erased on instantiation,
# but attempt to store it in __orig_class__
if cls.__origin__ is None:
return base_cls.__new__(cls)
if base_cls.__new__ is object.__new__:
return base_cls.__new__(cls)
else:
return base_cls.__new__(cls, *args, **kwds)
else:
origin = cls._gorg
obj = base_cls.__new__(origin)
if base_cls.__new__ is object.__new__:
obj = base_cls.__new__(origin)
else:
obj = base_cls.__new__(origin, *args, **kwds)
try:
obj.__orig_class__ = cls
except AttributeError:
Expand Down