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

Adding third ABC type to a loop causes false positive type error #16509

Closed
johnthagen opened this issue Nov 16, 2023 · 4 comments
Closed

Adding third ABC type to a loop causes false positive type error #16509

johnthagen opened this issue Nov 16, 2023 · 4 comments
Labels
bug mypy got something wrong topic-join-v-union Using join vs. using unions

Comments

@johnthagen
Copy link
Contributor

Bug Report

Adding a third ABC to a list of types in a for loop causes a Mypy type checking false positive error.

In the code below, if you remove the second C2 there is no error, but adding a third ABC to the iteration for some reason triggers this error.

To Reproduce

from abc import ABC, abstractmethod


class Base(ABC):
    @abstractmethod
    def a(self) -> None:
        ...


class C1(Base):
    def a(self) -> None:
        return


class C2(Base):
    def a(self) -> None:
        return


for c in (C1, C2, C2):
    c()

Expected Behavior

No type check error.

Actual Behavior

Produces false positive type check error:

main.py:24: error: Cannot instantiate abstract class "Abstract" with abstract attribute "a"  [abstract]

Your Environment

  • Mypy version used: 1.7.0
  • Mypy command-line flags: N/A
  • Mypy configuration options from mypy.ini (and other config files):
[tool.mypy]
strict = true
  • Python version used: 3.10.11
@johnthagen johnthagen added the bug mypy got something wrong label Nov 16, 2023
@AlexWaygood AlexWaygood added the topic-join-v-union Using join vs. using unions label Nov 16, 2023
@AlexWaygood
Copy link
Member

I think this is just another instance of #3115?

@JelleZijlstra
Copy link
Member

It's true though that mypy behaves differently depending on whether you iterate over (C1, C2) or (C1, C2, C2). Here's a slightly expanded mypy play: https://mypy-play.net/?mypy=latest&python=3.10&gist=41c9f315bc22c5c19c40f60196042567

I'm not sure how to explain that change. reveal_type() doesn't change between the two cases.

@KotlinIsland
Copy link
Contributor

If we change it to basedmypy we can see that it's working as expected
https://mypy-play.net/?mypy=basedmypy-latest&python=3.10&gist=41c9f315bc22c5c19c40f60196042567

@hauntsaninja
Copy link
Collaborator

Fixed in #17408

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-join-v-union Using join vs. using unions
Projects
None yet
Development

No branches or pull requests

5 participants