Skip to content

Commit dbaa36c

Browse files
[syntax-error] Fix a crash when the line and column can't be retrieved
Closes #3860
1 parent 65543fd commit dbaa36c

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

pylint/lint/pylinter.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -692,11 +692,9 @@ def _check_files(
692692
)
693693
msg = get_fatal_error_message(file.filepath, template_path)
694694
if isinstance(ex, AstroidError):
695-
symbol = "astroid-error"
696-
self.add_message(symbol, args=(file.filepath, msg))
695+
self.add_message("astroid-error", args=(file.filepath, msg))
697696
else:
698-
symbol = "fatal"
699-
self.add_message(symbol, args=msg)
697+
self.add_message("fatal", args=msg)
700698

701699
def _check_file(
702700
self,
@@ -918,7 +916,8 @@ def get_ast(
918916
"syntax-error",
919917
line=getattr(ex.error, "lineno", 0),
920918
col_offset=getattr(ex.error, "offset", None),
921-
args=str(ex.error),
919+
args=ex.error,
920+
confidence=HIGH,
922921
)
923922
except astroid.AstroidBuildingError as ex:
924923
self.add_message("parse-error", args=ex)
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
syntax-error:1:10:None:None::invalid syntax (<unknown>, line 1):UNDEFINED
1+
syntax-error:1:10:None:None::"Parsing Python code failed:
2+
invalid syntax (<unknown>, line 1)":HIGH

tests/test_self.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from unittest.mock import patch
2828

2929
import pytest
30+
from astroid import PY310_PLUS
3031
from py._path.local import LocalPath # type: ignore[import]
3132

3233
from pylint import extensions, modify_sys_path
@@ -569,10 +570,11 @@ def foobar(arg):
569570
expected_output=expected,
570571
)
571572

572-
def test_stdin_syntaxerror(self) -> None:
573+
def test_stdin_syntax_error(self) -> None:
573574
expected_output = (
574575
"************* Module a\n"
575-
"a.py:1:4: E0001: invalid syntax (<unknown>, line 1) (syntax-error)"
576+
"a.py:1:4: E0001: Parsing Python code failed:\n"
577+
"invalid syntax (<unknown>, line 1) (syntax-error)"
576578
)
577579

578580
with mock.patch(
@@ -584,6 +586,18 @@ def test_stdin_syntaxerror(self) -> None:
584586
)
585587
assert mock_stdin.call_count == 1
586588

589+
@pytest.mark.skipif(
590+
PY310_PLUS, reason="Not a syntax error for python 3.10 or superior"
591+
)
592+
def test_astroid_syntax_error(self, tmp_path) -> None:
593+
file_path = tmp_path / "a.py"
594+
with open(file_path, "w", encoding="utf8") as f:
595+
f.write("# encoding=UTF-9\n")
596+
self._test_output(
597+
[str(file_path), "--disable=all", "--enable=syntax-error"],
598+
expected_output="a.py': UTF-9 (syntax-error)",
599+
)
600+
587601
def test_version(self) -> None:
588602
def check(lines: list[str]) -> None:
589603
assert lines[0].startswith("pylint ")

0 commit comments

Comments
 (0)