Skip to content

Commit 85c2159

Browse files
authored
Add dataclass InitVar test case (#12798)
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).
1 parent e6bbf5f commit 85c2159

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

test-data/unit/check-dataclasses.test

+20
Original file line numberDiff line numberDiff line change
@@ -1724,3 +1724,23 @@ T = TypeVar("T", bound="NotDefined") # E: Name "NotDefined" is not defined
17241724
class C(Generic[T]):
17251725
x: float
17261726
[builtins fixtures/dataclasses.pyi]
1727+
1728+
[case testDataclassInitVarCannotBeSet]
1729+
# flags: --python-version 3.7
1730+
from dataclasses import dataclass, InitVar
1731+
1732+
@dataclass
1733+
class C:
1734+
x: InitVar[int] = 0
1735+
y: InitVar[str] = ''
1736+
1737+
def f(self) -> None:
1738+
# This works at runtime, but it seems like an abuse of the InitVar
1739+
# feature and thus we don't support it
1740+
self.x = 1 # E: "C" has no attribute "x"
1741+
self.y: str = 'x' # E: "C" has no attribute "y"
1742+
1743+
c = C()
1744+
c2 = C(x=1)
1745+
c.x # E: "C" has no attribute "x"
1746+
[builtins fixtures/dataclasses.pyi]

0 commit comments

Comments
 (0)