unsupported_version.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  2. # For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
  3. # Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
  4. """Checker for features used that are not supported by all python versions
  5. indicated by the py-version setting.
  6. """
  7. from __future__ import annotations
  8. from typing import TYPE_CHECKING
  9. from astroid import nodes
  10. from pylint.checkers import BaseChecker
  11. from pylint.checkers.utils import (
  12. only_required_for_messages,
  13. safe_infer,
  14. uninferable_final_decorators,
  15. )
  16. if TYPE_CHECKING:
  17. from pylint.lint import PyLinter
  18. class UnsupportedVersionChecker(BaseChecker):
  19. """Checker for features that are not supported by all python versions
  20. indicated by the py-version setting.
  21. """
  22. name = "unsupported_version"
  23. msgs = {
  24. "W2601": (
  25. "F-strings are not supported by all versions included in the py-version setting",
  26. "using-f-string-in-unsupported-version",
  27. "Used when the py-version set by the user is lower than 3.6 and pylint encounters "
  28. "a f-string.",
  29. ),
  30. "W2602": (
  31. "typing.final is not supported by all versions included in the py-version setting",
  32. "using-final-decorator-in-unsupported-version",
  33. "Used when the py-version set by the user is lower than 3.8 and pylint encounters "
  34. "a ``typing.final`` decorator.",
  35. ),
  36. }
  37. def open(self) -> None:
  38. """Initialize visit variables and statistics."""
  39. py_version = self.linter.config.py_version
  40. self._py36_plus = py_version >= (3, 6)
  41. self._py38_plus = py_version >= (3, 8)
  42. @only_required_for_messages("using-f-string-in-unsupported-version")
  43. def visit_joinedstr(self, node: nodes.JoinedStr) -> None:
  44. """Check f-strings."""
  45. if not self._py36_plus:
  46. self.add_message("using-f-string-in-unsupported-version", node=node)
  47. @only_required_for_messages("using-final-decorator-in-unsupported-version")
  48. def visit_decorators(self, node: nodes.Decorators) -> None:
  49. """Check decorators."""
  50. self._check_typing_final(node)
  51. def _check_typing_final(self, node: nodes.Decorators) -> None:
  52. """Add a message when the `typing.final` decorator is used and the
  53. py-version is lower than 3.8.
  54. """
  55. if self._py38_plus:
  56. return
  57. decorators = []
  58. for decorator in node.get_children():
  59. inferred = safe_infer(decorator)
  60. if inferred and inferred.qname() == "typing.final":
  61. decorators.append(decorator)
  62. for decorator in decorators or uninferable_final_decorators(node):
  63. self.add_message(
  64. "using-final-decorator-in-unsupported-version", node=decorator
  65. )
  66. def register(linter: PyLinter) -> None:
  67. linter.register_checker(UnsupportedVersionChecker(linter))