Skip to content

Optimize handling of long lines for checkers like 'missing-final-newline' #5925

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,4 @@ contributors:
- Daniel Brookman
- Téo Bouvard
- Konrad Weihmann
- Sergey B Kirpichev
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ Release date: TBA

Closes #5569

* Optimize parsing of long lines when ``missing-final-newline`` is enabled.

Closes #5724

* Fix false positives for ``used-before-assignment`` from using named
expressions in a ternary operator test and using that expression as
a call argument.
Expand Down
4 changes: 4 additions & 0 deletions doc/whatsnew/2.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ Other Changes
Closes #5771
Ref PyCQA/astroid#1382

* Optimize parsing of long lines when ``missing-final-newline`` is enabled.

Closes #5724

* Fix ``unnecessary_dict_index_lookup`` false positive when deleting a dictionary's entry.

Closes #4716
Expand Down
24 changes: 13 additions & 11 deletions pylint/utils/pragma_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@
# so that an option can be continued with the reasons
# why it is active or disabled.
OPTION_RGX = r"""
\s* # Any number of whitespace
\#? # One or zero hash
.* # Anything (as much as possible)
(\s* # Beginning of first matched group and any number of whitespaces
\# # Beginning of comment
.*? # Anything (as little as possible)
\bpylint: # pylint word and column
\s* # Any number of whitespaces
([^;#\n]+)) # Anything except semicolon or hash or newline (it is the second matched group)
# and end of the first matched group
[;#]{0,1}""" # From 0 to 1 repetition of semicolon or hash
(?:^\s*\#.* # Comment line or
|\s* # any number of whitespaces or
|\s*\#.* # a beginning of line comment
(?=\#.*?\bpylint:)) # with a following next "pylint:" pragma
(\# # Beginning of comment
.*? # Anything (as little as possible)
\bpylint: # pylint word and column
\s* # Any number of whitespaces
([^;#\n]+)) # Anything except semicolon or hash or
# newline (it is the second matched group)
# and end of the first matched group
[;#]{0,1} # From 0 to 1 repetition of semicolon or hash
"""
OPTION_PO = re.compile(OPTION_RGX, re.VERBOSE)


Expand Down
14 changes: 14 additions & 0 deletions tests/checkers/unittest_refactoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,17 @@ def test_process_tokens() -> None:
with pytest.raises(SystemExit) as cm:
Run([os.path.join(REGR_DATA, "very_long_line.py")], reporter=TextReporter())
assert cm.value.code == 0


@pytest.mark.skipif(not hasattr(signal, "setitimer"), reason="Assumes POSIX signals")
def test_issue_5724() -> None:
with timeout(25.0):
with pytest.raises(SystemExit) as cm:
Run(
[
os.path.join(REGR_DATA, "issue_5724.py"),
"--enable=missing-final-newline",
],
reporter=TextReporter(),
)
assert cm.value.code == 0
1 change: 1 addition & 0 deletions tests/regrtest_data/issue_5724.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a = "a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #a #"