Skip to content

Commit

Permalink
Detect aiofiles calls in UP015
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Aug 30, 2024
1 parent 28ab5f4 commit 9897dd9
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
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")

0 comments on commit 9897dd9

Please sign in to comment.