From e4eaa400bd811ccc373cbab71e5cbeb2b8ac714a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Mon, 11 Oct 2021 09:14:37 +0200 Subject: Add ``using-f-string-in-unsupported-version`` checker (#5139) Co-authored-by: Pierre Sassoulas --- ChangeLog | 3 +++ doc/whatsnew/2.12.rst | 3 +++ pylint/checkers/strings.py | 16 ++++++++++++++++ .../u/unsupported/unsupported_version_for_f_string.py | 5 +++++ .../u/unsupported/unsupported_version_for_f_string.rc | 2 ++ .../u/unsupported/unsupported_version_for_f_string.txt | 2 ++ 6 files changed, 31 insertions(+) create mode 100644 tests/functional/u/unsupported/unsupported_version_for_f_string.py create mode 100644 tests/functional/u/unsupported/unsupported_version_for_f_string.rc create mode 100644 tests/functional/u/unsupported/unsupported_version_for_f_string.txt diff --git a/ChangeLog b/ChangeLog index e1699f0d2..567eaeaa1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -29,6 +29,9 @@ 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) + What's New in Pylint 2.11.2? ============================ diff --git a/doc/whatsnew/2.12.rst b/doc/whatsnew/2.12.rst index f6d99185a..c143b52a5 100644 --- a/doc/whatsnew/2.12.rst +++ b/doc/whatsnew/2.12.rst @@ -27,6 +27,9 @@ 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) + Removed checkers ================ diff --git a/pylint/checkers/strings.py b/pylint/checkers/strings.py index f983e8afe..0c93076c5 100644 --- a/pylint/checkers/strings.py +++ b/pylint/checkers/strings.py @@ -47,6 +47,7 @@ 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 @@ -211,6 +212,12 @@ 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 = ( @@ -410,7 +417,12 @@ 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): return for value in node.values: @@ -418,6 +430,10 @@ 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/tests/functional/u/unsupported/unsupported_version_for_f_string.py b/tests/functional/u/unsupported/unsupported_version_for_f_string.py new file mode 100644 index 000000000..c7ae5e385 --- /dev/null +++ b/tests/functional/u/unsupported/unsupported_version_for_f_string.py @@ -0,0 +1,5 @@ +"""Tests for the use of f-strings whenever the py-version is set < 3.6""" +# pylint: disable=f-string-without-interpolation + +VAR = f"a simple f-string" # [using-f-string-in-unsupported-version] +VAR_TWO = f"a simple f-string {'with'} interpolation" # [using-f-string-in-unsupported-version] diff --git a/tests/functional/u/unsupported/unsupported_version_for_f_string.rc b/tests/functional/u/unsupported/unsupported_version_for_f_string.rc new file mode 100644 index 000000000..d0a4fe2d4 --- /dev/null +++ b/tests/functional/u/unsupported/unsupported_version_for_f_string.rc @@ -0,0 +1,2 @@ +[typing] +py-version=3.5 diff --git a/tests/functional/u/unsupported/unsupported_version_for_f_string.txt b/tests/functional/u/unsupported/unsupported_version_for_f_string.txt new file mode 100644 index 000000000..a30eda226 --- /dev/null +++ b/tests/functional/u/unsupported/unsupported_version_for_f_string.txt @@ -0,0 +1,2 @@ +using-f-string-in-unsupported-version:4:6::F-strings are not supported by all versions included in the py-version setting:HIGH +using-f-string-in-unsupported-version:5:10::F-strings are not supported by all versions included in the py-version setting:HIGH -- cgit v1.2.1