Skip to content

New analyser: fix crashes when plugins encounter a placeholder node #7132

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

Merged
merged 5 commits into from
Jul 2, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 3 additions & 1 deletion mypy/plugins/attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,9 @@ def _analyze_class(ctx: 'mypy.plugin.ClassDefContext',
# instance level assignments.
if attribute.name in ctx.cls.info.names:
node = ctx.cls.info.names[attribute.name].node
assert isinstance(node, Var)
if not isinstance(node, Var):
# This node may be not ready yet (i.e. a PlaceholderNode).
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we instead check for PlaceholderNode? If there's something unexpected there, weird things might happen.

continue
node.is_initialized_in_class = False

# Traverse the MRO and collect attributes from the parents.
Expand Down
4 changes: 3 additions & 1 deletion mypy/plugins/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,9 @@ def collect_attributes(self) -> List[DataclassAttribute]:
continue

node = cls.info.names[lhs.name].node
assert isinstance(node, Var)
if not isinstance(node, Var):
# This node may be not ready yet (i.e. a PlaceholderNode).
continue

# x: ClassVar[int] is ignored by dataclasses.
if node.is_classvar:
Expand Down
44 changes: 44 additions & 0 deletions test-data/unit/check-attr.test
Original file line number Diff line number Diff line change
Expand Up @@ -1107,3 +1107,47 @@ C(43)

class Yes: ...
[builtins fixtures/exception.pyi]

[case testTypeInAttrBad]
import attr

@attr.s
class C:
total = attr.ib(type=Bad) # E: Name 'Bad' is not defined
[builtins fixtures/bool.pyi]

[case testTypeInAttrBad2]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe rename the test case? There seems nothing bad here, just a forward reference.

import attr

@attr.s
class C:
total = attr.ib(type=Forward)

reveal_type(C.total) # N: Revealed type is '__main__.Forward'
C('no') # E: Argument 1 to "C" has incompatible type "str"; expected "Forward"
class Forward: ...
[builtins fixtures/bool.pyi]

[case testTypeInAttrBad3]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Similar to above, the test case name doesn't seem quite right.

import attr

@attr.s
class C:
total = attr.ib(default=func())

def func() -> int: ...

C()
C(1)
C(1, 2) # E: Too many arguments for "C"
[builtins fixtures/bool.pyi]

[case testTypeInAttrBad4]
import attr

@attr.s(frozen=True)
class C:
total = attr.ib(type=Bad) # E: Name 'Bad' is not defined

C(0).total = 1 # E: Property "total" defined in "C" is read-only
[builtins fixtures/bool.pyi]
23 changes: 23 additions & 0 deletions test-data/unit/check-dataclasses.test
Original file line number Diff line number Diff line change
Expand Up @@ -642,3 +642,26 @@ a < b

class Yes: ...
[builtins fixtures/list.pyi]

[case testDataclassFieldDeferred]
from dataclasses import field, dataclass

@dataclass
class C:
x: int = field(default=func())

def func() -> int: ...
C('no') # E: Argument 1 to "C" has incompatible type "str"; expected "int"
[builtins fixtures/bool.pyi]

[case testDataclassFieldDeferredFrozen]
from dataclasses import field, dataclass

@dataclass(frozen=True)
class C:
x: int = field(default=func())

def func() -> int: ...
c: C
c.x = 1 # E: Property "x" defined in "C" is read-only
[builtins fixtures/bool.pyi]