[3.13] gh-132159: Do not shadow user arguments in generated __new__ by @warnings.deprecated (GH-132160) (#132163)

gh-132159: Do not shadow user arguments in generated `__new__` by `@warnings.deprecated` (GH-132160)
(cherry picked from commit 7bb1e1a236)

Co-authored-by: Xuehai Pan <XuehaiPan@pku.edu.cn>
This commit is contained in:
Miss Islington (bot) 2025-04-06 19:00:54 +02:00 committed by GitHub
parent f7ab4ebb2c
commit 83070aa108
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 21 additions and 1 deletions

View file

@ -1628,6 +1628,25 @@ class Child(Base, Mixin):
instance = Child(42)
self.assertEqual(instance.a, 42)
def test_do_not_shadow_user_arguments(self):
new_called = False
new_called_cls = None
@deprecated("MyMeta will go away soon")
class MyMeta(type):
def __new__(mcs, name, bases, attrs, cls=None):
nonlocal new_called, new_called_cls
new_called = True
new_called_cls = cls
return super().__new__(mcs, name, bases, attrs)
with self.assertWarnsRegex(DeprecationWarning, "MyMeta will go away soon"):
class Foo(metaclass=MyMeta, cls='haha'):
pass
self.assertTrue(new_called)
self.assertEqual(new_called_cls, 'haha')
def test_existing_init_subclass(self):
@deprecated("C will go away soon")
class C:

View file

@ -589,7 +589,7 @@ def __call__(self, arg, /):
original_new = arg.__new__
@functools.wraps(original_new)
def __new__(cls, *args, **kwargs):
def __new__(cls, /, *args, **kwargs):
if cls is arg:
warn(msg, category=category, stacklevel=stacklevel + 1)
if original_new is not object.__new__:

View file

@ -0,0 +1 @@
Do not shadow user arguments in generated :meth:`!__new__` by decorator :class:`warnings.deprecated`. Patch by Xuehai Pan.