-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] Infer function call typevars in both directions #18155
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
Merged
Merged
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
|
Member
|
I'm guessing this also fixes astral-sh/ty#258? |
Member
Author
Yes I think you're right! |
* main: [ty] Add regression test for fixed `pyvenv.cfg` parsing bug (#18157)
dhruvmanila
reviewed
May 18, 2025
* origin/main: (25 commits) Cargo update (#18191) Update NPM Development dependencies (#18187) Update Rust crate bincode to v2 (#18188) Update astral-sh/setup-uv action to v6 (#18184) Update dependency react-resizable-panels to v3 (#18185) Update dependency ruff to v0.11.10 (#18171) Update pre-commit dependencies (#18172) Update docker/build-push-action action to v6.17.0 (#18174) Update uraimo/run-on-arch-action action to v3 (#18190) Update docker/metadata-action action to v5.7.0 (#18175) Update docker/setup-buildx-action action to v3.10.0 (#18176) Update taiki-e/install-action action to v2.51.2 (#18183) Update extractions/setup-just action to v3 (#18186) Update Rust crate jod-thread to v1 (#18189) Update Rust crate insta to v1.43.1 (#18180) Update dependency pyodide to v0.27.6 (#18170) Update react monorepo to v19.1.0 (#18178) Update taiki-e/install-action digest to 941e8a4 (#18168) Update peter-evans/find-comment action to v3.1.0 (#18177) Update cargo-bins/cargo-binstall action to v1.12.5 (#18169) ...
carljm
approved these changes
May 19, 2025
Contributor
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.
Nice!
Co-authored-by: Carl Meyer <carl@astral.sh>
dcreager
added a commit
that referenced
this pull request
May 19, 2025
dcreager
added a commit
that referenced
this pull request
May 19, 2025
…ations (#18204) This is a follow-on to #18155. For the example raised in astral-sh/ty#370: ```py import tempfile with tempfile.TemporaryDirectory() as tmp: ... ``` the new logic would notice that both overloads of `TemporaryDirectory` match, and combine their specializations, resulting in an inferred type of `str | bytes`. This PR updates the logic to match our other handling of other calls, where we only keep the _first_ matching overload. The result for this example then becomes `str`, matching the runtime behavior. (We still do not implement the full [overload resolution algorithm](https://typing.python.org/en/latest/spec/overload.html#overload-call-evaluation) from the spec.)
Sign up for free
to 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.
This primarily comes up with annotated
selfparameters in constructors:Here, we want infer a specialization of
{T = int}for a call that hits this overload.Normally when inferring a specialization of a function call, typevars appear in the parameter annotations, and not in the argument types. In this case, this is reversed: we need to verify that the
selfargument (C[T], as we have not yet completed specialization inference) is assignable to the parameter typeC[int].To do this, we simply look for a typevar/type in both directions when performing inference, and apply the inferred specialization to argument types as well as parameter types before verifying assignability.
As a wrinkle, this exposed that we were not checking subtyping/assignability for function literals correctly. Our function literal representation includes an optional specialization that should be applied to the signature. Before, function literals were considered subtypes of (assignable to) each other only if they were identical Salsa objects. Two function literals with different specializations should still be considered subtypes of (assignable to) each other if those specializations result in the same function signature (typically because the function doesn't use the typevars in the specialization).
Closes astral-sh/ty#370
Closes astral-sh/ty#100
Closes astral-sh/ty#258