brain_re.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
  2. # For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
  3. # Copyright (c) https://github.com/PyCQA/astroid/blob/main/CONTRIBUTORS.txt
  4. from __future__ import annotations
  5. from astroid import context, inference_tip, nodes
  6. from astroid.brain.helpers import register_module_extender
  7. from astroid.builder import _extract_single_node, parse
  8. from astroid.const import PY39_PLUS, PY311_PLUS
  9. from astroid.manager import AstroidManager
  10. def _re_transform() -> nodes.Module:
  11. # The RegexFlag enum exposes all its entries by updating globals()
  12. # In 3.6-3.10 all flags come from sre_compile
  13. # On 3.11+ all flags come from re._compiler
  14. if PY311_PLUS:
  15. import_compiler = "import re._compiler as _compiler"
  16. else:
  17. import_compiler = "import sre_compile as _compiler"
  18. return parse(
  19. f"""
  20. {import_compiler}
  21. NOFLAG = 0
  22. ASCII = _compiler.SRE_FLAG_ASCII
  23. IGNORECASE = _compiler.SRE_FLAG_IGNORECASE
  24. LOCALE = _compiler.SRE_FLAG_LOCALE
  25. UNICODE = _compiler.SRE_FLAG_UNICODE
  26. MULTILINE = _compiler.SRE_FLAG_MULTILINE
  27. DOTALL = _compiler.SRE_FLAG_DOTALL
  28. VERBOSE = _compiler.SRE_FLAG_VERBOSE
  29. TEMPLATE = _compiler.SRE_FLAG_TEMPLATE
  30. DEBUG = _compiler.SRE_FLAG_DEBUG
  31. A = ASCII
  32. I = IGNORECASE
  33. L = LOCALE
  34. U = UNICODE
  35. M = MULTILINE
  36. S = DOTALL
  37. X = VERBOSE
  38. T = TEMPLATE
  39. """
  40. )
  41. register_module_extender(AstroidManager(), "re", _re_transform)
  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. )
  74. if PY39_PLUS:
  75. func_to_add = _extract_single_node(CLASS_GETITEM_TEMPLATE)
  76. class_def.locals["__class_getitem__"] = [func_to_add]
  77. return iter([class_def])
  78. AstroidManager().register_transform(
  79. nodes.Call, inference_tip(infer_pattern_match), _looks_like_pattern_or_match
  80. )