Inference bug when subclassing a parameter? #11379
Replies: 1 comment
|
Your own guess is the answer — it's because When the base in a def decorate_cls[T](cls: type[T]) -> type[T]:
class SubClass(cls):
pass
reveal_type(SubClass) # type[SubClass]
return SubClass # error: type[SubClass] not assignable to type[T@decorate_cls]
class Concrete: pass
def concrete_case() -> type[Concrete]:
class Sub(Concrete):
pass
return Sub # fineAnd the reason your assert issubclass(SubClass, cls)
reveal_type(SubClass) # type[<subclass of SubClass and object*>]*
return cast(type[T], SubClass) # also silences it, but proves nothingBetween the two I'd keep the Worth knowing the def decorate_type_call[T](cls: type[T]) -> type[T]:
return type("SubClass", (cls,), {}) # error: type[SubClass] not assignable to type[T]Same cause — the bases are a runtime tuple, so there's nothing static to record. The general shape of it: a class statement with a dynamic base is one of the places where the static model genuinely can't follow what the runtime does, so the type system needs you to assert the connection. |
Uh oh!
There was an error while loading. Please reload this page.
Code sample in pyright playground
Is this a bug? Or is this because
clsis a term?All reactions