Skip to content

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

Merged
srittau merged 29 commits into
python:mainfrom
Avasam:Enable-Ruff-flake8-use-pathlib-(PTH)
May 5, 2025
Merged

Enable Ruff flake8-use-pathlib (PTH)#13795
srittau merged 29 commits into
python:mainfrom
Avasam:Enable-Ruff-flake8-use-pathlib-(PTH)

Conversation

@Avasam

@AvasamAvasam commented Apr 4, 2025

Copy link
Copy Markdown
Collaborator

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.
Edit: os-listdir (PTH208) was disabled, see #13795 (comment) and #13795 (comment)

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
@AvasamAvasam changed the title Enable ruff flake8 use pathlib (pth)Enable Ruff flake8-use-pathlib (PTH)Apr 4, 2025
Comment threadlib/ts_utils/utils.py Outdated
@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
Copy Markdown
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.

Comment threadscripts/sync_protobuf/tensorflow.py Outdated
Comment threadlib/ts_utils/utils.py Outdated
@Avasam
Avasam marked this pull request as ready for review May 3, 2025 21:23
Comment threadlib/ts_utils/requirements.py Outdated
Comment on lines +16 to +22
distributions = os.listdir(STUBS_PATH)
distributions = [distribution.name for distribution in STUBS_PATH.iterdir()]

return set(itertools.chain.from_iterable([read_dependencies(distribution).external_pkgs for distribution in distributions]))


def get_stubtest_system_requirements(distributions: Iterable[str] = (), platform: str = sys.platform) -> list[str]:
if not distributions:
distributions = os.listdir(STUBS_PATH)
distributions = [distribution.name for distribution in STUBS_PATH.iterdir()]

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

This seems like a straight up downgrade.
os-listdir (PTH208)

Only one other change of os.listdir --> Path.iterdir seems like an actual improvement. (in tests/mypy_test.py)

Comment threadtests/mypy_test.py Outdated
distributions_to_check: dict[str, PackageDependencies] = {}

for distribution in sorted(os.listdir("stubs")):
for distribution in sorted([distribution.name for distribution in STUBS_PATH.iterdir()]):

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

This seems like a straight up downgrade.
os-listdir (PTH208)

Only one other change of os.listdir --> Path.iterdir seems like an actual improvement. (in add_third_party_files above)

@srittau

Copy link
Copy Markdown
Collaborator

I've gone ahead and merged #13943 for now (extracting _get_relative() can be postponed to a subsequent PR) to unblock this PR. Which now has merge conflicts, of course.

@srittausrittau left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks, just two comments below.

Comment threadtests/pytype_test.py Outdated
Comment threadtests/pytype_test.py Outdated
@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

Comment threadtests/pytype_test.py Outdated
unused_stubs_prefix, unused_pkg, mod_path = fi.split("/", 2) # pyright: ignore[reportUnusedVariable]
missing_modules.add(os.path.splitext(mod_path)[0])
_ts_subdir, _distribution, module_path = line.split("/", 2)
missing_modules.add(module_path.rsplit(".", 1)[0])

@AvasamAvasamMay 5, 2025

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

This could also be:

Suggested change
missing_modules.add(module_path.rsplit(".", 1)[0])
missing_modules.add(module_path.removesuffix(".pyi"))

which reads clearer to me

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I agree, to me as well.

@AvasamAvasamMay 5, 2025

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Oh, haha, I know why this breaks. The suffix is .pyi\n 😅

I went with .strip() to keep the readability of .removesuffix(".pyi")

Comment threadtests/pytype_test.py
Comment on lines +200 to 215
missing_modules = {
associated_package
for distribution in stub_distributions
for external_req in read_dependencies(distribution).external_pkgs
for associated_package in _get_pkgs_associated_with_requirement(external_req.name)
}

with EXCLUDE_LIST.open() as f:
for line in f:
if not line.startswith("stubs/"):
# Skips comments, empty lines, and stdlib files, which are in
# the exclude list because pytype has its own version.
continue
unused_stubs_prefix, unused_pkg, mod_path = fi.split("/", 2) # pyright: ignore[reportUnusedVariable]
missing_modules.add(os.path.splitext(mod_path)[0])
_ts_subdir, _distribution, module_path = line.split("/", 2)
missing_modules.add(module_path.rsplit(".", 1)[0])
return missing_modules

@AvasamAvasamMay 5, 2025

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

FWIW, this could also be:

return {
associated_packagefordistributioninstub_distributionsforexternal_reqinread_dependencies(distribution).external_pkgsforassociated_packagein_get_pkgs_associated_with_requirement(external_req.name)
} | {
# Exclude the distribution and file extension from pathline.split("/", 2)[2].removesuffix(".pyi")
forlineinEXCLUDE_LIST.read_text().splitlines()
# Skips comments, empty lines, and stdlib files, which are in# the exclude list because pytype has its own version.ifline.startswith("stubs/")
}

(not sure for line in EXCLUDE_LIST.read_text().splitlines() is really much better than with EXCLUDE_LIST.open() as f: for line in f:, it's was more as a personal exercise to spot where comprehensions could be used. And could doesn't mean should XD)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I wouldn't want to do fairly extensive changes like this in a PR that's mostly about introducing Path. (Also, the code above is a bit too clever for my taste.)

@AvasamAvasamMay 5, 2025

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Yeah agreed. I don't wanna push this code-change.

Speaking of changes not directly related to introducing Path (or rather, enforcing it). There's still a few changes in the pytype script that were more a result of looking at it more closely. I could split those off as well if you prefer.

@Avasam
Avasam requested a review from srittauMay 5, 2025 15:49
@srittau
srittau merged commit 4265ee7 into python:mainMay 5, 2025
@Avasam
Avasam deleted the Enable-Ruff-flake8-use-pathlib-(PTH) branch May 5, 2025 17:15
mmingyu pushed a commit to mmingyu/typeshed that referenced this pull request May 16, 2025
Sign up for freeto 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

@Avasam@srittau@brianschubert