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

Add consider-ternary-expression extension #4871

Merged
merged 2 commits into from
Aug 20, 2021

Conversation

nickdrozd
Copy link
Contributor

Type of Changes

Type
✨ New feature

Description

Closes #4366

@nickdrozd
Copy link
Contributor Author

Something is off with the functional test specification. Am I missing something obvious?

Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a 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.

@cdce8p
Copy link
Member

cdce8p commented Aug 19, 2021

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

@Pierre-Sassoulas
Copy link
Member

To the point that I'm wondering if the checker should not be changed to :

  • if it's a ternary and it's longer than 88 characters, tell the user to remove the ternary
  • It's it's not a ternary and it's shorter than 88 characters, tell the user to consider a ternary

What do you think @nickdrozd ?

@cdce8p
Copy link
Member

cdce8p commented Aug 19, 2021

To the point that I'm wondering if the checker should not be changed to :

  • if it's a ternary and it's longer than 88 characters, tell the user to remove the ternary
  • It's it's not a ternary and it's shorter than 88 characters, tell the user to consider a ternary

I'm really not sure we should enforce any of it. IMO it doesn't make the code inherently better to use either.
I.e. even for the existing pylint code I don't think we should do most of these changes.

@nickdrozd
Copy link
Contributor Author

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.

@coveralls
Copy link

coveralls commented Aug 19, 2021

Pull Request Test Coverage Report for Build 1151047154

  • 31 of 35 (88.57%) changed or added relevant lines in 10 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.03%) to 92.81%

Changes Missing Coverage Covered Lines Changed/Added Lines %
pylint/checkers/imports.py 1 2 50.0%
pylint/extensions/consider_ternary_expression.py 21 24 87.5%
Totals Coverage Status
Change from base Build 1149750110: 0.03%
Covered Lines: 13450
Relevant Lines: 14492

💛 - Coveralls

Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a 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"
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
name = "consider_if_expression"
name = "consider_ternary_expression"

name = "consider_if_expression"
msgs = {
"W0160": (
"Consider rewriting as an if-expression",
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
"Consider rewriting as an if-expression",
"Consider rewriting as a ternary if-expression",

msgs = {
"W0160": (
"Consider rewriting as an if-expression",
"consider-if-expression",
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
"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",
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
"rewritten with a single assingment and an if-expression",
"rewritten with a single assignment as a ternary conditional",

)
}

def visit_if(self, node):
Copy link
Member

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 ? :)

@Pierre-Sassoulas
Copy link
Member

As for the check itself, I think it's okay if it suggests some more extreme changes,

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.

@Pierre-Sassoulas Pierre-Sassoulas added Checkers Related to a checker Enhancement ✨ Improvement to a component labels Aug 19, 2021
Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a 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 !

@Pierre-Sassoulas Pierre-Sassoulas merged commit 7a9bfc4 into pylint-dev:main Aug 20, 2021
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.10.0 milestone Aug 20, 2021
@Pierre-Sassoulas Pierre-Sassoulas changed the title Add consider-if-expression extension Add consider-ternary-expression extension Aug 20, 2021
@zrothberg
Copy link

https://nickdrozd.github.io/2021/09/02/new-pylint-checks.html

This dude just used your repo to write a terrible article.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Checkers Related to a checker Enhancement ✨ Improvement to a component
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New check: Use ternary for single assignment
5 participants