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 crash on overriding with frozen attrs #14186

Merged
merged 2 commits into from
Nov 25, 2022
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: 5 additions & 1 deletion mypy/plugins/attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,11 @@ def _make_frozen(ctx: mypy.plugin.ClassDefContext, attributes: list[Attribute])
if attribute.name in ctx.cls.info.names:
# This variable belongs to this class so we can modify it.
node = ctx.cls.info.names[attribute.name].node
assert isinstance(node, Var)
if not isinstance(node, Var):
# The superclass attribute was overridden with a non-variable.
# No need to do anything here, override will be verified during
# type checking.
continue
node.is_property = True
else:
# This variable belongs to a super class so create new Var so we
Expand Down
46 changes: 46 additions & 0 deletions test-data/unit/check-attr.test
Original file line number Diff line number Diff line change
Expand Up @@ -1788,3 +1788,49 @@ class C:
c = C(x=[C.D()])
reveal_type(c.x) # N: Revealed type is "builtins.list[__main__.C.D]"
[builtins fixtures/list.pyi]

[case testRedefinitionInFrozenClassNoCrash]
import attr

@attr.s
class MyData:
is_foo: bool = attr.ib()

@staticmethod # E: Name "is_foo" already defined on line 5
def is_foo(string: str) -> bool: ...
[builtins fixtures/classmethod.pyi]

[case testOverrideWithPropertyInFrozenClassNoCrash]
from attrs import frozen

@frozen(kw_only=True)
class Base:
name: str

@frozen(kw_only=True)
class Sub(Base):
first_name: str
last_name: str

@property
def name(self) -> str: ...
[builtins fixtures/property.pyi]

[case testOverrideWithPropertyInFrozenClassChecked]
from attrs import frozen

@frozen(kw_only=True)
class Base:
name: str

@frozen(kw_only=True)
class Sub(Base):
first_name: str
last_name: str

@property
def name(self) -> int: ... # E: Signature of "name" incompatible with supertype "Base"

# This matches runtime semantics
reveal_type(Sub) # N: Revealed type is "def (*, name: builtins.str, first_name: builtins.str, last_name: builtins.str) -> __main__.Sub"
[builtins fixtures/property.pyi]