[ty] Add hints to invalid-type-form for common mistakes (#18543)

Co-authored-by: Ben Bar-Or <ben.baror@ridewithvia.com>
Co-authored-by: Alex Waygood <alex.waygood@gmail.com>
This commit is contained in:
Ben Bar-Or 2025-06-09 02:40:05 +03:00 committed by GitHub
parent 301b9f4135
commit 1dc8f8f903
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 462 additions and 74 deletions

View file

@ -0,0 +1,91 @@
---
source: crates/ty_test/src/lib.rs
expression: snapshot
---
---
mdtest name: invalid.md - Tests for invalid types in type expressions - Diagnostics for common errors - List-literal used when you meant to use a list or tuple
mdtest path: crates/ty_python_semantic/resources/mdtest/annotations/invalid.md
---
# Python source files
## mdtest_snippet.py
```
1 | def _(
2 | x: [int], # error: [invalid-type-form]
3 | ) -> [int]: # error: [invalid-type-form]
4 | return x
5 | def _(
6 | x: [int, str], # error: [invalid-type-form]
7 | ) -> [int, str]: # error: [invalid-type-form]
8 | return x
```
# Diagnostics
```
error[invalid-type-form]: List literals are not allowed in this context in a type expression
--> src/mdtest_snippet.py:2:8
|
1 | def _(
2 | x: [int], # error: [invalid-type-form]
| ^^^^^ Did you mean `list[int]`?
3 | ) -> [int]: # error: [invalid-type-form]
4 | return x
|
info: See the following page for a reference on valid type expressions:
info: https://typing.python.org/en/latest/spec/annotations.html#type-and-annotation-expressions
info: rule `invalid-type-form` is enabled by default
```
```
error[invalid-type-form]: List literals are not allowed in this context in a type expression
--> src/mdtest_snippet.py:3:6
|
1 | def _(
2 | x: [int], # error: [invalid-type-form]
3 | ) -> [int]: # error: [invalid-type-form]
| ^^^^^ Did you mean `list[int]`?
4 | return x
5 | def _(
|
info: See the following page for a reference on valid type expressions:
info: https://typing.python.org/en/latest/spec/annotations.html#type-and-annotation-expressions
info: rule `invalid-type-form` is enabled by default
```
```
error[invalid-type-form]: List literals are not allowed in this context in a type expression
--> src/mdtest_snippet.py:6:8
|
4 | return x
5 | def _(
6 | x: [int, str], # error: [invalid-type-form]
| ^^^^^^^^^^ Did you mean `tuple[int, str]`?
7 | ) -> [int, str]: # error: [invalid-type-form]
8 | return x
|
info: See the following page for a reference on valid type expressions:
info: https://typing.python.org/en/latest/spec/annotations.html#type-and-annotation-expressions
info: rule `invalid-type-form` is enabled by default
```
```
error[invalid-type-form]: List literals are not allowed in this context in a type expression
--> src/mdtest_snippet.py:7:6
|
5 | def _(
6 | x: [int, str], # error: [invalid-type-form]
7 | ) -> [int, str]: # error: [invalid-type-form]
| ^^^^^^^^^^ Did you mean `tuple[int, str]`?
8 | return x
|
info: See the following page for a reference on valid type expressions:
info: https://typing.python.org/en/latest/spec/annotations.html#type-and-annotation-expressions
info: rule `invalid-type-form` is enabled by default
```

View file

@ -0,0 +1,129 @@
---
source: crates/ty_test/src/lib.rs
expression: snapshot
---
---
mdtest name: invalid.md - Tests for invalid types in type expressions - Diagnostics for common errors - Tuple-literal used when you meant to use a tuple
mdtest path: crates/ty_python_semantic/resources/mdtest/annotations/invalid.md
---
# Python source files
## mdtest_snippet.py
```
1 | def _(
2 | x: (), # error: [invalid-type-form]
3 | ) -> (): # error: [invalid-type-form]
4 | return x
5 | def _(
6 | x: (int,), # error: [invalid-type-form]
7 | ) -> (int,): # error: [invalid-type-form]
8 | return x
9 | def _(
10 | x: (int, str), # error: [invalid-type-form]
11 | ) -> (int, str): # error: [invalid-type-form]
12 | return x
```
# Diagnostics
```
error[invalid-type-form]: Tuple literals are not allowed in this context in a type expression
--> src/mdtest_snippet.py:2:8
|
1 | def _(
2 | x: (), # error: [invalid-type-form]
| ^^ Did you mean `tuple[()]`?
3 | ) -> (): # error: [invalid-type-form]
4 | return x
|
info: See the following page for a reference on valid type expressions:
info: https://typing.python.org/en/latest/spec/annotations.html#type-and-annotation-expressions
info: rule `invalid-type-form` is enabled by default
```
```
error[invalid-type-form]: Tuple literals are not allowed in this context in a type expression
--> src/mdtest_snippet.py:3:6
|
1 | def _(
2 | x: (), # error: [invalid-type-form]
3 | ) -> (): # error: [invalid-type-form]
| ^^ Did you mean `tuple[()]`?
4 | return x
5 | def _(
|
info: See the following page for a reference on valid type expressions:
info: https://typing.python.org/en/latest/spec/annotations.html#type-and-annotation-expressions
info: rule `invalid-type-form` is enabled by default
```
```
error[invalid-type-form]: Tuple literals are not allowed in this context in a type expression
--> src/mdtest_snippet.py:6:8
|
4 | return x
5 | def _(
6 | x: (int,), # error: [invalid-type-form]
| ^^^^^^ Did you mean `tuple[int]`?
7 | ) -> (int,): # error: [invalid-type-form]
8 | return x
|
info: See the following page for a reference on valid type expressions:
info: https://typing.python.org/en/latest/spec/annotations.html#type-and-annotation-expressions
info: rule `invalid-type-form` is enabled by default
```
```
error[invalid-type-form]: Tuple literals are not allowed in this context in a type expression
--> src/mdtest_snippet.py:7:6
|
5 | def _(
6 | x: (int,), # error: [invalid-type-form]
7 | ) -> (int,): # error: [invalid-type-form]
| ^^^^^^ Did you mean `tuple[int]`?
8 | return x
9 | def _(
|
info: See the following page for a reference on valid type expressions:
info: https://typing.python.org/en/latest/spec/annotations.html#type-and-annotation-expressions
info: rule `invalid-type-form` is enabled by default
```
```
error[invalid-type-form]: Tuple literals are not allowed in this context in a type expression
--> src/mdtest_snippet.py:10:8
|
8 | return x
9 | def _(
10 | x: (int, str), # error: [invalid-type-form]
| ^^^^^^^^^^ Did you mean `tuple[int, str]`?
11 | ) -> (int, str): # error: [invalid-type-form]
12 | return x
|
info: See the following page for a reference on valid type expressions:
info: https://typing.python.org/en/latest/spec/annotations.html#type-and-annotation-expressions
info: rule `invalid-type-form` is enabled by default
```
```
error[invalid-type-form]: Tuple literals are not allowed in this context in a type expression
--> src/mdtest_snippet.py:11:6
|
9 | def _(
10 | x: (int, str), # error: [invalid-type-form]
11 | ) -> (int, str): # error: [invalid-type-form]
| ^^^^^^^^^^ Did you mean `tuple[int, str]`?
12 | return x
|
info: See the following page for a reference on valid type expressions:
info: https://typing.python.org/en/latest/spec/annotations.html#type-and-annotation-expressions
info: rule `invalid-type-form` is enabled by default
```