How to annotate my subclass to avoid mypy error: Class cannot sublass "Foo" (has type "Any")

Viewed 628

I have a common library, lib_common that defines a basic pydantic BaseModel that I use in all other packages:

├── lib_common
    ├── __init__.py
    ├── models.py

where models.py contains:

from pydantic import BaseModel, Extra

class StrictBaseModel(BaseModel):
   class Config:
        extra = Extra.forbid

Whenever I try to import this into other packages and inherit from StrictBaseModel to define a pydantic model, I get the error: Class cannot subclass "StrictBaseModel" (has type "Any")

from pydantic import Field

from lib_common.models import StrictBaseModel

class Foo(StrictBaseModel):
    bar: str = Field(...)

I haven't found a good answer to what that actually means. What hints to I need to add to not get this warning? I don't want to just mute it with a # type: ignore or change my mypy settings. It's really weird to me that this error only occurs since lib_common is a separate python package that my other packages depend on.

I've seen multiple posts on this, with good answers like here: mypy calls error: Class cannot subclass 'ObjectType' (has type 'Any') on graphene that says to build a stub-file for the package you are importing. But these are all exclusively using external libraries. However, I have the option to just type-hint my imported/inherited class correctly.

How do I solve this? Thanks

0 Answers
Related