Skip to content

Commit bae017a

Browse files
committed
Do not shadow user arguments in generated __new__ by @deprecated
Backport of: python/cpython#132160
1 parent 8092c39 commit bae017a

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/test_typing_extensions.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,25 @@ class Child(Base, Mixin):
707707
instance = Child(42)
708708
self.assertEqual(instance.a, 42)
709709

710+
def test_do_not_shadow_user_arguments(self):
711+
new_called = False
712+
new_called_cls = None
713+
714+
@deprecated("MyMeta will go away soon")
715+
class MyMeta(type):
716+
def __new__(mcs, name, bases, attrs, cls=None):
717+
nonlocal new_called, new_called_cls
718+
new_called = True
719+
new_called_cls = cls
720+
return super().__new__(mcs, name, bases, attrs)
721+
722+
with self.assertWarnsRegex(DeprecationWarning, "MyMeta will go away soon"):
723+
class Foo(metaclass=MyMeta, cls='haha'):
724+
pass
725+
726+
self.assertTrue(new_called)
727+
self.assertEqual(new_called_cls, 'haha')
728+
710729
def test_existing_init_subclass(self):
711730
@deprecated("C will go away soon")
712731
class C:

src/typing_extensions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3203,7 +3203,7 @@ def __call__(self, arg: _T, /) -> _T:
32033203
original_new = arg.__new__
32043204

32053205
@functools.wraps(original_new)
3206-
def __new__(cls, *args, **kwargs):
3206+
def __new__(cls, /, *args, **kwargs):
32073207
if cls is arg:
32083208
warnings.warn(msg, category=category, stacklevel=stacklevel + 1)
32093209
if original_new is not object.__new__:

0 commit comments

Comments
 (0)