psf / black

The uncompromising Python code formatter
https://black.readthedocs.io/en/stable/
MIT License
38.88k stars 2.45k forks source link

Brackets around lambda expressions in functions #2958

Open jpy-git opened 2 years ago

jpy-git commented 2 years ago

Is your feature request related to a problem? Please describe.

At the moment black doesn't seem to have any strong opinions on brackets around lambda expressions in functions.

Describe the solution you'd like

I had a couple initial thoughts:

A) Remove brackets if it's used as a positional argument i.e.

map((lambda n: n**2), [1, 2, 3, 4, 5])

becomes

map(lambda n: n**2, [1, 2, 3, 4, 5])

B) require brackets when used as a keyword argument (or default arg in function def)

foo(func=lambda x: x**2)

becomes

foo(func=(lambda x: x**2))

Additional context

Saw option B) in a code review the other day and thought it would read better if the lambda expression was clearly grouped.

felix-hilden commented 2 years ago

My 2c: definitely remove for A. My first reaction for B was to also remove but it's fine for me either way. The value of a long lambda can be parenthesised anyway.