-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Fix AstroidError
in similarity checker with imports/signatures ignored
#6357
Fix AstroidError
in similarity checker with imports/signatures ignored
#6357
Conversation
for more information, see https://pre-commit.ci
Why do we need to parse code with astroid in the duplicate-code checker ? Isn't it a check where we can work directly on text ? |
Pull Request Test Coverage Report for Build 2180193525
💛 - Coveralls |
Multi-line imports: 76ce7c1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @jacobtylerwalls!
Co-authored-by: Daniël van Noord <[email protected]>
Need a solution for |
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
@DanielNoord, well I gave this a shot, and I think it's probably mergeable, but there are a whole host of problems you can create beyond the reported one, because anywhere you can put a disable comment, you can wipe out a line required for the code to be interpreted. E.g., this will still crash: def a():
x = [
None, None
] # pylint: disable=duplicate-code I'll open a separate PR to just catch |
Triple-quoted strings will mess with this also. Ultimately it's a losing battle. I'll open that follow-up PR. |
@jacobtylerwalls You have probably explored this, but can we move the |
No, it had not occurred to me! I was boiling in the water as I saw the problem scope get bigger. 🎈 I like that a lot. Should be simpler. |
pylint/checkers/similar.py
Outdated
@@ -557,7 +557,7 @@ def stripped_lines( | |||
ignore_docstrings: bool, | |||
ignore_imports: bool, | |||
ignore_signatures: bool, | |||
line_enabled_callback: Optional[Callable] = None, | |||
line_enabled_callback: Callable | None = None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fought with this locally and finally just no-verified
it. Why is this syntax okay with our minimum python version of 3.7.2? (Well, 3.6.2, since we are talking about backporting, potentially.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, did we future import annotations everywhere? ah. but still a problem with 3.6, then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we don't have to backport this if only one user reported it and 2.14 is coming soon.
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool. LMK if I should move the release note to 2.14. If we backport I guess we can just use different type annotation syntax.
pylint/checkers/similar.py
Outdated
@@ -557,7 +557,7 @@ def stripped_lines( | |||
ignore_docstrings: bool, | |||
ignore_imports: bool, | |||
ignore_signatures: bool, | |||
line_enabled_callback: Optional[Callable] = None, | |||
line_enabled_callback: Callable | None = None, |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
…red (pylint-dev#6357) Co-authored-by: Daniël van Noord <[email protected]>
This commit is hard to cherry-pick. I have the following non trivial conflicts to fix: diff --cc ChangeLog
index 126e82e2b,74d3d8005..000000000
--- a/ChangeLog
+++ b/ChangeLog
diff --cc pylint/checkers/similar.py
index 69b401fa4,11b12995d..000000000
--- a/pylint/checkers/similar.py
+++ b/pylint/checkers/similar.py
@@@ -359,28 -366,25 +360,47 @@@ class Similar
readlines = decoding_stream(stream, encoding).readlines
else:
readlines = stream.readlines # type: ignore[assignment] # hint parameter is incorrectly typed as non-optional
+
try:
++<<<<<<< HEAD
+ active_lines: List[str] = []
+ if hasattr(self, "linter"):
+ # Remove those lines that should be ignored because of disables
+ for index, line in enumerate(readlines()):
+ if self.linter._is_one_message_enabled("R0801", index + 1): # type: ignore[attr-defined]
+ active_lines.append(line)
+ else:
+ active_lines = readlines()
+
+ self.linesets.append(
+ LineSet(
+ streamid,
+ active_lines,
+ self.ignore_comments,
+ self.ignore_docstrings,
+ self.ignore_imports,
+ self.ignore_signatures,
+ )
+ )
++=======
+ lines = readlines()
++>>>>>>> 1664202ba... Fix `AstroidError` in similarity checker with imports/signatures ignored (#6357)
except UnicodeDecodeError:
- pass
+ lines = []
+
+ self.linesets.append(
+ LineSet(
+ streamid,
+ lines,
+ self.namespace.ignore_comments,
+ self.namespace.ignore_docstrings,
+ self.namespace.ignore_imports,
+ self.namespace.ignore_signatures,
+ line_enabled_callback=self.linter._is_one_message_enabled # type: ignore[attr-defined]
+ if hasattr(self, "linter")
+ else None,
+ )
+ )
def run(self) -> None:
"""Start looking for similarities and display results on stdout."""
@@@ -552,7 -560,8 +572,12 @@@ def stripped_lines
ignore_docstrings: bool,
ignore_imports: bool,
ignore_signatures: bool,
++<<<<<<< HEAD
+) -> List[LineSpecifs]:
++=======
+ line_enabled_callback: Callable[[str, int], bool] | None = None,
+ ) -> list[LineSpecifs]:
++>>>>>>> 1664202ba... Fix `AstroidError` in similarity checker with imports/signatures ignored (#6357)
"""Return tuples of line/line number/line type with leading/trailing whitespace and any ignored code features removed.
:param lines: a collection of lines
|
I think incoming is fine. |
Because of that the code is not compatible with python 3.6 the fix to the similarity checker is mixed with it.
|
Oh, that should be fixable by importing |
There were other problem with typing, I removed the typing, but the tests suite now fail with |
…red (pylint-dev#6357) Co-authored-by: Daniël van Noord <[email protected]>
Cherry-picked to |
…red (pylint-dev#6357) Co-authored-by: Daniël van Noord <[email protected]>
Type of Changes
Description
The array of lines being filtered by
append_stream()
needs to be parsable as valid python code if imports/signatures are being ignored, see: https://github.com/PyCQA/pylint/blob/adf660a7300148a31cdb8bc25301bfd21af0602b/pylint/checkers/similar.py#L565-L566.Solution now is to re-parse the AST before trying to filter out lines that were disabled.
Closes #6301