typing.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. from typing import Dict, List, NamedTuple, Set, Union
  2. import astroid.bases
  3. from astroid import nodes
  4. from pylint.checkers import BaseChecker
  5. from pylint.checkers.utils import (
  6. check_messages,
  7. is_node_in_type_annotation_context,
  8. safe_infer,
  9. )
  10. from pylint.interfaces import IAstroidChecker
  11. from pylint.lint import PyLinter
  12. from pylint.utils.utils import get_global_option
  13. class TypingAlias(NamedTuple):
  14. name: str
  15. name_collision: bool
  16. DEPRECATED_TYPING_ALIASES: Dict[str, TypingAlias] = {
  17. "typing.Tuple": TypingAlias("tuple", False),
  18. "typing.List": TypingAlias("list", False),
  19. "typing.Dict": TypingAlias("dict", False),
  20. "typing.Set": TypingAlias("set", False),
  21. "typing.FrozenSet": TypingAlias("frozenset", False),
  22. "typing.Type": TypingAlias("type", False),
  23. "typing.Deque": TypingAlias("collections.deque", True),
  24. "typing.DefaultDict": TypingAlias("collections.defaultdict", True),
  25. "typing.OrderedDict": TypingAlias("collections.OrderedDict", True),
  26. "typing.Counter": TypingAlias("collections.Counter", True),
  27. "typing.ChainMap": TypingAlias("collections.ChainMap", True),
  28. "typing.Awaitable": TypingAlias("collections.abc.Awaitable", True),
  29. "typing.Coroutine": TypingAlias("collections.abc.Coroutine", True),
  30. "typing.AsyncIterable": TypingAlias("collections.abc.AsyncIterable", True),
  31. "typing.AsyncIterator": TypingAlias("collections.abc.AsyncIterator", True),
  32. "typing.AsyncGenerator": TypingAlias("collections.abc.AsyncGenerator", True),
  33. "typing.Iterable": TypingAlias("collections.abc.Iterable", True),
  34. "typing.Iterator": TypingAlias("collections.abc.Iterator", True),
  35. "typing.Generator": TypingAlias("collections.abc.Generator", True),
  36. "typing.Reversible": TypingAlias("collections.abc.Reversible", True),
  37. "typing.Container": TypingAlias("collections.abc.Container", True),
  38. "typing.Collection": TypingAlias("collections.abc.Collection", True),
  39. "typing.Callable": TypingAlias("collections.abc.Callable", True),
  40. "typing.AbstractSet": TypingAlias("collections.abc.Set", False),
  41. "typing.MutableSet": TypingAlias("collections.abc.MutableSet", True),
  42. "typing.Mapping": TypingAlias("collections.abc.Mapping", True),
  43. "typing.MutableMapping": TypingAlias("collections.abc.MutableMapping", True),
  44. "typing.Sequence": TypingAlias("collections.abc.Sequence", True),
  45. "typing.MutableSequence": TypingAlias("collections.abc.MutableSequence", True),
  46. "typing.ByteString": TypingAlias("collections.abc.ByteString", True),
  47. "typing.MappingView": TypingAlias("collections.abc.MappingView", True),
  48. "typing.KeysView": TypingAlias("collections.abc.KeysView", True),
  49. "typing.ItemsView": TypingAlias("collections.abc.ItemsView", True),
  50. "typing.ValuesView": TypingAlias("collections.abc.ValuesView", True),
  51. "typing.ContextManager": TypingAlias("contextlib.AbstractContextManager", False),
  52. "typing.AsyncContextManager": TypingAlias(
  53. "contextlib.AbstractAsyncContextManager", False
  54. ),
  55. "typing.Pattern": TypingAlias("re.Pattern", True),
  56. "typing.Match": TypingAlias("re.Match", True),
  57. "typing.Hashable": TypingAlias("collections.abc.Hashable", True),
  58. "typing.Sized": TypingAlias("collections.abc.Sized", True),
  59. }
  60. ALIAS_NAMES = frozenset(key.split(".")[1] for key in DEPRECATED_TYPING_ALIASES)
  61. UNION_NAMES = ("Optional", "Union")
  62. class DeprecatedTypingAliasMsg(NamedTuple):
  63. node: Union[nodes.Name, nodes.Attribute]
  64. qname: str
  65. alias: str
  66. parent_subscript: bool
  67. class TypingChecker(BaseChecker):
  68. """Find issue specifically related to type annotations."""
  69. __implements__ = (IAstroidChecker,)
  70. name = "typing"
  71. priority = -1
  72. msgs = {
  73. "W6001": (
  74. "'%s' is deprecated, use '%s' instead",
  75. "deprecated-typing-alias",
  76. "Emitted when a deprecated typing alias is used.",
  77. ),
  78. "R6002": (
  79. "'%s' will be deprecated with PY39, consider using '%s' instead%s",
  80. "consider-using-alias",
  81. "Only emitted if 'runtime-typing=no' and a deprecated "
  82. "typing alias is used in a type annotation context in "
  83. "Python 3.7 or 3.8.",
  84. ),
  85. "R6003": (
  86. "Consider using alternative Union syntax instead of '%s'%s",
  87. "consider-alternative-union-syntax",
  88. "Emitted when 'typing.Union' or 'typing.Optional' is used "
  89. "instead of the alternative Union syntax 'int | None'.",
  90. ),
  91. }
  92. options = (
  93. (
  94. "runtime-typing",
  95. {
  96. "default": True,
  97. "type": "yn",
  98. "metavar": "<y or n>",
  99. "help": (
  100. "Set to ``no`` if the app / library does **NOT** need to "
  101. "support runtime introspection of type annotations. "
  102. "If you use type annotations **exclusively** for type checking "
  103. "of an application, you're probably fine. For libraries, "
  104. "evaluate if some users what to access the type hints "
  105. "at runtime first, e.g., through ``typing.get_type_hints``. "
  106. "Applies to Python versions 3.7 - 3.9"
  107. ),
  108. },
  109. ),
  110. )
  111. _should_check_typing_alias: bool
  112. """The use of type aliases (PEP 585) requires Python 3.9
  113. or Python 3.7+ with postponed evaluation.
  114. """
  115. _should_check_alternative_union_syntax: bool
  116. """The use of alternative union syntax (PEP 604) requires Python 3.10
  117. or Python 3.7+ with postponed evaluation.
  118. """
  119. def __init__(self, linter: PyLinter) -> None:
  120. """Initialize checker instance."""
  121. super().__init__(linter=linter)
  122. self._alias_name_collisions: Set[str] = set()
  123. self._consider_using_alias_msgs: List[DeprecatedTypingAliasMsg] = []
  124. def open(self) -> None:
  125. py_version = get_global_option(self, "py-version")
  126. self._py37_plus = py_version >= (3, 7)
  127. self._py39_plus = py_version >= (3, 9)
  128. self._py310_plus = py_version >= (3, 10)
  129. self._should_check_typing_alias = self._py39_plus or (
  130. self._py37_plus and self.config.runtime_typing is False
  131. )
  132. self._should_check_alternative_union_syntax = self._py310_plus or (
  133. self._py37_plus and self.config.runtime_typing is False
  134. )
  135. def _msg_postponed_eval_hint(self, node) -> str:
  136. """Message hint if postponed evaluation isn't enabled."""
  137. if self._py310_plus or "annotations" in node.root().future_imports:
  138. return ""
  139. return ". Add 'from __future__ import annotations' as well"
  140. @check_messages(
  141. "deprecated-typing-alias",
  142. "consider-using-alias",
  143. "consider-alternative-union-syntax",
  144. )
  145. def visit_name(self, node: nodes.Name) -> None:
  146. if self._should_check_typing_alias and node.name in ALIAS_NAMES:
  147. self._check_for_typing_alias(node)
  148. if self._should_check_alternative_union_syntax and node.name in UNION_NAMES:
  149. self._check_for_alternative_union_syntax(node, node.name)
  150. @check_messages(
  151. "deprecated-typing-alias",
  152. "consider-using-alias",
  153. "consider-alternative-union-syntax",
  154. )
  155. def visit_attribute(self, node: nodes.Attribute) -> None:
  156. if self._should_check_typing_alias and node.attrname in ALIAS_NAMES:
  157. self._check_for_typing_alias(node)
  158. if self._should_check_alternative_union_syntax and node.attrname in UNION_NAMES:
  159. self._check_for_alternative_union_syntax(node, node.attrname)
  160. def _check_for_alternative_union_syntax(
  161. self,
  162. node: Union[nodes.Name, nodes.Attribute],
  163. name: str,
  164. ) -> None:
  165. """Check if alternative union syntax could be used.
  166. Requires
  167. - Python 3.10
  168. - OR: Python 3.7+ with postponed evaluation in
  169. a type annotation context
  170. """
  171. inferred = safe_infer(node)
  172. if not (
  173. isinstance(inferred, nodes.FunctionDef)
  174. and inferred.qname() in {"typing.Optional", "typing.Union"}
  175. or isinstance(inferred, astroid.bases.Instance)
  176. and inferred.qname() == "typing._SpecialForm"
  177. ):
  178. return
  179. if not (self._py310_plus or is_node_in_type_annotation_context(node)):
  180. return
  181. self.add_message(
  182. "consider-alternative-union-syntax",
  183. node=node,
  184. args=(name, self._msg_postponed_eval_hint(node)),
  185. )
  186. def _check_for_typing_alias(
  187. self,
  188. node: Union[nodes.Name, nodes.Attribute],
  189. ) -> None:
  190. """Check if typing alias is deprecated or could be replaced.
  191. Requires
  192. - Python 3.9
  193. - OR: Python 3.7+ with postponed evaluation in
  194. a type annotation context
  195. For Python 3.7+: Only emitt message if change doesn't create
  196. any name collisions, only ever used in a type annotation
  197. context, and can safely be replaced.
  198. """
  199. inferred = safe_infer(node)
  200. if not isinstance(inferred, nodes.ClassDef):
  201. return
  202. alias = DEPRECATED_TYPING_ALIASES.get(inferred.qname(), None)
  203. if alias is None:
  204. return
  205. if self._py39_plus:
  206. self.add_message(
  207. "deprecated-typing-alias",
  208. node=node,
  209. args=(inferred.qname(), alias.name),
  210. )
  211. return
  212. # For PY37+, check for type annotation context first
  213. if not is_node_in_type_annotation_context(node) and isinstance(
  214. node.parent, nodes.Subscript
  215. ):
  216. if alias.name_collision is True:
  217. self._alias_name_collisions.add(inferred.qname())
  218. return
  219. self._consider_using_alias_msgs.append(
  220. DeprecatedTypingAliasMsg(
  221. node,
  222. inferred.qname(),
  223. alias.name,
  224. isinstance(node.parent, nodes.Subscript),
  225. )
  226. )
  227. @check_messages("consider-using-alias")
  228. def leave_module(self, node: nodes.Module) -> None:
  229. """After parsing of module is complete, add messages for
  230. 'consider-using-alias' check. Make sure results are safe
  231. to recommend / collision free.
  232. """
  233. if self._py37_plus and not self._py39_plus:
  234. msg_future_import = self._msg_postponed_eval_hint(node)
  235. for msg in self._consider_using_alias_msgs:
  236. if msg.qname in self._alias_name_collisions:
  237. continue
  238. self.add_message(
  239. "consider-using-alias",
  240. node=msg.node,
  241. args=(
  242. msg.qname,
  243. msg.alias,
  244. msg_future_import if msg.parent_subscript else "",
  245. ),
  246. )
  247. # Clear all module cache variables
  248. self._alias_name_collisions.clear()
  249. self._consider_using_alias_msgs.clear()
  250. def register(linter: PyLinter) -> None:
  251. linter.register_checker(TypingChecker(linter))