Skip to content

Commit c95335f

Browse files
author
lihu
committed
Add protection for pylint-dev#7229
Do not split on commas if they are between braces, since that indicates a quantifier. Also added a protection for slow implementations since existing workarounds may result in long strings of chained regular expressions.
1 parent cfc393a commit c95335f

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

tests/config/test_config.py

+37
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
from __future__ import annotations
66

7+
import re
78
import os
9+
import timeit
810
from pathlib import Path
911

1012
import pytest
@@ -111,6 +113,41 @@ def test_unknown_py_version(capsys: CaptureFixture) -> None:
111113
assert "the-newest has an invalid format, should be a version string." in output.err
112114

113115

116+
CSV_REGEX_COMMA_CASES = [
117+
("foo", ["foo"]),
118+
("foo,bar", ["foo", "bar"]),
119+
("foo, bar", ["foo", "bar"]),
120+
("foo, bar{1,3}", ["foo", "bar{1,3}"]),
121+
]
122+
123+
@pytest.mark.parametrize("input,expected", CSV_REGEX_COMMA_CASES)
124+
def test_csv_regex_comma_in_quantifier(input, expected) -> None:
125+
"""Check that we correctly parse a comma-separated regex when there are one
126+
or more commas within quantifier expressions.
127+
"""
128+
def _template_run(input):
129+
r = Run(
130+
[str(EMPTY_MODULE), rf"--bad-names-rgx={input}"],
131+
exit=False,
132+
)
133+
return r.linter.config.bad_names_rgxs
134+
assert _template_run(input) == [re.compile(regex) for regex in expected]
135+
136+
# Catch trivially nonlinear performance
137+
small_input_time = timeit.timeit(
138+
"_template_run(input*100)",
139+
globals=locals(),
140+
number=10,
141+
)
142+
large_input_time = timeit.timeit(
143+
"_template_run(input*1000)",
144+
globals=locals(),
145+
number=10,
146+
)
147+
fudge_factor = 3
148+
assert large_input_time < small_input_time * 10 * fudge_factor
149+
150+
114151
def test_short_verbose(capsys: CaptureFixture) -> None:
115152
"""Check that we correctly handle the -v flag."""
116153
Run([str(EMPTY_MODULE), "-v"], exit=False)

0 commit comments

Comments
 (0)