Skip to content

Black --preview crashes with nested long dict str keys #3261

@yilei

Description

@yilei

Describe the bug

black --preview crashed on some code.

To Reproduce

For example, take this code:

v = {
    ('a_very_long_str_a_very_long_str_a_very_long_str_a_very_long_str_'
     'a_very_long_str_a_very_long_str_a_very_long_str_'): {
        'key':
            'value',
    },
}

And run it with these arguments:

$ black file.py --preview

The resulting error is:

error: cannot format /Users/yileiyang/Downloads/t.py: Cannot parse: 3:122:         "a_very_long_str_a_very_long_str_a_very_long_str_a_very_long_str_a_very_long_str_a_very_long_str_a_very_long_str_": {

Oh no! 💥 💔 💥
1 file failed to reformat.

Expected behavior

Should not crash.

Environment

  • Black's version: 22.8.1.dev4+g383b228
  • OS and Python version: macOS, Python 3.9.12

Additional context

Looks like this was caused by #3162, I'll take a look.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C: crashBlack is crashing

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions