Skip to content

Commit f53c580

Browse files
authored
[pylint] Fix PLW1514 not recognizing the encoding positional argument of codecs.open (#18109)
<!-- Thank you for contributing to Ruff/ty! To help us out with reviewing, please consider the following: - Does this pull request include a summary of the change? (See below.) - Does this pull request include a descriptive title? (Please prefix with `[ty]` for ty pull requests.) - Does this pull request include references to any relevant issues? --> ## Summary Fixes #18107 <!-- What's the purpose of the change? What does it do, and why? --> ## Test Plan Snapshot tests <!-- How was it tested? -->
1 parent 2ceba6a commit f53c580

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

crates/ruff_linter/resources/test/fixtures/pylint/unspecified_encoding.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,6 @@ def func(*args, **kwargs):
9494
# Violation but not detectable
9595
x = Path("foo.txt")
9696
x.open()
97+
98+
# https://github.com/astral-sh/ruff/issues/18107
99+
codecs.open("plw1514.py", "r", "utf-8").close() # this is fine

crates/ruff_linter/src/rules/pylint/rules/unspecified_encoding.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,18 @@ fn is_violation(call: &ast::ExprCall, qualified_name: &Callee) -> bool {
215215
return false;
216216
}
217217
}
218+
219+
let encoding_param_pos = match qualified_name.segments() {
220+
// The `encoding` parameter position for `codecs.open`
221+
["codecs", _] => 2,
222+
// The `encoding` parameter position for `_io.open` and the builtin `open`
223+
_ => 3,
224+
};
225+
218226
// else mode not specified, defaults to text mode
219-
call.arguments.find_argument_value("encoding", 3).is_none()
227+
call.arguments
228+
.find_argument_value("encoding", encoding_param_pos)
229+
.is_none()
220230
}
221231
["tempfile", tempfile_class @ ("TemporaryFile" | "NamedTemporaryFile" | "SpooledTemporaryFile")] =>
222232
{

0 commit comments

Comments
 (0)