[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? -->
This commit is contained in:
Victor Hugo Gomes 2025-05-15 17:17:07 -03:00 committed by GitHub
parent 2ceba6ae67
commit f53c580c53
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 1 deletions

View file

@ -94,3 +94,6 @@ Path("foo.txt").write_text(text, **kwargs)
# Violation but not detectable
x = Path("foo.txt")
x.open()
# https://github.com/astral-sh/ruff/issues/18107
codecs.open("plw1514.py", "r", "utf-8").close() # this is fine

View file

@ -215,8 +215,18 @@ fn is_violation(call: &ast::ExprCall, qualified_name: &Callee) -> bool {
return false;
}
}
let encoding_param_pos = match qualified_name.segments() {
// The `encoding` parameter position for `codecs.open`
["codecs", _] => 2,
// The `encoding` parameter position for `_io.open` and the builtin `open`
_ => 3,
};
// else mode not specified, defaults to text mode
call.arguments.find_argument_value("encoding", 3).is_none()
call.arguments
.find_argument_value("encoding", encoding_param_pos)
.is_none()
}
["tempfile", tempfile_class @ ("TemporaryFile" | "NamedTemporaryFile" | "SpooledTemporaryFile")] =>
{