[red-knot] Fix bugs relating to assignability of dynamic type[] types (#14972)

This commit is contained in:
Alex Waygood
2024-12-15 01:15:10 +00:00
committed by GitHub
parent 53c7ef8bfe
commit fa46ba2306
2 changed files with 45 additions and 9 deletions

View File

@@ -1,13 +1,20 @@
# `type[Any]`
This file contains tests for non-fully-static `type[]` types, such as `type[Any]` and
`type[Unknown]`.
## Simple
```py
def f(x: type[Any]):
def f(x: type[Any], y: type[str]):
reveal_type(x) # revealed: type[Any]
# TODO: could be `<object.__repr__ type> & Any`
reveal_type(x.__repr__) # revealed: Any
# type[str] and type[Any] are assignable to each other
a: type[str] = x
b: type[Any] = y
class A: ...
x: type[Any] = object
@@ -70,3 +77,26 @@ def test(x: Any, y: SomethingUnknown):
reveal_type(y.__class__) # revealed: type[Unknown]
reveal_type(y.__class__.__class__.__class__.__class__) # revealed: type[Unknown]
```
## `type[Unknown]` has similar properties to `type[Any]`
```py
import abc
from typing import Any
from does_not_exist import SomethingUnknown # error: [unresolved-import]
has_unknown_type = SomethingUnknown.__class__
reveal_type(has_unknown_type) # revealed: type[Unknown]
def test(x: type[str], y: type[Any]):
"""Both `type[Any]` and `type[Unknown]` are assignable to all `type[]` types"""
a: type[Any] = x
b: type[str] = y
c: type[Any] = has_unknown_type
d: type[str] = has_unknown_type
def test2(a: type[Any]):
"""`type[Any]` and `type[Unknown]` are also assignable to all instances of `type` subclasses"""
b: abc.ABCMeta = a
b: abc.ABCMeta = has_unknown_type
```