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

Single-line match-cases with Ellipsis (...) as body #11676

Open
pfaion opened this issue Jun 1, 2024 · 2 comments
Open

Single-line match-cases with Ellipsis (...) as body #11676

pfaion opened this issue Jun 1, 2024 · 2 comments
Labels
formatter Related to the formatter style How should formatted code look

Comments

@pfaion
Copy link

pfaion commented Jun 1, 2024

Thanks for the great work on ruff!

Not sure if this a bug, probably rather a feature request. Curious to hear what you think about this:

I work with complexly nested data and sometimes I want to extract a value from potentially different parts of the data, depending on the structure of the data. I realized that you can quite nicely use "structural pattern matching" for that, e.g. here's a simple example:

class User(BaseModel):
    global_name: str
    # ...

class Member(BaseModel):
    nickname: Optional[str]
    user: User
    # ...

name: str
match member:
    case Member(nickname=str(name)): ...
    case Member(user=User(global_name=str(name))): ...
    
# Now `name` is bound, depending on which structure first "resolved". If the member had
# a nickname defined, we prefer using that. Otherwise we use the global_name.

I wrote the single-line case statements similar to how you can write single-line function signatures for e.g. Protocols (e.g. refer to #10026). However, ruff will always reformat them to:

name: str
match member:
    case Member(nickname=str(name)):
        ...
    case Member(user=User(global_name=str(name))):
        ...

I was wondering if case-statements should also allowed to be single-line if the body is an Ellipsis ... for consistency?

@AlexWaygood AlexWaygood added the formatter Related to the formatter label Jun 1, 2024
@MichaReiser MichaReiser added the style How should formatted code look label Jun 1, 2024
@MichaReiser
Copy link
Member

I'm open to this but I don't think it should be specific to match cases. Instead, it is a more general question if ... bodies should be collapsed in compound statements.

@pfaion
Copy link
Author

pfaion commented Jun 1, 2024

What other cases would there be? Any statement ending with a :?

First brainstorm:

if expr: ...
elif expr: ...
else: ...

while expr: ...

for val in expr: ...

class Foo: ...

def foo(): ...
async def foo(): ...

match expr: ...
    case pattern: ...

try: ...
except: ...
finally: ...

with expr as val: ...
async with expr as val: ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
formatter Related to the formatter style How should formatted code look
Projects
None yet
Development

No branches or pull requests

3 participants