-
Notifications
You must be signed in to change notification settings - Fork 85
π Sort need links and backlinks naturally in needs.json and HTML #1695
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ubmarco
wants to merge
5
commits into
master
Choose a base branch
from
mh-natural-sort
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+777
β23
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
16e9559
π Sort need links and backlinks naturally in needs.json and HTML output
ubmarco 16f2938
π Move changelog entry into a separate Unreleased section
ubmarco edc038c
π Mark 8.0.0 as released on 19.03.2026
ubmarco 637104e
π§ͺ Verify link sort is independent of needs_reproducible_json
ubmarco c35a387
π§ͺ Update test_need_constraints snapshot for natural link sort
ubmarco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
| # For NeedItem, we allow mutability, but only for values, i.e. it should not allow adding or removing keys. | ||
| from __future__ import annotations | ||
|
|
||
| import re | ||
| from collections.abc import Iterable, Iterator, Mapping, Sequence | ||
| from dataclasses import dataclass, field | ||
| from itertools import chain | ||
|
|
@@ -363,6 +364,28 @@ def to_link_string(self) -> str: | |
| return f"{base}{open_b}{self.condition}{close_b}" | ||
|
|
||
|
|
||
| _NATURAL_SORT_RE = re.compile(r"(\d+)") | ||
|
|
||
|
|
||
| def _natural_sort_key(value: str) -> list[str | int]: | ||
| """Build a sort key for natural ordering: digit runs are compared as ints. | ||
|
|
||
| For example, ``REQ_2`` < ``REQ_9`` < ``REQ_10`` (instead of the default | ||
| lexicographic ``REQ_10`` < ``REQ_2`` < ``REQ_9``). The result alternates | ||
| between str and int, always starting with a str, so mixed-type comparisons | ||
| are well-defined. | ||
| """ | ||
| return [ | ||
| int(part) if i % 2 else part | ||
| for i, part in enumerate(_NATURAL_SORT_RE.split(value)) | ||
| ] | ||
|
|
||
|
|
||
| def _link_natural_sort_key(link: NeedLink) -> list[str | int]: | ||
| """Natural sort key for a :class:`NeedLink`, based on its filter string.""" | ||
| return _natural_sort_key(link.to_filter_string()) | ||
|
|
||
|
|
||
| class NeedItem: | ||
| """A class representing a single need item.""" | ||
|
|
||
|
|
@@ -875,6 +898,23 @@ def reset_backlinks(self) -> None: | |
| for k in part.backlinks: | ||
| part.backlinks[k] = [] | ||
|
|
||
| def sort_links(self) -> None: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While doing, perhaps also remove duplicates |
||
| """Sort all link and backlink lists in place using natural ordering. | ||
|
|
||
| Sorts outgoing links, backlinks, and part backlinks by the link's | ||
| string representation, treating embedded digit sequences as integers | ||
| so that e.g. ``REQ_2`` < ``REQ_9`` < ``REQ_10``. This makes the order | ||
| of linked need IDs deterministic, regardless of the order in which | ||
| they were added or the iteration order of the needs collection. | ||
| """ | ||
| for value in self._links.values(): | ||
| value.sort(key=_link_natural_sort_key) | ||
| for value in self._backlinks.values(): | ||
| value.sort(key=_link_natural_sort_key) | ||
| for part in self._parts.values(): | ||
| for value in part.backlinks.values(): | ||
| value.sort(key=_link_natural_sort_key) | ||
|
|
||
| def add_backlink(self, link_type: str, backlink: str | NeedLink) -> None: | ||
| """Add a backlink to the need.""" | ||
| if link_type not in self._backlinks: | ||
|
|
||
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,8 +21,8 @@ | |
| 'SPEC_3', | ||
| ]), | ||
| 'link2': list([ | ||
| 'SPEC_2', | ||
| 'SPEC_1', | ||
| 'SPEC_2', | ||
| ]), | ||
| 'link2_back': list([ | ||
| 'SPEC_1', | ||
|
|
||
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In ubcode we do it a little differently