Skip to content

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

Closed as not planned
@awaelchli

Description

@awaelchli

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-join-v-unionUsing join vs. using unions

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions