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

Fix possibly-used-before-assignment issues in pylint codebase #9644

Closed
wants to merge 12 commits into from

Conversation

aatle
Copy link
Contributor

@aatle aatle commented May 17, 2024

Type of Changes

Type
βœ“ πŸ”¨ Refactoring

Description

This new check was disabled for pylint itself. Enabled, it gives 7 errors in 5 files in the codebase.

Enables the check for pylint and fixes the 7 errors.

Three of the errors were fixed by adding an else clause that explicitly raised ValueError for bad values instead of letting the code possibly run into an UnboundLocalError.
Two errors were caused by checking a bool flag (that never changes) in two separate conditionals: one where the variable set, the other where it was used.
The last two errors were caused by relying on a separate flag when setting a variable, for the conditional that uses the variable.

Closes #9637

Note: this is my first contribution to open source, so feedback and advice are welcome

Copy link

codecov bot commented May 17, 2024

Codecov Report

Attention: Patch coverage is 86.95652% with 3 lines in your changes are missing coverage. Please review.

Project coverage is 95.84%. Comparing base (978981d) to head (165cfdd).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #9644      +/-   ##
==========================================
- Coverage   95.86%   95.84%   -0.02%     
==========================================
  Files         174      174              
  Lines       18907    18898       -9     
==========================================
- Hits        18125    18113      -12     
- Misses        782      785       +3     
Files Coverage Ξ”
...heckers/refactoring/implicit_booleaness_checker.py 100.00% <100.00%> (ΓΈ)
pylint/checkers/similar.py 96.23% <100.00%> (-0.06%) ⬇️
pylint/checkers/__init__.py 90.00% <0.00%> (-3.11%) ⬇️
pylint/checkers/typecheck.py 96.51% <66.66%> (-0.11%) ⬇️
pylint/testutils/_primer/primer.py 94.33% <0.00%> (-1.82%) ⬇️

This comment has been minimized.

@Pierre-Sassoulas Pierre-Sassoulas added Maintenance Discussion or action around maintaining pylint or the dev workflow Skip news πŸ”‡ This change does not require a changelog entry labels May 18, 2024
@Pierre-Sassoulas
Copy link
Member

Hey @aatle thank you for your contribution ! It would be better if each commit was passing the tests (i.e. first commit activates the check and disable warnings locally, then each following commit remove a local disable and fix the associated warning). That way we can look at each individual commit during review and partially revert or drop them later. You can open a merge request for each too if necessary. (Sometime it's nice to move what's consensual out of the way.)

This comment has been minimized.

Copy link
Contributor

πŸ€– According to the primer, this change has no effect on the checked open source code. πŸ€–πŸŽ‰

This comment was generated for commit 165cfdd

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.

Could you please open a first merge request with bea0aae and the new local disable first and the commit with no value error raised like 9e8d43f ? I think those change are mergeable as is This will make you trusted enough to run the CI without our approval and ease both of our lives and also make the following review smaller :)

Comment on lines +112 to +113
else:
raise ValueError(stat_type)
Copy link
Member

Choose a reason for hiding this comment

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

I think I'd rather have default value like

      [
                ("convention", "NC"),
                ("refactor", "NC"),
                ("warning", "NC"),
                ("error", "NC"),
            ]

@DanielNoord
Copy link
Collaborator

@aatle does this need a rebase?

@aatle
Copy link
Contributor Author

aatle commented Jun 3, 2024

@DanielNoord I am not really sure (I'm not that experienced).
I had created and merged a new pull request which includes most of the content of this pull request because of the code coverage test failing for this one. The other uses ignores instead of raising ValueError in 3 cases.

I would think the best thing to do is to close this pull request and possibly the issue too. Merging this pull request wouldn't be that great, a new pull request to make these changes (if desired) would probably be better.

If you agree, this PR can be closed and probably the original issue too, as the warnings have all been fixed (mostly with ignores).

@DanielNoord
Copy link
Collaborator

I'm fine with closing this, but I'm interested to see what is left after rebasing on main. I think Pierre already gave some pointers to remove some of the ValueErrors and perhaps we can prevent all of them with little refactoring?

@aatle
Copy link
Contributor Author

aatle commented Jun 3, 2024

The else clause fixes are trivial, with one of three possibly using a default instead of a ValueError. I guess they can be rebased, with commits to remove the ignores and add code coverage for them.
However, removing the ignores for some of the other warnings is difficult, they are nasty cases with no obviously better solution.

@aatle
Copy link
Contributor Author

aatle commented Jun 3, 2024

Do you think this PR can be closed now? My main thoughts is that the benefits are small, the code would still give an error either way, just a different type of error, since the changes are just raising explicit errors. The original issue can also probably be closed.

@DanielNoord
Copy link
Collaborator

Let's close the PR and issue :)

@aatle aatle closed this Jun 4, 2024
@aatle aatle deleted the issue-9637 branch June 4, 2024 21:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Maintenance Discussion or action around maintaining pylint or the dev workflow Skip news πŸ”‡ This change does not require a changelog entry
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix possibly-used-before-assignment issues in our own codebase
3 participants