-
-
Notifications
You must be signed in to change notification settings - Fork 111
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
Concatenate to support Ellipsis argument #481
Concatenate to support Ellipsis argument #481
Conversation
I am sorry I had to recreate #442. Latest change since then: - if not TYPING_3_11_0:
- with self.assertRaisesRegex(
- TypeError,
- 'each arg must be a type',
- ):
- Concatenate[1, P]
# different location
+ def test_invalid_use(self):
+ # Assure that `_type_check` is called.
+ P = ParamSpec('P')
+ with self.assertRaisesRegex(
+ TypeError,
+ "each arg must be a type",
+ ):
+ Concatenate[(str,), P] |
CHANGELOG.md
Outdated
- Extended the Concatenate backport for Python 3.8-3.10 to now accept | ||
ellipsis as an argument. Patch by [Daraan](https://github.com/Daraan). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Extended the Concatenate backport for Python 3.8-3.10 to now accept | |
ellipsis as an argument. Patch by [Daraan](https://github.com/Daraan). | |
- Extended the `Concatenate` backport for Python 3.8-3.10 to now accept | |
`Ellipsis` as an argument. Patch by [Daraan](https://github.com/Daraan). |
src/test_typing_extensions.py
Outdated
): | ||
Concatenate[(str,), P] | ||
|
||
@skipUnless(TYPING_3_11_0 or (3, 10, 0) <= sys.version_info < (3, 10, 2), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case I think I'd want to block it on all 3.10 versions. It's a bad experience if something works on early 3.10 and breaks if people upgrade to a later bugfix release. Better to allow it only on 3.11+.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, the version check seems wrong here, the change was made in 3.10.3 not 3.10.2.
|
||
# 3.8-3.9.2 | ||
class _EllipsisDummy: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you make this inherit from ParamSpec, can it fix 3.10.3+?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might be missing something here. To what case are you referring here?
This backport of Concatenate[...]
also works for all 3.10, as the critical part is in _concatenate_getitem
Is it possibly related to the following test I jointly introduced also in #479?
@skipUnless(TYPING_3_11_0, # <-- Needs PR #479 to backport for 3.10
"Cannot be backported to <=3.9. See issue #48"
"Cannot use ... with typing._ConcatenateGenericAlias after 3.10.2")
def test_alias_subscription_with_ellipsis(self):
P = ParamSpec('P')
X = Callable[Concatenate[int, P], Any]
C1 = X[...] # <-- Needs PR #479 for 3.10.3+
self.assertEqual(get_args(C1), (Concatenate[int, ...], Any)) # <-- needs this PR
That aside; It is possible to do _EllipsisDummy = ParamSpec("_EllipsisDummy")
if needed as subclassing ParamSpec
is (currently) not possible. I thought using a new class might have less side-effects than using a ParamSpec instance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm referring to the comment right above here, about the test that works only in 3.10.0-3.10.2 but not 3.10.3+. Making _EllipsisDummy
an instance of ParamSpec might fix that test for later versions of 3.10.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am sorry for the confusion. I do think we talk about the same location :), if not correct me please. I made the necessary edits above.
#479 allows to backport that test to all versions of 3.10, however for #479 the test self.assertEqual(get_args(C1), (Concatenate[int, ...], Any))
would need this PR to work without an error on 3.10 as well.
Both PRs are needed make this test work for the 3.10 version range.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, thanks. #479 is merged now, so would you mind updating this PR? There's a merge conflict right now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Unified the code and the test we talked about, aside from that added one more test for invalid usage.
- Move changelog entry up - code style
Fixes #110.
This PR adds support for Ellipsis in Concatenate for the Python 3.8-3.10 backports.
Does not fix:
Concatenate
implementation generates runtime exception if parameterized by...
#48 : Using Ellipsis as argument for Callable with a generic.._ConcatenateGenericAlias
Changes:
- Changed _concatenate_getitem to reflect the Python3.11 typing module that allows the Ellipsis parameter.
- For Python 3.10 uses typing._ConcatenateGenericAlias and _concatenate_getitem
- For <=Python 3.9 the typing_extensions._ConcatenateGenericAlias and _concatenate_getitem like before
- For <3.9.2 uses a Dummy class that is temporarily swapped with an Ellipsis input
- added tests and updated tests