-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #14558 ## Summary - Add `typing.NoReturn` and `typing.Never` to known instances and infer them as `Type::Never` - Add `is_assignable_to` cases for `Type::Never` I skipped emitting diagnostic for when a function is annotated as `NoReturn` but it actually returns. ## Test Plan Added tests from https://github.com/python/typing/blob/main/conformance/tests/specialtypes_never.py except from generics and checking if the return value of the function and the annotations match. --------- Co-authored-by: Alex Waygood <[email protected]> Co-authored-by: Carl Meyer <[email protected]>
- Loading branch information
1 parent
f98eebd
commit 557d583
Showing
5 changed files
with
101 additions
and
8 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
crates/red_knot_python_semantic/resources/mdtest/annotations/never.md
This file contains 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,62 @@ | ||
# NoReturn & Never | ||
|
||
`NoReturn` is used to annotate the return type for functions that never return. `Never` is the | ||
bottom type, representing the empty set of Python objects. These two annotations can be used | ||
interchangeably. | ||
|
||
## Function Return Type Annotation | ||
|
||
```py | ||
from typing import NoReturn | ||
|
||
def stop() -> NoReturn: | ||
raise RuntimeError("no way") | ||
|
||
# revealed: Never | ||
reveal_type(stop()) | ||
``` | ||
|
||
## Assignment | ||
|
||
```py | ||
from typing import NoReturn, Never, Any | ||
|
||
# error: [invalid-type-parameter] "Type `typing.Never` expected no type parameter" | ||
x: Never[int] | ||
a1: NoReturn | ||
# TODO: Test `Never` is only available in python >= 3.11 | ||
a2: Never | ||
b1: Any | ||
b2: int | ||
|
||
def f(): | ||
# revealed: Never | ||
reveal_type(a1) | ||
# revealed: Never | ||
reveal_type(a2) | ||
|
||
# Never is assignable to all types. | ||
v1: int = a1 | ||
v2: str = a1 | ||
# Other types are not assignable to Never except for Never (and Any). | ||
v3: Never = b1 | ||
v4: Never = a2 | ||
v5: Any = b2 | ||
# error: [invalid-assignment] "Object of type `Literal[1]` is not assignable to `Never`" | ||
v6: Never = 1 | ||
``` | ||
|
||
## Typing Extensions | ||
|
||
```py | ||
from typing_extensions import NoReturn, Never | ||
|
||
x: NoReturn | ||
y: Never | ||
|
||
def f(): | ||
# revealed: Never | ||
reveal_type(x) | ||
# revealed: Never | ||
reveal_type(y) | ||
``` |
This file contains 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
This file contains 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
This file contains 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
This file contains 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