diff --git a/mypy/plugins/attrs.py b/mypy/plugins/attrs.py index 1593c73cd2bfe..63d3b763c3546 100644 --- a/mypy/plugins/attrs.py +++ b/mypy/plugins/attrs.py @@ -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. @@ -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) @@ -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. @@ -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 ) @@ -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) @@ -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) 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..dfa2330fe9d90 100644 --- a/test-data/unit/check-incremental.test +++ b/test-data/unit/check-incremental.test @@ -3063,6 +3063,10 @@ 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") @@ -3070,6 +3074,10 @@ 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") diff --git a/test-data/unit/check-plugin-attrs.test b/test-data/unit/check-plugin-attrs.test index 7d453dcf84d01..6a74bbc47c469 100644 --- a/test-data/unit/check-plugin-attrs.test +++ b/test-data/unit/check-plugin-attrs.test @@ -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