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

[mypyc] Support new syntax for generic functions and classes (PEP 695) #17357

Merged
merged 16 commits into from
Jun 11, 2024

Conversation

JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented Jun 10, 2024

Generate an implicit Generic base 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. T or Ts) at runtime. These are needed when evaluating base classes. Import TypeVar and friends from the _typing C extension instead of typing, since the latter is pretty slow to import, and we don't want to add a hidden new runtime dependency in case the full typing module 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.

Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@JukkaL JukkaL merged commit 415d49f into master Jun 11, 2024
18 checks passed
@JukkaL JukkaL deleted the mypyc-type-var-syntax branch June 11, 2024 16:13
JukkaL added a commit that referenced this pull request Jun 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 do
this using public APIs, so we directly modify the `TypeAliasType` object
that is used to represent a type alias at runtime in C. Unfortunately,
this is fragile and will need to be updated each time CPython updates
the internal representation of `TypeAliasType` objects.

Wrap the target of the type alias within a lambda expression, so that we
can easily create the lazy compute function in mypyc. This also reflects
how this is implemented in CPython.

Improve test stubs to avoid various false positives or confusing errors
in tests when type checking runtime operations on types. This also makes
some exisisting tests more realistic.

Follow-up to #17357.
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.

2 participants