brain_regex.py 3.4 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/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.inference_tip import inference_tip
  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. CLASS_GETITEM_TEMPLATE = """
  40. @classmethod
  41. def __class_getitem__(cls, item):
  42. return cls
  43. """
  44. def _looks_like_pattern_or_match(node: nodes.Call) -> bool:
  45. """Check for regex.Pattern or regex.Match call in stdlib.
  46. Match these patterns from stdlib/re.py
  47. ```py
  48. Pattern = type(...)
  49. Match = type(...)
  50. ```
  51. """
  52. return (
  53. node.root().name == "regex.regex"
  54. and isinstance(node.func, nodes.Name)
  55. and node.func.name == "type"
  56. and isinstance(node.parent, nodes.Assign)
  57. and len(node.parent.targets) == 1
  58. and isinstance(node.parent.targets[0], nodes.AssignName)
  59. and node.parent.targets[0].name in {"Pattern", "Match"}
  60. )
  61. def infer_pattern_match(node: nodes.Call, ctx: context.InferenceContext | None = None):
  62. """Infer regex.Pattern and regex.Match as classes.
  63. For PY39+ add `__class_getitem__`.
  64. """
  65. class_def = nodes.ClassDef(
  66. name=node.parent.targets[0].name,
  67. lineno=node.lineno,
  68. col_offset=node.col_offset,
  69. parent=node.parent,
  70. end_lineno=node.end_lineno,
  71. end_col_offset=node.end_col_offset,
  72. )
  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. def register(manager: AstroidManager) -> None:
  77. register_module_extender(manager, "regex", _regex_transform)
  78. manager.register_transform(
  79. nodes.Call, inference_tip(infer_pattern_match), _looks_like_pattern_or_match
  80. )