Mypy: Cannot Subclass Error

Created on 31 Oct 2017  Â·  8Comments  Â·  Source: python/mypy

I have a dummy base class:

class BaseModel:
    def __init__(self) -> None:
        pass

and I have another class extending this dummy base class:

class DendrogramModel(BaseModel):
    def __init__(self, test_dtm: Optional[pd.DataFrame] = None,
                 test_option: Optional[DendroOption] = None) -> None:
        super().__init__()
        self._test_dtm = test_dtm
        self._test_option = test_option

When I type check, mypy gives me the following error:
error: Class cannot subclass 'BaseModel' (has type 'Any')

I don't know why. Is it because I cannot extend the dummy base class?

question

Most helpful comment

Not stupid! It takes time to understand how all these things work together.
Explaining your problems to someone else is often a helpful debugging
technique. (So useful, in fact, that people have called it "rubber duck
debugging" -- explain the issue to your rubber ducky and suddenly it
becomes clear to you. There's even Wikipedia page for this. :-)

All 8 comments

The other class is in a different file right? And you have --follow-imports=strict on? You need to tell mypy to check both files.

Yes, they are in different files.

I don't think follow import allows strict.

Here is the command I used mypy .\lexos\models\dendro_model.py --ignore-missing-imports --strict

Yeah sorry, I meant skip, not strict. But it sounds like the other module can't be found. Where is it?

I also think that is caused by module not found.

Here is part of my project structure:

Lexos
- lexos/
  - __init__.py
  - models/
     - dendrogram_model.py
     - base_model.py
- test/
  - __ init__.py

Here is the import statement used in dendrogram_model.py

from lexos.models.base_model import BaseModel

according to the mypy documentation, mypy climbs up the __init__.py file, then it will definitely successfully found my project root.

There's no __init__.py in models/.

On Mon, Oct 30, 2017 at 8:14 PM, chantisnake notifications@github.com
wrote:

I also think that is caused by module not found.

Here is part of my project structure:

Lexos

  • lexos/

    • __init__.py

    • models/



      • dendrogram_model.py


      • base_model.py



  • test/

    • __ init__.py

Here is the import statement used in dendrogram_model.py

from lexos.models.base_model import BaseModel

according to the documentation of mypy documentation
http://mypy.readthedocs.io/en/latest/command_line.html#finding-imports,
mypy climbs up the __init__.py file, then it will definitely successfully
found my project root.

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/python/mypy/issues/4180#issuecomment-340647678, or mute
the thread
https://github.com/notifications/unsubscribe-auth/ACwrMlYT7wSIRhTywVHGZGg1VmMZvWLRks5sxpB8gaJpZM4QMDjt
.

--
--Guido van Rossum (python.org/~guido)

uh! how stupid of me...

Not stupid! It takes time to understand how all these things work together.
Explaining your problems to someone else is often a helpful debugging
technique. (So useful, in fact, that people have called it "rubber duck
debugging" -- explain the issue to your rubber ducky and suddenly it
becomes clear to you. There's even Wikipedia page for this. :-)

Not stupid!

:+1: Having it documented here also helped me resolve the same issue in my code :-)

Was this page helpful?
0 / 5 - 0 ratings