Allowable subclasses for a function that accepts base class with a custom metaclass #9082
-
I'm using a library that uses Pydantic. Pydantic's Is this intended behavior? Example that can demonstrate this without Pydantic at all: Code sample in pyright playground from abc import ABCMeta
class MyMeta(ABCMeta):
...
class MyBaseCls(metaclass=MyMeta):
...
class MySubclass(MyBaseCls):
...
def my_fun(some_cls: MyBaseCls):
...
my_fun(MySubclass)
# Argument of type "type[MySubclass]" cannot be assigned to parameter "some_cls" of type "MyBaseCls" in function "my_fun"
# "type[MyMeta]" is not assignable to "type[MyBaseCls]" |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The annotation you're using for the Alternatively, if you want to accept any instance of the |
Beta Was this translation helpful? Give feedback.
The annotation you're using for the
some_cls
parameter indicates that the corresponding argument value must be an instance ofMyBaseCls
. I think what you intend here is that the value must be a class object that is a subclass ofMyBaseCls
. If my assumption is correct, then you should change the annotation totype[MyBaseCls]
. This eliminates the error.Alternatively, if you want to accept any instance of the
MyMeta
metaclass, you could change the parameter's annotation toMyMeta
.