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

Use ruff to also sort imports #3237

Merged
merged 19 commits into from
Mar 23, 2025

Conversation

Matiiss
Copy link
Member

@Matiiss Matiiss commented Nov 24, 2024

Just a capability that was not being utilized (it was done before when we were using black + isort)

From ruff docs:

Sorting imports

Currently, the Ruff formatter does not sort imports. In order to both sort imports and format, call the Ruff linter and then the formatter:

ruff check --select I --fix
ruff format

A unified command for both linting and formatting is planned.

@Matiiss Matiiss requested a review from a team as a code owner November 24, 2024 16:47
Copy link
Member

@MyreMylar MyreMylar left a comment

Choose a reason for hiding this comment

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

Seems reasonable to me.

I'd rather have these automagically sorted in a consistent manner aligned with python coding standards than having to figure it out myself.

…written elsewhere (e.g., the CPython repository)
@Matiiss
Copy link
Member Author

Matiiss commented Mar 6, 2025

I added back the missing import and also rewrote the method to how I've seen it typically typed out elsewhere, such as the CPython repository: https://github.com/search?q=repo%3Apython%2Fcpython%20%22classmethod(types.GenericAlias)%22&type=code

@aatle
Copy link
Contributor

aatle commented Mar 7, 2025

Does the import sorting not also apply to stub files?

@Matiiss
Copy link
Member Author

Matiiss commented Mar 9, 2025

Does the import sorting not also apply to stub files?

Nope, pyproject.toml contains this

[tool.ruff]
exclude = [
  "buildconfig/",
  "docs/reST/",
  "setup.py",
]

Copy link
Contributor

@aatle aatle left a comment

Choose a reason for hiding this comment

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

LGTM;

@Matiiss
Copy link
Member Author

Matiiss commented Mar 9, 2025

There's a little TODO still left:

  • configure pyproject.toml to include buildconfig/stubs/pygame and run formatting again

@aatle
Copy link
Contributor

aatle commented Mar 9, 2025

I assumed that would be a separate PR, but I guess it could be put with this one for convenience, placing the large changes together.

@aatle
Copy link
Contributor

aatle commented Mar 12, 2025

This configuration should properly configure the ruff configuration:

[tool.ruff]
extend-exclude = [
    "buildconfig/*.py",  # *.pyi stub files still included
    "docs/reST",
    "setup.py",
]

Alternatively, include can be set to specific directories.

@Matiiss
Copy link
Member Author

Matiiss commented Mar 12, 2025

This configuration should properly configure the ruff configuration:

[tool.ruff]
extend-exclude = [
    "buildconfig/*.py",  # *.pyi stub files still included
    "docs/reST",
    "setup.py",
]

Alternatively, include can be set to specific directories.

Both options have a flaw I dislike, with the exclude it's not being quite as specific, for example, if there were other .py files in some other directories, they'd still get formatted.
Whereas the include requires being overly specific.

That said, considering that the reason we don't want buildconfig/*.py files formatted is because they're legacy files, if there were new files to be added in some other subdirectory, they'd likely not be a legacy thing and could be easily formatted, so I think I'll go with that approach for now.

I did consult ruff developers, however, it seems there isn't (currently) really a way to include a directory but exclude a specific subdirectory inside of it. (When I say include I mean including in the exclude and similar with excluding from exclude). There was some other quirky behavior I found, but that's besides the point 😁

@MightyJosip
Copy link
Member

Can you also add .ruff_cache dir into the gitignore
image

@Matiiss
Copy link
Member Author

Matiiss commented Mar 13, 2025

Can you also add .ruff_cache dir into the gitignore image

To be fair, it would be ignored by git anyway because it contains an auto-generated .gitignore inside of it, but I added it to the top level .gitignore anyway. (At least it's now visually consistent with other ignored directories 😁)

Copy link
Contributor

@aatle aatle left a comment

Choose a reason for hiding this comment

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

LGTM; thanks!

(Before merging, pull in from main and run pre-commit so that tests pass.)

Copy link
Member

@ankith26 ankith26 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the PR 👍

@ankith26 ankith26 merged commit 62a4930 into pygame-community:main Mar 23, 2025
26 checks passed
@ankith26 ankith26 added this to the 2.5.4 milestone Mar 23, 2025
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.

5 participants