Skip to content

Commit

Permalink
Add protection for pylint-dev#7229
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lihu committed Jul 28, 2022
1 parent cfc393a commit c95335f
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions tests/config/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

from __future__ import annotations

import re
import os
import timeit
from pathlib import Path

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


CSV_REGEX_COMMA_CASES = [
("foo", ["foo"]),
("foo,bar", ["foo", "bar"]),
("foo, bar", ["foo", "bar"]),
("foo, bar{1,3}", ["foo", "bar{1,3}"]),
]

@pytest.mark.parametrize("input,expected", CSV_REGEX_COMMA_CASES)
def test_csv_regex_comma_in_quantifier(input, expected) -> None:
"""Check that we correctly parse a comma-separated regex when there are one
or more commas within quantifier expressions.
"""
def _template_run(input):
r = Run(
[str(EMPTY_MODULE), rf"--bad-names-rgx={input}"],
exit=False,
)
return r.linter.config.bad_names_rgxs
assert _template_run(input) == [re.compile(regex) for regex in expected]

# Catch trivially nonlinear performance
small_input_time = timeit.timeit(
"_template_run(input*100)",
globals=locals(),
number=10,
)
large_input_time = timeit.timeit(
"_template_run(input*1000)",
globals=locals(),
number=10,
)
fudge_factor = 3
assert large_input_time < small_input_time * 10 * fudge_factor


def test_short_verbose(capsys: CaptureFixture) -> None:
"""Check that we correctly handle the -v flag."""
Run([str(EMPTY_MODULE), "-v"], exit=False)
Expand Down

0 comments on commit c95335f

Please sign in to comment.