[ty] Improve diagnostic when callable is used in a type expression instead of collections.abc.Callable or typing.Callable (#22180)

This commit is contained in:
Alex Waygood
2025-12-24 19:18:51 +00:00
committed by GitHub
parent 2de4464e92
commit 139149f87b
3 changed files with 81 additions and 2 deletions

View File

@@ -266,3 +266,12 @@ def _(
) -> (int, str): # error: [invalid-type-form]
return x
```
### Special-cased diagnostic for `callable` used in a type expression
```py
# error: [invalid-type-form]
# error: [invalid-type-form]
def decorator(fn: callable) -> callable:
return fn
```

View File

@@ -0,0 +1,49 @@
---
source: crates/ty_test/src/lib.rs
expression: snapshot
---
---
mdtest name: invalid.md - Tests for invalid types in type expressions - Diagnostics for common errors - Special-cased diagnostic for `callable` used in a type expression
mdtest path: crates/ty_python_semantic/resources/mdtest/annotations/invalid.md
---
# Python source files
## mdtest_snippet.py
```
1 | # error: [invalid-type-form]
2 | # error: [invalid-type-form]
3 | def decorator(fn: callable) -> callable:
4 | return fn
```
# Diagnostics
```
error[invalid-type-form]: Function `callable` is not valid in a type expression
--> src/mdtest_snippet.py:3:19
|
1 | # error: [invalid-type-form]
2 | # error: [invalid-type-form]
3 | def decorator(fn: callable) -> callable:
| ^^^^^^^^ Did you mean `collections.abc.Callable`?
4 | return fn
|
info: rule `invalid-type-form` is enabled by default
```
```
error[invalid-type-form]: Function `callable` is not valid in a type expression
--> src/mdtest_snippet.py:3:32
|
1 | # error: [invalid-type-form]
2 | # error: [invalid-type-form]
3 | def decorator(fn: callable) -> callable:
| ^^^^^^^^ Did you mean `collections.abc.Callable`?
4 | return fn
|
info: rule `invalid-type-form` is enabled by default
```