-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[red-knot] Add
typing.Any
as a spelling for the Any type
We already had a representation for the Any type, which we would use e.g. for expressions without type annotations. We now recognize `typing.Any` as a way to refer to this type explicitly. Like other special forms, this is tracked correctly through aliasing, and isn't confused with local definitions that happen to have the same name.
- Loading branch information
Showing
5 changed files
with
61 additions
and
2 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
crates/red_knot_python_semantic/resources/mdtest/annotations/any.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,50 @@ | ||
# Any | ||
|
||
## Annotation | ||
|
||
`typing.Any` is a way to name the Any type. | ||
|
||
```py | ||
from typing import Any | ||
|
||
x: Any = 1 | ||
x = "foo" | ||
|
||
def f(): | ||
reveal_type(x) # revealed: Any | ||
``` | ||
|
||
## Aliased to a different name | ||
|
||
If you alias `typing.Any` to another name, we still recognize that as a spelling | ||
of the Any type. | ||
|
||
```py | ||
from typing import Any as RenamedAny | ||
|
||
x: RenamedAny = 1 | ||
x = "foo" | ||
|
||
def f(): | ||
reveal_type(x) # revealed: Any | ||
``` | ||
|
||
## Shadowed class | ||
|
||
If you define your own class named `Any`, using that in a type expression refers | ||
to your class, and isn't a spelling of the Any type. | ||
|
||
> Note that the real name of the class shouldn't be `Any`, so that we can | ||
> distinguish it from the Any type in the assertion below. | ||
```py | ||
class LocalAny: | ||
pass | ||
|
||
Any = LocalAny | ||
|
||
x: Any = Any() | ||
|
||
def f(): | ||
reveal_type(x) # revealed: LocalAny | ||
``` |
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