diff options
author | Marc Mueller <30130371+cdce8p@users.noreply.github.com> | 2021-09-17 19:55:29 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-17 19:55:29 +0200 |
commit | e8d112132dbd88e91257cdb8380ddc4e1561e305 (patch) | |
tree | f647d0c773f2538c5f90413fad14803c3f3c5f98 | |
parent | 096b31f109c1ee34c668f7ffe271c6493c889b80 (diff) | |
download | pylint-git-e8d112132dbd88e91257cdb8380ddc4e1561e305.tar.gz |
Require Python 3.6 for consider f-string check (#5024)
* Require Python 3.6 for consider f-string check
Co-authored-by: Ville Skyttä <ville.skytta@iki.fi>
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
-rw-r--r-- | ChangeLog | 8 | ||||
-rw-r--r-- | pylint/checkers/refactoring/recommendation_checker.py | 18 | ||||
-rw-r--r-- | tests/functional/p/py_version/py_version_35.py | 14 | ||||
-rw-r--r-- | tests/functional/p/py_version/py_version_35.rc | 3 |
4 files changed, 35 insertions, 8 deletions
@@ -20,6 +20,11 @@ Release date: TBA .. Put bug fixes that should not wait for a new minor version here +* Don't emit ``consider-using-f-string`` if ``py-version`` is set to Python < ``3.6``. + ``f-strings`` were added in Python ``3.6`` + + Closes #5019 + * Fix regression for ``unspecified-encoding`` with ``pathlib.Path.read_text()`` Closes #5029 @@ -29,9 +34,6 @@ What's New in Pylint 2.11.1? ============================ Release date: 2021-09-16 -.. - Put bug fixes that should not wait for a new minor version here - * ``unspecified-encoding`` now checks the encoding of ``pathlib.Path()`` correctly Closes #5017 diff --git a/pylint/checkers/refactoring/recommendation_checker.py b/pylint/checkers/refactoring/recommendation_checker.py index 84c399988..b22f18acd 100644 --- a/pylint/checkers/refactoring/recommendation_checker.py +++ b/pylint/checkers/refactoring/recommendation_checker.py @@ -7,6 +7,7 @@ from astroid import nodes from pylint import checkers, interfaces from pylint.checkers import utils +from pylint.utils.utils import get_global_option class RecommendationChecker(checkers.BaseChecker): @@ -56,10 +57,15 @@ class RecommendationChecker(checkers.BaseChecker): "Formatting a regular string which could be a f-string", "consider-using-f-string", "Used when we detect a string that is being formatted with format() or % " - "which could potentially be a f-string. The use of f-strings is preferred.", + "which could potentially be a f-string. The use of f-strings is preferred. " + "Requires Python 3.6", ), } + def open(self) -> None: + py_version = get_global_option(self, "py-version") + self._py36_plus = py_version >= (3, 6) + @staticmethod def _is_builtin(node, function): inferred = utils.safe_infer(node) @@ -321,10 +327,12 @@ class RecommendationChecker(checkers.BaseChecker): @utils.check_messages("consider-using-f-string") def visit_const(self, node: nodes.Const) -> None: - if node.pytype() == "builtins.str" and not isinstance( - node.parent, nodes.JoinedStr - ): - self._detect_replacable_format_call(node) + if self._py36_plus: + # f-strings require Python 3.6 + if node.pytype() == "builtins.str" and not isinstance( + node.parent, nodes.JoinedStr + ): + self._detect_replacable_format_call(node) def _detect_replacable_format_call(self, node: nodes.Const) -> None: """Check whether a string is used in a call to format() or '%' and whether it diff --git a/tests/functional/p/py_version/py_version_35.py b/tests/functional/p/py_version/py_version_35.py new file mode 100644 index 000000000..ee880c54a --- /dev/null +++ b/tests/functional/p/py_version/py_version_35.py @@ -0,0 +1,14 @@ +"""Test warnings aren't emitted for features that require Python > 3.5""" +# pylint: disable=invalid-name + +# consider-using-f-string -> requires Python 3.6 +"Hello {}".format("World") + + +# ------ +# CodeStyle extension + +# consider-using-assignment-expr -> requires Python 3.8 +a1 = 2 +if a1: + ... diff --git a/tests/functional/p/py_version/py_version_35.rc b/tests/functional/p/py_version/py_version_35.rc new file mode 100644 index 000000000..6f19ec36a --- /dev/null +++ b/tests/functional/p/py_version/py_version_35.rc @@ -0,0 +1,3 @@ +[MASTER] +load-plugins=pylint.extensions.code_style +py-version=3.5 |