Skip to content

False error: Never has no attribute "foo" [attr-defined] #17198

Closed
@AndreyMZ

Description

@AndreyMZ

To Reproduce

test-mypy.py

from abc import ABCMeta, abstractmethod


class A(metaclass=ABCMeta):
    @abstractmethod
    def foo(self) -> None:
        pass

class B1(A, metaclass=ABCMeta):
    pass

class B2(A, metaclass=ABCMeta):
    pass

class C1(B1, B2):
    def foo(self) -> None:
        print("C1")

class C2(B1, B2):
    def foo(self) -> None:
        print("C2")


class Main:
    def __init__(self, b2: B2):
        self.b2: B2 = b2

    def f(self) -> None:
        if isinstance(self.b2, C1):
            pass
        elif isinstance(self.b2, C2):
            pass
        else:
            assert False

        self.b2.foo()

    def g(self) -> None:
        b2 = self.b2
        if isinstance(b2, C1):
            pass
        elif isinstance(b2, C2):
            pass
        else:
            assert False

        b2.foo()


if __name__ == '__main__':
    Main(C1()).f()
    Main(C2()).f()

Expected Behavior

> mypy .
Success: no issues found in 1 source file

Actual Behavior

> mypy .
test-mypy.py:36: error: Never has no attribute "foo"  [attr-defined]
Found 1 error in 1 file (checked 1 source file)

Note that there is no such error in Main.g.

Your Environment

  • Mypy version used: 1.10.0
  • Mypy command-line flags: -
  • Mypy configuration options from mypy.ini (and other config files): -
  • Python version used: 3.12.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions