Keep single line Python function definitions?

I've been a JetBrains user for years and years (primarily PHPStorm) now, but I can't find anywhere to set this in PyCharm.

If I have a piece of code, say:

class Foo:

    def some_func(self): ...

    def other_func(self): raise NotImplementedError


There doesn't seem to be any way to make PyCharm keep these single-line function definitions.  They're being defined like that specifically to save space when there are many function stubs defined for child classes.

PyCharm always formats them to

class Foo:

    def some_func(self):

        ...

    def other_func(self):

        raise NotImplementedError

Which I just don't want... 

No amount of tweaking the code formatting options seems to have any effect on this behavior.

The setting for single-line statements works fine.  Just not functions.

2 comments
Comment actions Permalink

+1

While not purely "pythonic", I tend to use single-line def's for properties to keep the avoid unnecessary line breaks.

@dataclass
class Number:
  num: int

  @property
  def double(self): return self.num * 2

  @property
  def triple(self): return self.num * 3

PyCharm doesn't show any warning, but it re-adds the line breaks whenever I auto-format the file.

EDIT: previous message said I found a solution, but I was mistaken.

0
Comment actions Permalink

Testing in 2022.2 with the following snippet:

class Foo:
def __init__(self): ...

def _do(self): ...

Reformatting this doesn't change single-line definitions to multi-line for me.

Please try resetting the code style settings to defaults and try again.

0

Please sign in to leave a comment.