Skip to content

Commit

Permalink
Handle type[Any] correctly (#14876)
Browse files Browse the repository at this point in the history
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
dcreager authored Dec 10, 2024
1 parent 03fb2e5 commit d4126f6
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 50 deletions.
53 changes: 53 additions & 0 deletions crates/red_knot_python_semantic/resources/mdtest/type_of/any.md
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]
```
Loading

0 comments on commit d4126f6

Please sign in to comment.