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

(regression: 0.960) Generic[Protocol] fails with dataclass #12876

Closed
JSS95 opened this issue May 26, 2022 · 4 comments · Fixed by #12908
Closed

(regression: 0.960) Generic[Protocol] fails with dataclass #12876

JSS95 opened this issue May 26, 2022 · 4 comments · Fixed by #12908
Labels
bug mypy got something wrong topic-dataclasses

Comments

@JSS95
Copy link

JSS95 commented May 26, 2022

Bug Report

The following code uses Generic with Protocol for dataclass. It passes with mypy 0.950, but fails with 0.960.

from dataclasses import dataclass
from typing import Protocol, Dict, TypeVar, Generic


class DataclassProtocol(Protocol):
    __dataclass_fields__: Dict


T = TypeVar("T", bound=DataclassProtocol)


@dataclass
class MyDataclass:
    x: int = 1


class MyGeneric(Generic[T]):
    ...


class MyClass(MyGeneric[MyDataclass]):
    ...

Error messge:

error: Type argument "MyDataclass" of "MyGeneric" must be a subtype of "DataclassProtocol"

Your Environment

  • Mypy version used: 0.960
  • Mypy command-line flags: no flag
  • Mypy configuration options from mypy.ini (and other config files): no config
  • Python version used: 3.10
  • Operating system and version: Ubuntu 20.04

(Edit)

Using typing.ClassVar fixes this.

from dataclasses import Field
from typing import Protocol, ClassVar, Dict

class DataclassProtocol(Protocol):
    __dataclass_fields__: ClassVar[Dict[str, Field]]
@JSS95 JSS95 added the bug mypy got something wrong label May 26, 2022
@JSS95 JSS95 changed the title (regression: 0.960) Generic[Protocol] fails (regression: 0.960) Generic[Protocol] fails with dataclass May 26, 2022
@hauntsaninja
Copy link
Collaborator

hauntsaninja commented May 27, 2022

Thanks for opening a new issue with repro! mypy_primer bisects this to #12762

JukkaL added a commit that referenced this issue May 30, 2022
The plugin may add attributes that are needed to perform the bound
check.

Fixes #12876.
JukkaL added a commit that referenced this issue May 30, 2022
The plugin may add attributes that are needed to perform the bound
check.

Fixes #12876.
JukkaL added a commit that referenced this issue Jun 1, 2022
The plugin may add attributes that are needed to perform the bound
check.

Fixes #12876.
@JSS95
Copy link
Author

JSS95 commented Feb 21, 2023

@JukkaL

Hello. I am encountering the same issue again.
Until mypy v0.982 the Generic[Protocol] worked fine, but from v0.990 to the latest v1.0.1 the test fails with the same error message.

@hauntsaninja
Copy link
Collaborator

Add ClassVar to the Protocol, I think

@JSS95
Copy link
Author

JSS95 commented Feb 22, 2023

@hauntsaninja Thanks! It worked.

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-dataclasses
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants