Closed MetRonnie closed 2 years ago
Thank you for the report, I'll fix that today in the evening 🤗
$ astpretty --no-show-offsets /dev/stdin <<< `cat example.txt`
Module(
body=[
Assign(
targets=[Name(id='my_dict', ctx=Store())],
value=Dict(
keys=[
Constant(value='foo', kind=None),
Constant(value='bar', kind=None),
],
values=[
List(
elts=[
Constant(value=1, kind=None),
Constant(value=2, kind=None),
Constant(value=3, kind=None),
Constant(value=4, kind=None),
],
ctx=Load(),
),
List(
elts=[
Constant(value=5, kind=None),
Constant(value=6, kind=None),
Constant(value=7, kind=None),
Constant(value=8, kind=None),
],
ctx=Load(),
),
],
),
type_comment=None,
),
Assign(
targets=[
Subscript(
value=Name(id='my_dict', ctx=Load()),
slice=Constant(value='both', kind=None),
ctx=Store(),
),
],
value=ListComp(
elt=Name(id='item', ctx=Load()),
generators=[
comprehension(
target=Name(id='_list', ctx=Store()),
iter=Call(
func=Attribute(
value=Name(id='my_dict', ctx=Load()),
attr='values',
ctx=Load(),
),
args=[],
keywords=[],
),
ifs=[],
is_async=0,
),
comprehension(
target=Name(id='item', ctx=Store()),
iter=Name(id='_list', ctx=Load()),
ifs=[],
is_async=0,
),
],
),
type_comment=None,
),
],
type_ignores=[],
)
Desired change
Explanation
Sometimes you need to add to a dictionary in a way that cannot be done until after the dictionary is already created.
Example
This is an example where the mentioned rule(s) would currently be suboptimal (unless I'm missing a trick):