Skip to content

Commit 8be7a33

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 8be7a33

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

tests/config/test_config.py

+40
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from __future__ import annotations
66

77
import os
8+
import re
9+
import timeit
810
from pathlib import Path
911

1012
import pytest
@@ -111,6 +113,44 @@ 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+
124+
@pytest.mark.parametrize("in_string,expected", CSV_REGEX_COMMA_CASES)
125+
def test_csv_regex_comma_in_quantifier(in_string, expected) -> None:
126+
"""Check that we correctly parse a comma-separated regex when there are one
127+
or more commas within quantifier expressions.
128+
"""
129+
130+
def _template_run(in_string):
131+
r = Run(
132+
[str(EMPTY_MODULE), rf"--bad-names-rgx={in_string}"],
133+
exit=False,
134+
)
135+
return r.linter.config.bad_names_rgxs
136+
137+
assert _template_run(in_string) == [re.compile(regex) for regex in expected]
138+
139+
# Catch trivially nonlinear performance
140+
small_input_time = timeit.timeit(
141+
"_template_run(in_string*100)",
142+
globals=locals(),
143+
number=10,
144+
)
145+
large_input_time = timeit.timeit(
146+
"_template_run(in_string*1000)",
147+
globals=locals(),
148+
number=10,
149+
)
150+
fudge_factor = 3
151+
assert large_input_time < small_input_time * 10 * fudge_factor
152+
153+
114154
def test_short_verbose(capsys: CaptureFixture) -> None:
115155
"""Check that we correctly handle the -v flag."""
116156
Run([str(EMPTY_MODULE), "-v"], exit=False)

0 commit comments

Comments
 (0)