0

I mostly agree with many of Black's formatting choices, including putting pass on a new line since usually it means I will implement the function later, but when I truly do want a function to be empty, I use ..., but then Black still puts it on a new line. Is there a Black setting to disable this? (Note: I still want it to put pass statements on a newline, if possible.

Th31AndOnly
  • 45
  • 1
  • 6
  • 3
    Black is (for better or worse, and with a small number of exceptions) unconfigurable *intentionally*. You either buy into its changes, or you don't use it. "Black is the uncompromising Python code formatter. **By using it, you agree to cede control** over minutiae of hand-formatting. In return, Black gives you speed, determinism, and freedom from pycodestyle nagging about formatting. You will save time and mental energy for more important matters." – chepner Jan 13 '21 at 18:07
  • Wildly tangential, I would consider using `pass` for intentionally empty functions, and a doc string to document a function whose implementation has been deferred for the time being. A doc string alone is syntactically sufficient to complete a `def` statement. – chepner Jan 13 '21 at 18:16
  • @g.d.d.c I kind of like that even though I'm using decorators; it looks pretty good. The only problem is with classes... I also need this to work with classes, I know it's niche, but the only other way I can think of it working is trying to make documentation with it, which wouldn't make sense for these. – Th31AndOnly Jan 13 '21 at 20:24

1 Answers1

1

Promoting my comment to an answer because there is no indication that this doesn't work with classes based on my testing.

You could declare your "empty" functions as lambdas: empty = lambda: ... - black won't reformat the lambda into a secondary line.

>>> class A:
...   def __init__(self):
...     self.val = 1
...   func = lambda self: self.val
... 
>>> 
>>> a = A()
>>> a.func()
1
g.d.d.c
  • 46,865
  • 9
  • 101
  • 111
  • 2
    Even though PEP-8 itself is not set in stone, violating it to appease a 3rd-party code formatter seems backwards. – chepner Jan 13 '21 at 21:41