Skip to content

Decorated classes below functions aren't separated with blank lines in .pyi files #18865

Closed

Description

@bzoracler

Summary

In a stub.pyi, I expect a blank line before a class (including its decorators) when it's defined below a function.

Before formatting:

def hello(): ...

@lambda _, /: _
class A: ...

After formatting:

def hello(): ...
@lambda _, /: _
class A: ...

For reference, the blank line is added by the formatter when the class sits below other statements, like imports, assignments, and other classes.

(I couldn't provide a ruff playground link because I couldn't figure out how to make it format a source file like it would do for stubs. Does such an option in the playground exist?)


See psf/black#4256 for reference - the missing blank line isn't intentional, but fixing it in black is apparently not trivial. Is it also not trivial in ruff?

Version

ruff 0.12.0

Activity

  1. added
    formatter
    on Jun 23, 2025
  2. MichaReiser commented on Jun 23, 2025

    @MichaReiser
    Member

    That makes sense to me. I think that should be fairly easy to change. It might be as easy as removing

    // Black for some reasons accepts decorators in place of empty lines
    // ```python
    // def _count1(): ...
    // @final
    // class LockType1: ...
    //
    // def _count2(): ...
    //
    // class LockType2: ...
    // ```
    let class_decorator_instead_of_empty_line = preceding.is_function_def_stmt()
    && following
    .as_class_def_stmt()
    .is_some_and(|class| !class.decorator_list.is_empty());

    We would need to gate the change behind preview mode because we can only release it as part of the next minor. See https://github.com/astral-sh/ruff/blob/1aad180aae84cd5d12d5a47c879183f559c871ed/crates/ruff_python_formatter/src/preview.rs#L1-L0 for a few exmaples on how to do that

  3. added
    help wanted
    style
    good first issue
    on Jun 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    formattergood first issuehelp wantedstyle

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions