Repository navigation
Fix attrs plugin: define/frozen default to order=False - #22364
Closed
kushwahaamar-dev wants to merge 1 commit into
Closed
kushwahaamar-dev wants to merge 1 commit into
kushwahaamar-dev wants to merge 1 commit into
Conversation
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
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #22290
attrs.define,attrs.mutableandattrs.frozen(and theattr.versions) default toorder=False. Only the oldattr.sAPI hasorderfolloweq. The plugin applied theattr.srule to all of them, so every@defineclass got__lt__and friends, and this type checks fine:I added an
order_defaultarg next tofrozen_default/slots_defaultand passFalsefor the define/frozen makers. Explicitorder=Truestill works, andattr.sis unchanged.The crash in the issue is the same bug. The generated
__lt__replaces the__lt__: intattribute, then on the second pass_analyze_classasserts the node is aVar. With the default fixed, that example doesn't generate__lt__anymore, but you can still hit the crash with an explicitorder=True, so I changed the assert to anisinstancecheck.testAttrsIncrementalArgumentsassumedattrs.frozenclasses are orderable, so I updated its expected output. I also added two cases tocheck-plugin-attrs.test(both fail on master).