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

[PEP 695] Fix covariance of frozen dataclasses #17783

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2004,7 +2004,8 @@ def infer_variance(info: TypeInfo, i: int) -> bool:
tvar = info.defn.type_vars[i]
self_type = fill_typevars(info)
for member in all_non_object_members(info):
if member in ("__init__", "__new__"):
# __mypy-replace is an implementation detail of the dataclass plugin
if member in ("__init__", "__new__", "__mypy-replace"):
continue
node = info[member].node
if isinstance(node, Var) and node.type is None:
Expand Down
19 changes: 19 additions & 0 deletions test-data/unit/check-python312.test
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,25 @@ if int():
if int():
f = e

[case testPEP695InferVarianceInFrozenDataclass]
# flags: --enable-incomplete-feature=NewGenericSyntax
from dataclasses import dataclass

@dataclass(frozen=True)
class Covariant[T]:
x: T

cov1: Covariant[float] = Covariant[int](1)
cov2: Covariant[int] = Covariant[float](1) # E: Incompatible types in assignment (expression has type "Covariant[float]", variable has type "Covariant[int]")

@dataclass(frozen=True)
class Invariant[T]:
x: list[T]

inv1: Invariant[float] = Invariant[int]([1]) # E: Incompatible types in assignment (expression has type "Invariant[int]", variable has type "Invariant[float]")
inv2: Invariant[int] = Invariant[float]([1]) # E: Incompatible types in assignment (expression has type "Invariant[float]", variable has type "Invariant[int]")
[builtins fixtures/tuple.pyi]

[case testPEP695InferVarianceCalculateOnDemand]
# flags: --enable-incomplete-feature=NewGenericSyntax

Expand Down
Loading