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

Concatenate to support Ellipsis argument #481

Merged
merged 23 commits into from
Oct 22, 2024

Conversation

Daraan
Copy link
Contributor

@Daraan Daraan commented Oct 5, 2024

Fixes #110.

This PR adds support for Ellipsis in Concatenate for the Python 3.8-3.10 backports.

Does not fix:

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

@Daraan
Copy link
Contributor Author

Daraan commented Oct 5, 2024

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]

@Daraan Daraan changed the title Concatenate/ellipsis support 110 Concatenate to support Ellipsis argument Oct 8, 2024
CHANGELOG.md Outdated
Comment on lines 19 to 20
- Extended the Concatenate backport for Python 3.8-3.10 to now accept
ellipsis as an argument. Patch by [Daraan](https://github.com/Daraan).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- 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 Show resolved Hide resolved
src/test_typing_extensions.py Outdated Show resolved Hide resolved
):
Concatenate[(str,), P]

@skipUnless(TYPING_3_11_0 or (3, 10, 0) <= sys.version_info < (3, 10, 2),
Copy link
Member

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+.

Copy link
Member

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: ...
Copy link
Member

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+?

Copy link
Contributor Author

@Daraan Daraan Oct 21, 2024

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.

Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

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.

@JelleZijlstra JelleZijlstra merged commit 3ebe884 into python:main Oct 22, 2024
21 checks passed
@Daraan Daraan deleted the concatenate/ellipsis-support-110 branch November 29, 2024 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Backport allowing passing an ellipsis as the last argument of Concatenate to 3.10
2 participants