diff --git a/crates/ruff_linter/src/rules/flake8_use_pathlib/rules/os_path_expanduser.rs b/crates/ruff_linter/src/rules/flake8_use_pathlib/rules/os_path_expanduser.rs index 14efef8c90..f1110f7d9e 100644 --- a/crates/ruff_linter/src/rules/flake8_use_pathlib/rules/os_path_expanduser.rs +++ b/crates/ruff_linter/src/rules/flake8_use_pathlib/rules/os_path_expanduser.rs @@ -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), ); } diff --git a/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview_full_name.py.snap b/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview_full_name.py.snap index d5fe727f4a..aa92f3ee7f 100644 --- a/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview_full_name.py.snap +++ b/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview_full_name.py.snap @@ -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 diff --git a/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview_import_as.py.snap b/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview_import_as.py.snap index 00d6ccde82..de63cbde9d 100644 --- a/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview_import_as.py.snap +++ b/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview_import_as.py.snap @@ -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 diff --git a/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview_import_from.py.snap b/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview_import_from.py.snap index db73ba81ee..c4da087955 100644 --- a/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview_import_from.py.snap +++ b/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview_import_from.py.snap @@ -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 diff --git a/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview_import_from_as.py.snap b/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview_import_from_as.py.snap index 23e111498c..e037400a27 100644 --- a/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview_import_from_as.py.snap +++ b/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview_import_from_as.py.snap @@ -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