11from __future__ import annotations
22
33import re
4- from typing import List , Pattern
4+ from typing import List , Pattern , Union
55
66from cucumber_expressions .errors import CucumberExpressionError
77
8- ILLEGAL_PARAMETER_NAME_PATTERN = r"([\[\]()$.|?*+])"
9- UNESCAPE_PATTERN = r"(\\([\[$.|?*+\]]))"
8+ ILLEGAL_PARAMETER_NAME_PATTERN = re .compile (r"([\[\]()$.|?*+])" )
109
1110
1211class ParameterType :
@@ -21,7 +20,7 @@ def _check_parameter_type_name(self, type_name):
2120
2221 @staticmethod
2322 def _is_valid_parameter_type_name (type_name ):
24- return not bool (re . compile ( ILLEGAL_PARAMETER_NAME_PATTERN ) .match (type_name ))
23+ return not bool (ILLEGAL_PARAMETER_NAME_PATTERN .match (type_name ))
2524
2625 def transform (self , group_values : list [str ]):
2726 """Transform values according to the lambda expression provided"""
@@ -56,7 +55,7 @@ def __init__(
5655 :param name: name of the parameter type
5756 :type name: Optional[str]
5857 :param regexp: regexp or list of regexps for capture groups
59- :type regexp: list[str] or str
58+ :type regexp: list[str], str, list[Pattern] or Pattern
6059 :param type: the return type of the transformed
6160 :type type: class
6261 :param transformer: transforms a str to (possibly) another type
@@ -73,7 +72,7 @@ def __init__(
7372 self .transformer = transformer
7473 self ._use_for_snippets = use_for_snippets
7574 self ._prefer_for_regexp_match = prefer_for_regexp_match
76- self .regexps = self .string_array (regexp )
75+ self .regexps = self .to_array (regexp )
7776
7877 @property
7978 def prefer_for_regexp_match (self ):
@@ -87,14 +86,20 @@ def use_for_snippets(self):
8786 def _get_regexp_source (regexp_pattern : Pattern ) -> str :
8887 invalid_flags = [re .I , re .M ]
8988 for invalid_flag in invalid_flags :
90- if invalid_flag in regexp_pattern .flags :
89+ _regexp_flags = regexp_pattern .flags
90+ _regexp_flags = (
91+ _regexp_flags if isinstance (_regexp_flags , list ) else [_regexp_flags ]
92+ )
93+ if invalid_flag .real in _regexp_flags :
9194 raise CucumberExpressionError (
92- f"ParameterType Regexps can't use flag: { invalid_flag } "
95+ f"ParameterType Regexps can't use flag: { str ( invalid_flag ) } "
9396 )
9497 return regexp_pattern .pattern
9598
96- def string_array (self , regexps ):
97- """Make a list of string regexps if not already"""
99+ def to_array (
100+ self , regexps : Union [List [str ], str , List [Pattern ], Pattern ]
101+ ) -> List [str ]:
102+ """Make a list of regexps if not already"""
98103 array : List = regexps if isinstance (regexps , list ) else [regexps ]
99104 return [
100105 regexp if isinstance (regexp , str ) else self ._get_regexp_source (regexp )
0 commit comments