Skip to content

Commit

Permalink
Do not split regex lists in quantifier ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
lihu committed Jul 28, 2022
1 parent c95335f commit bfe1d68
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pylint/config/argument.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def _py_version_transformer(value: str) -> tuple[int, ...]:
def _regexp_csv_transfomer(value: str) -> Sequence[Pattern[str]]:
"""Transforms a comma separated list of regular expressions."""
patterns: list[Pattern[str]] = []
for pattern in _csv_transformer(value):
for pattern in pylint_utils._check_regexp_csv(value):
patterns.append(re.compile(pattern))
return patterns

Expand Down
2 changes: 2 additions & 0 deletions pylint/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
HAS_ISORT_5,
IsortDriver,
_check_csv,
_check_regexp_csv,
_format_option_value,
_splitstrip,
_unquote,
Expand All @@ -34,6 +35,7 @@
"HAS_ISORT_5",
"IsortDriver",
"_check_csv",
"_check_regexp_csv",
"_format_option_value",
"_splitstrip",
"_unquote",
Expand Down
27 changes: 27 additions & 0 deletions pylint/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,20 @@
import tokenize
import warnings
from collections.abc import Sequence
from collections import deque
from io import BufferedReader, BytesIO
from typing import (
TYPE_CHECKING,
Any,
Deque,
Iterable,
List,
Pattern,
TextIO,
Tuple,
TypeVar,
Union,
Optional,
overload,
)

Expand Down Expand Up @@ -327,6 +331,29 @@ def _check_csv(value: list[str] | tuple[str] | str) -> Sequence[str]:
return value
return _splitstrip(value)

def _check_regexp_csv(value: list[str] | tuple[str] | str) -> Iterable[str]:
if isinstance(value, (list, tuple)):
return value

# None is a sentinel value here
regexps: Deque[Optional[Deque[str]]] = deque([None])
open_braces = False
for char in value:
if char == "{":
open_braces = True
elif char == "}" and open_braces:
open_braces = False

if char == "," and not open_braces:
regexps.append(None)
else:
if regexps[-1] is None:
regexps.pop()
regexps.append(deque([char]))
else:
regexps[-1].append(char)
yield from ("".join(regexp).strip() for regexp in regexps if regexp is not None)


def _comment(string: str) -> str:
"""Return string as a comment."""
Expand Down

0 comments on commit bfe1d68

Please sign in to comment.