-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] Understand legacy and PEP 695 ParamSpec
#21139
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 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
031db5d
[ty] Understand legacy and PEP 695 `ParamSpec`
dhruvmanila 1f692a8
Use `CallableType` to represent `ParamSpec` default
dhruvmanila a5070d1
Special case ParamSpec default when inferring deferred assignment def…
dhruvmanila 7929e6a
Turn off blacken-docs for invalid code snippet
dhruvmanila f1c498e
Update LSP E2E snapshots
dhruvmanila 82b28a9
Include `ParamSpec` when finding legacy type variables
dhruvmanila b554536
Update docs and schema
dhruvmanila 9c4c821
Update IDE tests
dhruvmanila b00471c
Remove `TodoPEP695ParamSpec`
dhruvmanila d0caebe
Merge branch 'main' into dhruv/paramspec
dhruvmanila 9cf93c7
Add more tests
dhruvmanila 647c2bc
Add example to rule doc
dhruvmanila 6dfac56
Use `@Todo` types for `ParamSpecArgs`/`ParamSpecKwargs`
dhruvmanila 5a9e1b1
Address review comments
dhruvmanila b1af7ce
Fix pre-commit
dhruvmanila 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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
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
154 changes: 154 additions & 0 deletions
154
crates/ty_python_semantic/resources/mdtest/paramspec.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,154 @@ | ||
| # `ParamSpec` | ||
|
|
||
| ## Definition | ||
|
|
||
| ### Valid | ||
|
|
||
| ```py | ||
| from typing import ParamSpec | ||
|
|
||
| P = ParamSpec("P") | ||
| reveal_type(type(P)) # revealed: <class 'ParamSpec'> | ||
| reveal_type(P) # revealed: typing.ParamSpec | ||
| reveal_type(P.__name__) # revealed: Literal["P"] | ||
| ``` | ||
|
|
||
| The paramspec name can also be provided as a keyword argument: | ||
|
|
||
| ```py | ||
| from typing import ParamSpec | ||
|
|
||
| P = ParamSpec(name="P") | ||
| reveal_type(P.__name__) # revealed: Literal["P"] | ||
| ``` | ||
|
|
||
| ### Must be directly assigned to a variable | ||
|
|
||
| ```py | ||
| from typing import ParamSpec | ||
|
|
||
| P = ParamSpec("P") | ||
| # error: [invalid-paramspec] | ||
| P1: ParamSpec = ParamSpec("P1") | ||
|
|
||
| # error: [invalid-paramspec] | ||
| tuple_with_typevar = ("foo", ParamSpec("W")) | ||
| reveal_type(tuple_with_typevar[1]) # revealed: ParamSpec | ||
| ``` | ||
|
|
||
| ```py | ||
| from typing_extensions import ParamSpec | ||
|
|
||
| T = ParamSpec("T") | ||
| # error: [invalid-paramspec] | ||
| P1: ParamSpec = ParamSpec("P1") | ||
|
|
||
| # error: [invalid-paramspec] | ||
| tuple_with_typevar = ("foo", ParamSpec("P2")) | ||
| reveal_type(tuple_with_typevar[1]) # revealed: ParamSpec | ||
| ``` | ||
|
|
||
| ### `TypeVar` parameter must match variable name | ||
dhruvmanila marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```py | ||
| from typing import ParamSpec | ||
|
|
||
| P1 = ParamSpec("P1") | ||
|
|
||
| # error: [invalid-paramspec] | ||
| P2 = ParamSpec("P3") | ||
| ``` | ||
|
|
||
| ### Accepts only a single `name` argument | ||
|
|
||
| > The runtime should accept bounds and covariant and contravariant arguments in the declaration just | ||
| > as typing.TypeVar does, but for now we will defer the standardization of the semantics of those | ||
| > options to a later PEP. | ||
| ```py | ||
| from typing import ParamSpec | ||
|
|
||
| # error: [invalid-paramspec] | ||
| P1 = ParamSpec("P1", bound=int) | ||
| # error: [invalid-paramspec] | ||
| P2 = ParamSpec("P2", int, str) | ||
| # error: [invalid-paramspec] | ||
| P3 = ParamSpec("P3", covariant=True) | ||
| # error: [invalid-paramspec] | ||
| P4 = ParamSpec("P4", contravariant=True) | ||
| ``` | ||
|
|
||
| ### Defaults | ||
|
|
||
| ```toml | ||
| [environment] | ||
| python-version = "3.13" | ||
| ``` | ||
|
|
||
| The default value for a `ParamSpec` can be either a list of types, `...`, or another `ParamSpec`. | ||
|
|
||
| ```py | ||
| from typing import ParamSpec | ||
|
|
||
| P1 = ParamSpec("P1", default=[int, str]) | ||
| P2 = ParamSpec("P2", default=...) | ||
| P3 = ParamSpec("P3", default=P2) | ||
| ``` | ||
|
|
||
| Other values are invalid. | ||
|
|
||
| ```py | ||
| # error: [invalid-paramspec] | ||
| P4 = ParamSpec("P4", default=int) | ||
| ``` | ||
|
|
||
| ### PEP 695 | ||
|
|
||
| ```toml | ||
| [environment] | ||
| python-version = "3.12" | ||
| ``` | ||
|
|
||
| #### Valid | ||
|
|
||
| ```py | ||
| def foo1[**P]() -> None: | ||
| reveal_type(P) # revealed: typing.ParamSpec | ||
|
|
||
| def foo2[**P = ...]() -> None: | ||
| reveal_type(P) # revealed: typing.ParamSpec | ||
|
|
||
| def foo2[**P = [int, str]]() -> None: ... | ||
dhruvmanila marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| #### Invalid | ||
|
|
||
| ParamSpec, when defined using the new syntax, does not allow defining bounds or constraints. | ||
|
|
||
| This results in a lot of syntax errors mainly because the AST doesn't accept them in this position. | ||
| The parser could do a better job in recovering from these errors. | ||
|
|
||
| <!-- blacken-docs:off --> | ||
|
|
||
| ```py | ||
| # error: [invalid-syntax] | ||
| # error: [invalid-syntax] | ||
| # error: [invalid-syntax] | ||
| # error: [invalid-syntax] | ||
| # error: [invalid-syntax] | ||
| # error: [invalid-syntax] | ||
| def foo[**P: int]() -> None: | ||
| # error: [invalid-syntax] | ||
| # error: [invalid-syntax] | ||
| pass | ||
| ``` | ||
|
|
||
| <!-- blacken-docs:on --> | ||
|
|
||
| #### Invalid default | ||
|
|
||
| ```py | ||
| # error: [invalid-paramspec] | ||
| def foo[**P = int]() -> None: | ||
| pass | ||
| ``` | ||
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.