Skip to content
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

ParamSpec: backport bpo-46676 #1059

Merged
merged 3 commits into from Feb 9, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions typing_extensions/CHANGELOG
@@ -1,5 +1,7 @@
# Release 4.x.x

- `ParamSpec` args and kwargs are now equal to themselves. Backport from
bpo-46676. Patch by Gregory Beauregard (@GBeauregard).
- Add `reveal_type`. Backport from bpo-46414.
- Runtime support for PEP 681 and `typing_extensions.dataclass_transform`.
- `Annotated` can now wrap `ClassVar` and `Final`. Backport from
Expand Down
9 changes: 9 additions & 0 deletions typing_extensions/src/test_typing_extensions.py
Expand Up @@ -2057,15 +2057,24 @@ def test_valid_uses(self):
self.assertTrue(hasattr(P, 'args'))
self.assertTrue(hasattr(P, 'kwargs'))

@skipIf((3, 10, 0) <= sys.version_info[:3] <= (3, 10, 2), "Needs bpo-46676.")
def test_args_kwargs(self):
P = ParamSpec('P')
P_2 = ParamSpec('P_2')
# Note: not in dir(P) because of __class__ hacks
self.assertTrue(hasattr(P, 'args'))
self.assertTrue(hasattr(P, 'kwargs'))
self.assertIsInstance(P.args, ParamSpecArgs)
self.assertIsInstance(P.kwargs, ParamSpecKwargs)
self.assertIs(P.args.__origin__, P)
self.assertIs(P.kwargs.__origin__, P)
self.assertEqual(P.args, P.args)
self.assertEqual(P.kwargs, P.kwargs)
self.assertNotEqual(P.args, P_2.args)
self.assertNotEqual(P.kwargs, P_2.kwargs)
self.assertNotEqual(P.args, P.kwargs)
self.assertNotEqual(P.kwargs, P.args)
self.assertNotEqual(P.args, P_2.kwargs)
self.assertEqual(repr(P.args), "P.args")
self.assertEqual(repr(P.kwargs), "P.kwargs")

Expand Down
10 changes: 10 additions & 0 deletions typing_extensions/src/typing_extensions.py
Expand Up @@ -1635,6 +1635,11 @@ def __init__(self, origin):
def __repr__(self):
return f"{self.__origin__.__name__}.args"

def __eq__(self, other):
if not isinstance(other, ParamSpecArgs):
return NotImplemented
return self.__origin__ == other.__origin__

class ParamSpecKwargs(_Immutable):
"""The kwargs for a ParamSpec object.

Expand All @@ -1653,6 +1658,11 @@ def __init__(self, origin):
def __repr__(self):
return f"{self.__origin__.__name__}.kwargs"

def __eq__(self, other):
if not isinstance(other, ParamSpecKwargs):
return NotImplemented
return self.__origin__ == other.__origin__

# 3.10+
if hasattr(typing, 'ParamSpec'):
ParamSpec = typing.ParamSpec
Expand Down