Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[PEP 695] Fix covariance of frozen dataclasses#17783

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
JukkaL merged 1 commit intomasterfromvariance-dataclass
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes fromall 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
[PEP 695] Fix covariance of frozen dataclasses
Fix this conformance test:```@DataClass(frozen=True)class ShouldBeCovariant4[T]:    x: Tvo4_1: ShouldBeCovariant4[float] = ShouldBeCovariant4[int](1)  # OKvo4_2: ShouldBeCovariant4[int] = ShouldBeCovariant4[float](1)  # E```Link:https://github.com/python/typing/blob/main/conformance/tests/generics_variance_inference.py#L66
  • Loading branch information
@JukkaL
JukkaL committedSep 18, 2024
commit4a479dd4efd6d7ae2d559bc236ed9ba8cb715012
3 changes: 2 additions & 1 deletionmypy/subtypes.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionstest-data/unit/check-python312.test
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp