Skip to content

add checker for return in finally #8440

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 1 commit into from
May 21, 2023

Conversation

hofrob
Copy link
Contributor

@hofrob hofrob commented Mar 12, 2023

Type of Changes

Type
✨ New feature

Description

Closes #8260

@hofrob hofrob force-pushed the return-or-yield-in-finally branch 2 times, most recently from 6208ffc to f895b59 Compare March 12, 2023 12:11
@codecov
Copy link

codecov bot commented Mar 12, 2023

Codecov Report

Merging #8440 (a1d3404) into main (d7e9fd6) will increase coverage by 0.08%.
The diff coverage is 100.00%.

❗ Current head a1d3404 differs from pull request most recent head 32213e9. Consider uploading reports for the commit 32213e9 to get more accurate results

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #8440      +/-   ##
==========================================
+ Coverage   95.82%   95.90%   +0.08%     
==========================================
  Files         173      174       +1     
  Lines       18375    18370       -5     
==========================================
+ Hits        17607    17618      +11     
+ Misses        768      752      -16     
Impacted Files Coverage Δ
pylint/checkers/base/basic_checker.py 97.91% <100.00%> (+0.04%) ⬆️

... and 53 files with indirect coverage changes

@Pierre-Sassoulas Pierre-Sassoulas added the Enhancement ✨ Improvement to a component label Mar 12, 2023
@github-actions

This comment has been minimized.

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 working on this, it looks pretty good already !

@hofrob
Copy link
Contributor Author

hofrob commented Mar 12, 2023

I'm not too happy with those examples. The problem is, that pylint should probably not make recommendations on how to fix it. The only real suggestion this checker could offer, would be "the finally should only contain shutdown/close commands". If there's a good source where it says what to do in more detail, I'd add it.

I added this checker to the pylint/checkers/refactoring/refactoring_checker.py module, but if you have another suggestion, I'll gladly move it. I'm also walking through all statements in a finalbody. This might impact performance, though I hope that projects do not have a lot of logic inside of their finally clauses 😬.

Other than that, let me know if I should improve the wording or anything else really.

The fact that this new checker only produces one more message in all the checked repos is a good sign in my opinion. I'm not sure if the usage of yield in finally in pycopg2 is a legitimate one?

@hofrob hofrob marked this pull request as ready for review March 12, 2023 13:13
@Pierre-Sassoulas
Copy link
Member

It's a very good sign when there's only one new message raised and it's hard to tell if it's a false positive :)

@hofrob hofrob force-pushed the return-or-yield-in-finally branch from f895b59 to 44c8026 Compare March 12, 2023 13:52
@Pierre-Sassoulas Pierre-Sassoulas added this to the 3.0.0 milestone Mar 12, 2023
@hofrob hofrob force-pushed the return-or-yield-in-finally branch 2 times, most recently from 710fadc to 9f17956 Compare March 12, 2023 14:03
@hofrob
Copy link
Contributor Author

hofrob commented Mar 12, 2023

After looking at the generated docs, I thought adding comments to the bad example might be a bit more helpful.

@hofrob hofrob force-pushed the return-or-yield-in-finally branch from 9f17956 to 9235647 Compare March 12, 2023 14:10
@github-actions

This comment has been minimized.

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.

I like the embedded comment in the doc

@nickdrozd
Copy link
Contributor

The primer is showing one hit that looks like a false positive:

    def _exit_gen(self) -> PQGen[None]:
        """
        Exit current pipeline by sending a Sync and fetch back all remaining results.
        """
        try:
            self._enqueue_sync()
            yield from self._communicate_gen()
        finally:
            # No need to force flush since we emitted a sync just before.
            yield from self._fetch_gen(flush=False)

That function yields at least one item in any case, and possibly more. But I don't see any bug there.

I think the warning should only trigger if there is a return somewhere.

@hofrob
Copy link
Contributor Author

hofrob commented Mar 13, 2023

    def _exit_gen(self) -> PQGen[None]:
        """
        Exit current pipeline by sending a Sync and fetch back all remaining results.
        """
        try:
            self._enqueue_sync()
            yield from self._communicate_gen()
        finally:
            # No need to force flush since we emitted a sync just before.
            yield from self._fetch_gen(flush=False)

I'm trying to understand why this yield is in a finally clause. It more or less looks like they're trying to skip "bare-except" lint warnings this way 😀. Because that's what this actually does, right?

So I'm still not convinced that this is actually a false positive. yield from self._fetch_gen(flush=False) should come after the try/except with actual exceptions they want to catch.

😕

@nickdrozd
Copy link
Contributor

I'm trying to understand why this yield is in a finally clause. It more or less looks like they're trying to skip "bare-except" lint warnings this way grinning. Because that's what this actually does, right?

I'm not sure why it's like that. It's not skipping bare-except because it doesn't actually catch anything. So if there's an exception in try, it will interfere with the yield in finally? But self._fetch_gen needs to get called in any case, which might result in side effects.

Barring side effects, I can't think of a good reason to do it this way. So maybe it isn't a false positive. But I could be wrong.

@github-actions

This comment has been minimized.

Copy link
Member

@jacobtylerwalls jacobtylerwalls left a comment

Choose a reason for hiding this comment

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

Thanks for this!

Agree with @nickdrozd we should just deal with return. I don't think yield is really a problem in finally. I'd be happy to merge this if just affecting return.

@hofrob hofrob force-pushed the return-or-yield-in-finally branch from a1d3404 to c1f74d8 Compare May 21, 2023 12:23
@hofrob
Copy link
Contributor Author

hofrob commented May 21, 2023

Ok, I see that including yield is a step too far. I removed references to yield now but will do a final (:wink:) check and wait for the CI to finish.

Thanks for chiming in! This PR has bothered me for weeks. But I totally agree now, that this should only look at return statements.

@hofrob hofrob force-pushed the return-or-yield-in-finally branch 2 times, most recently from 8b48836 to 408e985 Compare May 21, 2023 12:34
@hofrob hofrob changed the title add checker for return/yield in finally add checker for return in finally May 21, 2023
@github-actions

This comment has been minimized.

@jacobtylerwalls jacobtylerwalls added the Control flow Requires control flow understanding label May 21, 2023
@jacobtylerwalls jacobtylerwalls modified the milestones: 3.0.0, 3.0.0a7 May 21, 2023
@Pierre-Sassoulas
Copy link
Member

The doc fail is due to a bug that was fixed on main :)

@hofrob hofrob force-pushed the return-or-yield-in-finally branch from 408e985 to 8a342b4 Compare May 21, 2023 13:38
@github-actions

This comment has been minimized.

@github-actions
Copy link
Contributor

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

This comment was generated for commit 32213e9

Copy link
Member

@jacobtylerwalls jacobtylerwalls left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution!

@jacobtylerwalls jacobtylerwalls dismissed Pierre-Sassoulas’s stale review May 21, 2023 17:11

All comments addressed

@jacobtylerwalls jacobtylerwalls merged commit 0e71f89 into pylint-dev:main May 21, 2023
@Pierre-Sassoulas
Copy link
Member

Great new check, thank you !

@hofrob hofrob deleted the return-or-yield-in-finally branch May 28, 2023 18:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Control flow Requires control flow understanding Enhancement ✨ Improvement to a component
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Prevent usage of return, yield in try/finally
4 participants