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

Fix dataclass plugin interaction with member expressions #6739

Merged
merged 2 commits into from
Apr 30, 2019
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
6 changes: 3 additions & 3 deletions mypy/plugins/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from mypy.nodes import (
ARG_OPT, ARG_POS, MDEF, Argument, AssignmentStmt, CallExpr,
Context, Expression, FuncDef, JsonDict, NameExpr,
Context, Expression, FuncDef, JsonDict, NameExpr, RefExpr,
SymbolTableNode, TempNode, TypeInfo, Var, TypeVarExpr
)
from mypy.plugin import ClassDefContext
Expand Down Expand Up @@ -300,7 +300,7 @@ def collect_attributes(self) -> List[DataclassAttribute]:
Context(line=attr.line, column=attr.column),
)

found_default = found_default or attr.has_default
found_default = found_default or (attr.has_default and attr.is_in_init)

return all_attrs

Expand Down Expand Up @@ -337,7 +337,7 @@ def _collect_field_args(expr: Expression) -> Tuple[bool, Dict[str, Expression]]:
"""
if (
isinstance(expr, CallExpr) and
isinstance(expr.callee, NameExpr) and
isinstance(expr.callee, RefExpr) and
expr.callee.fullname == 'dataclasses.field'
):
# field() only takes keyword arguments.
Expand Down
19 changes: 19 additions & 0 deletions test-data/unit/check-dataclasses.test
Original file line number Diff line number Diff line change
Expand Up @@ -586,3 +586,22 @@ class B:

reveal_type(B) # E: Revealed type is 'def (x: Any) -> __main__.B'
[builtins fixtures/list.pyi]

[case testMemberExprWorksAsField]
import dataclasses

@dataclasses.dataclass
class A:
x: int = dataclasses.field(metadata={"doc": "foo"})
y: str

@dataclasses.dataclass
class B:
x: int = dataclasses.field(init=False, default=1)
y: str

@dataclasses.dataclass
class C:
x: int = dataclasses.field(default=1)
y: str = dataclasses.field(metadata={"doc": "foo"}) # E: Attributes without a default cannot follow attributes with one
[builtins fixtures/dict.pyi]