From 01d908257b2b2a1c2e89c4760befc1cb49b42d97 Mon Sep 17 00:00:00 2001 From: Navpreet Sidhu Date: Mon, 26 Oct 2026 22:38:52 +0530 Subject: [PATCH] Don't add ordering methods to attrs.define and attrs.frozen classes by default --- mypy/plugins/attrs.py | 10 +++++---- mypy/plugins/default.py | 10 +++++++-- test-data/unit/check-incremental.test | 2 +- test-data/unit/check-plugin-attrs.test | 29 ++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/mypy/plugins/attrs.py b/mypy/plugins/attrs.py index 1593c73cd2bfe..40531ba747feb 100644 --- a/mypy/plugins/attrs.py +++ b/mypy/plugins/attrs.py @@ -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) @@ -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. @@ -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 ) @@ -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) diff --git a/mypy/plugins/default.py b/mypy/plugins/default.py index 67352a05e4474..b6f06c7690575 100644 --- a/mypy/plugins/default.py +++ b/mypy/plugins/default.py @@ -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 diff --git a/test-data/unit/check-incremental.test b/test-data/unit/check-incremental.test index cf1369691f083..75ecfb2ff3885 100644 --- a/test-data/unit/check-incremental.test +++ b/test-data/unit/check-incremental.test @@ -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) diff --git a/test-data/unit/check-plugin-attrs.test b/test-data/unit/check-plugin-attrs.test index 7d453dcf84d01..3021b0532849b 100644 --- a/test-data/unit/check-plugin-attrs.test +++ b/test-data/unit/check-plugin-attrs.test @@ -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