Skip to content

Commit

Permalink
Handle new SyntaxError tuple on 3.10
Browse files Browse the repository at this point in the history
Closes #1372
  • Loading branch information
sigmavirus24 committed Aug 13, 2021
1 parent d25cc10 commit 9b5ad32
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/flake8/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,14 +443,26 @@ def _extract_syntax_information(exception: Exception) -> Tuple[int, int]:
token = ()
row, column = (1, 0)

if column > 0 and token and isinstance(exception, SyntaxError):
if (
column > 0
and token
and isinstance(exception, SyntaxError)
and len(token) == 4 # Python 3.9 or earlier
):
# NOTE(sigmavirus24): SyntaxErrors report 1-indexed column
# numbers. We need to decrement the column number by 1 at
# least.
column_offset = 1
row_offset = 0
# See also: https://github.com/pycqa/flake8/issues/169
physical_line = token[-1]
# See also: https://github.com/pycqa/flake8/issues/169,
# https://github.com/PyCQA/flake8/issues/1372
# On Python 3.9 and earlier, token will be a 4-item tuple with the
# last item being the string. Starting with 3.10, they added to
# the tuple so now instead of it ending with the code that failed
# to parse, it ends with the end of the section of code that
# failed to parse. Luckily the absolute position in the tuple is
# stable across versions so we can use that here
physical_line = token[3]

# NOTE(sigmavirus24): Not all "tokens" have a string as the last
# argument. In this event, let's skip trying to find the correct
Expand Down
20 changes: 20 additions & 0 deletions tests/integration/test_checker.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Integration tests for the checker submodule."""
import sys
from unittest import mock

import pytest
Expand Down Expand Up @@ -336,3 +337,22 @@ def test_acquire_when_multiprocessing_pool_can_not_initialize():

pool.assert_called_once_with(2, checker._pool_init)
assert result is None


def test_handling_syntaxerrors_across_pythons():
"""Do we properly handle exception argument tuples?
Python 3.10 added more information to the SyntaxError parse token tuple.
We need to handle that correctly to avoid crashing.
https://github.com/PyCQA/flake8/issues/1372
"""
if sys.version_info[:2] < (3, 10):
# Python 3.9 or older
err = SyntaxError("invalid syntax", ("<unknown>", 2, 5, "bad python:\n"))
expected = (2, 4)
else:
err = SyntaxError("invalid syntax", ("<unknown>", 2, 1, "bad python:\n", 2, 11))
expected = (2, 1)
file_checker = checker.FileChecker("-", {}, mock.MagicMock())
actual = file_checker._extract_syntax_information(err)
assert actual == expected

0 comments on commit 9b5ad32

Please sign in to comment.