Skip to content

[flake8-use-pathlib] Make PTH119 and PTH120 fixes unsafe because they can change behavior#20118

Merged
dylwil3 merged 7 commits intoastral-sh:mainfrom
chirizxc:pth_119_and_120
Sep 2, 2025
Merged

[flake8-use-pathlib] Make PTH119 and PTH120 fixes unsafe because they can change behavior#20118
dylwil3 merged 7 commits intoastral-sh:mainfrom
chirizxc:pth_119_and_120

Conversation

@chirizxc
Copy link
Copy Markdown
Contributor

@chirizxc chirizxc commented Aug 27, 2025

Summary

Fixes #20112

Test Plan

cargo nextest run flake8_use_pathlib

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Aug 27, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+0 -0 violations, +0 -780 fixes in 5 projects; 50 projects unchanged)

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

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

- airflow-core/src/airflow/config_templates/default_webserver_config.py:32:27: PTH120 [*] `os.path.dirname()` should be replaced by `Path.parent`
+ airflow-core/src/airflow/config_templates/default_webserver_config.py:32:27: PTH120 `os.path.dirname()` should be replaced by `Path.parent`
- airflow-core/src/airflow/configuration.py:175:34: PTH120 [*] `os.path.dirname()` should be replaced by `Path.parent`
+ airflow-core/src/airflow/configuration.py:175:34: PTH120 `os.path.dirname()` should be replaced by `Path.parent`
- airflow-core/src/airflow/configuration.py:2259:21: PTH120 [*] `os.path.dirname()` should be replaced by `Path.parent`
+ airflow-core/src/airflow/configuration.py:2259:21: PTH120 `os.path.dirname()` should be replaced by `Path.parent`
- airflow-core/src/airflow/configuration.py:2259:5: PTH120 [*] `os.path.dirname()` should be replaced by `Path.parent`
+ airflow-core/src/airflow/configuration.py:2259:5: PTH120 `os.path.dirname()` should be replaced by `Path.parent`
- airflow-core/src/airflow/configuration.py:2268:21: PTH120 [*] `os.path.dirname()` should be replaced by `Path.parent`
+ airflow-core/src/airflow/configuration.py:2268:21: PTH120 `os.path.dirname()` should be replaced by `Path.parent`
... 195 additional changes omitted for rule PTH120
- airflow-core/src/airflow/utils/email.py:208:20: PTH119 [*] `os.path.basename()` should be replaced by `Path.name`
+ airflow-core/src/airflow/utils/email.py:208:20: PTH119 `os.path.basename()` should be replaced by `Path.name`
- airflow-core/tests/unit/models/test_dagbag.py:313:40: PTH119 [*] `os.path.basename()` should be replaced by `Path.name`
+ airflow-core/tests/unit/models/test_dagbag.py:313:40: PTH119 `os.path.basename()` should be replaced by `Path.name`
- airflow-core/tests/unit/models/test_dagbag.py:377:20: PTH119 [*] `os.path.basename()` should be replaced by `Path.name`
+ airflow-core/tests/unit/models/test_dagbag.py:377:20: PTH119 `os.path.basename()` should be replaced by `Path.name`
- airflow-core/tests/unit/utils/log/test_file_processor_handler.py:44:16: PTH119 [*] `os.path.basename()` should be replaced by `Path.name`
+ airflow-core/tests/unit/utils/log/test_file_processor_handler.py:44:16: PTH119 `os.path.basename()` should be replaced by `Path.name`
... 266 additional changes omitted for project

apache/superset (+0 -0 violations, +0 -26 fixes)

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

- docker/pythonpath_dev/superset_config.py:123:16: PTH120 [*] `os.path.dirname()` should be replaced by `Path.parent`
+ docker/pythonpath_dev/superset_config.py:123:16: PTH120 `os.path.dirname()` should be replaced by `Path.parent`
- scripts/cypress_run.py:135:18: PTH120 [*] `os.path.dirname()` should be replaced by `Path.parent`
+ scripts/cypress_run.py:135:18: PTH120 `os.path.dirname()` should be replaced by `Path.parent`
- scripts/erd/erd.py:173:22: PTH120 [*] `os.path.dirname()` should be replaced by `Path.parent`
+ scripts/erd/erd.py:173:22: PTH120 `os.path.dirname()` should be replaced by `Path.parent`
... 17 additional changes omitted for rule PTH120
- superset/db_engine_specs/hive.py:82:50: PTH119 [*] `os.path.basename()` should be replaced by `Path.name`
+ superset/db_engine_specs/hive.py:82:50: PTH119 `os.path.basename()` should be replaced by `Path.name`
- superset/utils/core.py:776:20: PTH119 [*] `os.path.basename()` should be replaced by `Path.name`
+ superset/utils/core.py:776:20: PTH119 `os.path.basename()` should be replaced by `Path.name`
... 16 additional changes omitted for project

bokeh/bokeh (+0 -0 violations, +0 -142 fixes)

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

- examples/server/app/export_csv/main.py:15:23: PTH120 [*] `os.path.dirname()` should be replaced by `Path.parent`
+ examples/server/app/export_csv/main.py:15:23: PTH120 `os.path.dirname()` should be replaced by `Path.parent`
- examples/server/app/movies/main.py:28:16: PTH120 [*] `os.path.dirname()` should be replaced by `Path.parent`
+ examples/server/app/movies/main.py:28:16: PTH120 `os.path.dirname()` should be replaced by `Path.parent`
- examples/server/app/simple_hdf5/main.py:17:11: PTH120 [*] `os.path.dirname()` should be replaced by `Path.parent`
+ examples/server/app/simple_hdf5/main.py:17:11: PTH120 `os.path.dirname()` should be replaced by `Path.parent`
... 81 additional changes omitted for rule PTH120
- scripts/sri.py:30:24: PTH119 [*] `os.path.basename()` should be replaced by `Path.name`
+ scripts/sri.py:30:24: PTH119 `os.path.basename()` should be replaced by `Path.name`
- src/bokeh/application/handlers/code.py:169:31: PTH119 [*] `os.path.basename()` should be replaced by `Path.name`
+ src/bokeh/application/handlers/code.py:169:31: PTH119 `os.path.basename()` should be replaced by `Path.name`
... 132 additional changes omitted for project

milvus-io/pymilvus (+0 -0 violations, +0 -8 fixes)

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

- examples/bulk_import/example_bulkinsert_csv.py:305:74: PTH119 [*] `os.path.basename()` should be replaced by `Path.name`
+ examples/bulk_import/example_bulkinsert_csv.py:305:74: PTH119 `os.path.basename()` should be replaced by `Path.name`
- examples/bulk_import/example_bulkinsert_json.py:347:74: PTH119 [*] `os.path.basename()` should be replaced by `Path.name`
+ examples/bulk_import/example_bulkinsert_json.py:347:74: PTH119 `os.path.basename()` should be replaced by `Path.name`
- examples/bulk_import/example_bulkinsert_numpy.py:349:74: PTH119 [*] `os.path.basename()` should be replaced by `Path.name`
+ examples/bulk_import/example_bulkinsert_numpy.py:349:74: PTH119 `os.path.basename()` should be replaced by `Path.name`
- examples/bulk_import/example_bulkinsert_parquet.py:207:20: PTH119 [*] `os.path.basename()` should be replaced by `Path.name`
+ examples/bulk_import/example_bulkinsert_parquet.py:207:20: PTH119 `os.path.basename()` should be replaced by `Path.name`

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

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

- docs/conf.py:11:49: PTH120 [*] `os.path.dirname()` should be replaced by `Path.parent`
+ docs/conf.py:11:49: PTH120 `os.path.dirname()` should be replaced by `Path.parent`
- manage.py:7:12: PTH120 [*] `os.path.dirname()` should be replaced by `Path.parent`
+ manage.py:7:12: PTH120 `os.path.dirname()` should be replaced by `Path.parent`
- scripts/lib/check_rabbitmq_queue.py:10:14: PTH120 [*] `os.path.dirname()` should be replaced by `Path.parent`
+ scripts/lib/check_rabbitmq_queue.py:10:14: PTH120 `os.path.dirname()` should be replaced by `Path.parent`
- scripts/lib/check_rabbitmq_queue.py:10:30: PTH120 [*] `os.path.dirname()` should be replaced by `Path.parent`
+ scripts/lib/check_rabbitmq_queue.py:10:30: PTH120 `os.path.dirname()` should be replaced by `Path.parent`
- scripts/lib/check_rabbitmq_queue.py:10:46: PTH120 [*] `os.path.dirname()` should be replaced by `Path.parent`
+ scripts/lib/check_rabbitmq_queue.py:10:46: PTH120 `os.path.dirname()` should be replaced by `Path.parent`
- scripts/lib/clean_emoji_cache.py:27:25: PTH120 [*] `os.path.dirname()` should be replaced by `Path.parent`
... 248 additional changes omitted for rule PTH120
- scripts/lib/zulip_tools.py:566:16: PTH119 [*] `os.path.basename()` should be replaced by `Path.name`
+ scripts/lib/zulip_tools.py:566:16: PTH119 `os.path.basename()` should be replaced by `Path.name`
- tools/setup/generate_bots_integrations_static_files.py:64:65: PTH119 [*] `os.path.basename()` should be replaced by `Path.name`
+ tools/setup/generate_bots_integrations_static_files.py:64:65: PTH119 `os.path.basename()` should be replaced by `Path.name`
- tools/setup/generate_bots_integrations_static_files.py:69:60: PTH119 [*] `os.path.basename()` should be replaced by `Path.name`
+ tools/setup/generate_bots_integrations_static_files.py:69:60: PTH119 `os.path.basename()` should be replaced by `Path.name`
- zerver/data_import/slack.py:885:24: PTH119 [*] `os.path.basename()` should be replaced by `Path.name`
+ zerver/data_import/slack.py:885:24: PTH119 `os.path.basename()` should be replaced by `Path.name`
- zerver/lib/compatibility.py:15:17: PTH119 [*] `os.path.basename()` should be replaced by `Path.name`
... 300 additional changes omitted for project

Changes by rule (2 rules affected)

code total + violation - violation + fix - fix
PTH120 570 0 0 0 570
PTH119 210 0 0 0 210

@dscorbett
Copy link
Copy Markdown

Another normalization that could be worth mentioning is that pathlib eliminates "." components, as in "a/./b""a/b".

# Conflicts:
#	crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview_full_name.py.snap
#	crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview_import_as.py.snap
#	crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview_import_from.py.snap
#	crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview_import_from_as.py.snap
@dylwil3 dylwil3 self-requested a review August 29, 2025 19:24
@dylwil3 dylwil3 added fixes Related to suggested fixes for violations preview Related to preview mode features labels Sep 1, 2025
Copy link
Copy Markdown
Collaborator

@dylwil3 dylwil3 left a comment

Choose a reason for hiding this comment

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

Thank you! Small tweak in wording and a question about factoring out some shared code

Comment thread crates/ruff_linter/src/rules/flake8_use_pathlib/rules/os_path_dirname.rs Outdated
Comment thread crates/ruff_linter/src/rules/flake8_use_pathlib/rules/os_path_basename.rs Outdated
@chirizxc
Copy link
Copy Markdown
Contributor Author

chirizxc commented Sep 2, 2025

@dylwil3 idk if we could use #[allow(clippy::too_many_arguments)], but this way we don't duplicate the fn`s for safe and unsafe fixes

@dylwil3
Copy link
Copy Markdown
Collaborator

dylwil3 commented Sep 2, 2025

@dylwil3 idk if we could use #[allow(clippy::too_many_arguments)], but this way we don't duplicate the fn`s for safe and unsafe fixes

Clippy doesn't seem to complain locally if I remove the allow - we'll see if CI passes 😄

@chirizxc
Copy link
Copy Markdown
Contributor Author

chirizxc commented Sep 2, 2025

hmm, it's weird, i had something like 7>6, but if CI passes, it's okay!

Copy link
Copy Markdown
Collaborator

@dylwil3 dylwil3 left a comment

Choose a reason for hiding this comment

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

Thank you!

@dylwil3 dylwil3 merged commit d5e48a0 into astral-sh:main Sep 2, 2025
35 checks passed
@chirizxc chirizxc deleted the pth_119_and_120 branch September 2, 2025 16:22
second-ed pushed a commit to second-ed/ruff that referenced this pull request Sep 9, 2025
…e they can change behavior (astral-sh#20118)

## Summary

Fixes astral-sh#20112

## Test Plan
`cargo nextest run flake8_use_pathlib`

---------

Co-authored-by: dylwil3 <[email protected]>
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 preview Related to preview mode features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PTH119 and PTH120 should be marked unsafe

3 participants