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

Fix unspecified-encoding for Path() #5020

Merged
merged 1 commit into from
Sep 16, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ Release date: TBA
..
Put bug fixes that should not wait for a new minor version here

* ``unspecified-encoding`` now checks the encoding of ``pathlib.Path()`` correctly

Closes #5017


What's New in Pylint 2.11.0?
Expand Down
13 changes: 10 additions & 3 deletions pylint/checkers/stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ def visit_call(self, node: nodes.Call) -> None:
or isinstance(node.func, nodes.Attribute)
and node.func.attrname in OPEN_FILES_ENCODING
):
self._check_open_encoded(node)
self._check_open_encoded(node, inferred.root().name)
elif inferred.root().name == UNITTEST_CASE:
self._check_redundant_assert(node, inferred)
elif isinstance(inferred, nodes.ClassDef):
Expand Down Expand Up @@ -609,11 +609,18 @@ def _check_open_mode(self, node):
):
self.add_message("bad-open-mode", node=node, args=mode_arg.value)

def _check_open_encoded(self, node: nodes.Call) -> None:
def _check_open_encoded(self, node: nodes.Call, open_module: str) -> None:
"""Check that the encoded argument of an open call is valid."""
mode_arg = None
try:
mode_arg = utils.get_argument_from_call(node, position=1, keyword="mode")
if open_module == "_io":
mode_arg = utils.get_argument_from_call(
node, position=1, keyword="mode"
)
elif open_module == "pathlib":
mode_arg = utils.get_argument_from_call(
node, position=0, keyword="mode"
)
except utils.NoSuchArgumentError:
pass

Expand Down
8 changes: 8 additions & 0 deletions tests/functional/u/unspecified_encoding_py38.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,11 @@
Path(FILENAME).write_text("string") # [unspecified-encoding]
Path(FILENAME).write_text("string", encoding=None) # [unspecified-encoding]
Path(FILENAME).write_text("string", encoding=LOCALE_ENCODING) # [unspecified-encoding]

LOCALE_ENCODING = locale.getlocale()[1]
Path(FILENAME).open("w+b")
Path(FILENAME).open() # [unspecified-encoding]
Path(FILENAME).open("wt") # [unspecified-encoding]
Path(FILENAME).open("w+") # [unspecified-encoding]
Path(FILENAME).open("w", encoding=None) # [unspecified-encoding]
Path(FILENAME).open("w", encoding=LOCALE_ENCODING)
4 changes: 4 additions & 0 deletions tests/functional/u/unspecified_encoding_py38.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ unspecified-encoding:65:0::Using open without explicitly specifying an encoding:
unspecified-encoding:72:0::Using open without explicitly specifying an encoding:HIGH
unspecified-encoding:73:0::Using open without explicitly specifying an encoding:HIGH
unspecified-encoding:74:0::Using open without explicitly specifying an encoding:HIGH
unspecified-encoding:78:0::Using open without explicitly specifying an encoding:HIGH
unspecified-encoding:79:0::Using open without explicitly specifying an encoding:HIGH
unspecified-encoding:80:0::Using open without explicitly specifying an encoding:HIGH
unspecified-encoding:81:0::Using open without explicitly specifying an encoding:HIGH