-
Notifications
You must be signed in to change notification settings - Fork 19
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
[SIM117] Merge with statements for context managers that have same scope #35
Comments
I love it! Should be easy to implement. I hope to find the time for it tomorrow :-) |
|
@Skylion007 I've just added a PR which does the trick. I would also write you in the README for that rule. What do you think about that? I can also adjust it a little bit. I want to make contributions like yours a bit more visible. |
Thanks for including me in the ReadMe. Left a minor nit on the PR, but otherwise looks good! |
Merged and released :-) |
Explanation
A little known feature of Python is that a with statement can contain multiple context managers. This allows the code only one with statement (and therefore only 1 level of indentation). This rule has a similar rational to SIM102.
This rule should be applied if and only if:
Caveat when implementing: If the context names are really long, the with statement may be broken over a line break. A new feature in the Python 3.10 alpha will be to allow parentheses to be used to break the with statement over multiple lines.
Example
Consider the following context managers:
can be transformed into the following:
The text was updated successfully, but these errors were encountered: