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 incorrect consider-using-ternary
when condition is inferable as False
#5227
Fix incorrect consider-using-ternary
when condition is inferable as False
#5227
Conversation
Pull Request Test Coverage Report for Build 1399479523
π - Coveralls |
β¦ as False * Properly infer the condition in old ternary statements and suggest ``simplify-boolean-expression`` over ``consider-using-ternary`` if it is False * Integration test for this behavior
6781de3
to
9bcdf07
Compare
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, I think this is a great bug fix. I just have one small remark.
@@ -1396,7 +1396,7 @@ def visit_return(self, node: nodes.Return) -> None: | |||
if inferred_truth_value in (None, astroid.Uninferable): | |||
truth_boolean_value = True | |||
else: | |||
truth_boolean_value = truth_value.bool_value() | |||
truth_boolean_value = inferred_truth_value.bool_value() # type: ignore |
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.
What is the mypy error here ? Any way we can fix this ?
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.
Below is the output I get running mypy with tox on my laptop. The last three errors I don't get on my desktop, but I haven't delved into what the differential is.
I assumed the first error was ignorable because logically inferred_truth_value
should never be None
at this line. I'm not very familiar with mypy so I should have asked proactively but if you have a pointer of what to look at here I can look into it more.
pylint/checkers/refactoring/refactoring_checker.py:1399: error: Item "None" of "Optional[Any]" has no attribute "bool_value"
tests/lint/test_pylinter.py:7: error: unused "type: ignore" comment
tests/test_pylint_runners.py:8: error: unused "type: ignore" comment
tests/test_self.py:59: error: unused "type: ignore" comment
Found 4 errors in 4 files (checked 207 source files)
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.
If you modify L1396, you could remove the type: ignore
here.
if inferred_truth_value is None or inferred_truth_value == astroid.Uninferable:
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 for the tip! I'll brush up on handling mypy output more too.
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.
mypy
can't really handle in
conditions unfortunately. More explicit ones are a bit easier, thus the suggestion works.
@@ -1396,7 +1396,7 @@ def visit_return(self, node: nodes.Return) -> None: | |||
if inferred_truth_value in (None, astroid.Uninferable): | |||
truth_boolean_value = True | |||
else: | |||
truth_boolean_value = truth_value.bool_value() | |||
truth_boolean_value = inferred_truth_value.bool_value() # type: ignore |
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.
If you modify L1396, you could remove the type: ignore
here.
if inferred_truth_value is None or inferred_truth_value == astroid.Uninferable:
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.
LGTM!
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.
Nice merge request again @areveny !
Type of Changes
Description
Closes #5200
See that issue for my triage/explanation of the fix.