Skip to content
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

Raise syntax-error correctly on invalid encodings #7553

Merged
merged 2 commits into from Oct 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/7522.bugfix
@@ -0,0 +1,3 @@
Fixed an issue where ``syntax-error`` couldn't be raised on files with invalid encodings.

Closes #7522
5 changes: 4 additions & 1 deletion pylint/lint/pylinter.py
Expand Up @@ -1016,9 +1016,12 @@ def get_ast(
data, modname, filepath
)
except astroid.AstroidSyntaxError as ex:
line = getattr(ex.error, "lineno", None)
if line is None:
line = 0
self.add_message(
"syntax-error",
line=getattr(ex.error, "lineno", 0),
line=line,
col_offset=getattr(ex.error, "offset", None),
args=f"Parsing failed: '{ex.error}'",
confidence=HIGH,
Expand Down
1 change: 1 addition & 0 deletions tests/regrtest_data/invalid_encoding.py
@@ -0,0 +1 @@
# -*- coding: lala -*-
5 changes: 5 additions & 0 deletions tests/test_self.py
Expand Up @@ -1191,6 +1191,11 @@ def test_ignore_path_recursive_current_dir(self) -> None:
code=0,
)

def test_syntax_error_invalid_encoding(self) -> None:
module = join(HERE, "regrtest_data", "invalid_encoding.py")
expected_output = "unknown encoding"
self._test_output([module, "-E"], expected_output=expected_output)


class TestCallbackOptions:
"""Test for all callback options we support."""
Expand Down