-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] defer inference of legacy TypeVar bound/constraints/defaults #20598
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
175b54d
[ty] defer inference of legacy TypeVar bound/constraints/defaults
carljm 337987e
temporary fix for typevar identity with mark_inferable
carljm 030d443
Apply suggestions from code review
carljm 5719863
more review comments
carljm e3b9f5a
prefer typing_extensions over setting Python version
carljm b234e94
also error on typing_extensions.TypeVar instantiation in wrong place
carljm 2313684
add typevar-creation diagnostics tests, fix a couple issues
carljm 249c0aa
improve diagnostics
carljm 75eda5e
check boolean value of infer_variance kwarg, too
carljm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
139 changes: 139 additions & 0 deletions
139
crates/ty_python_semantic/resources/mdtest/diagnostics/legacy_typevars.md
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| # Legacy typevar creation diagnostics | ||
|
|
||
| The full tests for these features are in `generics/legacy/variables.md`. | ||
|
|
||
| <!-- snapshot-diagnostics --> | ||
|
|
||
| ## Must have a name | ||
|
|
||
| ```py | ||
| from typing import TypeVar | ||
|
|
||
| # error: [invalid-legacy-type-variable] | ||
| T = TypeVar() | ||
| ``` | ||
|
|
||
| ## Name can't be given more than once | ||
|
|
||
| ```py | ||
| from typing import TypeVar | ||
|
|
||
| # error: [invalid-legacy-type-variable] | ||
| T = TypeVar("T", name="T") | ||
| ``` | ||
|
|
||
| ## Must be directly assigned to a variable | ||
|
|
||
| > A `TypeVar()` expression must always directly be assigned to a variable (it should not be used as | ||
| > part of a larger expression). | ||
|
|
||
| ```py | ||
| from typing import TypeVar | ||
|
|
||
| T = TypeVar("T") | ||
| # error: [invalid-legacy-type-variable] | ||
| U: TypeVar = TypeVar("U") | ||
|
|
||
| # error: [invalid-legacy-type-variable] | ||
| tuple_with_typevar = ("foo", TypeVar("W")) | ||
| ``` | ||
|
|
||
| ## `TypeVar` parameter must match variable name | ||
|
|
||
| > The argument to `TypeVar()` must be a string equal to the variable name to which it is assigned. | ||
|
|
||
| ```py | ||
| from typing import TypeVar | ||
|
|
||
| # error: [invalid-legacy-type-variable] | ||
| T = TypeVar("Q") | ||
| ``` | ||
|
|
||
| ## No variadic arguments | ||
|
|
||
| ```py | ||
| from typing import TypeVar | ||
|
|
||
| types = (int, str) | ||
|
|
||
| # error: [invalid-legacy-type-variable] | ||
| T = TypeVar("T", *types) | ||
|
|
||
| # error: [invalid-legacy-type-variable] | ||
| S = TypeVar("S", **{"bound": int}) | ||
| ``` | ||
|
|
||
| ## Cannot have only one constraint | ||
|
|
||
| > `TypeVar` supports constraining parametric types to a fixed set of possible types...There should | ||
| > be at least two constraints, if any; specifying a single constraint is disallowed. | ||
|
|
||
| ```py | ||
| from typing import TypeVar | ||
|
|
||
| # error: [invalid-legacy-type-variable] | ||
| T = TypeVar("T", int) | ||
| ``` | ||
|
|
||
| ## Cannot have both bound and constraint | ||
|
|
||
| ```py | ||
| from typing import TypeVar | ||
|
|
||
| # error: [invalid-legacy-type-variable] | ||
| T = TypeVar("T", int, str, bound=bytes) | ||
| ``` | ||
|
|
||
| ## Cannot be both covariant and contravariant | ||
|
|
||
| > To facilitate the declaration of container types where covariant or contravariant type checking is | ||
| > acceptable, type variables accept keyword arguments `covariant=True` or `contravariant=True`. At | ||
| > most one of these may be passed. | ||
|
|
||
| ```py | ||
| from typing import TypeVar | ||
|
|
||
| # error: [invalid-legacy-type-variable] | ||
| T = TypeVar("T", covariant=True, contravariant=True) | ||
| ``` | ||
|
|
||
| ## Boolean parameters must be unambiguous | ||
|
|
||
| ```py | ||
| from typing_extensions import TypeVar | ||
|
|
||
| def cond() -> bool: | ||
| return True | ||
|
|
||
| # error: [invalid-legacy-type-variable] | ||
| T = TypeVar("T", covariant=cond()) | ||
|
|
||
| # error: [invalid-legacy-type-variable] | ||
| U = TypeVar("U", contravariant=cond()) | ||
|
|
||
| # error: [invalid-legacy-type-variable] | ||
| V = TypeVar("V", infer_variance=cond()) | ||
| ``` | ||
|
|
||
| ## Invalid keyword arguments | ||
|
|
||
| ```py | ||
| from typing import TypeVar | ||
|
|
||
| # error: [invalid-legacy-type-variable] | ||
| T = TypeVar("T", invalid_keyword=True) | ||
| ``` | ||
|
|
||
| ## Invalid feature for this Python version | ||
|
|
||
| ```toml | ||
| [environment] | ||
| python-version = "3.10" | ||
| ``` | ||
|
|
||
| ```py | ||
| from typing import TypeVar | ||
|
|
||
| # error: [invalid-legacy-type-variable] | ||
| T = TypeVar("T", default=int) | ||
| ``` |
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
Oops, something went wrong.
Oops, something went wrong.
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.
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.
Can we create an issue for this. It's otherwise hard to remember
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.
I don't think it will be hard to remember, because I have to fix it in order to fix the cases in astral-sh/ty#256 related to
TypeAliasType. So in that sense we already have an issue that ensures we won't forget it.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.
(And I plan to do this PR next.)