Why does pylance point out a type issue when using ABCMeta.register?

Viewed 27

Example code using register to add C as a virtual subclass of A:

from abc import ABC, abstractmethod

class A(ABC):
    @abstractmethod
    def f(self) -> int:
        ...

class B(A):  # normal subclass
    def f(self) -> int:
        return 1

class C:  # no subclass, to be registered
    def f(self) -> int:
        return 2

A.register(C)

def caller(x: A):
    print(isinstance(x, A))
    print(x.f())

b = B()
c = C()

caller(b)
caller(c)  # ← This is where pylance will complain

This yields:

True
1
True
2

So this works as expected. However, Pylance complains about a type issue in the very last line of the code: Argument of type "C" cannot be assigned to parameter "x" of type "A" in function "caller" "C" is incompatible with "A"PylancereportGeneralTypeIssues.

Why is that?

0 Answers
Related