Skip to content

Use ruff to also sort imports - #3237

Merged
ankith26 merged 19 commits into
pygame-community:mainfrom
Matiiss:matiiss-ruff-sort-imports
Mar 23, 2025
Merged

ankith26 merged 19 commits into
pygame-community:mainfrom
Matiiss:matiiss-ruff-sort-imports

Conversation

@Matiiss

@Matiiss Matiiss commented Nov 24, 2024

Copy link
Copy Markdown
Member

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
Comment thread .pre-commit-config.yaml Resolved
Comment thread pyproject.toml Resolved
Comment thread pyproject.toml Resolved

@MyreMylar MyreMylar left a comment

Copy link
Copy Markdown
Member

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.

Comment thread src_py/sprite.py Resolved
…written elsewhere (e.g., the CPython repository)
@Matiiss

Matiiss commented Mar 6, 2025 •

Copy link
Copy Markdown
Member Author

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

aatle commented Mar 7, 2025

Copy link
Copy Markdown
Member

Does the import sorting not also apply to stub files?

@Matiiss

Matiiss commented Mar 9, 2025

Copy link
Copy Markdown
Member Author

Does the import sorting not also apply to stub files?

Nope, pyproject.toml contains this

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

@aatle aatle left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM;

@Matiiss

Matiiss commented Mar 9, 2025

Copy link
Copy Markdown
Member Author

There's a little TODO still left:

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

@aatle

aatle commented Mar 9, 2025

Copy link
Copy Markdown
Member

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

aatle commented Mar 12, 2025

Copy link
Copy Markdown
Member

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

Matiiss commented Mar 12, 2025

Copy link
Copy Markdown
Member Author

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 😁

Comment thread buildconfig/stubs/pygame/typing.pyi Resolved
Comment thread buildconfig/stubs/pygame/__init__.pyi Resolved
@JoKing-1999

Copy link
Copy Markdown
Member

Can you also add .ruff_cache dir into the gitignore
image

@Matiiss

Matiiss commented Mar 13, 2025

Copy link
Copy Markdown
Member Author

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 😁)

Comment thread pyproject.toml Resolved

@aatle aatle left a comment

Copy link
Copy Markdown
Member

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.)

@ankith26 ankith26 left a comment

Copy link
Copy Markdown
Member

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 👍

Comment thread src_py/sprite.py Resolved
@ankith26
ankith26 merged commit 62a4930 into pygame-community:main Mar 23, 2025
@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