[ty] support generic aliases in type[...], like type[C[int]] (#21552)
Closes https://github.com/astral-sh/ty/issues/1101.
This commit is contained in:
@@ -171,7 +171,7 @@ class Config:
|
||||
import generic_a
|
||||
import generic_b
|
||||
|
||||
# TODO should be error: [invalid-assignment] "Object of type `<class 'generic_b.Container[int]'>` is not assignable to `type[generic_a.Container[int]]`"
|
||||
# error: [invalid-assignment] "Object of type `<class 'generic_b.Container[int]'>` is not assignable to `type[generic_a.Container[int]]`"
|
||||
container: type[generic_a.Container[int]] = generic_b.Container[int]
|
||||
```
|
||||
|
||||
|
||||
@@ -174,6 +174,39 @@ def _(x: Foo[int], y: Bar[str], z: list[bytes]):
|
||||
reveal_type(type(z)) # revealed: type[list[bytes]]
|
||||
```
|
||||
|
||||
## Checking generic `type[]` types
|
||||
|
||||
```toml
|
||||
[environment]
|
||||
python-version = "3.12"
|
||||
```
|
||||
|
||||
```py
|
||||
class C[T]:
|
||||
pass
|
||||
|
||||
class D[T]:
|
||||
pass
|
||||
|
||||
var: type[C[int]] = C[int]
|
||||
var: type[C[int]] = D[int] # error: [invalid-assignment] "Object of type `<class 'D[int]'>` is not assignable to `type[C[int]]`"
|
||||
```
|
||||
|
||||
However, generic `Protocol` classes are still TODO:
|
||||
|
||||
```py
|
||||
from typing import Protocol
|
||||
|
||||
class Proto[U](Protocol):
|
||||
def some_method(self): ...
|
||||
|
||||
# TODO: should be error: [invalid-assignment]
|
||||
var: type[Proto[int]] = C[int]
|
||||
|
||||
def _(p: type[Proto[int]]):
|
||||
reveal_type(p) # revealed: type[@Todo(type[T] for protocols)]
|
||||
```
|
||||
|
||||
## `@final` classes
|
||||
|
||||
`type[]` types are eagerly converted to class-literal types if a class decorated with `@final` is
|
||||
|
||||
@@ -243,13 +243,13 @@ static_assert(is_assignable_to(TypeOf[Bar[int]], type[Foo[int]]))
|
||||
static_assert(is_assignable_to(TypeOf[Bar[bool]], type[Foo[int]]))
|
||||
static_assert(is_assignable_to(TypeOf[Bar], type[Foo[int]]))
|
||||
static_assert(is_assignable_to(TypeOf[Bar[Any]], type[Foo[int]]))
|
||||
static_assert(is_assignable_to(TypeOf[Bar[Unknown]], type[Foo[int]]))
|
||||
static_assert(is_assignable_to(TypeOf[Bar], type[Foo]))
|
||||
static_assert(is_assignable_to(TypeOf[Bar[Any]], type[Foo[Any]]))
|
||||
static_assert(is_assignable_to(TypeOf[Bar[Any]], type[Foo[int]]))
|
||||
|
||||
# TODO: these should pass (all subscripts inside `type[]` type expressions are currently TODO types)
|
||||
static_assert(not is_assignable_to(TypeOf[Bar[int]], type[Foo[bool]])) # error: [static-assert-error]
|
||||
static_assert(not is_assignable_to(TypeOf[Foo[bool]], type[Bar[int]])) # error: [static-assert-error]
|
||||
static_assert(not is_assignable_to(TypeOf[Bar[int]], type[Foo[bool]]))
|
||||
static_assert(not is_assignable_to(TypeOf[Foo[bool]], type[Bar[int]]))
|
||||
```
|
||||
|
||||
## `type[]` is not assignable to types disjoint from `builtins.type`
|
||||
|
||||
Reference in New Issue
Block a user