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 1 commit
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/functional/s/syntax/syntax_error_invalid_encoding.py
@@ -0,0 +1 @@
# -*- coding: lala -*- # [syntax-error]
@@ -0,0 +1 @@
syntax-error:1:0:None:None::"Parsing failed: 'unknown encoding for '/Users/daniel/DocumentenLaptop/Programming/Github/pylint/tests/functional/s/syntax/syntax_error_invalid_encoding.py': lala'":HIGH
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
syntax-error:1:0:None:None::"Parsing failed: 'unknown encoding for '/Users/daniel/DocumentenLaptop/Programming/Github/pylint/tests/functional/s/syntax/syntax_error_invalid_encoding.py': lala'":HIGH
syntax-error:1:0:None:None::"Parsing failed: 'unknown encoding for '/Users/daniel/DocumentenLaptop/Programming/Github/pylint/tests/functional/s/syntax/syntax_error_invalid_encoding.py': lala'":HIGH

This can't work, we probably need a unit test for this or to change to a relative path.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah. I'll make a unittest