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

[contextmanager-generator-missing-cleanup][new feature] Warn about @contextlib.contextmanager without try/finally in generator functions #9133

Merged
merged 32 commits into from
May 12, 2024

Conversation

rhyn0
Copy link
Contributor

@rhyn0 rhyn0 commented Oct 8, 2023

  • add: initial layout for new error
  • add: test cases for new checker
  • add: first pass at new checker

Type of Changes

Type
✨ New feature

Description

Add a checker for contextlib.contextmanager decorated generator functions that don't have cleanup handling for GeneratorExit.

Closes #2832

rhyn0 added 4 commits October 7, 2023 23:32
created new _BasicChecker inheriting class
registered this new linter like the rest in __init__.py
original example case included
a slightly modified positive case
2 negative cases where exception is handled properly
Checks that contextmanager decorated functions that are generators
handle GeneratorExit cleanly
@Pierre-Sassoulas Pierre-Sassoulas added the Enhancement ✨ Improvement to a component label Oct 8, 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 your contribution, a lot of work went into this clearly. Would you mind adding a good/bad example in https://github.com/pylint-dev/pylint/tree/main/doc/data/messages too ? It's not enforced yet but we're close to being fully documented :)

pylint/checkers/base/function_checker.py Outdated Show resolved Hide resolved
pylint/checkers/base/function_checker.py Outdated Show resolved Hide resolved
pylint/checkers/base/function_checker.py Outdated Show resolved Hide resolved
pylint/checkers/base/function_checker.py Outdated Show resolved Hide resolved
pylint/checkers/base/function_checker.py Outdated Show resolved Hide resolved
pylint/checkers/base/function_checker.py Outdated Show resolved Hide resolved
rhyn0 added 4 commits October 13, 2023 19:23
Changed the detection logic of a bad generator function to test whether
there was attempt at cleanup code
removed extra pylint disables from heading of test cases
output files got updated due to newlines above
@rhyn0 rhyn0 marked this pull request as ready for review October 14, 2023 02:29
@github-actions

This comment has been minimized.

@jacobtylerwalls jacobtylerwalls added the Needs decision 🔒 Needs a decision before implemention or rejection label Nov 5, 2023
@jacobtylerwalls jacobtylerwalls added Needs PR This issue is accepted, sufficiently specified and now needs an implementation Work in progress and removed Needs decision 🔒 Needs a decision before implemention or rejection Needs PR This issue is accepted, sufficiently specified and now needs an implementation labels Feb 19, 2024
@jacobtylerwalls jacobtylerwalls added this to the 3.2.0 milestone Feb 23, 2024
@rhyn0 rhyn0 requested a review from DudeNr33 as a code owner March 3, 2024 21:43
Copy link

codecov bot commented Mar 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.83%. Comparing base (e31a155) to head (3bd7132).
Report is 181 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #9133      +/-   ##
==========================================
+ Coverage   95.76%   95.83%   +0.07%     
==========================================
  Files         173      174       +1     
  Lines       18665    18887     +222     
==========================================
+ Hits        17874    18101     +227     
+ Misses        791      786       -5     
Files Coverage Δ
pylint/checkers/base/__init__.py 100.00% <100.00%> (ø)
pylint/checkers/base/function_checker.py 100.00% <100.00%> (ø)

... and 39 files with indirect coverage changes

This comment has been minimized.

This comment has been minimized.

@Pierre-Sassoulas
Copy link
Member

Does this need reviews or do you need to polish some things still @rhyn0 ? I would be glad to have this in 3.2.0 😄

@jacobtylerwalls
Copy link
Member

From what I can tell it's very close:

  • some unresolved documentation comments
  • docs check is failing
  • TODO statements about yield and asyncwith

Happy to give the whole thing a once-over when those are in 👍

This comment has been minimized.

@rhyn0
Copy link
Contributor Author

rhyn0 commented May 5, 2024

Failing ReadTheDocs error seems to be the following

doc/user_guide/messages/warning/implicit-str-concat.rst:55: WARNING: Lexing literal_block '[STRING_CONSTANT]\ncheck-str-concat-over-line-jumps = yes' as "toml" resulted in an error at token: 'y'. Retrying in relaxed mode.

Not sure if I need to change this here

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.

Looking forward to merging this shortly. Just some minor feedback.

doc/whatsnew/fragments/2832.feature Outdated Show resolved Hide resolved
doc/whatsnew/fragments/2832.feature Outdated Show resolved Hide resolved
pylint/checkers/base/function_checker.py Outdated Show resolved Hide resolved
tests/functional/c/consider/consider_using_with_open.py Outdated Show resolved Hide resolved
@jacobtylerwalls
Copy link
Member

Not sure if I need to change this here

Good call, let's not block your work on that. If it hasn't been fixed on main yet, please open an issue!

Copy link
Contributor

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

This comment was generated for commit 3bd7132

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 new check @rhyn0!

@jacobtylerwalls jacobtylerwalls merged commit d5ad55d into pylint-dev:main May 12, 2024
43 of 44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement ✨ Improvement to a component
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Warn about @contextlib.contextmanager without try/finally in generator functions
4 participants