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

Enable Ruff flake8-use-pathlib (PTH) #13795

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

Avasam
Copy link
Collaborator

@Avasam Avasam commented Apr 4, 2025

Kept this one for last. Closes #13295
Some rules in this group are forcing some refactoring to use pathlib. In some cases, it's a lot cleaner, in others it's debatable.

I unconditionally followed all the rules. Please indicate changes you disagree with/preferred in the old style. I'll revert those and disable the relevant rules.

There's also a few with read and with write that can be further rewritten to be more concise with Path.read_text and Path.write_text

@Avasam Avasam marked this pull request as draft April 4, 2025 19:10
@Avasam Avasam changed the title Enable ruff flake8 use pathlib (pth) Enable Ruff flake8-use-pathlib (PTH) Apr 4, 2025
@@ -203,7 +203,7 @@ def allowlists(distribution_name: str) -> list[str]:

@functools.cache
def get_gitignore_spec() -> pathspec.PathSpec:
with open(".gitignore", encoding="UTF-8") as f:
with Path(".gitignore").open(encoding="UTF-8") as f:
Copy link
Collaborator

Choose a reason for hiding this comment

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

I prefer the simple open() in this and similar cases. But it's no deal breaker for me. The rest looks good.

with open(path, encoding="utf-8") as file:
with path.open(encoding="utf-8") as file:
filedata = file.read()
Copy link
Contributor

Choose a reason for hiding this comment

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

In cases like this I'm neutral about path.open(...), but I think path.read_text() would be an improvement

@@ -203,7 +203,7 @@ def allowlists(distribution_name: str) -> list[str]:

@functools.cache
def get_gitignore_spec() -> pathspec.PathSpec:
with open(".gitignore", encoding="UTF-8") as f:
with Path(".gitignore").open(encoding="UTF-8") as f:
return pathspec.PathSpec.from_lines("gitwildmatch", f.readlines())
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Unrelated to your above comment or this PR directly. But I just read pathspec's doc and found this:

There is a specialized class, pathspec.GitIgnoreSpec, which more closely
implements the behavior of gitignore. This uses GitWildMatchPattern
pattern by default and handles some edge cases differently from the generic
PathSpec class. GitIgnoreSpec can be used without specifying the pattern
factory::

spec = pathspec.GitIgnoreSpec.from_lines(spec_text.splitlines())

Suggested change
return pathspec.PathSpec.from_lines("gitwildmatch", f.readlines())
return pathspec.GitIgnoreSpec.from_lines(f.readlines())

Copy link
Contributor

Choose a reason for hiding this comment

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

Could also omit the call to .readlines(). GitIgnoreSpec.from_lines takes an iterable of lines, so passing the file i/o object directly would work. Not sure if there's any reason to prefer one way or another, though

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You're completely right! I would assume .readlines() to just be a waste here since you have to both read the file, then iterate over the list again, instead of streaming it once.

PathSpec.from_lines simply does patterns = [pattern_factory(line) for line in lines if line]

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I opened #13797

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.

Applying more Ruff groups to this repository
3 participants