-
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] PEP 695 type aliases (#14357)
## Summary Add support for (non-generic) type aliases. The main motivation behind this was to get rid of panics involving expressions in (generic) type aliases. But it turned out the best way to fix it was to implement (partial) support for type aliases. ```py type IntOrStr = int | str reveal_type(IntOrStr) # revealed: typing.TypeAliasType reveal_type(IntOrStr.__name__) # revealed: Literal["IntOrStr"] x: IntOrStr = 1 reveal_type(x) # revealed: Literal[1] def f() -> None: reveal_type(x) # revealed: int | str ``` ## Test Plan - Updated corpus test allow list to reflect that we don't panic anymore. - Added Markdown-based test for type aliases (`type_alias.md`)
- Loading branch information
Showing
9 changed files
with
298 additions
and
51 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
crates/red_knot_python_semantic/resources/mdtest/type_alias.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,71 @@ | ||
# Type aliases | ||
|
||
## Basic | ||
|
||
```py | ||
type IntOrStr = int | str | ||
|
||
reveal_type(IntOrStr) # revealed: typing.TypeAliasType | ||
reveal_type(IntOrStr.__name__) # revealed: Literal["IntOrStr"] | ||
|
||
x: IntOrStr = 1 | ||
|
||
reveal_type(x) # revealed: Literal[1] | ||
|
||
def f() -> None: | ||
reveal_type(x) # revealed: int | str | ||
``` | ||
|
||
## `__value__` attribute | ||
|
||
```py | ||
type IntOrStr = int | str | ||
|
||
# TODO: This should either fall back to the specified type from typeshed, | ||
# which is `Any`, or be the actual type of the runtime value expression | ||
# `int | str`, i.e. `types.UnionType`. | ||
reveal_type(IntOrStr.__value__) # revealed: @Todo(instance attributes) | ||
``` | ||
|
||
## Invalid assignment | ||
|
||
```py | ||
type OptionalInt = int | None | ||
|
||
# error: [invalid-assignment] | ||
x: OptionalInt = "1" | ||
``` | ||
|
||
## Type aliases in type aliases | ||
|
||
```py | ||
type IntOrStr = int | str | ||
type IntOrStrOrBytes = IntOrStr | bytes | ||
|
||
x: IntOrStrOrBytes = 1 | ||
|
||
def f() -> None: | ||
reveal_type(x) # revealed: int | str | bytes | ||
``` | ||
|
||
## Aliased type aliases | ||
|
||
```py | ||
type IntOrStr = int | str | ||
MyIntOrStr = IntOrStr | ||
|
||
x: MyIntOrStr = 1 | ||
|
||
# error: [invalid-assignment] | ||
y: MyIntOrStr = None | ||
``` | ||
|
||
## Generic type aliases | ||
|
||
```py | ||
type ListOrSet[T] = list[T] | set[T] | ||
|
||
# TODO: Should be `tuple[typing.TypeVar | typing.ParamSpec | typing.TypeVarTuple, ...]`, | ||
# as specified in the `typeshed` stubs. | ||
reveal_type(ListOrSet.__type_params__) # revealed: @Todo(instance attributes) | ||
``` |
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
Oops, something went wrong.