[ty] Improve several "Did you mean?" suggestions (#21597)

This commit is contained in:
Alex Waygood
2025-11-25 10:29:01 +00:00
committed by GitHub
parent 747c39a26a
commit b19ddca69b
7 changed files with 183 additions and 64 deletions

View File

@@ -35,6 +35,8 @@ bad_nesting: Literal[LiteralString] # error: [invalid-type-form]
`LiteralString` cannot be parameterized.
<!-- snapshot-diagnostics -->
```py
from typing_extensions import LiteralString
@@ -42,7 +44,6 @@ from typing_extensions import LiteralString
a: LiteralString[str]
# error: [invalid-type-form]
# error: [unresolved-reference] "Name `foo` used when not defined"
b: LiteralString["foo"]
```

View File

@@ -34,29 +34,27 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/annotations/invalid.md
# Diagnostics
```
error[invalid-type-form]: Variable of type `<module 'datetime'>` is not allowed in a type expression
error[invalid-type-form]: Module `datetime` is not valid in a type expression
--> src/foo.py:3:10
|
1 | import datetime
2 |
3 | def f(x: datetime): ... # error: [invalid-type-form]
| ^^^^^^^^
| ^^^^^^^^ Did you mean to use the module's member `datetime.datetime`?
|
info: Did you mean to use the module's member `datetime.datetime` instead?
info: rule `invalid-type-form` is enabled by default
```
```
error[invalid-type-form]: Variable of type `<module 'PIL.Image'>` is not allowed in a type expression
error[invalid-type-form]: Module `PIL.Image` is not valid in a type expression
--> src/bar.py:3:10
|
1 | from PIL import Image
2 |
3 | def g(x: Image): ... # error: [invalid-type-form]
| ^^^^^
| ^^^^^ Did you mean to use the module's member `Image.Image`?
|
info: Did you mean to use the module's member `Image.Image` instead?
info: rule `invalid-type-form` is enabled by default
```

View File

@@ -0,0 +1,52 @@
---
source: crates/ty_test/src/lib.rs
expression: snapshot
---
---
mdtest name: literal_string.md - `LiteralString` - Usages - Parameterized
mdtest path: crates/ty_python_semantic/resources/mdtest/annotations/literal_string.md
---
# Python source files
## mdtest_snippet.py
```
1 | from typing_extensions import LiteralString
2 |
3 | # error: [invalid-type-form]
4 | a: LiteralString[str]
5 |
6 | # error: [invalid-type-form]
7 | b: LiteralString["foo"]
```
# Diagnostics
```
error[invalid-type-form]: `LiteralString` expects no type parameter
--> src/mdtest_snippet.py:4:4
|
3 | # error: [invalid-type-form]
4 | a: LiteralString[str]
| ^^^^^^^^^^^^^^^^^^
5 |
6 | # error: [invalid-type-form]
|
info: rule `invalid-type-form` is enabled by default
```
```
error[invalid-type-form]: `LiteralString` expects no type parameter
--> src/mdtest_snippet.py:7:4
|
6 | # error: [invalid-type-form]
7 | b: LiteralString["foo"]
| -------------^^^^^^^
| |
| Did you mean `Literal`?
|
info: rule `invalid-type-form` is enabled by default
```

View File

@@ -0,0 +1,34 @@
---
source: crates/ty_test/src/lib.rs
expression: snapshot
---
---
mdtest name: typed_dict.md - `TypedDict` - Error cases - `typing.TypedDict` is not allowed in type expressions
mdtest path: crates/ty_python_semantic/resources/mdtest/typed_dict.md
---
# Python source files
## mdtest_snippet.py
```
1 | from typing import TypedDict
2 |
3 | # error: [invalid-type-form] "The special form `typing.TypedDict` is not allowed in type expressions"
4 | x: TypedDict = {"name": "Alice"}
```
# Diagnostics
```
error[invalid-type-form]: The special form `typing.TypedDict` is not allowed in type expressions
--> src/mdtest_snippet.py:4:4
|
3 | # error: [invalid-type-form] "The special form `typing.TypedDict` is not allowed in type expressions"
4 | x: TypedDict = {"name": "Alice"}
| ^^^^^^^^^
|
help: You might have meant to use a concrete TypedDict or `collections.abc.Mapping[str, object]`
info: rule `invalid-type-form` is enabled by default
```

View File

@@ -1407,10 +1407,12 @@ msg.content
### `typing.TypedDict` is not allowed in type expressions
<!-- snapshot-diagnostics -->
```py
from typing import TypedDict
# error: [invalid-type-form] "The special form `typing.TypedDict` is not allowed in type expressions."
# error: [invalid-type-form] "The special form `typing.TypedDict` is not allowed in type expressions"
x: TypedDict = {"name": "Alice"}
```