Skip to content

Commit

Permalink
Fix unspecified-encoding for Path()
Browse files Browse the repository at this point in the history
This closes #5017
  • Loading branch information
DanielNoord authored and Pierre-Sassoulas committed Sep 16, 2021
1 parent 67957e2 commit f45ab49
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
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

0 comments on commit f45ab49

Please sign in to comment.