Skip to content

Commit

Permalink
Add unspecified-encoding checker pylint-dev#3826
Browse files Browse the repository at this point in the history
This adds an unspecified-encoding checker that adds a warning
whenever open() is called without an explicit encoding argument.
This closes pylint-dev#3826
  • Loading branch information
DanielNoord committed Jul 26, 2021
1 parent e04de25 commit 393c927
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CONTRIBUTORS.txt
Expand Up @@ -521,3 +521,5 @@ contributors:
* Yilei Yang: contributor

* Marcin Kurczewski (rr-): contributor

* Daniel van Noord (DanielNoord): contributor
5 changes: 3 additions & 2 deletions ChangeLog
Expand Up @@ -6,8 +6,9 @@ What's New in Pylint 2.10.0?
============================
Release date: TBA

..
Put new features here and also in 'doc/whatsnew/2.10.rst'
* Added ``unspecified-encoding``: Emitted when open() is called without specifying an encoding

Closes #3826


What's New in Pylint 2.9.6?
Expand Down
2 changes: 2 additions & 0 deletions doc/whatsnew/2.10.rst
Expand Up @@ -12,7 +12,9 @@ Summary -- Release highlights
New checkers
============

* Added ``unspecified-encoding``: Emitted when open() is called without specifying an encoding

Closes #3826

Other Changes
=============
Expand Down
22 changes: 22 additions & 0 deletions pylint/checkers/stdlib.py
Expand Up @@ -30,6 +30,7 @@
# Copyright (c) 2021 Marc Mueller <30130371+cdce8p@users.noreply.github.com>
# Copyright (c) 2021 Matus Valo <matusvalo@users.noreply.github.com>
# Copyright (c) 2021 victor <16359131+jiajunsu@users.noreply.github.com>
# Copyright (c) 2021 Daniel van Noord <13665637+DanielNoord@users.noreply.github.com>

# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
Expand Down Expand Up @@ -425,6 +426,13 @@ class StdlibChecker(DeprecatedMixin, BaseChecker):
"deprecated-decorator",
"The decorator is marked as deprecated and will be removed in the future.",
),
"W1514": (
"Using open without explicitly specifying an encoding",
"unspecified-encoding",
"It is better to specify an encoding when opening documents. "
"Failing to do so can create problems when opening documents on other systems. "
"See https://www.python.org/dev/peps/pep-0597/",
),
}

def __init__(self, linter=None):
Expand Down Expand Up @@ -485,6 +493,7 @@ def _check_shallow_copy_environ(self, node):
"subprocess-popen-preexec-fn",
"subprocess-run-check",
"deprecated-class",
"unspecified-encoding",
)
def visit_call(self, node):
"""Visit a Call node."""
Expand All @@ -496,6 +505,8 @@ def visit_call(self, node):
if inferred.root().name == OPEN_MODULE:
if getattr(node.func, "name", None) in OPEN_FILES:
self._check_open_mode(node)
if getattr(node.func, "name", None) == "open":
self._check_open_encoded(node)
elif inferred.root().name == UNITTEST_CASE:
self._check_redundant_assert(node, inferred)
elif isinstance(inferred, astroid.ClassDef):
Expand Down Expand Up @@ -573,6 +584,17 @@ def _check_open_mode(self, node):
):
self.add_message("bad-open-mode", node=node, args=mode_arg.value)

def _check_open_encoded(self, node):
"""Check that the encoded argument of an open call is valid."""
try:
open_arg = utils.get_argument_from_call(node, keyword="encoding")
except utils.NoSuchArgumentError:
self.add_message("unspecified-encoding", node=node)
if open_arg:
open_arg = utils.safe_infer(open_arg)
if isinstance(open_arg, astroid.Const) and open_arg.value is None:
self.add_message("unspecified-encoding", node=node)

def _check_env_function(self, node, infer):
env_name_kwarg = "key"
env_value_kwarg = "default"
Expand Down
7 changes: 7 additions & 0 deletions tests/functional/u/unspecified-encoding_py3.py
@@ -0,0 +1,7 @@
"""Warnings for using open() without specifying an encoding"""
# pylint: disable=consider-using-with

NAME = "foo.bar"
open(NAME, "wb", encoding="utf-8")
open(NAME, "w", encoding=None) # [unspecified-encoding]
open(NAME, "rb") # [unspecified-encoding]
2 changes: 2 additions & 0 deletions tests/functional/u/unspecified-encoding_py3.txt
@@ -0,0 +1,2 @@
bad-open-mode:6:0::"Using open without explicitly specifying an encoding"
bad-open-mode:7:0::"Using open without explicitly specifying an encoding"

0 comments on commit 393c927

Please sign in to comment.