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

Formatting instability in async call chain #8644

Closed
roshanjrajan-zip opened this issue Nov 13, 2023 · 3 comments
Closed

Formatting instability in async call chain #8644

roshanjrajan-zip opened this issue Nov 13, 2023 · 3 comments
Labels
bug Something isn't working formatter Related to the formatter

Comments

@roshanjrajan-zip
Copy link

roshanjrajan-zip commented Nov 13, 2023

I don't know if this is expected but I am getting an error when formatting the following code. I would expect Ruff to come to a solution and not have differences when running with --no-cache or not.

Ruff Settings: Nothing specific for format but have line-length set to 88.
Ruff Version: v0.1.4/0.1.5

Steps to reproduce:

  1. Create file called test.py with the following code
# test.py
def func():
    test_data = await (
        Stream.from_async(async_data.data())
        .flat_map_async(lambda data: data.async_data())
        .map(lambda data: data.data())
        .filter_async(is_valid_data)
        .to_list()
    )
  1. Call ruff format test.py --isolated . Can call this repeatedly and it stays the same as the following.
def func():
    test_data = await Stream.from_async(async_data.data()).flat_map_async(
        lambda data: data.async_data()
    ).map(lambda data: data.data()).filter_async(is_valid_data).to_list()
  1. Now if I run this code in CI where it runs the github action on v0.1.4 with format check it fails. When I call ruff format test.py --isolated --no-cache it formats the code to the following.
def func():
    test_data = (
        await Stream.from_async(async_data.data())
        .flat_map_async(lambda data: data.async_data())
        .map(lambda data: data.data())
        .filter_async(is_valid_data)
        .to_list()
    )

This is stable and all calls to ruff format w/o the --no-cache flag do not reformat the file. This also allows it to pass in CI with the format check. Any help/pointers on how to make sure this is stable is greatly appreciated!

@roshanjrajan-zip roshanjrajan-zip changed the title Formatter: Using no_cache formats code differently which impacts formatting on other machines/CI Formatter: Using --no-cache formats code differently which impacts formatting on other machines/CI Nov 13, 2023
@charliermarsh
Copy link
Member

I think the issue is that there's an instability in the formatting:

# test.py
def func():
    test_data = await (
        Stream.from_async(async_data.data())
        .flat_map_async(lambda data: data.async_data())
        .map(lambda data: data.data())
        .filter_async(is_valid_data)
        .to_list()
    )

# Format once...
def func():
    test_data = await Stream.from_async(async_data.data()).flat_map_async(
        lambda data: data.async_data()
    ).map(lambda data: data.data()).filter_async(is_valid_data).to_list()

# Format twice...
def func():
    test_data = (
        await Stream.from_async(async_data.data())
        .flat_map_async(lambda data: data.async_data())
        .map(lambda data: data.data())
        .filter_async(is_valid_data)
        .to_list()
    )

It's just a bug in the formatter. Will need to ensure it's fixed for the next release. Thanks!

(If you want a workaround, I'd suggest just applying that final formatting, which Ruff will retain.)

@charliermarsh charliermarsh added bug Something isn't working formatter Related to the formatter labels Nov 13, 2023
@roshanjrajan-zip
Copy link
Author

Will do and thanks for looking into this! Really appreciate the great work you are doing! 🔥

@charliermarsh charliermarsh changed the title Formatter: Using --no-cache formats code differently which impacts formatting on other machines/CI Formatting instability in async call chain Nov 13, 2023
@charliermarsh charliermarsh changed the title Formatting instability in async call chain Formatting instability in async call chain Nov 13, 2023
@charliermarsh
Copy link
Member

Closed by #8676.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working formatter Related to the formatter
Projects
None yet
Development

No branches or pull requests

2 participants