Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PLW1507] Mark fix unsafe #16343

Merged
merged 2 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,11 @@
import os

copied_env = copy.copy(os.environ) # [shallow-copy-environ]


# Test case where the proposed fix is wrong, i.e., unsafe fix
# Ref: https://github.com/astral-sh/ruff/issues/16274#event-16423475135

os.environ["X"] = "0"
env = copy.copy(os.environ)
os.environ["X"] = "1"
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ use crate::checkers::ast::Checker;
/// env = os.environ.copy()
/// ```
///
/// ## Fix safety
///
/// This rule's fix is marked as unsafe because replacing a shallow copy with a deep copy can lead
/// to unintended side effects. If the program modifies the shallow copy at some point, changing it
/// to a deep copy may prevent those modifications from affecting the original data, potentially
/// altering the program's behavior.
///
/// ## References
/// - [Python documentation: `copy` — Shallow and deep copy operations](https://docs.python.org/3/library/copy.html)
/// - [Python documentation: `os.environ`](https://docs.python.org/3/library/os.html#os.environ)
Expand Down Expand Up @@ -82,7 +89,7 @@ pub(crate) fn shallow_copy_environ(checker: &Checker, call: &ast::ExprCall) {
}

let mut diagnostic = Diagnostic::new(ShallowCopyEnviron, call.range());
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement(
format!("{}.copy()", checker.locator().slice(arg)),
call.range(),
)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,29 @@ shallow_copy_environ.py:4:14: PLW1507 [*] Shallow copy of `os.environ` via `copy
|
= help: Replace with `os.environ.copy()`

Safe fix
Unsafe fix
1 1 | import copy
2 2 | import os
3 3 |
4 |-copied_env = copy.copy(os.environ) # [shallow-copy-environ]
4 |+copied_env = os.environ.copy() # [shallow-copy-environ]
5 5 |
6 6 |
7 7 | # Test case where the proposed fix is wrong, i.e., unsafe fix

shallow_copy_environ.py:11:7: PLW1507 [*] Shallow copy of `os.environ` via `copy.copy(os.environ)`
|
10 | os.environ["X"] = "0"
11 | env = copy.copy(os.environ)
| ^^^^^^^^^^^^^^^^^^^^^ PLW1507
12 | os.environ["X"] = "1"
|
= help: Replace with `os.environ.copy()`

ℹ Unsafe fix
8 8 | # Ref: https://github.com/astral-sh/ruff/issues/16274#event-16423475135
9 9 |
10 10 | os.environ["X"] = "0"
11 |-env = copy.copy(os.environ)
11 |+env = os.environ.copy()
12 12 | os.environ["X"] = "1"
Loading