-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[ty] More type-variable default validation #23639
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
3 commits
Select commit
Hold shift + click to select a range
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
39 changes: 39 additions & 0 deletions
39
...ing_rules_for_ty…_-_Type_parameter_defau…_-_Function_nested_in_c…_(1a50b4ccb10b95dd).snap
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,39 @@ | ||
| --- | ||
| source: crates/ty_test/src/lib.rs | ||
| assertion_line: 624 | ||
| expression: snapshot | ||
| --- | ||
|
|
||
| --- | ||
| mdtest name: scoping.md - Scoping rules for type variables - Type parameter defaults cannot reference outer-scope type parameters - Function nested in class | ||
| mdtest path: crates/ty_python_semantic/resources/mdtest/generics/scoping.md | ||
| --- | ||
|
|
||
| # Python source files | ||
|
|
||
| ## mdtest_snippet.py | ||
|
|
||
| ``` | ||
| 1 | class C[T]: | ||
| 2 | # error: [invalid-type-variable-default] | ||
| 3 | def f[U = T](self): ... | ||
| 4 | def g[U = int](self): ... # OK | ||
| ``` | ||
|
|
||
| # Diagnostics | ||
|
|
||
| ``` | ||
| error[invalid-type-variable-default]: Invalid default for type parameter `U` | ||
| --> src/mdtest_snippet.py:1:9 | ||
| | | ||
| 1 | class C[T]: | ||
| | - `T` defined here | ||
| 2 | # error: [invalid-type-variable-default] | ||
| 3 | def f[U = T](self): ... | ||
| | ^ `T` is a type parameter bound in an outer scope | ||
| 4 | def g[U = int](self): ... # OK | ||
| | | ||
| info: See https://typing.python.org/en/latest/spec/generics.html#scoping-rules | ||
| info: rule `invalid-type-variable-default` is enabled by default | ||
|
|
||
| ``` |
46 changes: 46 additions & 0 deletions
46
...ing_rules_for_ty…_-_Type_parameter_defau…_-_Legacy_TypeVar_in_me…_(2ed4c18a38ed9090).snap
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,46 @@ | ||
| --- | ||
| source: crates/ty_test/src/lib.rs | ||
| expression: snapshot | ||
| --- | ||
|
|
||
| --- | ||
| mdtest name: scoping.md - Scoping rules for type variables - Type parameter defaults cannot reference outer-scope type parameters - Legacy TypeVar in method with outer-scope class TypeVar | ||
| mdtest path: crates/ty_python_semantic/resources/mdtest/generics/scoping.md | ||
| --- | ||
|
|
||
| # Python source files | ||
|
|
||
| ## mdtest_snippet.py | ||
|
|
||
| ``` | ||
| 1 | from typing import TypeVar, Generic | ||
| 2 | | ||
| 3 | T1 = TypeVar("T1") | ||
| 4 | T2 = TypeVar("T2", default=T1) | ||
| 5 | | ||
| 6 | class Foo(Generic[T1]): | ||
| 7 | # error: [invalid-type-variable-default] "Invalid use of type variable `T2`: default of `T2` refers to out-of-scope type variable `T1`" | ||
| 8 | def method(self, x: T2) -> T2: | ||
| 9 | return x | ||
| ``` | ||
|
|
||
| # Diagnostics | ||
|
|
||
| ``` | ||
| error[invalid-type-variable-default]: Invalid use of type variable `T2` | ||
| --> src/mdtest_snippet.py:4:1 | ||
| | | ||
| 3 | T1 = TypeVar("T1") | ||
| 4 | T2 = TypeVar("T2", default=T1) | ||
| | ------------------------------ `T2` defined here | ||
| 5 | | ||
| 6 | class Foo(Generic[T1]): | ||
| 7 | # error: [invalid-type-variable-default] "Invalid use of type variable `T2`: default of `T2` refers to out-of-scope type variable … | ||
| 8 | def method(self, x: T2) -> T2: | ||
| | ^^ Default of `T2` references out-of-scope type variable `T1` | ||
| 9 | return x | ||
| | | ||
| info: See https://typing.python.org/en/latest/spec/generics.html#scoping-rules | ||
| info: rule `invalid-type-variable-default` is enabled by default | ||
|
|
||
| ``` |
49 changes: 49 additions & 0 deletions
49
...ing_rules_for_ty…_-_Type_parameter_defau…_-_Legacy_TypeVar_in_ne…_(a1aca17ea750ffdd).snap
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,49 @@ | ||
| --- | ||
| source: crates/ty_test/src/lib.rs | ||
| assertion_line: 624 | ||
| expression: snapshot | ||
| --- | ||
|
|
||
| --- | ||
| mdtest name: scoping.md - Scoping rules for type variables - Type parameter defaults cannot reference outer-scope type parameters - Legacy TypeVar in nested function | ||
| mdtest path: crates/ty_python_semantic/resources/mdtest/generics/scoping.md | ||
| --- | ||
|
|
||
| # Python source files | ||
|
|
||
| ## mdtest_snippet.py | ||
|
|
||
| ``` | ||
| 1 | from typing import TypeVar, Generic | ||
| 2 | | ||
| 3 | T = TypeVar("T") | ||
| 4 | U = TypeVar("U", default=T) | ||
| 5 | | ||
| 6 | def outer(x: T) -> T: | ||
| 7 | # error: [invalid-type-variable-default] | ||
| 8 | def inner(y: U) -> U: | ||
| 9 | return y | ||
| 10 | return x | ||
| ``` | ||
|
|
||
| # Diagnostics | ||
|
|
||
| ``` | ||
| error[invalid-type-variable-default]: Invalid use of type variable `U` | ||
| --> src/mdtest_snippet.py:4:1 | ||
| | | ||
| 3 | T = TypeVar("T") | ||
| 4 | U = TypeVar("U", default=T) | ||
| | --------------------------- `U` defined here | ||
| 5 | | ||
| 6 | def outer(x: T) -> T: | ||
| 7 | # error: [invalid-type-variable-default] | ||
| 8 | def inner(y: U) -> U: | ||
| | ^ Default of `U` references out-of-scope type variable `T` | ||
| 9 | return y | ||
| 10 | return x | ||
| | | ||
| info: See https://typing.python.org/en/latest/spec/generics.html#scoping-rules | ||
| info: rule `invalid-type-variable-default` is enabled by default | ||
|
|
||
| ``` |
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.