google / yapf

A formatter for Python files
Apache License 2.0
13.73k stars 885 forks source link

Incorrect dict indentation inside nested context? #1169

Open danijar opened 11 months ago

danijar commented 11 months ago

Hi, I'm trying to adopt YAPF for a larger code base and am running into a few edge cases. Is there a configuration option to achieve the following desired output? Thanks!

Actual:

batcher = embodied.Batcher(
    sources=[generator] * self.config.batch_size,
    workers=self.config.data_loaders, postprocess=lambda x: {
    **self._convert_inps(x, self.train_devices), 'rng':
    self._next_rngs(self.train_devices)}, prefetch_source=4)

Desired:

batcher = embodied.Batcher(
    sources=[generator] * self.config.batch_size,
    workers=self.config.data_loaders,
    postprocess=lambda x: {
        **self._convert_inps(x, self.train_devices),
        'rng': self._next_rngs(self.train_devices)},
    prefetch_source=4)

pyproject.toml

[tool.yapf]
based_on_style = "pep8"
indent_width = 2
allow_multiline_lambdas = true
continuation_align_style = "FIXED"
continuation_indent_width = 4
dedent_closing_brackets = false
each_dict_entry_on_separate_line = false
spaces_around_power_operator = true
split_arguments_when_comma_terminated = true
split_before_closing_bracket = false
split_before_expression_after_opening_paren = true
split_before_first_argument = true
split_before_logical_operator = false
split_before_named_assigns = false
bwendling commented 10 months ago

I don't think we have a configuration option to do something like this. The current syntax isn't desirable, because it's less readable, so this should be fixed somehow. I'm a bit surprised that the brackets ({ and }) didn't cause YAPF to indent things, but it's probably due to some other formatting decision getting in the way.

danijar commented 10 months ago

Yep, I'm also a bit surprised. Do you have an idea what's causing this issue/how it can be fixed?

rishabhRsinghvi commented 10 months ago

@danijar think I can help.

danijar commented 10 months ago

That would be awesome!!

danijar commented 7 months ago

@rishabhRsinghvi Were you able to find out what's causing the issue here? This is a blocker for our adoption unfortunately and would be amazing to fix.