Skip to content

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 no linter changes.

Linter (preview)

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

apache/airflow (+0 -0 violations, +8 -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: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`
- 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`
- 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`
- 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 (+0 -0 violations, +12 -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`
- 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`
- 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/mattermost.py:361:21: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size`
+ zerver/data_import/slack.py:1550:70: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size`
- zerver/data_import/slack.py:1550: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/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`
- 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 20 0 0 20 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

@MichaReiser
Copy link
Member

is it worth splitting pr if the changes are the same as this pr? the question is more to #18752 (comment)

Let's start with one fix. Then the second PR can generalize it and apply it to the other rules.

Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Thanks! I have some questions and suggestions here.

Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Thanks, this looks good. I often forget this, but based on our versioning policy, adding a safe fix actually needs to be done in preview. Could you add a preview method like this and gate the fix behind that?

// https://github.com/astral-sh/ruff/pull/16719
pub(crate) const fn is_fix_manual_dict_comprehension_enabled(settings: &LinterSettings) -> bool {
settings.preview.is_enabled()
}

10 10 | os.path.getsize("filename")
11 11 | os.path.getsize(b"filename")
12 |-os.path.getsize(Path("filename"))
12 |+Path(Path("filename")).stat().st_size
Copy link
Contributor

Choose a reason for hiding this comment

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

Have you looked into how hard it would be to detect this? I think it would just be a match on the argument to see if it's a call to Path. I don't think this is a deal-breaker for this PR, but it is a bit unfortunate, as you pointed out.

I think if you look just for an Expr::Call, it should avoid your concern about additional attributes like Path(...).resolve() because those would be Expr::Attributes.

@chirizxc
Copy link
Contributor Author

chirizxc commented Jun 24, 2025

Thanks, this looks good. I often forget this, but based on our versioning policy, adding a safe fix actually needs to be done in preview. Could you add a preview method like this and gate the fix behind that?

// https://github.com/astral-sh/ruff/pull/16719
pub(crate) const fn is_fix_manual_dict_comprehension_enabled(settings: &LinterSettings) -> bool {
settings.preview.is_enabled()
}

should the changes disappear after that in the snapshots?

@chirizxc
Copy link
Contributor Author

Thanks, this looks good. I often forget this, but based on our versioning policy, adding a safe fix actually needs to be done in preview. Could you add a preview method like this and gate the fix behind that?

// https://github.com/astral-sh/ruff/pull/16719
pub(crate) const fn is_fix_manual_dict_comprehension_enabled(settings: &LinterSettings) -> bool {
settings.preview.is_enabled()
}

should the changes disappear after that in the spanshots?

изображение
i added is_fix_os_path_path_getsize_enabled, but the changes of fix to the snapshots are gone

@ntBre
Copy link
Contributor

ntBre commented Jun 24, 2025

Yes, you'll need to add a version of the PTH202 test that enables preview mode to see the preview snapshot changes.

@chirizxc
Copy link
Contributor Author

done

Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Thanks!

@ntBre ntBre added the preview Related to preview mode features label Jun 24, 2025
@ntBre ntBre changed the title [flake8-use-pathlib] add autofix for PTH202 [flake8-use-pathlib] Add autofix for PTH202 Jun 24, 2025
@ntBre ntBre enabled auto-merge (squash) June 24, 2025 17:55
@ntBre ntBre merged commit 3220242 into astral-sh:main Jun 24, 2025
33 checks passed
@chirizxc chirizxc deleted the feat/pth202-autofix branch June 24, 2025 18:18
dcreager added a commit that referenced this pull request Jun 24, 2025
* main:
  [ty] Fix false positives when subscripting an object inferred as having an `Intersection` type (#18920)
  [`flake8-use-pathlib`] Add autofix for `PTH202` (#18763)
  [ty] Add relative import completion tests
  [ty] Clarify what "cursor" means
  [ty] Add a cursor test builder
  [ty] Enforce sort order of completions (#18917)
  [formatter] Fix missing blank lines before decorated classes in .pyi files (#18888)
  Apply fix availability and applicability when adding to `DiagnosticGuard` and remove `NoqaCode::rule` (#18834)
  py-fuzzer: allow relative executable paths (#18915)
  [ty] Change `environment.root` to accept multiple paths (#18913)
  [ty] Rename `src.root` setting to `environment.root` (#18760)
  Use file path for detecting package root (#18914)
  Consider virtual path for various server actions (#18910)
  [ty] Introduce `UnionType::try_from_elements` and `UnionType::try_map` (#18911)
  [ty] Support narrowing on `isinstance()`/`issubclass()` if the second argument is a dynamic, intersection, union or typevar type (#18900)
  [ty] Add decorator check for implicit attribute assignments (#18587)
  [`ruff`] Trigger `RUF037` for empty string and byte strings (#18862)
  [ty] Avoid duplicate diagnostic in unpacking (#18897)
  [`pyupgrade`] Extend version detection to include `sys.version_info.major` (`UP036`) (#18633)
  [`ruff`] Frozen Dataclass default should be valid (`RUF009`) (#18735)
ntBre pushed a commit that referenced this pull request Jul 7, 2025
…#18922)

<!--
Thank you for contributing to Ruff/ty! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title? (Please prefix
with `[ty]` for ty pull
  requests.)
- Does this pull request include references to any relevant issues?
-->

## Summary
Part of #2331 |
[#18763](#18763 (comment))
<!-- What's the purpose of the change? What does it do, and why? -->

## Test Plan
update snapshots
<!-- How was it tested? -->
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.

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