[TSP] Return contextual expected type from getExpectedType#3672
Closed
rchiodo wants to merge 2 commits into
Closed
[TSP] Return contextual expected type from getExpectedType#3672rchiodo wants to merge 2 commits into
rchiodo wants to merge 2 commits into
Conversation
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
kinto0
reviewed
Jun 4, 2026
| // 1111111 | ||
| // 0123456789012345678 | ||
| let source = "x: int | str = 42\n"; | ||
| let (mut tsp, file_uri, snapshot) = setup_project(source); |
Contributor
There was a problem hiding this comment.
nit: we probably don't need an integration test for these. we can probably just use a normal unit test
Contributor
|
@kinto0 has imported this pull request. If you are a Meta employee, you can view this in D107524019. |
Contributor
|
discussed offline. closing |
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
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.
Summary
typeServer/getExpectedTypewas returning the inferred (computed) type at the cursor instead of the contextually expected type that the position imposes. For example, inx: int | str = 4, querying on4returnedintrather thanint | str.This change makes
get_expected_type_at_positionwalk the covering AST nodes innermost-first and, when the cursor lies in a context that supplies an expected type, look up that contextual type. Recognized contexts:Transaction::expected_call_argument_type(nowpub(crate)so the non-WASM server can call it).return <expr>— uses the enclosing function's return annotation (resolved viaget_type_traceover the annotation's full range so unions likeint | strare preserved instead of collapsing to the leading identifier).def f(x: T = <expr>)— uses the parameter's annotationT.x: T = <expr>— uses the LHS declared typeT.Other contexts (yield expressions, container-literal elements under an annotation, etc.) still fall back to the inferred type at the cursor; tests document that behavior so future fixes can flip the expectations.
Fixes microsoft/pylance-release#8060
Test Plan
Added 8 new tests in
pyrefly/lib/test/tsp/tsp_interaction/get_type_queries.rscovering each newly supported context plus the documented fallbacks:test_get_expected_type_call_positional_argumenttest_get_expected_type_call_keyword_argumenttest_get_expected_type_return_statement_valuetest_get_expected_type_default_parameter_valuetest_get_expected_type_yield_falls_back_to_value_typetest_get_expected_type_list_element_falls_back_to_container_typetest_get_expected_type_dict_value_falls_back_to_container_typeAll 14
test_get_expected_type*tests pass:Also ran
python3 test.py --no-test --no-conformance --no-jsonschema— formatting and linting clean.cargo clippy --all-targets --all-featuresproduces no new warnings (only 2 pre-existing unused-import warnings inpyrefly_lsp_interaction_tests, unrelated to this branch).