Skip to content

sorting: use modern NamedTuple for SortTuple#1971

Merged
mscuthbert merged 4 commits into
masterfrom
sorttuple-3.12
Jul 1, 2026
Merged

sorting: use modern NamedTuple for SortTuple#1971
mscuthbert merged 4 commits into
masterfrom
sorttuple-3.12

Conversation

@mscuthbert

Copy link
Copy Markdown
Member

Some cleanup for sorting.SortTuple:

typing: note that inf and -inf are 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)

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)
@coveralls

coveralls commented Jul 1, 2026

Copy link
Copy Markdown

Coverage Status

coverage: 93.297%. remained the same — sorttuple-3.12 into master

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)
@mscuthbert mscuthbert merged commit 7ec322d into master Jul 1, 2026
7 checks passed
@mscuthbert mscuthbert deleted the sorttuple-3.12 branch July 1, 2026 22:33
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.

2 participants