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
10 changes: 6 additions & 4 deletions mypy/plugins/attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,14 @@ 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.
"""
cmp = _get_decorator_optional_bool_argument(ctx, "cmp")
eq = _get_decorator_optional_bool_argument(ctx, "eq")
order = _get_decorator_optional_bool_argument(ctx, "order")
order = _get_decorator_optional_bool_argument(ctx, "order", order_default)

if cmp is not None and any((eq is not None, order is not None)):
ctx.api.fail('Don\'t mix "cmp" with "eq" and "order"', ctx.reason)
Expand Down Expand Up @@ -310,6 +310,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 +332,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 +341,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
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
2 changes: 1 addition & 1 deletion test-data/unit/check-incremental.test
Original file line number Diff line number Diff line change
Expand Up @@ -3048,7 +3048,7 @@ NoCmp(1) >= NoCmp(2)

[file a.py]
import attrs
@attrs.frozen
@attrs.frozen(order=True)
class Frozen:
x: int
@attrs.define(init=False)
Expand Down
29 changes: 29 additions & 0 deletions test-data/unit/check-plugin-attrs.test
Original file line number Diff line number Diff line change
Expand Up @@ -1760,6 +1760,35 @@ class B(A):

[builtins fixtures/plugin_attrs.pyi]

[case testAttrsDefineNoOrderByDefault]
import attrs

@attrs.define
class A:
x: int

@attrs.frozen
class B:
x: int

@attrs.define(order=True)
class C:
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)
[builtins fixtures/plugin_attrs.pyi]

[case testAttrsDefineFrozenAttributeNamedLikeOrderMethod]
import attrs

@attrs.define
@attrs.frozen
class C:
__lt__: int
[builtins fixtures/plugin_attrs.pyi]

[case testRuntimeSlotsAttr]
from attr import dataclass

Expand Down
Loading