Skip to content
Closed
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/plugins/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ def collect_attributes(self) -> Optional[List[DataclassAttribute]]:
if isinstance(node, PlaceholderNode):
# This node is not ready yet.
return None
assert isinstance(node, Var)
if not isinstance(node, Var):
continue

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still dangerous, what if node is a Decorator? I would just replace the old assert with if not isinstance(node, Var): continue.


# x: ClassVar[int] is ignored by dataclasses.
if node.is_classvar:
Expand Down
27 changes: 27 additions & 0 deletions test-data/unit/check-dataclasses.test
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,33 @@ class Foo:
[builtins fixtures/list.pyi]
[out]

[case testMutableProperty]
# flags: --python-version 3.6
from dataclasses import dataclass
from typing import Optional

@dataclass(init=False)
class C:
a: Optional[int]

def __init__(self, a: Optional[int] = None) -> None:
self._a: Optional[int] = None
self.a = a

@property
def a(self) -> Optional[int]:
return self._a

@a.setter
def a(self, value: Optional[int]) -> Optional[int]:
self._a = value
return self._a

c = C()
c.a = 10
c.a = "foo" # E: Incompatible types in assignment (expression has type "str", variable has type "Optional[int]")

[builtins fixtures/list.pyi]
[case testDisallowUntypedWorksForward]
# flags: --disallow-untyped-defs --python-version 3.7
from dataclasses import dataclass
Expand Down