diff --git a/crates/ruff_linter/src/rules/flake8_use_pathlib/rules/os_symlink.rs b/crates/ruff_linter/src/rules/flake8_use_pathlib/rules/os_symlink.rs index 43f5827383..57e068b93f 100644 --- a/crates/ruff_linter/src/rules/flake8_use_pathlib/rules/os_symlink.rs +++ b/crates/ruff_linter/src/rules/flake8_use_pathlib/rules/os_symlink.rs @@ -1,4 +1,3 @@ -use anyhow::anyhow; use ruff_diagnostics::{Applicability, Edit, Fix}; use ruff_macros::{ViolationMetadata, derive_message_formats}; use ruff_python_ast::ExprCall; @@ -105,6 +104,14 @@ pub(crate) fn os_symlink(checker: &Checker, call: &ExprCall, segments: &[&str]) return; }; + let target_is_directory_arg = call.arguments.find_argument_value("target_is_directory", 2); + + if let Some(expr) = &target_is_directory_arg { + if expr.as_boolean_literal_expr().is_none() { + return; + } + } + diagnostic.try_set_fix(|| { let (import_edit, binding) = checker.importer().get_or_import_symbol( &ImportRequest::import("pathlib", "Path"), @@ -122,16 +129,14 @@ pub(crate) fn os_symlink(checker: &Checker, call: &ExprCall, segments: &[&str]) let src_code = locator.slice(src.range()); let dst_code = locator.slice(dst.range()); - let target_is_directory = call - .arguments - .find_argument_value("target_is_directory", 2) + let target_is_directory = target_is_directory_arg .and_then(|expr| { let code = locator.slice(expr.range()); expr.as_boolean_literal_expr() - .is_some_and(|bl| !bl.value) + .is_none_or(|bl| bl.value) .then_some(format!(", target_is_directory={code}")) }) - .ok_or_else(|| anyhow!("Non-boolean value passed for `target_is_directory`."))?; + .unwrap_or_default(); let replacement = if is_pathlib_path_call(checker, dst) { format!("{dst_code}.symlink_to({src_code}{target_is_directory})") diff --git a/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview__PTH211_PTH211.py.snap b/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview__PTH211_PTH211.py.snap index 9fa340479f..d6f0db9f8a 100644 --- a/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview__PTH211_PTH211.py.snap +++ b/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview__PTH211_PTH211.py.snap @@ -1,7 +1,7 @@ --- source: crates/ruff_linter/src/rules/flake8_use_pathlib/mod.rs --- -PTH211 `os.symlink` should be replaced by `Path.symlink_to` +PTH211 [*] `os.symlink` should be replaced by `Path.symlink_to` --> PTH211.py:5:1 | 5 | os.symlink("usr/bin/python", "tmp/python") @@ -11,7 +11,17 @@ PTH211 `os.symlink` should be replaced by `Path.symlink_to` | help: Replace with `Path(...).symlink_to(...)` -PTH211 `os.symlink` should be replaced by `Path.symlink_to` +ℹ Safe fix +2 2 | from pathlib import Path +3 3 | +4 4 | +5 |-os.symlink("usr/bin/python", "tmp/python") + 5 |+Path("tmp/python").symlink_to("usr/bin/python") +6 6 | os.symlink(b"usr/bin/python", b"tmp/python") +7 7 | Path("tmp/python").symlink_to("usr/bin/python") # Ok +8 8 | + +PTH211 [*] `os.symlink` should be replaced by `Path.symlink_to` --> PTH211.py:6:1 | 5 | os.symlink("usr/bin/python", "tmp/python") @@ -21,7 +31,17 @@ PTH211 `os.symlink` should be replaced by `Path.symlink_to` | help: Replace with `Path(...).symlink_to(...)` -PTH211 `os.symlink` should be replaced by `Path.symlink_to` +ℹ Safe fix +3 3 | +4 4 | +5 5 | os.symlink("usr/bin/python", "tmp/python") +6 |-os.symlink(b"usr/bin/python", b"tmp/python") + 6 |+Path(b"tmp/python").symlink_to(b"usr/bin/python") +7 7 | Path("tmp/python").symlink_to("usr/bin/python") # Ok +8 8 | +9 9 | os.symlink("usr/bin/python", "tmp/python", target_is_directory=True) + +PTH211 [*] `os.symlink` should be replaced by `Path.symlink_to` --> PTH211.py:9:1 | 7 | Path("tmp/python").symlink_to("usr/bin/python") # Ok @@ -33,7 +53,17 @@ PTH211 `os.symlink` should be replaced by `Path.symlink_to` | help: Replace with `Path(...).symlink_to(...)` -PTH211 `os.symlink` should be replaced by `Path.symlink_to` +ℹ Safe fix +6 6 | os.symlink(b"usr/bin/python", b"tmp/python") +7 7 | Path("tmp/python").symlink_to("usr/bin/python") # Ok +8 8 | +9 |-os.symlink("usr/bin/python", "tmp/python", target_is_directory=True) + 9 |+Path("tmp/python").symlink_to("usr/bin/python", target_is_directory=True) +10 10 | os.symlink(b"usr/bin/python", b"tmp/python", target_is_directory=True) +11 11 | Path("tmp/python").symlink_to("usr/bin/python", target_is_directory=True) # Ok +12 12 | + +PTH211 [*] `os.symlink` should be replaced by `Path.symlink_to` --> PTH211.py:10:1 | 9 | os.symlink("usr/bin/python", "tmp/python", target_is_directory=True) @@ -43,6 +73,16 @@ PTH211 `os.symlink` should be replaced by `Path.symlink_to` | help: Replace with `Path(...).symlink_to(...)` +ℹ Safe fix +7 7 | Path("tmp/python").symlink_to("usr/bin/python") # Ok +8 8 | +9 9 | os.symlink("usr/bin/python", "tmp/python", target_is_directory=True) +10 |-os.symlink(b"usr/bin/python", b"tmp/python", target_is_directory=True) + 10 |+Path(b"tmp/python").symlink_to(b"usr/bin/python", target_is_directory=True) +11 11 | Path("tmp/python").symlink_to("usr/bin/python", target_is_directory=True) # Ok +12 12 | +13 13 | fd = os.open(".", os.O_RDONLY) + PTH211 `os.symlink` should be replaced by `Path.symlink_to` --> PTH211.py:17:1 | @@ -70,12 +110,12 @@ help: Replace with `Path(...).symlink_to(...)` 16 16 | 17 17 | os.symlink(src="usr/bin/python", dst="tmp/python", unknown=True) 18 |-os.symlink("usr/bin/python", dst="tmp/python", target_is_directory=False) - 18 |+Path("tmp/python").symlink_to("usr/bin/python", target_is_directory=False) + 18 |+Path("tmp/python").symlink_to("usr/bin/python") 19 19 | 20 20 | os.symlink(src="usr/bin/python", dst="tmp/python", dir_fd=None) 21 21 | -PTH211 `os.symlink` should be replaced by `Path.symlink_to` +PTH211 [*] `os.symlink` should be replaced by `Path.symlink_to` --> PTH211.py:20:1 | 18 | os.symlink("usr/bin/python", dst="tmp/python", target_is_directory=False) @@ -87,7 +127,17 @@ PTH211 `os.symlink` should be replaced by `Path.symlink_to` | help: Replace with `Path(...).symlink_to(...)` -PTH211 `os.symlink` should be replaced by `Path.symlink_to` +ℹ Safe fix +17 17 | os.symlink(src="usr/bin/python", dst="tmp/python", unknown=True) +18 18 | os.symlink("usr/bin/python", dst="tmp/python", target_is_directory=False) +19 19 | +20 |-os.symlink(src="usr/bin/python", dst="tmp/python", dir_fd=None) + 20 |+Path("tmp/python").symlink_to("usr/bin/python") +21 21 | +22 22 | os.symlink("usr/bin/python", dst="tmp/python", target_is_directory= True ) +23 23 | os.symlink("usr/bin/python", dst="tmp/python", target_is_directory="nonboolean") + +PTH211 [*] `os.symlink` should be replaced by `Path.symlink_to` --> PTH211.py:22:1 | 20 | os.symlink(src="usr/bin/python", dst="tmp/python", dir_fd=None) @@ -98,6 +148,14 @@ PTH211 `os.symlink` should be replaced by `Path.symlink_to` | help: Replace with `Path(...).symlink_to(...)` +ℹ Safe fix +19 19 | +20 20 | os.symlink(src="usr/bin/python", dst="tmp/python", dir_fd=None) +21 21 | +22 |-os.symlink("usr/bin/python", dst="tmp/python", target_is_directory= True ) + 22 |+Path("tmp/python").symlink_to("usr/bin/python", target_is_directory=True) +23 23 | os.symlink("usr/bin/python", dst="tmp/python", target_is_directory="nonboolean") + PTH211 `os.symlink` should be replaced by `Path.symlink_to` --> PTH211.py:23:1 |