Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatter: Ellipses in stub files get formatted on a single line #5822

Closed
konstin opened this issue Jul 17, 2023 · 10 comments · Fixed by #6592
Closed

Formatter: Ellipses in stub files get formatted on a single line #5822

konstin opened this issue Jul 17, 2023 · 10 comments · Fixed by #6592
Labels
formatter Related to the formatter good first issue Good for newcomers help wanted Contributions especially welcome

Comments

@konstin
Copy link
Member

konstin commented Jul 17, 2023

If a class, method or function is stub out with an ellipsis (...), black gives them their own line in python files and appends them end-of-line after the colon in stub files:

scratch.py

class Animal:
    ...


class Cat(Animal):
    def meow(self):
        ...


def f():
    ...

scratch.pyi

class Animal: ...

class Cat(Animal):
    def meow(self): ...

def f(): ...
@konstin konstin added the formatter Related to the formatter label Jul 17, 2023
@MichaReiser
Copy link
Member

Note: This requires tracking the source type and passing it to the formatter context.

@andersk
Copy link
Contributor

andersk commented Aug 2, 2023

With any luck, Black will soon be changing here to allow the concise form in both .py and .pyi files.

@konstin
Copy link
Member Author

konstin commented Aug 3, 2023

Applying the same formatting for source and stub files would be nice, and it would also reduce formatter complexity because it gives us one variable that changes formatting less.

@hauntsaninja
Copy link
Contributor

Black still handles other things (like whitespace) differently between py and pyi, so you'll still need to put source type in formatter context if you wish to format stub files like Black does.

@MichaReiser
Copy link
Member

We should probably implement this anyway for now, because the change will be part of Black's new preview style.

@konstin
Copy link
Member Author

konstin commented Aug 4, 2023

Added the file type information to the context #6331

konstin added a commit that referenced this issue Aug 4, 2023
… stub files (#6331)

**Summary** This adds the information whether we're in a .py python
source file or in a .pyi stub file to enable people working on #5822 and
related issues.

I'm not completely happy with `Default` for something that depends on
the input.

**Test Plan** None, this is currently unused, i'm leaving this to first
implementation of stub file specific formatting.

---------

Co-authored-by: Micha Reiser <micha@reiser.io>
@konstin konstin added good first issue Good for newcomers help wanted Contributions especially welcome labels Aug 10, 2023
@konstin
Copy link
Member Author

konstin commented Aug 10, 2023

PyFormatOptions now has a source_type field

@saulshanabrook
Copy link

Hey! I would love to try to adopt this formatting in my normal Python files as well. I saw that black I believe supports this under the next flag and was curious if that might be possible to support in ruff too.


For what it's worth, my use case is for a library that allows other libraries to create DSLs based off uninterpreted functions, so none of them will have bodies. I have gotten some feedback that it's annoying that it takes an extra line for every definition.

@MichaReiser
Copy link
Member

@saulshanabrook You can give it a try by running the formatter with --preview. We plan to release the new preview styles soon. See #8678

@saulshanabrook
Copy link

Oh wow, thank you @MichaReiser! That's great!

PedramNavid added a commit to dagster-io/dagster that referenced this issue Mar 5, 2024
## Summary & Motivation

Upgrades ruff to 0.3.0. Main change is ellipses now are formatted on a single line: astral-sh/ruff#5822

## How I Tested These Changes

bk
PedramNavid added a commit to dagster-io/dagster that referenced this issue Mar 28, 2024
## Summary & Motivation

Upgrades ruff to 0.3.0. Main change is ellipses now are formatted on a single line: astral-sh/ruff#5822

## How I Tested These Changes

bk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
formatter Related to the formatter good first issue Good for newcomers help wanted Contributions especially welcome
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants