brain_regex.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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
  9. from astroid.manager import AstroidManager
  10. def _regex_transform() -> nodes.Module:
  11. """The RegexFlag enum exposes all its entries by updating globals().
  12. We hard-code the flags for now.
  13. # pylint: disable-next=line-too-long
  14. See https://github.com/mrabarnett/mrab-regex/blob/2022.10.31/regex_3/regex.py#L200
  15. """
  16. return parse(
  17. """
  18. A = ASCII = 0x80 # Assume ASCII locale.
  19. B = BESTMATCH = 0x1000 # Best fuzzy match.
  20. D = DEBUG = 0x200 # Print parsed pattern.
  21. E = ENHANCEMATCH = 0x8000 # Attempt to improve the fit after finding the first
  22. # fuzzy match.
  23. F = FULLCASE = 0x4000 # Unicode full case-folding.
  24. I = IGNORECASE = 0x2 # Ignore case.
  25. L = LOCALE = 0x4 # Assume current 8-bit locale.
  26. M = MULTILINE = 0x8 # Make anchors look for newline.
  27. P = POSIX = 0x10000 # POSIX-style matching (leftmost longest).
  28. R = REVERSE = 0x400 # Search backwards.
  29. S = DOTALL = 0x10 # Make dot match newline.
  30. U = UNICODE = 0x20 # Assume Unicode locale.
  31. V0 = VERSION0 = 0x2000 # Old legacy behaviour.
  32. DEFAULT_VERSION = V0
  33. V1 = VERSION1 = 0x100 # New enhanced behaviour.
  34. W = WORD = 0x800 # Default Unicode word breaks.
  35. X = VERBOSE = 0x40 # Ignore whitespace and comments.
  36. T = TEMPLATE = 0x1 # Template (present because re module has it).
  37. """
  38. )
  39. register_module_extender(AstroidManager(), "regex", _regex_transform)
  40. CLASS_GETITEM_TEMPLATE = """
  41. @classmethod
  42. def __class_getitem__(cls, item):
  43. return cls
  44. """
  45. def _looks_like_pattern_or_match(node: nodes.Call) -> bool:
  46. """Check for regex.Pattern or regex.Match call in stdlib.
  47. Match these patterns from stdlib/re.py
  48. ```py
  49. Pattern = type(...)
  50. Match = type(...)
  51. ```
  52. """
  53. return (
  54. node.root().name == "regex.regex"
  55. and isinstance(node.func, nodes.Name)
  56. and node.func.name == "type"
  57. and isinstance(node.parent, nodes.Assign)
  58. and len(node.parent.targets) == 1
  59. and isinstance(node.parent.targets[0], nodes.AssignName)
  60. and node.parent.targets[0].name in {"Pattern", "Match"}
  61. )
  62. def infer_pattern_match(node: nodes.Call, ctx: context.InferenceContext | None = None):
  63. """Infer regex.Pattern and regex.Match as classes.
  64. For PY39+ add `__class_getitem__`.
  65. """
  66. class_def = nodes.ClassDef(
  67. name=node.parent.targets[0].name,
  68. lineno=node.lineno,
  69. col_offset=node.col_offset,
  70. parent=node.parent,
  71. )
  72. if PY39_PLUS:
  73. func_to_add = _extract_single_node(CLASS_GETITEM_TEMPLATE)
  74. class_def.locals["__class_getitem__"] = [func_to_add]
  75. return iter([class_def])
  76. AstroidManager().register_transform(
  77. nodes.Call, inference_tip(infer_pattern_match), _looks_like_pattern_or_match
  78. )