diff options
author | Daniël van Noord <13665637+DanielNoord@users.noreply.github.com> | 2021-10-13 14:11:22 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-13 14:11:22 +0200 |
commit | 207a175ae1dd7c7c6d87a7ba5819f26d2bedf9c9 (patch) | |
tree | 3b610bf02f3e9518fc73b2655e09952c3ae8ab63 | |
parent | 92100d0cf40f19ebaec13c73698c574d7a72d39a (diff) | |
download | pylint-git-207a175ae1dd7c7c6d87a7ba5819f26d2bedf9c9.tar.gz |
Create new ``UnsupportedVersionChecker`` checker class (#5148)
Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | doc/whatsnew/2.12.rst | 7 | ||||
-rw-r--r-- | pylint/checkers/strings.py | 13 | ||||
-rw-r--r-- | pylint/checkers/unsupported_version.py | 49 |
4 files changed, 59 insertions, 17 deletions
@@ -29,8 +29,11 @@ Release date: TBA ``use-implicit-booleaness-not-len`` in order to be consistent with ``use-implicit-booleaness-not-comparison``. -* Added ``using-f-string-in-unsupported-version`` checker. Issued when ``py-version`` - is set to a version that does not support f-strings (< 3.6) +* Created new ``UnsupportedVersionChecker`` checker class that includes checks for features + not supported by all versions indicated by a ``py-version``. + + * Added ``using-f-string-in-unsupported-version`` checker. Issued when ``py-version`` + is set to a version that does not support f-strings (< 3.6) What's New in Pylint 2.11.2? diff --git a/doc/whatsnew/2.12.rst b/doc/whatsnew/2.12.rst index c143b52a5..68a3dcac4 100644 --- a/doc/whatsnew/2.12.rst +++ b/doc/whatsnew/2.12.rst @@ -27,8 +27,11 @@ New checkers Closes #3197 -* Added ``using-f-string-in-unsupported-version`` checker. Issued when ``py-version`` - is set to a version that does not support f-strings (< 3.6) +* Created new ``UnsupportedVersionChecker`` checker class that includes checks for features + not supported by all versions indicated by a ``py-version``. + + * Added ``using-f-string-in-unsupported-version`` checker. Issued when ``py-version`` + is set to a version that does not support f-strings (< 3.6) Removed checkers diff --git a/pylint/checkers/strings.py b/pylint/checkers/strings.py index 0c93076c5..45b23eee5 100644 --- a/pylint/checkers/strings.py +++ b/pylint/checkers/strings.py @@ -47,7 +47,6 @@ from astroid import nodes from pylint.checkers import BaseChecker, BaseTokenChecker, utils from pylint.checkers.utils import check_messages from pylint.interfaces import IAstroidChecker, IRawChecker, ITokenChecker -from pylint.utils import get_global_option if TYPE_CHECKING: from typing import Counter # typing.Counter added in Python 3.6.1 @@ -212,12 +211,6 @@ MSGS = { # pylint: disable=consider-using-namedtuple-or-dataclass "Used when we detect a string that does not have any interpolation variables, " "in which case it can be either a normal string without formatting or a bug in the code.", ), - "W1311": ( - "F-strings are not supported by all versions included in the py-version setting", - "using-f-string-in-unsupported-version", - "Used when the py-version set by the user is lower than 3.6 and pylint encounters " - "a f-string.", - ), } OTHER_NODES = ( @@ -417,10 +410,8 @@ class StringFormatChecker(BaseChecker): ) @check_messages("f-string-without-interpolation") - @check_messages("using-f-string-in-unsupported-version") def visit_joinedstr(self, node: nodes.JoinedStr) -> None: self._check_interpolation(node) - self._check_unsupported_version(node) def _check_interpolation(self, node: nodes.JoinedStr) -> None: if isinstance(node.parent, nodes.FormattedValue): @@ -430,10 +421,6 @@ class StringFormatChecker(BaseChecker): return self.add_message("f-string-without-interpolation", node=node) - def _check_unsupported_version(self, node: nodes.JoinedStr) -> None: - if get_global_option(self, "py-version") < (3, 6): - self.add_message("using-f-string-in-unsupported-version", node=node) - @check_messages(*MSGS) def visit_call(self, node: nodes.Call) -> None: func = utils.safe_infer(node.func) diff --git a/pylint/checkers/unsupported_version.py b/pylint/checkers/unsupported_version.py new file mode 100644 index 000000000..917d64cba --- /dev/null +++ b/pylint/checkers/unsupported_version.py @@ -0,0 +1,49 @@ +# Copyright (c) 2021 Daniël 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 + +"""Checker for features used that are not supported by all python versions +indicated by the py-version setting. +""" + +from astroid import nodes + +from pylint.checkers import BaseChecker +from pylint.checkers.utils import check_messages +from pylint.interfaces import IAstroidChecker +from pylint.lint import PyLinter +from pylint.utils import get_global_option + + +class UnsupportedVersionChecker(BaseChecker): + """Checker for features that are not supported by all python versions + indicated by the py-version setting. + """ + + __implements__ = (IAstroidChecker,) + name = "unsupported_version" + msgs = { + "W1601": ( + "F-strings are not supported by all versions included in the py-version setting", + "using-f-string-in-unsupported-version", + "Used when the py-version set by the user is lower than 3.6 and pylint encounters " + "a f-string.", + ), + } + + def open(self) -> None: + """Initialize visit variables and statistics.""" + py_version = get_global_option(self, "py-version") + self._py36_plus = py_version >= (3, 6) + + @check_messages("using-f-string-in-unsupported-version") + def visit_joinedstr(self, node: nodes.JoinedStr) -> None: + """Check f-strings""" + if not self._py36_plus: + self.add_message("using-f-string-in-unsupported-version", node=node) + + +def register(linter: PyLinter) -> None: + """Required method to auto register this checker""" + linter.register_checker(UnsupportedVersionChecker(linter)) |