8000 Do not shadow user arguments in generated `__new__` by `@deprecated` by Viicos · Pull Request #581 · python/typing_extensions · GitHub
[go: up one dir, main page]

Skip to content

Do not shadow user arguments in generated __new__ by @deprecated #581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
- Fix `TypeError` when taking the union of `typing_extensions.TypeAliasType` and a
`typing.TypeAliasType` on Python 3.12 and 3.13.
Patch by [Joren Hammudoglu](https://github.com/jorenham).
- Backport from CPython PR [#132160](https://github.com/python/cpython/pull/132160)
to avoid having user arguments shadowed in generated `__new__` by
`@typing_extensions.deprecated`.
Patch by [Victorien Plot](https://github.com/Viicos).

# Release 4.13.1 (April 3, 2025)

Expand Down
19 changes: 19 additions & 0 deletions src/test_typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,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
5 changes: 3 additions & 2 deletions src/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3123,7 +3123,8 @@ def method(self) -> None:
return arg


if hasattr(warnings, "deprecated"):
# Python 3.13.3+ contains a fix for the wrapped __new__
if sys.version_info >= (3, 13, 3):
deprecated = warnings.deprecated
else:
_T = typing.TypeVar("_T")
Expand Down Expand Up @@ -3203,7 +3204,7 @@ def __call__(self, arg: _T, /) -> _T:
original_new = arg.__new__

@functools.wraps(original_new)
def __new__(cls, *args, **kwargs):
def __new__(cls, /, *args, **kwargs):
if cls is arg:
warnings.warn(msg, category=category, stacklevel=stacklevel + 1)
if original_new is not object.__new__:
Expand Down
Loading
0