#3286·ty

Object of type `type[P]` is not assignable to attribute `cls` of type `<class 'P'>`: ?

Author: m-u-xyzCreated Apr 16, 2026Updated Sep 29, 2026
Labelsbugtype propertiesattributes

Question

What does this error even mean? I have to admit that I understand neither the "invalid assignment" nor the "implicit shadowing".

python
class P:
    pass

class Test:
    cls = P

    def __init__(self, prefix: P | None):
        if isinstance(prefix, P):
            self.cls = type(prefix)
$ ty check /tmp/t.py
error[invalid-assignment]: Object of type `type[P]` is not assignable to attribute `cls` of type `<class 'P'>`
 --> /tmp/t.py:9:13
  |
7 |     def __init__(self, prefix: P | None):
8 |         if isinstance(prefix, P):
9 |             self.cls = type(prefix)
  |             ^^^^^^^^
  |
info: Implicit shadowing of class `P`. Add an annotation to make it explicit if this is intentional

Version

0.0.31