Closed atombrella closed 2 years ago
$ astpretty --no-show-offsets /dev/stdin <<< `cat example.txt`
Module(
body=[
Assign(
targets=[Name(id='a', ctx=Store())],
value=Dict(keys=[], values=[]),
type_comment=None,
),
Assign(
targets=[
Subscript(
value=Name(id='a', ctx=Load()),
slice=Constant(value='b', kind=None),
ctx=Store(),
),
],
value=Constant(value='c', kind=None),
type_comment=None,
),
],
type_ignores=[],
)
One would need to acct on ast.Assign
(=node)
value
is ast.Dict
andnode.next_sibling
is also Assign
Then it could be added to the node directly.
Explanation
Do not use a line to create a dictionary, and then populate it. Do it directly. PyCharm has this check, which is rather neat; I think it's in Pylint as I think this linter is used behind the scenes when you run code inspection on a project. It's a bit faster (nanoseconds, whatever), and it reads nicer.
Example