-
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.
[red-knot] support
typing.Union
in type annotations (#14499)
Fix #14498 ## Summary This PR adds `typing.Union` support ## Test Plan I created new tests in mdtest. --------- Co-authored-by: Carl Meyer <[email protected]>
- Loading branch information
Showing
4 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
crates/red_knot_python_semantic/resources/mdtest/annotations/union.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,61 @@ | ||
# Union | ||
|
||
## Annotation | ||
|
||
`typing.Union` can be used to construct union types same as `|` operator. | ||
|
||
```py | ||
from typing import Union | ||
|
||
a: Union[int, str] | ||
a1: Union[int, bool] | ||
a2: Union[int, Union[float, str]] | ||
a3: Union[int, None] | ||
a4: Union[Union[float, str]] | ||
a5: Union[int] | ||
a6: Union[()] | ||
|
||
def f(): | ||
# revealed: int | str | ||
reveal_type(a) | ||
# Since bool is a subtype of int we simplify to int here. But we do allow assigning boolean values (see below). | ||
# revealed: int | ||
reveal_type(a1) | ||
# revealed: int | float | str | ||
reveal_type(a2) | ||
# revealed: int | None | ||
reveal_type(a3) | ||
# revealed: float | str | ||
reveal_type(a4) | ||
# revealed: int | ||
reveal_type(a5) | ||
# revealed: Never | ||
reveal_type(a6) | ||
``` | ||
|
||
## Assignment | ||
|
||
```py | ||
from typing import Union | ||
|
||
a: Union[int, str] | ||
a = 1 | ||
a = "" | ||
a1: Union[int, bool] | ||
a1 = 1 | ||
a1 = True | ||
# error: [invalid-assignment] "Object of type `Literal[b""]` is not assignable to `int | str`" | ||
a = b"" | ||
``` | ||
|
||
## Typing Extensions | ||
|
||
```py | ||
from typing_extensions import Union | ||
|
||
a: Union[int, str] | ||
|
||
def f(): | ||
# revealed: int | str | ||
reveal_type(a) | ||
``` |
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