]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-132159: Do not shadow user arguments in generated `__new__` by `@warnings.deprecat...
authorXuehai Pan <XuehaiPan@pku.edu.cn>
Sun, 6 Apr 2025 16:37:37 +0000 (00:37 +0800)
committerGitHub <noreply@github.com>
Sun, 6 Apr 2025 16:37:37 +0000 (16:37 +0000)
Lib/test/test_warnings/__init__.py
Lib/warnings.py
Misc/NEWS.d/next/Library/2025-04-06-16-12-49.gh-issue-132159.WvBfBm.rst [new file with mode: 0644]

index 6f4c569d2476014c0c3fee449e2162646c227848..de4280cd22f0c98da892f03df1b4e8b841c60a0f 100644 (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:
index df844253ab4e6d5887ad1efbbbecb7b2b2bed6a8..0307f89dafef5aff552667381f1032db303c4096 100644 (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__:
diff --git a/Misc/NEWS.d/next/Library/2025-04-06-16-12-49.gh-issue-132159.WvBfBm.rst b/Misc/NEWS.d/next/Library/2025-04-06-16-12-49.gh-issue-132159.WvBfBm.rst
new file mode 100644 (file)
index 0000000..8cec76e
--- /dev/null
@@ -0,0 +1 @@
+Do not shadow user arguments in generated :meth:`!__new__` by decorator :class:`warnings.deprecated`. Patch by Xuehai Pan.