FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-132159: Do not shadow user arguments in generated `__new__` by `@warnings.deprecated` by XuehaiPan · Pull Request #132160 · python/cpython · GitHub

Repository navigation

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

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
19 changes: 19 additions & 0 deletions Lib/test/test_warnings/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1653,6 +1653,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:
Expand Down
2 changes: 1 addition & 1 deletion Lib/warnings.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,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__:
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Do not shadow user arguments in generated :meth:`!__new__` by decorator :class:`warnings.deprecated`. Patch by Xuehai Pan.

Back | FazBrowse Home | New Git URL