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

[type-abstract] reports with a tuple when the actual type is a dict #17399

Open
bzoracler opened this issue Jun 18, 2024 · 2 comments
Open

[type-abstract] reports with a tuple when the actual type is a dict #17399

bzoracler opened this issue Jun 18, 2024 · 2 comments
Labels
bug mypy got something wrong

Comments

@bzoracler
Copy link
Contributor

Bug Report, To Reproduce, & Actual Behaviour

See mypy Playground

from abc import ABC, abstractmethod

class Abstract(ABC):
    @abstractmethod
    def method(self) -> None: ...

class Unimplemented(Abstract): ...

class Parent:
    others: dict[str, type[Abstract]]

class Child(Parent):
    others = {"abstract": Unimplemented}  # E: Only concrete class can be given where "tuple[str, type[Abstract]]" is expected  [type-abstract]

Expected Behavior

Reports the error message with "dict[str, type[Abstract]]".

Your Environment

  • Mypy version used: 1.10
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.10, 3.12
@bzoracler bzoracler added the bug mypy got something wrong label Jun 18, 2024
@TeamSpen210
Copy link
Contributor

The reason this happens is because when you have type[T], you're allowed to call that with the appropriate parameters to get a result of T out. In order for that to be allowed, Mypy checks to see if the provided type matches the specified type's __init__.With an abstract class, it's not legal to create an instance of that, so that means they cannot be assigned to type[T]. See here in the type specification docs, and also #4717. To solve this, we sort of need a way to indicate whether or not you want to be able to construct the type...

@Kakadus
Copy link

Kakadus commented Sep 18, 2024

The reason this happens is because when you have type[T], you're allowed to call that with the appropriate parameters to get a result of T out. In order for that to be allowed, Mypy checks to see if the provided type matches the specified type's __init__.With an abstract class, it's not legal to create an instance of that, so that means they cannot be assigned to type[T]. See here in the type specification docs, and also #4717. To solve this, we sort of need a way to indicate whether or not you want to be able to construct the type...

Thanks for the reply, you are 100% right. Actually my comment was off-topic, so I deleted it (basically the same as OP, just without the dict part). The issue from the OP is that the error message complains about a tuple[str, type[Abstract]] while dict[str, type[Abstract] was used for annotation

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

3 participants