sorting: use modern NamedTuple for SortTuple#1971
Merged
Merged
Conversation
Define SortTuple directly as a class-based typing.NamedTuple, dropping the _SortTupleBase functional-form base and the no-op __new__ (a pure passthrough since 2015). The split existed only to allow overriding __new__, which mypy forbids on a class-syntax NamedTuple. - priority is now typed int|float to reflect the ±inf sentinels (the loose __new__ used to hide this); classSortOrder stays int|float. - Comparison dunders call tuple.__eq__/__lt__/__gt__ directly: bare super() in a class-syntax NamedTuple body triggers a __classcell__ RuntimeError at class creation. - modify() now delegates to _replace() (== copy.replace() on 3.13+); a bad field name raises ValueError instead of being silently ignored. - base.py: fix docstring prose that misattributed priority -inf to classSortOrder. AI-assisted (Claude)
Class-syntax typing.NamedTuple does not get its _replace/_fields/_asdict members recognized by astroid, so pylint raises false-positive E1101 no-member. Add inline disables. Known upstream bug, open since 2020: pylint-dev/pylint#4070 AI-assisted (Claude)
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.
Some cleanup for sorting.SortTuple:
typing: note that
infand-infare technically floats, so priority has to allow floats if we are going to allow them (which is important).note that
modify(from Python 2.5 and earlier) is same as (since Py 2.6/3.0)_replace()and (since Py 3.13)copy.replace()-- use_replace()internally, which also now catches errors on typos.AI-assisted mostly for confirming that there wouldn't be incompatible repercussions. (Claude)