Skip to content

Simplify boolean return logic #14012

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

Merged
merged 16 commits into from
Nov 10, 2022
Merged

Conversation

nickdrozd
Copy link
Contributor

I was poking around in the code and I noticed that there are a lot of bare boolean returns. For example:

        if self.subtype_context.keep_erased_types:
            return False
        return True

This can be simplified to:

        return not self.subtype_context.keep_erased_types

That's a pretty simple case. Other cases are more complicated, but similar in spirit.

This is a refactoring PR that should not change any behavior. By my count it results in a net reduction of 32 return statements.

If this sort of change is welcome, I can keep going.

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

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

Thanks, but several of these make the code less readable. In general, some of these sequences of if conditions are pretty subtle and it's much easier to read them one at a time than to have them compressed into a single statement. I've commented on the ones I think are worthwhile, if you want to pursue this PR, please remove the others :-)

@nickdrozd
Copy link
Contributor Author

Okay, I think I got all of them.

@nickdrozd
Copy link
Contributor Author

I fixed the existing changes and added a few more. All the new changes are pretty small, and they can be kept or not.

In the spirit of simplified boolean logic, it would be easier if you mark the ones to reject, rather than the ones to keep 😄

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

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

Thanks, reviewed the new changes. Please don't add any others in this PR :-)

@github-actions

This comment has been minimized.

@github-actions
Copy link
Contributor

github-actions bot commented Nov 9, 2022

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

Copy link
Collaborator

@hauntsaninja hauntsaninja 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!

@hauntsaninja hauntsaninja merged commit b18281c into python:master Nov 10, 2022
@nickdrozd
Copy link
Contributor Author

Thanks for reviewing!

@nickdrozd nickdrozd deleted the simplify-bool-returns branch November 11, 2022 05:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants