Skip to content

Addons: refactor flyout sorting #11207

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

Closed
humitos opened this issue Mar 11, 2024 · 2 comments · Fixed by #11278
Closed

Addons: refactor flyout sorting #11207

humitos opened this issue Mar 11, 2024 · 2 comments · Fixed by #11278
Assignees
Labels
Accepted Accepted issue on our roadmap Improvement Minor improvement to code Priority: low Low priority

Comments

@humitos
Copy link
Member

humitos commented Mar 11, 2024

sort_versions_python_packaging and sort_versions_custom_pattern functions share most of their code. The only part that differs is what's inside try/catch. Here is the code involved:

def sort_versions_custom_pattern(version_list, raw_pattern, latest_stable_at_beginning):
"""
Sort Read the Docs versions using a custom pattern.
All the invalid version (raise ``PatternError``) are added at the end
sorted alphabetically.
It uses ``Bumpver`` behinds the scenes for the parsing and sorting.
https://github.com/mbarkhau/bumpver
"""
alphabetically_sorted_version_list = sorted(
version_list,
key=operator.attrgetter("slug"),
)
initial_versions = []
valid_versions = []
invalid_versions = []
for i, version in enumerate(alphabetically_sorted_version_list):
if latest_stable_at_beginning:
if version.slug in (STABLE, LATEST):
# It relies on the version list sorted alphabetically first ("l" comes first than "s")
initial_versions.append((version, version.slug))
continue
try:
valid_versions.append(
(
version,
parse_version_info(
version.slug,
raw_pattern=raw_pattern,
),
)
)
except PatternError:
# When the version is invalid, we put it at the end while keeping
# the alphabetically sorting between the invalid ones.
invalid_versions.append((version, None))
all_versions = (
initial_versions
+ sorted(valid_versions, key=operator.itemgetter(1), reverse=True)
+ invalid_versions
)
return [item[0] for item in all_versions if item[0] is not None]

Reference #11069 (comment)

@humitos humitos self-assigned this Mar 11, 2024
@humitos humitos moved this to Planned in 📍Roadmap Mar 11, 2024
@humitos humitos added Improvement Minor improvement to code Accepted Accepted issue on our roadmap Priority: low Low priority labels Mar 12, 2024
@humitos
Copy link
Member Author

humitos commented Apr 15, 2024

I jumped into this issue and I'm still not sure what's the best way to refactor this code. Basically, the only part we need to change is the try/except clause. All the rest is identical.

On that try/except clause the different parts are:

  • the exception that needs to be handled
  • the second argument required to generate the tuple

Would it be enough to isolate all this code in a function that receives exception= and parse_version=? 🤔

@humitos
Copy link
Member Author

humitos commented Apr 15, 2024

I implemented this idea in #11278

@humitos humitos moved this from Planned to Needs review in 📍Roadmap Apr 15, 2024
humitos added a commit that referenced this issue Apr 23, 2024
* Addons: refactor sorting versions for flyout

Closes #11207

* Apply suggestions from code review

Co-authored-by: Santos Gallegos <[email protected]>

---------

Co-authored-by: Santos Gallegos <[email protected]>
@github-project-automation github-project-automation bot moved this from Needs review to Done in 📍Roadmap Apr 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Accepted Accepted issue on our roadmap Improvement Minor improvement to code Priority: low Low priority
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

1 participant