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

Fix runtime behaviour of PEP 696 #293

Merged
merged 16 commits into from
Mar 12, 2024
Merged

Conversation

NCPlayz
Copy link
Contributor

@NCPlayz NCPlayz commented Oct 27, 2023

In PEP 696 there are two failure modes specified:

DefaultStrT = TypeVar("DefaultStrT", default=str)
DefaultIntT = TypeVar("DefaultIntT", default=int)
DefaultBoolT = TypeVar("DefaultBoolT", default=bool)
T = TypeVar("T")
T2 = TypeVar("T2")

 # 1. Invalid: non-default TypeVars cannot follow ones with defaults
class NonDefaultFollowsDefault(Generic[DefaultStrT, T]): ...

class AllTheDefaults(Generic[T1, T2, DefaultStrT, DefaultIntT, DefaultBoolT]): ...

# 2. Invalid: expected 2 arguments to AllTheDefaults
AllTheDefaults[int]

This PR aims to implement the runtime behaviour for this PEP that was missed out in the initial support PR #77.
It also brings support for this to Python 3.11+.

One note I had was that the current error will say Too few parameters for AllTheDefaults; actual 1, expected 5.
For TypeVarLikes with defaults they aren't actually required, so is it worth computing the minimum required then saying expected at least 2 for this case?

Let me know if you have any suggestions!

@cpython-cla-bot
Copy link

cpython-cla-bot bot commented Oct 27, 2023

All commit authors signed the Contributor License Agreement.
CLA signed

@JelleZijlstra JelleZijlstra self-requested a review October 27, 2023 17:05
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! cc @Gobot1234

src/test_typing_extensions.py Outdated Show resolved Hide resolved
src/typing_extensions.py Outdated Show resolved Hide resolved
src/typing_extensions.py Outdated Show resolved Hide resolved
src/typing_extensions.py Outdated Show resolved Hide resolved
@cdce8p
Copy link
Contributor

cdce8p commented Mar 7, 2024

What's the status of this one? With basic / experimental support for PEP 696 coming in the next mypy release this is basically necessary to use TypeVar defaults.

@JelleZijlstra
Copy link
Member

It would be good to get more review (if @Gobot1234 or you could review, I'd appreciate it). There's also a CI failure.

Copy link
Contributor

@Gobot1234 Gobot1234 left a comment

Choose a reason for hiding this comment

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

I don't have the necessary internal knowledge of how typing.py works anymore really to be able to review this properly

src/test_typing_extensions.py Outdated Show resolved Hide resolved
@cdce8p
Copy link
Contributor

cdce8p commented Mar 7, 2024

It would be good to get more review (if @Gobot1234 or you could review, I'd appreciate it).

👍🏻 I'll look at it tomorrow.

Co-authored-by: James Hilton-Balfe <[email protected]>
Copy link
Contributor

@cdce8p cdce8p left a comment

Choose a reason for hiding this comment

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

Left a few comments. Most of them are just some minor style ones.
Overall this looks good to me! Thanks @NCPlayz 👍🏻

--
@JelleZijlstra I left suggestions everywhere I could. If you like feel free to merge them. Would be awesome to get this released soon.

src/typing_extensions.py Outdated Show resolved Hide resolved
src/typing_extensions.py Outdated Show resolved Hide resolved
src/typing_extensions.py Outdated Show resolved Hide resolved
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
src/test_typing_extensions.py Outdated Show resolved Hide resolved
src/test_typing_extensions.py Show resolved Hide resolved
src/test_typing_extensions.py Outdated Show resolved Hide resolved
@NCPlayz
Copy link
Contributor Author

NCPlayz commented Mar 7, 2024

Thanks for the reviews! Fixed a minor bug but everything seems to be passing.

src/test_typing_extensions.py Outdated Show resolved Hide resolved
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. We need to be extra careful in the cases where we monkeypatch CPython, so I left some comments there.

I'll look into adding a test to this repo that runs the test_typing suite with the typing_extensions monkeypatches enabled.

src/typing_extensions.py Outdated Show resolved Hide resolved
src/typing_extensions.py Outdated Show resolved Hide resolved
@JelleZijlstra
Copy link
Member

I'd like to make sure this still passes tests after we merge #353.

@JelleZijlstra
Copy link
Member

Ci didn't run

@JelleZijlstra
Copy link
Member

Ci didn't run

@JelleZijlstra JelleZijlstra reopened this Mar 11, 2024
@JelleZijlstra
Copy link
Member

I may have made CI not run any more somehow, will investigate.

@JelleZijlstra JelleZijlstra merged commit 8170fc7 into python:main Mar 12, 2024
18 checks passed
@NCPlayz
Copy link
Contributor Author

NCPlayz commented Mar 12, 2024

Oh.. just noticed that some changes to unit tests might have broken in one of the merge commits -- or I misunderstood whether the merge commits were meant to change those?

@JelleZijlstra
Copy link
Member

What specifically is broken? Feel free to send another PR with any fixes.

@@ -5712,7 +5712,6 @@ class Y(Generic[T], NamedTuple):
self.assertEqual(a.x, 3)

things = "arguments" if sys.version_info >= (3, 10) else "parameters"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems like these lines got re-added -- but maybe they were breaking for 3.8/3.9?

Copy link
Member

Choose a reason for hiding this comment

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

I think so, yes. I see you commented on this in #293 (comment) before; I don't feel too strongly but I think it's best to match CPython's behavior.

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.

4 participants