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

[pyupgrade] Detect aiofiles.open calls in UP015 #13173

Merged
merged 1 commit into from
Aug 30, 2024
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
7 changes: 7 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pyupgrade/UP015.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,10 @@
open("foo", "rt")
open("f", "r", encoding="UTF-8")
open("f", "wt")


import aiofiles

aiofiles.open("foo", "U")
aiofiles.open("foo", "r")
aiofiles.open("foo", mode="r")
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,16 @@ impl AlwaysFixableViolation for RedundantOpenModes {

/// UP015
pub(crate) fn redundant_open_modes(checker: &mut Checker, call: &ast::ExprCall) {
if !checker.semantic().match_builtin_expr(&call.func, "open") {
if !checker
.semantic()
.resolve_qualified_name(&call.func)
.is_some_and(|qualified_name| {
matches!(
qualified_name.segments(),
["" | "builtins" | "aiofiles", "open"]
)
})
{
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -899,4 +899,55 @@ UP015.py:70:1: UP015 [*] Unnecessary open mode parameters, use ""rb""
72 72 | open = 1
73 73 | open("foo", "U")

UP015.py:85:1: UP015 [*] Unnecessary open mode parameters
|
83 | import aiofiles
84 |
85 | aiofiles.open("foo", "U")
| ^^^^^^^^^^^^^^^^^^^^^^^^^ UP015
86 | aiofiles.open("foo", "r")
87 | aiofiles.open("foo", mode="r")
|
= help: Remove open mode parameters

ℹ Safe fix
82 82 |
83 83 | import aiofiles
84 84 |
85 |-aiofiles.open("foo", "U")
85 |+aiofiles.open("foo")
86 86 | aiofiles.open("foo", "r")
87 87 | aiofiles.open("foo", mode="r")

UP015.py:86:1: UP015 [*] Unnecessary open mode parameters
|
85 | aiofiles.open("foo", "U")
86 | aiofiles.open("foo", "r")
| ^^^^^^^^^^^^^^^^^^^^^^^^^ UP015
87 | aiofiles.open("foo", mode="r")
|
= help: Remove open mode parameters

ℹ Safe fix
83 83 | import aiofiles
84 84 |
85 85 | aiofiles.open("foo", "U")
86 |-aiofiles.open("foo", "r")
86 |+aiofiles.open("foo")
87 87 | aiofiles.open("foo", mode="r")

UP015.py:87:1: UP015 [*] Unnecessary open mode parameters
|
85 | aiofiles.open("foo", "U")
86 | aiofiles.open("foo", "r")
87 | aiofiles.open("foo", mode="r")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ UP015
|
= help: Remove open mode parameters

ℹ Safe fix
84 84 |
85 85 | aiofiles.open("foo", "U")
86 86 | aiofiles.open("foo", "r")
87 |-aiofiles.open("foo", mode="r")
87 |+aiofiles.open("foo")
Loading