Skip to content

Commit f45ab49

Browse files
DanielNoordPierre-Sassoulas
authored andcommitted
Fix unspecified-encoding for Path()
This closes #5017
1 parent 67957e2 commit f45ab49

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

ChangeLog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ Release date: TBA
2020
..
2121
Put bug fixes that should not wait for a new minor version here
2222

23+
* ``unspecified-encoding`` now checks the encoding of ``pathlib.Path()`` correctly
24+
25+
Closes #5017
2326

2427

2528
What's New in Pylint 2.11.0?

pylint/checkers/stdlib.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ def visit_call(self, node: nodes.Call) -> None:
531531
or isinstance(node.func, nodes.Attribute)
532532
and node.func.attrname in OPEN_FILES_ENCODING
533533
):
534-
self._check_open_encoded(node)
534+
self._check_open_encoded(node, inferred.root().name)
535535
elif inferred.root().name == UNITTEST_CASE:
536536
self._check_redundant_assert(node, inferred)
537537
elif isinstance(inferred, nodes.ClassDef):
@@ -609,11 +609,18 @@ def _check_open_mode(self, node):
609609
):
610610
self.add_message("bad-open-mode", node=node, args=mode_arg.value)
611611

612-
def _check_open_encoded(self, node: nodes.Call) -> None:
612+
def _check_open_encoded(self, node: nodes.Call, open_module: str) -> None:
613613
"""Check that the encoded argument of an open call is valid."""
614614
mode_arg = None
615615
try:
616-
mode_arg = utils.get_argument_from_call(node, position=1, keyword="mode")
616+
if open_module == "_io":
617+
mode_arg = utils.get_argument_from_call(
618+
node, position=1, keyword="mode"
619+
)
620+
elif open_module == "pathlib":
621+
mode_arg = utils.get_argument_from_call(
622+
node, position=0, keyword="mode"
623+
)
617624
except utils.NoSuchArgumentError:
618625
pass
619626

tests/functional/u/unspecified_encoding_py38.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,11 @@
7272
Path(FILENAME).write_text("string") # [unspecified-encoding]
7373
Path(FILENAME).write_text("string", encoding=None) # [unspecified-encoding]
7474
Path(FILENAME).write_text("string", encoding=LOCALE_ENCODING) # [unspecified-encoding]
75+
76+
LOCALE_ENCODING = locale.getlocale()[1]
77+
Path(FILENAME).open("w+b")
78+
Path(FILENAME).open() # [unspecified-encoding]
79+
Path(FILENAME).open("wt") # [unspecified-encoding]
80+
Path(FILENAME).open("w+") # [unspecified-encoding]
81+
Path(FILENAME).open("w", encoding=None) # [unspecified-encoding]
82+
Path(FILENAME).open("w", encoding=LOCALE_ENCODING)

tests/functional/u/unspecified_encoding_py38.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ unspecified-encoding:65:0::Using open without explicitly specifying an encoding:
1919
unspecified-encoding:72:0::Using open without explicitly specifying an encoding:HIGH
2020
unspecified-encoding:73:0::Using open without explicitly specifying an encoding:HIGH
2121
unspecified-encoding:74:0::Using open without explicitly specifying an encoding:HIGH
22+
unspecified-encoding:78:0::Using open without explicitly specifying an encoding:HIGH
23+
unspecified-encoding:79:0::Using open without explicitly specifying an encoding:HIGH
24+
unspecified-encoding:80:0::Using open without explicitly specifying an encoding:HIGH
25+
unspecified-encoding:81:0::Using open without explicitly specifying an encoding:HIGH

0 commit comments

Comments
 (0)