checker.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  2. # For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE
  3. # Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt
  4. """Basic checker for Python code."""
  5. from __future__ import annotations
  6. import argparse
  7. import collections
  8. import itertools
  9. import re
  10. import sys
  11. from collections.abc import Iterable
  12. from enum import Enum, auto
  13. from re import Pattern
  14. from typing import TYPE_CHECKING, cast
  15. import astroid
  16. from astroid import bases, nodes, util
  17. from astroid.typing import InferenceResult
  18. from pylint import constants, interfaces
  19. from pylint.checkers import utils
  20. from pylint.checkers.base.basic_checker import _BasicChecker
  21. from pylint.checkers.base.name_checker.naming_style import (
  22. KNOWN_NAME_TYPES,
  23. KNOWN_NAME_TYPES_WITH_STYLE,
  24. NAMING_STYLES,
  25. _create_naming_options,
  26. )
  27. from pylint.checkers.utils import is_property_deleter, is_property_setter
  28. from pylint.typing import Options
  29. if TYPE_CHECKING:
  30. from pylint.lint.pylinter import PyLinter
  31. _BadNamesTuple = tuple[nodes.NodeNG, str, str, interfaces.Confidence]
  32. # Default patterns for name types that do not have styles
  33. DEFAULT_PATTERNS = {
  34. "typevar": re.compile(
  35. r"^_{0,2}(?!T[A-Z])(?:[A-Z]+|(?:[A-Z]+[a-z]+)+(?:T)?(?<!Type))(?:_co(?:ntra)?)?$"
  36. ),
  37. "paramspec": re.compile(r"^_{0,2}(?:[A-Z]+|(?:[A-Z]+[a-z]+)+(?:P)?(?<!Type))$"),
  38. "typevartuple": re.compile(r"^_{0,2}(?:[A-Z]+|(?:[A-Z]+[a-z]+)+(?:Ts)?(?<!Type))$"),
  39. "typealias": re.compile(
  40. r"^_{0,2}(?!T[A-Z]|Type)[A-Z]+[a-z0-9]+(?:[A-Z][a-z0-9]+)*$"
  41. ),
  42. }
  43. BUILTIN_PROPERTY = "builtins.property"
  44. TYPE_VAR_QNAMES = {
  45. "typevar": {
  46. "typing.TypeVar",
  47. "typing_extensions.TypeVar",
  48. },
  49. "paramspec": {
  50. "typing.ParamSpec",
  51. "typing_extensions.ParamSpec",
  52. },
  53. "typevartuple": {
  54. "typing.TypeVarTuple",
  55. "typing_extensions.TypeVarTuple",
  56. },
  57. }
  58. class TypeVarVariance(Enum):
  59. invariant = auto()
  60. covariant = auto()
  61. contravariant = auto()
  62. double_variant = auto()
  63. inferred = auto()
  64. def _get_properties(config: argparse.Namespace) -> tuple[set[str], set[str]]:
  65. """Returns a tuple of property classes and names.
  66. Property classes are fully qualified, such as 'abc.abstractproperty' and
  67. property names are the actual names, such as 'abstract_property'.
  68. """
  69. property_classes = {BUILTIN_PROPERTY}
  70. property_names: set[str] = set() # Not returning 'property', it has its own check.
  71. if config is not None:
  72. property_classes.update(config.property_classes)
  73. property_names.update(
  74. prop.rsplit(".", 1)[-1] for prop in config.property_classes
  75. )
  76. return property_classes, property_names
  77. def _redefines_import(node: nodes.AssignName) -> bool:
  78. """Detect that the given node (AssignName) is inside an
  79. exception handler and redefines an import from the tryexcept body.
  80. Returns True if the node redefines an import, False otherwise.
  81. """
  82. current = node
  83. while current and not isinstance(current.parent, nodes.ExceptHandler):
  84. current = current.parent
  85. if not (current and utils.error_of_type(current.parent, ImportError)):
  86. return False
  87. try_block = current.parent.parent
  88. for import_node in try_block.nodes_of_class((nodes.ImportFrom, nodes.Import)):
  89. for name, alias in import_node.names:
  90. if alias:
  91. if alias == node.name:
  92. return True
  93. elif name == node.name:
  94. return True
  95. return False
  96. def _determine_function_name_type(
  97. node: nodes.FunctionDef, config: argparse.Namespace
  98. ) -> str:
  99. """Determine the name type whose regex the function's name should match.
  100. :param node: A function node.
  101. :param config: Configuration from which to pull additional property classes.
  102. :returns: One of ('function', 'method', 'attr')
  103. """
  104. property_classes, property_names = _get_properties(config)
  105. if not node.is_method():
  106. return "function"
  107. if is_property_setter(node) or is_property_deleter(node):
  108. # If the function is decorated using the prop_method.{setter,getter}
  109. # form, treat it like an attribute as well.
  110. return "attr"
  111. decorators = node.decorators.nodes if node.decorators else []
  112. for decorator in decorators:
  113. # If the function is a property (decorated with @property
  114. # or @abc.abstractproperty), the name type is 'attr'.
  115. if isinstance(decorator, nodes.Name) or (
  116. isinstance(decorator, nodes.Attribute)
  117. and decorator.attrname in property_names
  118. ):
  119. inferred = utils.safe_infer(decorator)
  120. if (
  121. inferred
  122. and hasattr(inferred, "qname")
  123. and inferred.qname() in property_classes
  124. ):
  125. return "attr"
  126. return "method"
  127. # Name categories that are always consistent with all naming conventions.
  128. EXEMPT_NAME_CATEGORIES = {"exempt", "ignore"}
  129. def _is_multi_naming_match(
  130. match: re.Match[str] | None, node_type: str, confidence: interfaces.Confidence
  131. ) -> bool:
  132. return (
  133. match is not None
  134. and match.lastgroup is not None
  135. and match.lastgroup not in EXEMPT_NAME_CATEGORIES
  136. and not (node_type == "method" and confidence == interfaces.INFERENCE_FAILURE)
  137. )
  138. class NameChecker(_BasicChecker):
  139. msgs = {
  140. "C0103": (
  141. '%s name "%s" doesn\'t conform to %s',
  142. "invalid-name",
  143. "Used when the name doesn't conform to naming rules "
  144. "associated to its type (constant, variable, class...).",
  145. ),
  146. "C0104": (
  147. 'Disallowed name "%s"',
  148. "disallowed-name",
  149. "Used when the name matches bad-names or bad-names-rgxs- (unauthorized names).",
  150. {
  151. "old_names": [
  152. ("C0102", "blacklisted-name"),
  153. ]
  154. },
  155. ),
  156. "C0105": (
  157. "Type variable name does not reflect variance%s",
  158. "typevar-name-incorrect-variance",
  159. "Emitted when a TypeVar name doesn't reflect its type variance. "
  160. "According to PEP8, it is recommended to add suffixes '_co' and "
  161. "'_contra' to the variables used to declare covariant or "
  162. "contravariant behaviour respectively. Invariant (default) variables "
  163. "do not require a suffix. The message is also emitted when invariant "
  164. "variables do have a suffix.",
  165. ),
  166. "C0131": (
  167. "TypeVar cannot be both covariant and contravariant",
  168. "typevar-double-variance",
  169. 'Emitted when both the "covariant" and "contravariant" '
  170. 'keyword arguments are set to "True" in a TypeVar.',
  171. ),
  172. "C0132": (
  173. 'TypeVar name "%s" does not match assigned variable name "%s"',
  174. "typevar-name-mismatch",
  175. "Emitted when a TypeVar is assigned to a variable "
  176. "that does not match its name argument.",
  177. ),
  178. }
  179. _options: Options = (
  180. (
  181. "good-names",
  182. {
  183. "default": ("i", "j", "k", "ex", "Run", "_"),
  184. "type": "csv",
  185. "metavar": "<names>",
  186. "help": "Good variable names which should always be accepted,"
  187. " separated by a comma.",
  188. },
  189. ),
  190. (
  191. "good-names-rgxs",
  192. {
  193. "default": "",
  194. "type": "regexp_csv",
  195. "metavar": "<names>",
  196. "help": "Good variable names regexes, separated by a comma. If names match any regex,"
  197. " they will always be accepted",
  198. },
  199. ),
  200. (
  201. "bad-names",
  202. {
  203. "default": ("foo", "bar", "baz", "toto", "tutu", "tata"),
  204. "type": "csv",
  205. "metavar": "<names>",
  206. "help": "Bad variable names which should always be refused, "
  207. "separated by a comma.",
  208. },
  209. ),
  210. (
  211. "bad-names-rgxs",
  212. {
  213. "default": "",
  214. "type": "regexp_csv",
  215. "metavar": "<names>",
  216. "help": "Bad variable names regexes, separated by a comma. If names match any regex,"
  217. " they will always be refused",
  218. },
  219. ),
  220. (
  221. "name-group",
  222. {
  223. "default": (),
  224. "type": "csv",
  225. "metavar": "<name1:name2>",
  226. "help": (
  227. "Colon-delimited sets of names that determine each"
  228. " other's naming style when the name regexes"
  229. " allow several styles."
  230. ),
  231. },
  232. ),
  233. (
  234. "include-naming-hint",
  235. {
  236. "default": False,
  237. "type": "yn",
  238. "metavar": "<y or n>",
  239. "help": "Include a hint for the correct naming format with invalid-name.",
  240. },
  241. ),
  242. (
  243. "property-classes",
  244. {
  245. "default": ("abc.abstractproperty",),
  246. "type": "csv",
  247. "metavar": "<decorator names>",
  248. "help": "List of decorators that produce properties, such as "
  249. "abc.abstractproperty. Add to this list to register "
  250. "other decorators that produce valid properties. "
  251. "These decorators are taken in consideration only for invalid-name.",
  252. },
  253. ),
  254. )
  255. options: Options = _options + _create_naming_options()
  256. def __init__(self, linter: PyLinter) -> None:
  257. super().__init__(linter)
  258. self._name_group: dict[str, str] = {}
  259. self._bad_names: dict[str, dict[str, list[_BadNamesTuple]]] = {}
  260. self._name_regexps: dict[str, re.Pattern[str]] = {}
  261. self._name_hints: dict[str, str] = {}
  262. self._good_names_rgxs_compiled: list[re.Pattern[str]] = []
  263. self._bad_names_rgxs_compiled: list[re.Pattern[str]] = []
  264. def open(self) -> None:
  265. self.linter.stats.reset_bad_names()
  266. for group in self.linter.config.name_group:
  267. for name_type in group.split(":"):
  268. self._name_group[name_type] = f"group_{group}"
  269. regexps, hints = self._create_naming_rules()
  270. self._name_regexps = regexps
  271. self._name_hints = hints
  272. self._good_names_rgxs_compiled = [
  273. re.compile(rgxp) for rgxp in self.linter.config.good_names_rgxs
  274. ]
  275. self._bad_names_rgxs_compiled = [
  276. re.compile(rgxp) for rgxp in self.linter.config.bad_names_rgxs
  277. ]
  278. def _create_naming_rules(self) -> tuple[dict[str, Pattern[str]], dict[str, str]]:
  279. regexps: dict[str, Pattern[str]] = {}
  280. hints: dict[str, str] = {}
  281. for name_type in KNOWN_NAME_TYPES:
  282. if name_type in KNOWN_NAME_TYPES_WITH_STYLE:
  283. naming_style_name = getattr(
  284. self.linter.config, f"{name_type}_naming_style"
  285. )
  286. regexps[name_type] = NAMING_STYLES[naming_style_name].get_regex(
  287. name_type
  288. )
  289. else:
  290. naming_style_name = "predefined"
  291. regexps[name_type] = DEFAULT_PATTERNS[name_type]
  292. custom_regex_setting_name = f"{name_type}_rgx"
  293. custom_regex = getattr(self.linter.config, custom_regex_setting_name, None)
  294. if custom_regex is not None:
  295. regexps[name_type] = custom_regex
  296. if custom_regex is not None:
  297. hints[name_type] = f"{custom_regex.pattern!r} pattern"
  298. else:
  299. hints[name_type] = f"{naming_style_name} naming style"
  300. return regexps, hints
  301. @utils.only_required_for_messages("disallowed-name", "invalid-name")
  302. def visit_module(self, node: nodes.Module) -> None:
  303. self._check_name("module", node.name.split(".")[-1], node)
  304. self._bad_names = {}
  305. def leave_module(self, _: nodes.Module) -> None:
  306. for all_groups in self._bad_names.values():
  307. if len(all_groups) < 2:
  308. continue
  309. groups: collections.defaultdict[int, list[list[_BadNamesTuple]]] = (
  310. collections.defaultdict(list)
  311. )
  312. min_warnings = sys.maxsize
  313. prevalent_group, _ = max(all_groups.items(), key=lambda item: len(item[1]))
  314. for group in all_groups.values():
  315. groups[len(group)].append(group)
  316. min_warnings = min(len(group), min_warnings)
  317. if len(groups[min_warnings]) > 1:
  318. by_line = sorted(
  319. groups[min_warnings],
  320. key=lambda group: min(
  321. warning[0].lineno
  322. for warning in group
  323. if warning[0].lineno is not None
  324. ),
  325. )
  326. warnings: Iterable[_BadNamesTuple] = itertools.chain(*by_line[1:])
  327. else:
  328. warnings = groups[min_warnings][0]
  329. for args in warnings:
  330. self._raise_name_warning(prevalent_group, *args)
  331. @utils.only_required_for_messages("disallowed-name", "invalid-name")
  332. def visit_classdef(self, node: nodes.ClassDef) -> None:
  333. self._check_name("class", node.name, node)
  334. for attr, anodes in node.instance_attrs.items():
  335. if not any(
  336. node.instance_attr_ancestors(attr)
  337. ) and not utils.is_assign_name_annotated_with(anodes[0], "Final"):
  338. self._check_name("attr", attr, anodes[0])
  339. @utils.only_required_for_messages("disallowed-name", "invalid-name")
  340. def visit_functiondef(self, node: nodes.FunctionDef) -> None:
  341. # Do not emit any warnings if the method is just an implementation
  342. # of a base class method.
  343. confidence = interfaces.HIGH
  344. if node.is_method():
  345. if utils.overrides_a_method(node.parent.frame(), node.name):
  346. return
  347. confidence = (
  348. interfaces.INFERENCE
  349. if utils.has_known_bases(node.parent.frame())
  350. else interfaces.INFERENCE_FAILURE
  351. )
  352. self._check_name(
  353. _determine_function_name_type(node, config=self.linter.config),
  354. node.name,
  355. node,
  356. confidence,
  357. )
  358. # Check argument names
  359. args = node.args.args
  360. if args is not None:
  361. self._recursive_check_names(args)
  362. visit_asyncfunctiondef = visit_functiondef
  363. @utils.only_required_for_messages(
  364. "disallowed-name",
  365. "invalid-name",
  366. "typevar-name-incorrect-variance",
  367. "typevar-double-variance",
  368. "typevar-name-mismatch",
  369. )
  370. def visit_assignname( # pylint: disable=too-many-branches,too-many-statements
  371. self, node: nodes.AssignName
  372. ) -> None:
  373. """Check module level assigned names."""
  374. frame = node.frame()
  375. assign_type = node.assign_type()
  376. # Check names defined in comprehensions
  377. if isinstance(assign_type, nodes.Comprehension):
  378. self._check_name("inlinevar", node.name, node)
  379. elif isinstance(assign_type, nodes.TypeVar):
  380. self._check_name("typevar", node.name, node)
  381. elif isinstance(assign_type, nodes.ParamSpec):
  382. self._check_name("paramspec", node.name, node)
  383. elif isinstance(assign_type, nodes.TypeVarTuple):
  384. self._check_name("typevartuple", node.name, node)
  385. elif isinstance(assign_type, nodes.TypeAlias):
  386. self._check_name("typealias", node.name, node)
  387. # Check names defined in module scope
  388. elif isinstance(frame, nodes.Module):
  389. # Check names defined in AnnAssign nodes
  390. if isinstance(assign_type, nodes.AnnAssign) and self._assigns_typealias(
  391. assign_type.annotation
  392. ):
  393. self._check_name("typealias", node.name, node)
  394. # Check names defined in Assign nodes
  395. elif isinstance(assign_type, (nodes.Assign, nodes.AnnAssign)):
  396. inferred_assign_type = (
  397. utils.safe_infer(assign_type.value) if assign_type.value else None
  398. )
  399. # Check TypeVar's and TypeAliases assigned alone or in tuple assignment
  400. if isinstance(node.parent, nodes.Assign):
  401. if typevar_node_type := self._assigns_typevar(assign_type.value):
  402. self._check_name(
  403. typevar_node_type, assign_type.targets[0].name, node
  404. )
  405. return
  406. if self._assigns_typealias(assign_type.value):
  407. self._check_name("typealias", assign_type.targets[0].name, node)
  408. return
  409. if (
  410. isinstance(node.parent, nodes.Tuple)
  411. and isinstance(assign_type.value, nodes.Tuple)
  412. # protect against unbalanced tuple unpacking
  413. and node.parent.elts.index(node) < len(assign_type.value.elts)
  414. ):
  415. assigner = assign_type.value.elts[node.parent.elts.index(node)]
  416. if typevar_node_type := self._assigns_typevar(assigner):
  417. self._check_name(
  418. typevar_node_type,
  419. assign_type.targets[0]
  420. .elts[node.parent.elts.index(node)]
  421. .name,
  422. node,
  423. )
  424. return
  425. if self._assigns_typealias(assigner):
  426. self._check_name(
  427. "typealias",
  428. assign_type.targets[0]
  429. .elts[node.parent.elts.index(node)]
  430. .name,
  431. node,
  432. )
  433. return
  434. elif inferred_assign_type in (None, util.Uninferable):
  435. return
  436. # Check classes (TypeVar's are classes so they need to be excluded first)
  437. elif self._should_check_class_regex(inferred_assign_type):
  438. self._check_name("class", node.name, node)
  439. # Don't emit if the name redefines an import in an ImportError except handler
  440. # nor any other reassignment.
  441. elif (
  442. not (redefines_import := _redefines_import(node))
  443. and not isinstance(
  444. inferred_assign_type, (nodes.FunctionDef, nodes.Lambda)
  445. )
  446. and not utils.is_reassigned_before_current(node, node.name)
  447. and not utils.is_reassigned_after_current(node, node.name)
  448. and not utils.get_node_first_ancestor_of_type(
  449. node, (nodes.For, nodes.While)
  450. )
  451. ):
  452. if not self._meets_exception_for_non_consts(
  453. inferred_assign_type, node.name
  454. ):
  455. self._check_name("const", node.name, node)
  456. else:
  457. node_type = "variable"
  458. iattrs = tuple(node.frame().igetattr(node.name))
  459. if (
  460. util.Uninferable in iattrs
  461. and self._name_regexps["const"].match(node.name) is not None
  462. ):
  463. return
  464. # Do the exclusive assignment analysis on attrs, not iattrs.
  465. # iattrs locations could be anywhere (inference result).
  466. attrs = tuple(node.frame().getattr(node.name))
  467. if len(attrs) > 1 and all(
  468. astroid.are_exclusive(*combo)
  469. for combo in itertools.combinations(attrs, 2)
  470. ):
  471. node_type = "const"
  472. if not self._meets_exception_for_non_consts(
  473. inferred_assign_type, node.name
  474. ):
  475. self._check_name(
  476. node_type,
  477. node.name,
  478. node,
  479. disallowed_check_only=redefines_import,
  480. )
  481. # Check names defined in function scopes
  482. elif isinstance(frame, nodes.FunctionDef):
  483. # global introduced variable aren't in the function locals
  484. if node.name in frame and node.name not in frame.argnames():
  485. if not _redefines_import(node):
  486. if isinstance(
  487. assign_type, nodes.AnnAssign
  488. ) and self._assigns_typealias(assign_type.annotation):
  489. self._check_name("typealias", node.name, node)
  490. else:
  491. self._check_name("variable", node.name, node)
  492. # Check names defined in class scopes
  493. elif isinstance(frame, nodes.ClassDef) and not any(
  494. frame.local_attr_ancestors(node.name)
  495. ):
  496. if utils.is_assign_name_annotated_with_class_var_typing_name(node, "Final"):
  497. self._check_name("class_const", node.name, node)
  498. elif utils.is_assign_name_annotated_with(node, "Final"):
  499. if frame.is_dataclass:
  500. self._check_name("class_attribute", node.name, node)
  501. else:
  502. self._check_name("class_const", node.name, node)
  503. elif utils.is_enum_member(node):
  504. self._check_name("class_const", node.name, node)
  505. else:
  506. self._check_name("class_attribute", node.name, node)
  507. def _meets_exception_for_non_consts(
  508. self, inferred_assign_type: InferenceResult | None, name: str
  509. ) -> bool:
  510. if isinstance(inferred_assign_type, nodes.Const):
  511. return False
  512. regexp = self._name_regexps["variable"]
  513. return regexp.match(name) is not None
  514. def _should_check_class_regex(
  515. self, inferred_assign_type: InferenceResult | None
  516. ) -> bool:
  517. if isinstance(inferred_assign_type, nodes.ClassDef):
  518. return True
  519. if isinstance(inferred_assign_type, bases.Instance) and {
  520. "EnumMeta",
  521. "TypedDict",
  522. }.intersection(
  523. {
  524. ancestor.name
  525. for ancestor in cast(InferenceResult, inferred_assign_type).mro()
  526. }
  527. ):
  528. return True
  529. if (
  530. isinstance(inferred_assign_type, nodes.FunctionDef)
  531. and inferred_assign_type.qname() == "typing.Annotated"
  532. ):
  533. return True
  534. return False
  535. def _recursive_check_names(self, args: list[nodes.AssignName]) -> None:
  536. """Check names in a possibly recursive list <arg>."""
  537. for arg in args:
  538. self._check_name("argument", arg.name, arg)
  539. def _find_name_group(self, node_type: str) -> str:
  540. return self._name_group.get(node_type, node_type)
  541. def _raise_name_warning(
  542. self,
  543. prevalent_group: str | None,
  544. node: nodes.NodeNG,
  545. node_type: str,
  546. name: str,
  547. confidence: interfaces.Confidence,
  548. warning: str = "invalid-name",
  549. ) -> None:
  550. type_label = constants.HUMAN_READABLE_TYPES[node_type]
  551. hint = self._name_hints[node_type]
  552. if prevalent_group:
  553. # This happens in the multi naming match case. The expected
  554. # prevalent group needs to be spelled out to make the message
  555. # correct.
  556. hint = f"the `{prevalent_group}` group in the {hint}"
  557. if self.linter.config.include_naming_hint:
  558. hint += f" ({self._name_regexps[node_type].pattern!r} pattern)"
  559. args = (
  560. (type_label.capitalize(), name, hint)
  561. if warning == "invalid-name"
  562. else (type_label.capitalize(), name)
  563. )
  564. self.add_message(warning, node=node, args=args, confidence=confidence)
  565. self.linter.stats.increase_bad_name(node_type, 1)
  566. def _name_allowed_by_regex(self, name: str) -> bool:
  567. return name in self.linter.config.good_names or any(
  568. pattern.match(name) for pattern in self._good_names_rgxs_compiled
  569. )
  570. def _name_disallowed_by_regex(self, name: str) -> bool:
  571. return name in self.linter.config.bad_names or any(
  572. pattern.match(name) for pattern in self._bad_names_rgxs_compiled
  573. )
  574. def _check_name(
  575. self,
  576. node_type: str,
  577. name: str,
  578. node: nodes.NodeNG,
  579. confidence: interfaces.Confidence = interfaces.HIGH,
  580. disallowed_check_only: bool = False,
  581. ) -> None:
  582. """Check for a name using the type's regexp."""
  583. def _should_exempt_from_invalid_name(node: nodes.NodeNG) -> bool:
  584. if node_type == "variable":
  585. inferred = utils.safe_infer(node)
  586. if isinstance(inferred, nodes.ClassDef):
  587. return True
  588. return False
  589. if self._name_allowed_by_regex(name=name):
  590. return
  591. if self._name_disallowed_by_regex(name=name):
  592. self.linter.stats.increase_bad_name(node_type, 1)
  593. self.add_message(
  594. "disallowed-name", node=node, args=name, confidence=interfaces.HIGH
  595. )
  596. return
  597. regexp = self._name_regexps[node_type]
  598. match = regexp.match(name)
  599. if _is_multi_naming_match(match, node_type, confidence):
  600. name_group = self._find_name_group(node_type)
  601. bad_name_group = self._bad_names.setdefault(name_group, {})
  602. # Ignored because this is checked by the if statement
  603. warnings = bad_name_group.setdefault(match.lastgroup, []) # type: ignore[union-attr, arg-type]
  604. warnings.append((node, node_type, name, confidence))
  605. if (
  606. match is None
  607. and not disallowed_check_only
  608. and not _should_exempt_from_invalid_name(node)
  609. ):
  610. self._raise_name_warning(None, node, node_type, name, confidence)
  611. # Check TypeVar names for variance suffixes
  612. if node_type == "typevar":
  613. self._check_typevar(name, node)
  614. @staticmethod
  615. def _assigns_typevar(node: nodes.NodeNG | None) -> str | None:
  616. """Check if a node is assigning a TypeVar and return TypeVar type."""
  617. if isinstance(node, nodes.Call):
  618. inferred = utils.safe_infer(node.func)
  619. if isinstance(inferred, nodes.ClassDef):
  620. qname = inferred.qname()
  621. for typevar_node_typ, qnames in TYPE_VAR_QNAMES.items():
  622. if qname in qnames:
  623. return typevar_node_typ
  624. return None
  625. @staticmethod
  626. def _assigns_typealias(node: nodes.NodeNG | None) -> bool:
  627. """Check if a node is assigning a TypeAlias."""
  628. inferred = utils.safe_infer(node)
  629. if isinstance(inferred, (nodes.ClassDef, bases.UnionType)):
  630. qname = inferred.qname()
  631. if qname == "typing.TypeAlias":
  632. return True
  633. if qname in {".Union", "builtins.Union", "builtins.UnionType"}:
  634. # Union is a special case because it can be used as a type alias
  635. # or as a type annotation. We only want to check the former.
  636. assert node is not None
  637. return not isinstance(node.parent, nodes.AnnAssign)
  638. elif isinstance(inferred, nodes.FunctionDef):
  639. # TODO: when py3.12 is minimum, remove this condition
  640. # TypeAlias became a class in python 3.12
  641. if inferred.qname() == "typing.TypeAlias":
  642. return True
  643. return False
  644. def _check_typevar(self, name: str, node: nodes.AssignName) -> None:
  645. """Check for TypeVar lint violations."""
  646. variance: TypeVarVariance = TypeVarVariance.invariant
  647. match node.parent:
  648. case nodes.Assign():
  649. keywords = node.assign_type().value.keywords
  650. args = node.assign_type().value.args
  651. case nodes.Tuple():
  652. keywords = (
  653. node.assign_type().value.elts[node.parent.elts.index(node)].keywords
  654. )
  655. args = node.assign_type().value.elts[node.parent.elts.index(node)].args
  656. case _: # PEP 695 generic type nodes
  657. keywords = ()
  658. args = ()
  659. variance = TypeVarVariance.inferred
  660. name_arg = None
  661. for kw in keywords:
  662. if variance == TypeVarVariance.double_variant:
  663. pass
  664. elif kw.arg == "covariant" and kw.value.value:
  665. variance = (
  666. TypeVarVariance.covariant
  667. if variance != TypeVarVariance.contravariant
  668. else TypeVarVariance.double_variant
  669. )
  670. elif kw.arg == "contravariant" and kw.value.value:
  671. variance = (
  672. TypeVarVariance.contravariant
  673. if variance != TypeVarVariance.covariant
  674. else TypeVarVariance.double_variant
  675. )
  676. if kw.arg == "name" and isinstance(kw.value, nodes.Const):
  677. name_arg = kw.value.value
  678. if name_arg is None and args and isinstance(args[0], nodes.Const):
  679. name_arg = args[0].value
  680. match variance:
  681. case TypeVarVariance.inferred:
  682. # Ignore variance check for PEP 695 type parameters.
  683. # The variance is inferred by the type checker.
  684. # Adding _co or _contra suffix can help to reason about TypeVar.
  685. pass
  686. case TypeVarVariance.double_variant:
  687. self.add_message(
  688. "typevar-double-variance",
  689. node=node,
  690. confidence=interfaces.INFERENCE,
  691. )
  692. self.add_message(
  693. "typevar-name-incorrect-variance",
  694. node=node,
  695. args=("",),
  696. confidence=interfaces.INFERENCE,
  697. )
  698. case TypeVarVariance.covariant if not name.endswith("_co"):
  699. suggest_name = f"{re.sub('_contra$', '', name)}_co"
  700. self.add_message(
  701. "typevar-name-incorrect-variance",
  702. node=node,
  703. args=(f'. "{name}" is covariant, use "{suggest_name}" instead'),
  704. confidence=interfaces.INFERENCE,
  705. )
  706. case TypeVarVariance.contravariant if not name.endswith("_contra"):
  707. suggest_name = f"{re.sub('_co$', '', name)}_contra"
  708. self.add_message(
  709. "typevar-name-incorrect-variance",
  710. node=node,
  711. args=(f'. "{name}" is contravariant, use "{suggest_name}" instead'),
  712. confidence=interfaces.INFERENCE,
  713. )
  714. case TypeVarVariance.invariant if name.endswith(("_co", "_contra")):
  715. suggest_name = re.sub("_contra$|_co$", "", name)
  716. self.add_message(
  717. "typevar-name-incorrect-variance",
  718. node=node,
  719. args=(f'. "{name}" is invariant, use "{suggest_name}" instead'),
  720. confidence=interfaces.INFERENCE,
  721. )
  722. if name_arg is not None and name_arg != name:
  723. self.add_message(
  724. "typevar-name-mismatch",
  725. node=node,
  726. args=(name_arg, name),
  727. confidence=interfaces.INFERENCE,
  728. )