-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Recognizing wrapped classes as supporting a Protocol #6568
Copy link
Copy link
Closed
Labels
featurepriority-2-lowtopic-pluginsThe plugin API and ideas for new pluginsThe plugin API and ideas for new plugins
Description
from dataclasses import dataclass
from typing import Dict
from typing_extensions import Protocol
@dataclass
class A:
pass
class IsDataclass(Protocol):
__dataclass_fields__: Dict
def dataclass_only(x: IsDataclass):
pass
dataclass_only(A())When running mypy on this code, I'd expect no warning, but I get
test.py:16: error: Argument 1 to "dataclass_only" has incompatible type "A"; expected "IsDataclass".
While checking for __dataclass_fields__ might not look like the best thing to go for, it's what dataclasses.is_dataclass checks for as well, so this seems like the most reliable condition right now.
I'd assume it happens because the dataclass-wrapper adds this attribute, and mypy only checks the class definition itself. Can this be fixed?
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
featurepriority-2-lowtopic-pluginsThe plugin API and ideas for new pluginsThe plugin API and ideas for new plugins