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
17 changes: 10 additions & 7 deletions mypy/plugins/attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def expand_typevar_from_subtype(self, sub_type: TypeInfo) -> None:
self.init_type = None


def _determine_eq_order(ctx: mypy.plugin.ClassDefContext) -> bool:
def _determine_eq_order(ctx: mypy.plugin.ClassDefContext, order_default: bool | None) -> bool:
"""
Validate the combination of *cmp*, *eq*, and *order*. Derive the effective
value of order.
Expand All @@ -254,12 +254,13 @@ def _determine_eq_order(ctx: mypy.plugin.ClassDefContext) -> bool:
if cmp is not None:
return cmp

# If left None, equality is on and ordering mirrors equality.
# If left None, equality is on and ordering mirrors equality, unless the
# decorator has its own default (attrs.define and friends use order=False).
if eq is None:
eq = True

if order is None:
order = eq
order = eq if order_default is None else order_default

if eq is False and order is True:
ctx.api.fail("eq must be True if order is True", ctx.reason)
Expand Down Expand Up @@ -310,6 +311,7 @@ def attr_class_maker_callback(
auto_attribs_default: bool | None = False,
frozen_default: bool = False,
slots_default: bool = False,
order_default: bool | None = None,
) -> bool:
"""Add necessary dunder methods to classes decorated with attr.s.

Expand All @@ -331,7 +333,7 @@ def attr_class_maker_callback(
# This hook is called during semantic analysis, but it uses a bunch of
# type-checking ops, so it needs the strict optional set properly.
return attr_class_maker_callback_impl(
ctx, auto_attribs_default, frozen_default, slots_default
ctx, auto_attribs_default, frozen_default, slots_default, order_default
)


Expand All @@ -340,12 +342,13 @@ def attr_class_maker_callback_impl(
auto_attribs_default: bool | None,
frozen_default: bool,
slots_default: bool,
order_default: bool | None,
) -> bool:
info = ctx.cls.info

init = _get_decorator_bool_argument(ctx, "init", True)
frozen = _get_frozen(ctx, frozen_default)
order = _determine_eq_order(ctx)
order = _determine_eq_order(ctx, order_default)
slots = _get_decorator_bool_argument(ctx, "slots", slots_default)

auto_attribs = _get_decorator_optional_bool_argument(ctx, "auto_attribs", auto_attribs_default)
Expand Down Expand Up @@ -458,8 +461,8 @@ def _analyze_class(
if isinstance(node, PlaceholderNode):
# This node is not ready yet.
continue
assert isinstance(node, Var), node
node.is_initialized_in_class = False
if isinstance(node, Var):
node.is_initialized_in_class = False

# Traverse the MRO and collect attributes from the parents.
taken_attr_names = set(own_attrs)
Expand Down
10 changes: 8 additions & 2 deletions mypy/plugins/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,17 @@ def get_class_decorator_hook_2(
return partial(attr_class_maker_callback, auto_attribs_default=True)
elif fullname in attr_frozen_makers:
return partial(
attr_class_maker_callback, auto_attribs_default=None, frozen_default=True
attr_class_maker_callback,
auto_attribs_default=None,
frozen_default=True,
order_default=False,
)
elif fullname in attr_define_makers:
return partial(
attr_class_maker_callback, auto_attribs_default=None, slots_default=True
attr_class_maker_callback,
auto_attribs_default=None,
slots_default=True,
order_default=False,
)
return None

Expand Down
8 changes: 8 additions & 0 deletions test-data/unit/check-incremental.test
Original file line number Diff line number Diff line change
Expand Up @@ -3063,13 +3063,21 @@ class NoCmp:
[stale]
[out1]
main:3: error: Property "x" defined in "Frozen" is read-only
main:7: error: Unsupported left operand type for < ("Frozen")
main:8: error: Unsupported left operand type for <= ("Frozen")
main:9: error: Unsupported left operand type for > ("Frozen")
main:10: error: Unsupported left operand type for >= ("Frozen")
main:12: error: Unsupported left operand type for < ("NoCmp")
main:13: error: Unsupported left operand type for <= ("NoCmp")
main:14: error: Unsupported left operand type for > ("NoCmp")
main:15: error: Unsupported left operand type for >= ("NoCmp")

[out2]
main:3: error: Property "x" defined in "Frozen" is read-only
main:7: error: Unsupported left operand type for < ("Frozen")
main:8: error: Unsupported left operand type for <= ("Frozen")
main:9: error: Unsupported left operand type for > ("Frozen")
main:10: error: Unsupported left operand type for >= ("Frozen")
main:12: error: Unsupported left operand type for < ("NoCmp")
main:13: error: Unsupported left operand type for <= ("NoCmp")
main:14: error: Unsupported left operand type for > ("NoCmp")
Expand Down
51 changes: 51 additions & 0 deletions test-data/unit/check-plugin-attrs.test
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,57 @@ a = A(5)
a.a = 16 # E: Property "a" defined in "A" is read-only
[builtins fixtures/plugin_attrs.pyi]

[case testAttrsNextGenOrderDefaultsToFalse]
import attr
import attrs

@attrs.define
class A:
x: int

@attrs.frozen
class B:
x: int

@attrs.mutable
class C:
x: int

@attr.define
class D:
x: int

@attrs.define(order=True)
class E:
x: int

@attr.s(auto_attribs=True)
class F:
x: int

A(1) < A(2) # E: Unsupported left operand type for < ("A")
B(1) <= B(2) # E: Unsupported left operand type for <= ("B")
C(1) > C(2) # E: Unsupported left operand type for > ("C")
D(1) >= D(2) # E: Unsupported left operand type for >= ("D")
E(1) < E(2)
F(1) < F(2)
[builtins fixtures/plugin_attrs.pyi]

[case testAttrsReprocessedClassWithComparisonMethodAttribute]
import attr
import attrs

@attrs.define
@attrs.frozen
class A:
__lt__: int

@attr.s(auto_attribs=True, order=True)
@attr.s(auto_attribs=True, order=True)
class B:
__lt__: int
[builtins fixtures/plugin_attrs.pyi]

[case testAttrsNextGenDetect]
from attr import define, field

Expand Down
Loading