-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] Infer parameter specializations of generic aliases #18021
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
Conversation
|
| // Keep this field private, so that the only way of constructing `NominalInstanceType` instances | ||
| // is through the `Type::instance` constructor function. | ||
| class: ClassType<'db>, | ||
| pub(super) class: ClassType<'db>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, the comment here says to keep this field private ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Woof, talk about tunnel vision! I had done this to be able to pattern-match on the field so that I could have a match arm that only applied to nominal instances of generic aliases.
I'll back this out and tweak the match arm — though keeping it that way might become more difficult in the future if/when the match becomes more complex, with fallthroughs being needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, actually there's another trick we can use here. Let me push it up as a proposal to see what you think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here you go — if we add an unused PhantomData field, we can mark that as private to prevent construction, but still allow read/match access to the class field. wdyt? (If you're 👍 then I'll also remove the class() accessor method in favor of just reading the field, but didn't want to clutter the diff with that yet if you prefer the original)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I like the way that it allows for easier pattern-matching while preserving the property that it's only possible to construct NominalInstanceTypes from the Type::instance() method!
| ( | ||
| Type::NominalInstance(NominalInstanceType { | ||
| class: ClassType::Generic(formal_alias), | ||
| }), | ||
| Type::NominalInstance(NominalInstanceType { | ||
| class: ClassType::Generic(actual_alias), | ||
| }), | ||
| ) if formal_alias.origin(self.db) == actual_alias.origin(self.db) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about generic protocol instances? Do we need to add a branch for them as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, though I'd like to tackle that as a follow-on PR
| // Keep this field private, so that the only way of constructing `NominalInstanceType` instances | ||
| // is through the `Type::instance` constructor function. | ||
| class: ClassType<'db>, | ||
| _phantom: PhantomData<()>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, that's clever! It's a shame I'm only learning about this trick now -- much of the diff in #17525 came from me making the class field private, and I guess that was unnecessary
* main: disable jemalloc on android (#18033) [ty] Fix incorrect type of `src.root` in documentation (#18040) [ty] Refine message for why a rule is enabled (#18038) [ty] Remove brackets around option names (#18037) Update pre-commit dependencies (#18025) Update docker/build-push-action action to v6.16.0 (#18030) Update docker/login-action action to v3.4.0 (#18031) Update taiki-e/install-action digest to 83254c5 (#18022) Update cargo-bins/cargo-binstall action to v1.12.4 (#18023) Update Rust crate ctrlc to v3.4.7 (#18027) Update Rust crate clap to v4.5.38 (#18026) Update Rust crate jiff to v0.2.13 (#18029) Update Rust crate getrandom to v0.3.3 (#18028) Update dependency ruff to v0.11.9 (#18024) [`pylint`] add fix safety section (`PLW1514`) (#17932) python_stdlib: update for 3.14 (#18014) [`ruff`] add fix safety section (`RUF033`) (#17760) [`pylint`] add fix safety section (`PLC0414`) (#17802)
carljm
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We adds the parentheses, then we takes the parentheses away again.
(Also, the actual fix here is excellent)
…eepish * origin/main: [ty] Induct into instances and subclasses when finding and applying generics (#18052) [ty] Allow classes to inherit from `type[Any]` or `type[Unknown]` (#18060) [ty] Allow a class to inherit from an intersection if the intersection contains a dynamic type and the intersection is not disjoint from `type` (#18055) [ty] Narrowing for `hasattr()` (#18053) Update reference documentation for `--python-version` (#18056) [`flake8-bugbear`] Ignore `B028` if `skip_file_prefixes` is present (#18047) [`airflow`] Apply try-catch guard to all AIR3 rules (`AIR3`) (#17887) [`pylint`] add fix safety section (`PLW3301`) (#17878) Update `--python` to accept paths to executables in virtual environments (#17954) [`pylint`] add fix safety section (`PLE4703`) (#17824) [`ruff`] Implement a recursive check for `RUF060` (#17976) [`flake8-use-pathlib`] `PTH*` suppress diagnostic for all `os.*` functions that have the `dir_fd` parameter (#17968) [`refurb`] Mark autofix as safe only for number literals in `FURB116` (#17692) [`flake8-simplify`] Fix `SIM905` autofix for `rsplit` creating a reversed list literal (#18045) Avoid initializing progress bars early (#18049)
…eepish * origin/main: [ty] Understand homogeneous tuple annotations (#17998)
…eep-dish * origin/main: [ty] Infer parameter specializations of generic aliases (#18021) [ty] Understand homogeneous tuple annotations (#17998) [ty] Induct into instances and subclasses when finding and applying generics (#18052) [ty] Allow classes to inherit from `type[Any]` or `type[Unknown]` (#18060) [ty] Allow a class to inherit from an intersection if the intersection contains a dynamic type and the intersection is not disjoint from `type` (#18055) [ty] Narrowing for `hasattr()` (#18053) Update reference documentation for `--python-version` (#18056) [`flake8-bugbear`] Ignore `B028` if `skip_file_prefixes` is present (#18047) [`airflow`] Apply try-catch guard to all AIR3 rules (`AIR3`) (#17887) [`pylint`] add fix safety section (`PLW3301`) (#17878) Update `--python` to accept paths to executables in virtual environments (#17954) [`pylint`] add fix safety section (`PLE4703`) (#17824) [`ruff`] Implement a recursive check for `RUF060` (#17976) [`flake8-use-pathlib`] `PTH*` suppress diagnostic for all `os.*` functions that have the `dir_fd` parameter (#17968) [`refurb`] Mark autofix as safe only for number literals in `FURB116` (#17692) [`flake8-simplify`] Fix `SIM905` autofix for `rsplit` creating a reversed list literal (#18045) Avoid initializing progress bars early (#18049)
…c protocols (#18054) Follows on from (and depends on) #18021. This updates our function specialization inference to infer type mappings from parameters that are generic protocols. For now, this only works when the argument _explicitly_ implements the protocol by listing it as a base class. (We end up using exactly the same logic as for generic classes in #18021.) For this to work with classes that _implicitly_ implement the protocol, we will have to check the types of the protocol members (which we are not currently doing), so that we can infer the specialization of the protocol that the class implements. --------- Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
) This updates our function specialization inference to infer type mappings from parameters that are generic aliases, e.g.: ```py def f[T](x: list[T]) -> T: ... reveal_type(f(["a", "b"])) # revealed: str ``` Though note that we're still inferring the type of list literals as `list[Unknown]`, so for now we actually need something like the following in our tests: ```py def _(x: list[str]): reveal_type(f(x)) # revealed: str ```
…c protocols (astral-sh#18054) Follows on from (and depends on) astral-sh#18021. This updates our function specialization inference to infer type mappings from parameters that are generic protocols. For now, this only works when the argument _explicitly_ implements the protocol by listing it as a base class. (We end up using exactly the same logic as for generic classes in astral-sh#18021.) For this to work with classes that _implicitly_ implement the protocol, we will have to check the types of the protocol members (which we are not currently doing), so that we can infer the specialization of the protocol that the class implements. --------- Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This updates our function specialization inference to infer type mappings from parameters that are generic aliases, e.g.:
Though note that we're still inferring the type of list literals as
list[Unknown], so for now we actually need something like the following in our tests: