Skip to content

[flake8-use-pathlib] add autofix for PTH202 #18763

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

chirizxc
Copy link
Contributor

@chirizxc chirizxc commented Jun 18, 2025

Summary

/closes #2331

Test Plan

update snapshots

@ntBre ntBre self-requested a review June 18, 2025 17:50
@ntBre ntBre added the fixes Related to suggested fixes for violations label Jun 18, 2025
@chirizxc
Copy link
Contributor Author

this not the final version yet, and i still have some questions

@chirizxc
Copy link
Contributor Author

chirizxc commented Jun 18, 2025

should we remove [ import os, import import os.path, from os.path import getsize ] if it is not needed after the fix?

import os

x = os.path.getsize(filename="filename")
y = os.path.getsize(filename=b"filename")
z = os.path.getsize(filename=__file__)

=>

import os # unused
from pathlib import Path # we also have to import it if it wasn't there before

x = Path("filename").stat().st_size
y = Path(b"filename").stat().st_size
z = Path(__file__).stat().st_size

@ntBre
Copy link
Contributor

ntBre commented Jun 18, 2025

I don't think you need to remove the imports, you can defer to unused-import (F401) for that, but you do need to add the Path import if it's required for the fix.

Let me know if you have more questions or when it's ready for review!

@chirizxc
Copy link
Contributor Author

as well as, for example

os.path.getsize(Path("filename"))

=>

Path(Path("filename")).stat().st_size

@chirizxc
Copy link
Contributor Author

chirizxc commented Jun 18, 2025

The call to Path with parentheses here seems redundant, but I don't think we can consider it unnecessary since it might have additional methods like .parents[3] or .resolve()

Copy link
Contributor

github-actions bot commented Jun 18, 2025

ruff-ecosystem results

Linter (stable)

ℹ️ ecosystem check detected linter changes. (+10 -0 violations, +0 -0 fixes in 2 projects; 53 projects unchanged)

apache/airflow (+4 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --no-preview --select ALL

+ airflow-core/tests/unit/utils/test_log_handlers.py:375:29: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size`
+ airflow-core/tests/unit/utils/test_log_handlers.py:376:30: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size`
+ dev/breeze/src/airflow_breeze/utils/parallel.py:308:24: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size`
+ providers/amazon/src/airflow/providers/amazon/aws/transfers/dynamodb_to_s3.py:244:16: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size`

zulip/zulip (+6 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --no-preview --select ALL

+ scripts/setup/generate_secrets.py:65:47: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size`
+ tools/lib/test_server.py:64:45: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size`
+ zerver/data_import/mattermost.py:361:21: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size`
+ zerver/data_import/slack.py:1537:70: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size`
+ zerver/lib/export.py:2955:41: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size`
+ zerver/lib/upload/local.py:110:45: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size`

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
PTH202 10 10 0 0 0

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+10 -0 violations, +0 -0 fixes in 2 projects; 53 projects unchanged)

apache/airflow (+4 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --preview --select ALL

+ airflow-core/tests/unit/utils/test_log_handlers.py:375:29: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size`
+ airflow-core/tests/unit/utils/test_log_handlers.py:376:30: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size`
+ dev/breeze/src/airflow_breeze/utils/parallel.py:308:24: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size`
+ providers/amazon/src/airflow/providers/amazon/aws/transfers/dynamodb_to_s3.py:244:16: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size`

zulip/zulip (+6 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --preview --select ALL

+ scripts/setup/generate_secrets.py:65:47: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size`
+ tools/lib/test_server.py:64:45: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size`
+ zerver/data_import/mattermost.py:361:21: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size`
+ zerver/data_import/slack.py:1537:70: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size`
+ zerver/lib/export.py:2955:41: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size`
+ zerver/lib/upload/local.py:110:45: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size`

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
PTH202 10 10 0 0 0

@chirizxc
Copy link
Contributor Author

chirizxc commented Jun 18, 2025

as i understand: PTH203, PTH204, PTH205 will be exactly the same in implementation

@chirizxc
Copy link
Contributor Author

is it worth splitting pr if the changes are the same as this pr? the question is more to this one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixes Related to suggested fixes for violations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

autofix for flake8-use-pathlib (PTH) rules
2 participants