Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3.1k
[mypyc] Support new syntax for generic functions and classes (PEP 695)#17357
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Contributor
According tomypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
jhance approved these changesJun 11, 2024
JukkaL added a commit that referenced this pull requestJun 17, 2024
The main tricky bit is supporting uses of type alias objects at runtime.Python evaluates values of type aliases lazily, but there's no way to dothis using public APIs, so we directly modify the `TypeAliasType` objectthat is used to represent a type alias at runtime in C. Unfortunately,this is fragile and will need to be updated each time CPython updatesthe internal representation of `TypeAliasType` objects.Wrap the target of the type alias within a lambda expression, so that wecan easily create the lazy compute function in mypyc. This also reflectshow this is implemented in CPython.Improve test stubs to avoid various false positives or confusing errorsin tests when type checking runtime operations on types. This also makessome exisisting tests more realistic.Follow-up to#17357.
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Generate an implicit
Genericbase class for new-style generic classes. For this to work, also create C statics that can be used to access type variable objects (e.g.TorTs) at runtime. These are needed when evaluating base classes. ImportTypeVarand friends from the_typingC extension instead oftyping, since the latter is pretty slow to import, and we don't want to add a hidden new runtime dependency in case the fulltypingmodule isn't needed.Generic functions don't need any changes, since they don't support indexing with a type, and type variable types aren't valid in runtime contexts. Type erasure seems sufficient, especially considering that mypyc doesn't support classes nested within functions. (I'm not 100% sure about this though, and we might need to put function type variables into statics eventually.)
Update builtins test fixtures used in mypyc tests to not defined type variables such as
T, since these leak into tests and can produce unexpected or unrealistic results.Ignore upper bounds and value restrictions. These are only used for type checking. This should only affect introspection of type variables, which isn't properly supported in compiled code anyway.
New type alias syntax is not supported in this PR.