How to declare instance variables in ABC which implementations could have different types

I have an ABC that there will be an instance variable, let's say record in all of its implementation classes. For readability, I want to let readers know the instances of that ABC are supposed to have a record instance variable.

However, the type of record varies in different implementation classes. And I want to avoid having my ABC specify all the possible types that record could use (e.g. record: DjangoModelA | DjangoModelB in ABC) because this sounds very not right to me.

Here is a simple version of my current code:

class Abstract(ABC):
    record: None

class SubA(Abstract):
    record: DjangoModelA

class SubB(Abstract):
    record: DjangoModelB

which mypy complains:

error: Incompatible types in assignment (expression has type "DjangoModelA", base class "Abstract" defined the type as "None")
error: Incompatible types in assignment (expression has type "DjangoModelB", base class "Abstract" defined the type as "None")

How should I declare the abstract base class to make mypy and me happy with it?



from Recent Questions - Stack Overflow https://ift.tt/3yQUQMQ
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation