Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
AndreyMZ opened this issue Apr 30, 2024 · 0 comments
Open

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

AndreyMZ opened this issue Apr 30, 2024 · 0 comments
Labels
bug mypy got something wrong

Comments

@AndreyMZ
Copy link

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
@AndreyMZ AndreyMZ added the bug mypy got something wrong label Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

1 participant