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

Inheriting from untyped class and overriding __init__ breaks abstract class detection #17781

Open
Kakadus opened this issue Sep 18, 2024 · 1 comment
Labels
bug mypy got something wrong

Comments

@Kakadus
Copy link

Kakadus commented Sep 18, 2024

Bug Report

I'm creating an ABC inheriting from an untyped class and override its __init__ function. Then, all child classes are detected as being abstract, even though they are not.

The error is not caused when removing the overriding the init function or removing the inheritance from the untyped base class

To Reproduce

from abc import ABC, abstractmethod

from untyped import Base  # type: ignore[import-not-found]


class Abstract(ABC, Base):
    
    def __init__(self):
       super().__init__()
    
    @abstractmethod
    def method(self) -> None: ...


class Concrete(Abstract):
    
    def method(self) -> None: ...
    

C: type[Abstract] = Concrete  # E: Can only assign concrete classes to a variable of type "type[Abstract]"  [type-abstract]

https://mypy-play.net/?mypy=1.11.2&python=3.12&gist=6b11b5a5c3ef54947fb0874cade6ce98

Expected Behavior

This code should not produce any errors as the abstract method is implemented.

Actual Behavior

  • main.py:20: error: Can only assign concrete classes to a variable of type "type[Abstract]" [type-abstract]
    Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.11.2 & master
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.12
@Kakadus Kakadus added the bug mypy got something wrong label Sep 18, 2024
@Kakadus
Copy link
Author

Kakadus commented Sep 18, 2024

Making Abstract generic, this leads to this funny error: Expression is of type "dict[str, type[Abstract[Any]]]", not "dict[str, type[Abstract[Any]]]":

from abc import ABC, abstractmethod

from typing import assert_type, TypeVar, Generic

from untyped import Base  # type: ignore[import-not-found]

T = TypeVar("T")

class Abstract(ABC, Base, Generic[T]):
    
    def __init__(self):
       super().__init__()
    
    @abstractmethod
    def method(self) -> None: ...


class ConcreteA(Abstract[str]):
    
    def method(self) -> None: ...

class ConcreteB(Abstract[int]):
    
    def method(self) -> None: ...
    

a = {"a": ConcreteA, "b": ConcreteB}

assert_type(a, dict[str, type[Abstract]])  # Expression is of type "dict[str, type[Abstract[Any]]]", not "dict[str, type[Abstract[Any]]]"  [assert-type]

https://mypy-play.net/?mypy=1.11.2&python=3.12&gist=4f015c7663819c49d6f1d86f751f9187

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