Skip to content
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

Introduce sort-all Hook for pre-commit #4052

Merged
merged 2 commits into from Jan 8, 2024
Merged

Introduce sort-all Hook for pre-commit #4052

merged 2 commits into from Jan 8, 2024

Conversation

Bibo-Joshi
Copy link
Member

I found https://pypi.org/project/sort-all/ and found it really useful for keeping the __all__ variables sorted.
Admittedly, it's not the best maintained package, but the core functionality works fine. One downside is that comments in the list will be removed, but so far this doesn't affect us.
I would indeed hope that something similar can be set up for __slots__.

@Poolitzer
Copy link
Member

It's not a lot of code maybe it makes sense to build smth for us with slots and all?

@harshil21
Copy link
Member

@Poolitzer that's what I was thinking as well, maybe open an issue in Ruff, cause it will get implemented pretty quickly if accepted

@Bibo-Joshi
Copy link
Member Author

It's not a lot of code maybe it makes sense to build smth for us with slots and all?

Have already forked :D https://github.com/Bibo-Joshi/sort-all

that's what I was thinking as well, maybe open an issue in Ruff, cause it will get implemented pretty quickly if accepted

also a nice idea! would you mind doing that? Things to mention IMO are

  • the sort-all package for reference
  • comment-handling
  • extensibility for general iterables of strings with user defined names

If you like the idea in general, I'd like to go ahead with the package that we have so far :)

@Bibo-Joshi
Copy link
Member Author

@Bibo-Joshi @clot27 points to astral-sh/ruff#1198 in which it's mentioned that isort can do this already. I'll have to check

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey! Looks like you edited the (optional) requirements or the pre-commit hooks. I'm just a friendly reminder to keep the additional dependencies for the hooks in sync with the requirements :)

@Bibo-Joshi
Copy link
Member Author

isort does support sorting __all__ via the sort_reexports option even though this option is not documted in the options list (only here). One major bummer however is that this doesn't seem to honor the black settings, in particular it uses single quotes (' instead of ") and it collapses multiline definitions (see also PyCQA/isort#2193). One would have to apply black again and the pre-commit step would always fails.
I would therefore not consider the isort implementation as useful for us ATM.

@Bibo-Joshi
Copy link
Member Author

PyCQA/isort#2193 also mentions https://pypi.org/project/asort/. Even though that package is even less maintained than sort-all, it worked well on first try and even kept comments. I'll have another look at it in time

@Bibo-Joshi
Copy link
Member Author

Welp, asort completely deletes the content of __all__ when run against our code base and also messes up several unrelated things. so not an option …

Copy link
Member

@harshil21 harshil21 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me as a temporary solution. I suggested this package in the ruff issue: astral-sh/ruff#1198 (comment)

@Bibo-Joshi Bibo-Joshi merged commit dc284a1 into master Jan 8, 2024
23 of 24 checks passed
@Bibo-Joshi Bibo-Joshi deleted the sort-all-hook branch January 8, 2024 17:35
@github-actions github-actions bot locked and limited conversation to collaborators Jan 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants