Skip to content

Commit

Permalink
Add dataclass InitVar test case (#12798)
Browse files Browse the repository at this point in the history
This test case cover a recent change in functionality:
if an attribute is defined as an InitVar, it can't be
assigned to. Previously this was supported (probably
by accident).
  • Loading branch information
JukkaL authored May 16, 2022
1 parent e6bbf5f commit 85c2159
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions test-data/unit/check-dataclasses.test
Original file line number Diff line number Diff line change
Expand Up @@ -1724,3 +1724,23 @@ T = TypeVar("T", bound="NotDefined") # E: Name "NotDefined" is not defined
class C(Generic[T]):
x: float
[builtins fixtures/dataclasses.pyi]

[case testDataclassInitVarCannotBeSet]
# flags: --python-version 3.7
from dataclasses import dataclass, InitVar

@dataclass
class C:
x: InitVar[int] = 0
y: InitVar[str] = ''

def f(self) -> None:
# This works at runtime, but it seems like an abuse of the InitVar
# feature and thus we don't support it
self.x = 1 # E: "C" has no attribute "x"
self.y: str = 'x' # E: "C" has no attribute "y"

c = C()
c2 = C(x=1)
c.x # E: "C" has no attribute "x"
[builtins fixtures/dataclasses.pyi]

0 comments on commit 85c2159

Please sign in to comment.