diff options
author | Daniƫl van Noord <13665637+DanielNoord@users.noreply.github.com> | 2021-09-17 13:58:43 +0200 |
---|---|---|
committer | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2021-09-17 14:09:48 +0200 |
commit | 77e5bd78438ba26bc4dd4837d3535bbc227b8e5c (patch) | |
tree | 7fe4edf40492fc8ee4a6f93cd7425087d6c896f6 | |
parent | 6f24105d90224a585d5e5b06361415c05a7030b9 (diff) | |
download | pylint-git-77e5bd78438ba26bc4dd4837d3535bbc227b8e5c.tar.gz |
Fix ``unspecified-encoding`` for ``Path.read_text()``
Closes #5029
-rw-r--r-- | ChangeLog | 6 | ||||
-rw-r--r-- | pylint/checkers/stdlib.py | 11 | ||||
-rw-r--r-- | tests/functional/u/unspecified_encoding_py38.py | 1 | ||||
-rw-r--r-- | tests/functional/u/unspecified_encoding_py38.txt | 6 |
4 files changed, 18 insertions, 6 deletions
@@ -17,6 +17,12 @@ What's New in Pylint 2.11.2? ============================ Release date: TBA +.. + Put bug fixes that should not wait for a new minor version here + +* Fix regression for ``unspecified-encoding`` with ``pathlib.Path.read_text()`` + + Closes #5029 What's New in Pylint 2.11.1? diff --git a/pylint/checkers/stdlib.py b/pylint/checkers/stdlib.py index a4cd88a4d..508c358e1 100644 --- a/pylint/checkers/stdlib.py +++ b/pylint/checkers/stdlib.py @@ -629,9 +629,14 @@ class StdlibChecker(DeprecatedMixin, BaseChecker): if not mode_arg or "b" not in mode_arg.value: encoding_arg = None try: - encoding_arg = utils.get_argument_from_call( - node, position=None, keyword="encoding" - ) + if open_module == "pathlib" and node.func.attrname == "read_text": + encoding_arg = utils.get_argument_from_call( + node, position=0, keyword="encoding" + ) + else: + encoding_arg = utils.get_argument_from_call( + node, position=None, keyword="encoding" + ) except utils.NoSuchArgumentError: self.add_message("unspecified-encoding", node=node) diff --git a/tests/functional/u/unspecified_encoding_py38.py b/tests/functional/u/unspecified_encoding_py38.py index 8f143474b..79c974bc2 100644 --- a/tests/functional/u/unspecified_encoding_py38.py +++ b/tests/functional/u/unspecified_encoding_py38.py @@ -58,6 +58,7 @@ with io.open(FILENAME, encoding=LOCALE_ENCODING) as f: # [unspecified-encoding] LOCALE_ENCODING = locale.getlocale()[1] Path(FILENAME).read_text(encoding=LOCALE_ENCODING) Path(FILENAME).read_text(encoding="utf8") +Path(FILENAME).read_text("utf8") LOCALE_ENCODING = None Path(FILENAME).read_text() # [unspecified-encoding] diff --git a/tests/functional/u/unspecified_encoding_py38.txt b/tests/functional/u/unspecified_encoding_py38.txt index 832b3c750..a83165421 100644 --- a/tests/functional/u/unspecified_encoding_py38.txt +++ b/tests/functional/u/unspecified_encoding_py38.txt @@ -13,13 +13,13 @@ unspecified-encoding:39:0::Using open without explicitly specifying an encoding: unspecified-encoding:48:5::Using open without explicitly specifying an encoding:HIGH unspecified-encoding:51:5::Using open without explicitly specifying an encoding:HIGH unspecified-encoding:55:5::Using open without explicitly specifying an encoding:HIGH -unspecified-encoding:63:0::Using open without explicitly specifying an encoding:HIGH unspecified-encoding:64:0::Using open without explicitly specifying an encoding:HIGH unspecified-encoding:65:0::Using open without explicitly specifying an encoding:HIGH -unspecified-encoding:72:0::Using open without explicitly specifying an encoding:HIGH +unspecified-encoding:66: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:75: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 +unspecified-encoding:82:0::Using open without explicitly specifying an encoding:HIGH |