[`flake8-use-pathlib`] Make `PTH111` fix unsafe because it can change behavior (#20215)

## Summary

Fixes https://github.com/astral-sh/ruff/issues/20214

## Test Plan
This commit is contained in:
chiri 2025-09-17 18:23:55 +03:00 committed by GitHub
parent ac5488086f
commit c585c9f6d4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 10 additions and 2 deletions

View File

@ -1,3 +1,4 @@
use ruff_diagnostics::Applicability;
use ruff_macros::{ViolationMetadata, derive_message_formats};
use ruff_python_ast::ExprCall;
@ -35,7 +36,10 @@ use crate::{FixAvailability, Violation};
/// especially on older versions of Python.
///
/// ## Fix Safety
/// This rule's fix is marked as unsafe if the replacement would remove comments attached to the original expression.
/// This rule's fix is always marked as unsafe because the behaviors of
/// `os.path.expanduser` and `Path.expanduser` differ when a user's home
/// directory can't be resolved: `os.path.expanduser` returns the
/// input unchanged, while `Path.expanduser` raises `RuntimeError`.
///
/// ## References
/// - [Python documentation: `Path.expanduser`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.expanduser)
@ -71,6 +75,6 @@ pub(crate) fn os_path_expanduser(checker: &Checker, call: &ExprCall, segments: &
"path",
is_fix_os_path_expanduser_enabled(checker.settings()),
OsPathExpanduser,
None,
Some(Applicability::Unsafe),
);
}

View File

@ -260,6 +260,7 @@ help: Replace with `Path(...).expanduser()`
20 | bbb = os.path.isdir(p)
21 | bbbb = os.path.isfile(p)
22 | bbbbb = os.path.islink(p)
note: This is an unsafe fix and may change runtime behavior
PTH112 [*] `os.path.isdir()` should be replaced by `Path.is_dir()`
--> full_name.py:19:7

View File

@ -260,6 +260,7 @@ help: Replace with `Path(...).expanduser()`
20 | bbb = foo_p.isdir(p)
21 | bbbb = foo_p.isfile(p)
22 | bbbbb = foo_p.islink(p)
note: This is an unsafe fix and may change runtime behavior
PTH112 [*] `os.path.isdir()` should be replaced by `Path.is_dir()`
--> import_as.py:19:7

View File

@ -268,6 +268,7 @@ help: Replace with `Path(...).expanduser()`
22 | bbb = isdir(p)
23 | bbbb = isfile(p)
24 | bbbbb = islink(p)
note: This is an unsafe fix and may change runtime behavior
PTH112 [*] `os.path.isdir()` should be replaced by `Path.is_dir()`
--> import_from.py:21:7

View File

@ -268,6 +268,7 @@ help: Replace with `Path(...).expanduser()`
27 | bbb = xisdir(p)
28 | bbbb = xisfile(p)
29 | bbbbb = xislink(p)
note: This is an unsafe fix and may change runtime behavior
PTH112 [*] `os.path.isdir()` should be replaced by `Path.is_dir()`
--> import_from_as.py:26:7