-
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 (#14742)
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. Closes #14544
- Loading branch information
Showing
4 changed files
with
104 additions
and
2 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
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,75 @@ | ||
# 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. | ||
|
||
```py | ||
class Any: | ||
pass | ||
|
||
x: Any | ||
|
||
def f(): | ||
reveal_type(x) # revealed: Any | ||
|
||
# This verifies that we're not accidentally seeing typing.Any, since str is assignable | ||
# to that but not to our locally defined class. | ||
y: Any = "not an Any" # error: [invalid-assignment] | ||
``` | ||
|
||
## Subclass | ||
|
||
The spec allows you to define subclasses of `Any`. | ||
|
||
TODO: Handle assignments correctly. `Subclass` has an unknown superclass, which might be `int`. The | ||
assignment to `x` should not be allowed, even when the unknown superclass is `int`. The assignment | ||
to `y` should be allowed, since `Subclass` might have `int` as a superclass, and is therefore | ||
assignable to `int`. | ||
|
||
```py | ||
from typing import Any | ||
|
||
class Subclass(Any): | ||
pass | ||
|
||
reveal_type(Subclass.__mro__) # revealed: tuple[Literal[Subclass], Any, Literal[object]] | ||
|
||
x: Subclass = 1 # error: [invalid-assignment] | ||
# TODO: no diagnostic | ||
y: int = Subclass() # error: [invalid-assignment] | ||
|
||
def f() -> Subclass: | ||
pass | ||
|
||
reveal_type(f()) # revealed: Subclass | ||
``` |
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