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

Added support for Concatenate with Ellipsis #442

Closed
wants to merge 0 commits into from
Closed

Conversation

Daraan
Copy link
Contributor

@Daraan Daraan commented Aug 8, 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

Copy link

cpython-cla-bot bot commented Aug 8, 2024

All commit authors signed the Contributor License Agreement.
CLA signed

@Daraan Daraan marked this pull request as draft August 8, 2024 16:51
@Daraan Daraan marked this pull request as ready for review August 9, 2024 00:08
@Daraan Daraan marked this pull request as draft August 9, 2024 00:16
@Daraan Daraan marked this pull request as ready for review August 9, 2024 01:42
@Daraan Daraan force-pushed the main branch 2 times, most recently from f518a15 to dd275c0 Compare August 9, 2024 02:22
@Daraan Daraan changed the title Added tests for Concatenate with ellipsis Added support for Concatenate with Ellipsis Aug 9, 2024
@Daraan Daraan force-pushed the main branch 2 times, most recently from 100be12 to 7e0aac8 Compare August 9, 2024 09:04
Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

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

Thanks for these fixes!

Please avoid force pushing for future changes; it makes it harder to track the history of the code. Instead, just push new commits to your branch, and we'll squash them when we merge the PR.

src/typing_extensions.py Outdated Show resolved Hide resolved
src/typing_extensions.py Outdated Show resolved Hide resolved
src/test_typing_extensions.py Outdated Show resolved Hide resolved
src/test_typing_extensions.py Outdated Show resolved Hide resolved
@JelleZijlstra JelleZijlstra self-requested a review August 14, 2024 14:36
@Daraan Daraan requested a review from AlexWaygood September 25, 2024 15:03
@Daraan
Copy link
Contributor Author

Daraan commented Oct 2, 2024

I've made a rework (moved the parts to _create_concatenate_alias) that is now simpler and also works for Python 3.8 and fully solves #110 on the current version range.

@@ -5267,6 +5270,10 @@ class Y(Protocol[T, P]):
self.assertEqual(G2.__args__, (int, Concatenate[int, P_2]))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should the __args__ here be replaced by get_args?

Copy link
Member

Choose a reason for hiding this comment

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

fine to keep it as it is, realistically this attribute is never going away :)

Comment on lines 5429 to 5442
@skipIf(TYPING_3_11_0, "Args can be non-types in 3.11+")
def test_invalid_uses_before_3_11(self):
P = ParamSpec('P')
with self.assertRaisesRegex(
TypeError,
'each arg must be a type',
):
Concatenate[1, P]

with self.assertRaisesRegex(
TypeError,
'each arg must be a type.',
):
Concatenate[1, ..., P]
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if it's really worth adding this test? It seems like in later Python versions we decided that it was better to not raise exceptions in these cases, so that seems like the "desirable" behaviour, since we aim to reflect the behaviour in the latest version of the typing module as much as possible. That means that therefore the behaviour of raising exceptions on these in earlier Python versions is sort-of undesirable behaviour. I wouldn't see it as a big priority for us to fix it, but I'm also not sure it's necessary to have these assertions, if they're arguably asserting undesirable behaviour?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the reasoning and I do agree with you. The reasons why _type_check was made more lax was to prevent bugs and have less restrictions to implement new features.

The only reason I see to keep at least the original test would be to test for a regression like forgetting to call _type_check at all.

I would roll back the new test, it does not test something that is desirable to not pass. However, the reason to keep the other one around is not that strong. Would you prefer to keep it? In that case I'll add some comment to it.

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've changed the test to assure the _type_check but with the currently undesired behavior.

@AlexWaygood
Copy link
Member

Looks like you might have closed this accidentally. Probably this is something to do with the fact that it was a PR from your fork's main branch. I think it's unfortunately impossible to repair PRs when they get to this state — do you want to recreate the PR using a different branch on your fork? :-)

@Daraan
Copy link
Contributor Author

Daraan commented Oct 5, 2024

Thanks for your kind reply. Yep I've just butchered this 🙈, I am sorry. It's as you assume related to the fork from main ...

@AlexWaygood
Copy link
Member

We've all been there, pretty sure I've done it at least once :-)

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
3 participants