Skip to content

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

Description

@AndreyMZ

To Reproduce

test-mypy.py

fromabcimportABCMeta, abstractmethodclassA(metaclass=ABCMeta):
@abstractmethoddeffoo(self) ->None:
passclassB1(A, metaclass=ABCMeta):
passclassB2(A, metaclass=ABCMeta):
passclassC1(B1, B2):
deffoo(self) ->None:
print("C1")
classC2(B1, B2):
deffoo(self) ->None:
print("C2")
classMain:
def__init__(self, b2: B2):
self.b2: B2=b2deff(self) ->None:
ifisinstance(self.b2, C1):
passelifisinstance(self.b2, C2):
passelse:
assertFalseself.b2.foo()
defg(self) ->None:
b2=self.b2ifisinstance(b2, C1):
passelifisinstance(b2, C2):
passelse:
assertFalseb2.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