Closed Skylion007 closed 3 years ago
I love it! Should be easy to implement. I hope to find the time for it tomorrow :-)
$ astpretty --no-show-offsets /dev/stdin <<< `cat example.txt`
Module(
body=[
With(
items=[
withitem(
context_expr=Call(
func=Name(id='A', ctx=Load()),
args=[],
keywords=[],
),
optional_vars=Name(id='a', ctx=Store()),
),
],
body=[
With(
items=[
withitem(
context_expr=Call(
func=Name(id='B', ctx=Load()),
args=[],
keywords=[],
),
optional_vars=Name(id='b', ctx=Store()),
),
],
body=[
Expr(
value=Call(
func=Name(id='print', ctx=Load()),
args=[Constant(value='hello', kind=None)],
keywords=[],
),
),
],
type_comment=None,
),
],
type_comment=None,
),
],
type_ignores=[],
)
@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: