Skip to content

Inconsistent error "Cannot instantiate abstract class" depends on size of iterable #13044

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

Closed
awaelchli opened this issue Jun 30, 2022 · 3 comments
Labels
bug mypy got something wrong topic-join-v-union Using join vs. using unions

Comments

@awaelchli
Copy link

Bug Report

The mypy check over a loop with three or more classes that have abstract methods fails.
Apologies in advance for the terrible title but I couldn't find a good short way to express the issue here.

To Reproduce

The following fails:

from abc import ABC, abstractmethod


class Base(ABC):

    @staticmethod
    @abstractmethod
    def detect() -> bool:
        pass


class A(Base):
    @staticmethod
    def detect() -> bool:
        return True


class AA(Base):
    @staticmethod
    def detect() -> bool:
        return True


class AAA(Base):
    @staticmethod
    def detect() -> bool:
        return True


# With three of them mypy FAILS.
# ------------------------------
for cls in (AAA, AA, A):
    if cls.detect():
        cls()

The error:

error: Cannot instantiate abstract class "Base" with abstract attribute "detect"  [abstract]

However, this ONLY appears with three of these subclasses in a tuple. Any combination of classes that have three or less passes the mypy test:

# Any of the below combinations of iterables work however

for cls in (AA, A):
    if cls.detect():
        cls()


for cls in (AAA, AA):
    if cls.detect():
        cls()


for cls in (AAA, A):
    if cls.detect():
        cls()

Expected Behavior

There should either be consistently no error or an error in all cases regardless of over how many classes we iterate.

Your Environment

  • Mypy version used: mypy 0.961 (compiled: yes)
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.10
  • Operating system and version: MacOS
@awaelchli awaelchli added the bug mypy got something wrong label Jun 30, 2022
@awaelchli
Copy link
Author

I'm not sure exactly, but perhaps this is a duplicate of #3115?

@AlexWaygood
Copy link
Member

I'm not sure exactly, but perhaps this is a duplicate of #3115?

Correct

@AlexWaygood AlexWaygood added the topic-join-v-union Using join vs. using unions label Jun 30, 2022
@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Jun 30, 2022
@AlexWaygood
Copy link
Member

Also I can't reproduce: I get the same (bad) error whether it's a two-element tuple or a three-element tuple: https://mypy-play.net/?gist=fa096551bb49746165501ebd5016670b

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

2 participants