Skip to content

Fix attrs plugin: define/frozen default to order=False - #22364

Closed
kushwahaamar-dev wants to merge 1 commit into
python:masterfrom
kushwahaamar-dev:attrs-define-order-default
Closed

kushwahaamar-dev wants to merge 1 commit into
python:masterfrom
kushwahaamar-dev:attrs-define-order-default

Conversation

@kushwahaamar-dev

Copy link
Copy Markdown

Fixes #22290

attrs.define, attrs.mutable and attrs.frozen (and the attr. versions) default to order=False. Only the old attr.s API has order follow eq. The plugin applied the attr.s rule to all of them, so every @define class got __lt__ and friends, and this type checks fine:

import attrs

@attrs.define
class D:
    x: int

D(1) < D(2)  # TypeError at runtime

I added an order_default arg next to frozen_default/slots_default and pass False for the define/frozen makers. Explicit order=True still works, and attr.s is unchanged.

The crash in the issue is the same bug. The generated __lt__ replaces the __lt__: int attribute, then on the second pass _analyze_class asserts the node is a Var. With the default fixed, that example doesn't generate __lt__ anymore, but you can still hit the crash with an explicit order=True, so I changed the assert to an isinstance check.

testAttrsIncrementalArguments assumed attrs.frozen classes are orderable, so I updated its expected output. I also added two cases to check-plugin-attrs.test (both fail on master).

attrs.define, attrs.mutable and attrs.frozen (and the attr.* aliases)
default to order=False, unlike attr.s where order mirrors eq. The plugin
treated them like attr.s and synthesized __lt__/__le__/__gt__/__ge__, so
comparisons that raise TypeError at runtime type-checked fine.

Also stop asserting that an attribute node is a Var in _analyze_class:
when a class is processed again, a generated method may already have
replaced an attribute with the same name, which crashed mypy.

Fixes python#22290
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@ilevkivskyi ilevkivskyi closed this Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

attrs plugin crashes with order comparison methods

2 participants