[ty] Improve disambiguation of types via fully qualified names (#20141)

This commit is contained in:
Alex Waygood 2025-08-29 09:44:18 +01:00 committed by GitHub
parent 0d7ed32494
commit 04dc223710
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 183 additions and 88 deletions

View file

@ -260,6 +260,11 @@ def f(cond: bool) -> int:
<!-- snapshot-diagnostics -->
```toml
[environment]
python-version = "3.12"
```
```py
# error: [invalid-return-type]
def f() -> int:
@ -279,6 +284,18 @@ T = TypeVar("T")
# error: [invalid-return-type]
def m(x: T) -> T: ...
class A[T]: ...
def f() -> A[int]:
class A[T]: ...
return A[int]() # error: [invalid-return-type]
class B: ...
def g() -> B:
class B: ...
return B() # error: [invalid-return-type]
```
## Invalid return type in stub file

View file

@ -30,6 +30,18 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/function/return_type.md
16 |
17 | # error: [invalid-return-type]
18 | def m(x: T) -> T: ...
19 |
20 | class A[T]: ...
21 |
22 | def f() -> A[int]:
23 | class A[T]: ...
24 | return A[int]() # error: [invalid-return-type]
25 |
26 | class B: ...
27 |
28 | def g() -> B:
29 | class B: ...
30 | return B() # error: [invalid-return-type]
```
# Diagnostics
@ -91,9 +103,45 @@ error[invalid-return-type]: Function always implicitly returns `None`, which is
17 | # error: [invalid-return-type]
18 | def m(x: T) -> T: ...
| ^
19 |
20 | class A[T]: ...
|
info: Consider changing the return annotation to `-> None` or adding a `return` statement
info: Only functions in stub files, methods on protocol classes, or methods with `@abstractmethod` are permitted to have empty bodies
info: rule `invalid-return-type` is enabled by default
```
```
error[invalid-return-type]: Return type does not match returned value
--> src/mdtest_snippet.py:22:12
|
20 | class A[T]: ...
21 |
22 | def f() -> A[int]:
| ------ Expected `mdtest_snippet.A[int]` because of return type
23 | class A[T]: ...
24 | return A[int]() # error: [invalid-return-type]
| ^^^^^^^^ expected `mdtest_snippet.A[int]`, found `mdtest_snippet.<locals of function 'f'>.A[int]`
25 |
26 | class B: ...
|
info: rule `invalid-return-type` is enabled by default
```
```
error[invalid-return-type]: Return type does not match returned value
--> src/mdtest_snippet.py:28:12
|
26 | class B: ...
27 |
28 | def g() -> B:
| - Expected `mdtest_snippet.B` because of return type
29 | class B: ...
30 | return B() # error: [invalid-return-type]
| ^^^ expected `mdtest_snippet.B`, found `mdtest_snippet.<locals of function 'g'>.B`
|
info: rule `invalid-return-type` is enabled by default
```