-
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.
This adds support for `type[Any]`, which represents an unknown type (not an instance of an unknown type), and `type`, which we are choosing to interpret as `type[object]`. Closes #14546
- Loading branch information
Showing
6 changed files
with
232 additions
and
50 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
crates/red_knot_python_semantic/resources/mdtest/type_of/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,53 @@ | ||
# type[Any] | ||
|
||
## Simple | ||
|
||
```py | ||
def f(x: type[Any]): | ||
reveal_type(x) # revealed: type[Any] | ||
# TODO: could be `<object.__repr__ type> & Any` | ||
reveal_type(x.__repr__) # revealed: Any | ||
|
||
class A: ... | ||
|
||
x: type[Any] = object | ||
x: type[Any] = type | ||
x: type[Any] = A | ||
x: type[Any] = A() # error: [invalid-assignment] | ||
``` | ||
|
||
## Bare type | ||
|
||
The interpretation of bare `type` is not clear: existing wording in the spec does not match the | ||
behavior of mypy or pyright. For now we interpret it as simply "an instance of `builtins.type`", | ||
which is equivalent to `type[object]`. This is similar to the current behavior of mypy, and pyright | ||
in strict mode. | ||
|
||
```py | ||
def f(x: type): | ||
reveal_type(x) # revealed: type | ||
reveal_type(x.__repr__) # revealed: @Todo(instance attributes) | ||
|
||
class A: ... | ||
|
||
x: type = object | ||
x: type = type | ||
x: type = A | ||
x: type = A() # error: [invalid-assignment] | ||
``` | ||
|
||
## type[object] != type[Any] | ||
|
||
```py | ||
def f(x: type[object]): | ||
reveal_type(x) # revealed: type[object] | ||
# TODO: bound method types | ||
reveal_type(x.__repr__) # revealed: Literal[__repr__] | ||
|
||
class A: ... | ||
|
||
x: type[object] = object | ||
x: type[object] = type | ||
x: type[object] = A | ||
x: type[object] = A() # error: [invalid-assignment] | ||
``` |
Oops, something went wrong.