Skip to content

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

@smurfix

Description

@smurfix

Question

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions