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

Extend unspecified-encoding checker #4946

Merged
merged 1 commit into from
Sep 1, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ Release date: TBA

Closes #3282

* The ``unspecified-encoding`` checker now also checks calls to ``pathlib.Path().read_text()``
and ``pathlib.Path().write_text()``

Closes #4945


What's New in Pylint 2.10.3?
============================
Expand Down
5 changes: 5 additions & 0 deletions doc/whatsnew/2.11.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,8 @@ Other Changes
* Fix false positive ``dict-iter-missing-items`` for dictionaries only using tuples as keys

Closes #3282

* The ``unspecified-encoding`` checker now also checks calls to ``pathlib.Path().read_text()``
and ``pathlib.Path().write_text()``

Closes #4945
6 changes: 3 additions & 3 deletions pylint/checkers/stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@
from pylint.interfaces import IAstroidChecker

OPEN_FILES_MODE = ("open", "file")
OPEN_FILES_ENCODING = ("open",)
OPEN_FILES_ENCODING = ("open", "read_text", "write_text")
UNITTEST_CASE = "unittest.case"
THREADING_THREAD = "threading.Thread"
COPY_COPY = "copy.copy"
OS_ENVIRON = "os._Environ"
ENV_GETTERS = ("os.getenv",)
SUBPROCESS_POPEN = "subprocess.Popen"
SUBPROCESS_RUN = "subprocess.run"
OPEN_MODULE = "_io"
OPEN_MODULE = {"_io", "pathlib"}
DEBUG_BREAKPOINTS = ("builtins.breakpoint", "sys.breakpointhook", "pdb.set_trace")


Expand Down Expand Up @@ -515,7 +515,7 @@ def visit_call(self, node: nodes.Call) -> None:
for inferred in utils.infer_all(node.func):
if inferred is astroid.Uninferable:
continue
if inferred.root().name == OPEN_MODULE:
if inferred.root().name in OPEN_MODULE:
if (
isinstance(node.func, nodes.Name)
and node.func.name in OPEN_FILES_MODE
Expand Down
19 changes: 19 additions & 0 deletions tests/functional/u/unspecified_encoding_py38.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# pylint: disable=consider-using-with
import io
import locale
from pathlib import Path

FILENAME = "foo.bar"
open(FILENAME, "w", encoding="utf-8")
Expand Down Expand Up @@ -53,3 +54,21 @@
LOCALE_ENCODING = None
with io.open(FILENAME, encoding=LOCALE_ENCODING) as f: # [unspecified-encoding]
pass

LOCALE_ENCODING = locale.getlocale()[1]
Path(FILENAME).read_text(encoding=LOCALE_ENCODING)
Path(FILENAME).read_text(encoding="utf8")

LOCALE_ENCODING = None
Path(FILENAME).read_text() # [unspecified-encoding]
Path(FILENAME).read_text(encoding=None) # [unspecified-encoding]
Path(FILENAME).read_text(encoding=LOCALE_ENCODING) # [unspecified-encoding]

LOCALE_ENCODING = locale.getlocale()[1]
Path(FILENAME).write_text("string", encoding=LOCALE_ENCODING)
Path(FILENAME).write_text("string", encoding="utf8")

LOCALE_ENCODING = None
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]
36 changes: 21 additions & 15 deletions tests/functional/u/unspecified_encoding_py38.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
unspecified-encoding:10:0::"Using open without explicitly specifying an encoding"
unspecified-encoding:11:0::"Using open without explicitly specifying an encoding"
unspecified-encoding:12:0::"Using open without explicitly specifying an encoding"
unspecified-encoding:13:0::"Using open without explicitly specifying an encoding"
unspecified-encoding:14:0::"Using open without explicitly specifying an encoding"
unspecified-encoding:23:5::"Using open without explicitly specifying an encoding"
unspecified-encoding:26:5::"Using open without explicitly specifying an encoding"
unspecified-encoding:30:5::"Using open without explicitly specifying an encoding"
unspecified-encoding:35:0::"Using open without explicitly specifying an encoding"
unspecified-encoding:36:0::"Using open without explicitly specifying an encoding"
unspecified-encoding:37:0::"Using open without explicitly specifying an encoding"
unspecified-encoding:38:0::"Using open without explicitly specifying an encoding"
unspecified-encoding:47:5::"Using open without explicitly specifying an encoding"
unspecified-encoding:50:5::"Using open without explicitly specifying an encoding"
unspecified-encoding:54:5::"Using open without explicitly specifying an encoding"
unspecified-encoding:11:0::Using open without explicitly specifying an encoding:HIGH
unspecified-encoding:12:0::Using open without explicitly specifying an encoding:HIGH
unspecified-encoding:13:0::Using open without explicitly specifying an encoding:HIGH
unspecified-encoding:14:0::Using open without explicitly specifying an encoding:HIGH
unspecified-encoding:15:0::Using open without explicitly specifying an encoding:HIGH
unspecified-encoding:24:5::Using open without explicitly specifying an encoding:HIGH
unspecified-encoding:27:5::Using open without explicitly specifying an encoding:HIGH
unspecified-encoding:31:5::Using open without explicitly specifying an encoding:HIGH
unspecified-encoding:36:0::Using open without explicitly specifying an encoding:HIGH
unspecified-encoding:37:0::Using open without explicitly specifying an encoding:HIGH
unspecified-encoding:38:0::Using open without explicitly specifying an encoding:HIGH
unspecified-encoding:39:0::Using open without explicitly specifying an encoding:HIGH
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:73:0::Using open without explicitly specifying an encoding:HIGH
unspecified-encoding:74:0::Using open without explicitly specifying an encoding:HIGH