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

This commit is contained in:
Xuehai Pan 2025-04-07 00:37:37 +08:00 committed by GitHub
parent c0661df42a
commit 7bb1e1a236
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 21 additions and 1 deletions

View file

@ -1653,6 +1653,25 @@ class DeprecatedTests(PyPublicAPITests):
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

@ -597,7 +597,7 @@ class deprecated:
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.