mypy icon indicating copy to clipboard operation
mypy copied to clipboard

mypy fails to find base method for override when an `__init__.py` is present for sources root

Open aidangallagher4 opened this issue 1 year ago • 2 comments

Bug Report

[Example given here is as stripped down as possible.] The structure of my project is as follows

src
├── __init__.py
├── parent.py
└── child.py
pyproject.toml
requirements.txt

When overriding a classmethod (create_from) of a base class Parent in parent.py i'm seeing the error

Method "create_from" is marked as an override, but no base method was found with this name  [misc]

This only occurs when __init__.py exists. If I delete the __init__.py then the error vanishes

I have also noticed that this error doesn't occur if Parent and Child are in the same module

To Reproduce

You will need to reproduce the structure above with the following files

__init__.py: blank

src/parent.py

from typing import Self

class Parent:
    @classmethod
    def create_from(cls) -> Self:
        return cls()

src/child.py

from typing import Self, override

from core import Parent

class Child(Parent):
    @classmethod
    @override
    def create_from(cls) -> Self:
        return cls()

pyproject.toml

[tool.mypy]
ignore_missing_imports = true

requirements.txt

mypy~=1.15.0

Expected Behavior

mypy should pass

Actual Behavior

It raises

Method "create_from" is marked as an override, but no base method was found with this name  [misc]

Your Environment

See above for mypy configuration: it is run in the command line with mypy src. I am running this on Python 3.13.1, and as in the requirements.txt, version 1.15.0 of mypy

aidangallagher4 avatar Apr 15 '25 09:04 aidangallagher4

Hi! This looks like the expected behavior. Placing an __init__.py file in src indicates that src is the top-level package. If you intend for parent and child to be top-level modules (and not submodules of src), you should omit the __init__.py file. If you do intend for src to be a package, then you'll need to adjust the import from core import Parent (sic) accordingly (e.g. to from .parent import Parent or from src.parent import Parent). See also this SO answer for more on how filesystem layout translates to modules names in Python.

When src/__init__.py exists, mypy is unable to follow the import from parent import Parent since no module named parent exists (only src.parent does). As a result, no type information is available for Parent, so mypy treats Parent as having type Any in subsequent analysis. This is the cause of the override error in Child, since the parent class (Any) does not have a classmethod named create_from. Mypy usually warns you about cases like this by emitting an import-not-found error; however, that error was suppressed due to ignore_missing_imports being set to true in pyproject.toml. It's usually best to only set ignore_missing_imports=true for specific modules that you know won't have type information available and which do not contribute to static type analysis. You can refer to the Common Issues and Missing Imports sections of the docs for more on the effects of this setting and and other tips on how to handle missing import issues.

Since mypy is behaving as expected and in accordance with how Python's import system works, I'm closing this as not-planned.

brianschubert avatar Apr 16 '25 13:04 brianschubert

thanks very much, brian

aidangallagher4 avatar Apr 16 '25 15:04 aidangallagher4