brain_re.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
  2. # For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE
  3. # Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt
  4. from __future__ import annotations
  5. from astroid import context, nodes
  6. from astroid.brain.helpers import register_module_extender
  7. from astroid.builder import _extract_single_node, parse
  8. from astroid.const import PY311_PLUS
  9. from astroid.inference_tip import inference_tip
  10. from astroid.manager import AstroidManager
  11. def _re_transform() -> nodes.Module:
  12. # The RegexFlag enum exposes all its entries by updating globals()
  13. # In 3.6-3.10 all flags come from sre_compile
  14. # On 3.11+ all flags come from re._compiler
  15. if PY311_PLUS:
  16. import_compiler = "import re._compiler as _compiler"
  17. else:
  18. import_compiler = "import sre_compile as _compiler"
  19. return parse(
  20. f"""
  21. {import_compiler}
  22. NOFLAG = 0
  23. ASCII = _compiler.SRE_FLAG_ASCII
  24. IGNORECASE = _compiler.SRE_FLAG_IGNORECASE
  25. LOCALE = _compiler.SRE_FLAG_LOCALE
  26. UNICODE = _compiler.SRE_FLAG_UNICODE
  27. MULTILINE = _compiler.SRE_FLAG_MULTILINE
  28. DOTALL = _compiler.SRE_FLAG_DOTALL
  29. VERBOSE = _compiler.SRE_FLAG_VERBOSE
  30. TEMPLATE = _compiler.SRE_FLAG_TEMPLATE
  31. DEBUG = _compiler.SRE_FLAG_DEBUG
  32. A = ASCII
  33. I = IGNORECASE
  34. L = LOCALE
  35. U = UNICODE
  36. M = MULTILINE
  37. S = DOTALL
  38. X = VERBOSE
  39. T = TEMPLATE
  40. """
  41. )
  42. CLASS_GETITEM_TEMPLATE = """
  43. @classmethod
  44. def __class_getitem__(cls, item):
  45. return cls
  46. """
  47. def _looks_like_pattern_or_match(node: nodes.Call) -> bool:
  48. """Check for re.Pattern or re.Match call in stdlib.
  49. Match these patterns from stdlib/re.py
  50. ```py
  51. Pattern = type(...)
  52. Match = type(...)
  53. ```
  54. """
  55. return (
  56. node.root().name == "re"
  57. and isinstance(node.func, nodes.Name)
  58. and node.func.name == "type"
  59. and isinstance(node.parent, nodes.Assign)
  60. and len(node.parent.targets) == 1
  61. and isinstance(node.parent.targets[0], nodes.AssignName)
  62. and node.parent.targets[0].name in {"Pattern", "Match"}
  63. )
  64. def infer_pattern_match(node: nodes.Call, ctx: context.InferenceContext | None = None):
  65. """Infer re.Pattern and re.Match as classes.
  66. For PY39+ add `__class_getitem__`.
  67. """
  68. class_def = nodes.ClassDef(
  69. name=node.parent.targets[0].name,
  70. lineno=node.lineno,
  71. col_offset=node.col_offset,
  72. parent=node.parent,
  73. end_lineno=node.end_lineno,
  74. end_col_offset=node.end_col_offset,
  75. )
  76. func_to_add = _extract_single_node(CLASS_GETITEM_TEMPLATE)
  77. class_def.locals["__class_getitem__"] = [func_to_add]
  78. return iter([class_def])
  79. def register(manager: AstroidManager) -> None:
  80. register_module_extender(manager, "re", _re_transform)
  81. manager.register_transform(
  82. nodes.Call, inference_tip(infer_pattern_match), _looks_like_pattern_or_match
  83. )