-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add consider-ternary-expression
extension
#4871
Conversation
Something is off with the functional test specification. Am I missing something obvious? |
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.
Hey, thank you for implementing this. Could you revert all the ternary changes that do not fit on one line using black ? I think bigger ternary make the code harder to understand.
I agree with that. Ternary ifs should only really be used for short conditions, something like var = 2 if some_var else 3 |
To the point that I'm wondering if the checker should not be changed to :
What do you think @nickdrozd ? |
I'm really not sure we should enforce any of it. IMO it doesn't make the code inherently better to use either. |
Okay, I took out the changes that don't result in one-liners. Personally, I would change all of them. It's not about lines of text, it's about complexity -- one single assign statement is less complex than two. It's a shame that Python makes if-expressions so clunky to use. In Lisp and Rust if-expressions are used all over the place with no problems. As for the check itself, I think it's okay if it suggests some more extreme changes, like if-expressions that would get broken across multiple lines. The check is an extension, disabled by default. So anyone who enables the extension can be safely assumed to be a relatively sophisticated Pylint user, and therefore also someone who can deal with any false positives or iffy cases. |
Pull Request Test Coverage Report for Build 1151047154
💛 - Coveralls |
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.
Thank you for cleaning up the MR, I left some comment, mostly I think the expected name is "ternary operator" or "ternary conditional".
class ConsiderIfExpressionChecker(BaseChecker): | ||
|
||
__implements__ = (IAstroidChecker,) | ||
name = "consider_if_expression" |
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.
name = "consider_if_expression" | |
name = "consider_ternary_expression" |
name = "consider_if_expression" | ||
msgs = { | ||
"W0160": ( | ||
"Consider rewriting as an if-expression", |
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.
"Consider rewriting as an if-expression", | |
"Consider rewriting as a ternary if-expression", |
msgs = { | ||
"W0160": ( | ||
"Consider rewriting as an if-expression", | ||
"consider-if-expression", |
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.
"consider-if-expression", | |
"consider-ternary-expression", |
"Consider rewriting as an if-expression", | ||
"consider-if-expression", | ||
"Multiple assign statements spread across if/else blocks can be " | ||
"rewritten with a single assingment and an if-expression", |
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.
"rewritten with a single assingment and an if-expression", | |
"rewritten with a single assignment as a ternary conditional", |
) | ||
} | ||
|
||
def visit_if(self, node): |
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.
Could you add the typing, please ? :)
Yes I agree, I was trying to transform it into something complicated that I would personally use, but if someone want only ternary and have to activate this plugin, then it can be very opinionated without problems. |
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.
Great work, thank you !
consider-ternary-expression
extension
https://nickdrozd.github.io/2021/09/02/new-pylint-checks.html This dude just used your repo to write a terrible article. |
Type of Changes
Description
Closes #4366