Skip to content

Compare formatted and unformatted ASTs during formatter tests - #8624

Merged
charliermarsh merged 2 commits into
mainfrom
charlie/norm
Nov 13, 2023
Merged

charliermarsh merged 2 commits into
mainfrom
charlie/norm

Conversation

@charliermarsh

@charliermarsh charliermarsh commented Nov 12, 2023 •

Copy link
Copy Markdown
Member

Summary

This PR implements validation in the formatter tests to ensure that we don't modify the AST during formatting. Black has similar logic.

In implementing this, I learned that Black actually does modify the AST, and their test infrastructure normalizes the AST to wipe away those differences. Specifically, Black changes the indentation of docstrings, which does modify the AST; and it also inserts parentheses in del statements, which changes the AST too.

Ruff also does both these things, so we also implement the same normalization using a new visitor that allows for modifying the AST.

Closes #8184.

Test Plan

cargo test

body: (&expr.body).into(),
}
}
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(These aren't necessary, but I made the changes before learning that we need to normalize the AST, figured they're worth having anyway.)

format_module_source(&content, options.clone()).expect("Formatting to succeed");
let formatted = printed.as_code();

ensure_stability_when_formatting_twice(formatted, options.clone(), input_path);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not clear to me why we're doing this, we do this exact thing immediately before the if-else.

@github-actions

github-actions Bot commented Nov 12, 2023 •

Copy link
Copy Markdown
Contributor

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@charliermarsh charliermarsh added the formatter Related to the Ruff formatter label Nov 12, 2023

@konstin konstin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have to introduce a new Normalized AST specifically for formatting, which is the main downside here (ongoing maintenance whenever we modify the AST).

That's a lot of duplicated code/work/analysis. Could we avoid avoid that by using an AST visitor that applies these change to the cloned AST nodes and then uses the regular comparable AST?

.header("Unformatted", "Formatted")
.to_string();
panic!(
r#"Reformatting the unformatted code of {} resulted in AST changes.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
r#"Reformatting the unformatted code of {} resulted in AST changes.
r#"Reformatting the unformatted code of {} resulted in AST changes. Please open an issue at https://github.com/astral-sh/ruff/issues/new with your configuration and the failing code snippet

or something along the lines

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine to omit since this is in tests only.

@charliermarsh

Copy link
Copy Markdown
Member Author

I think that would require similar boilerplate, would it not? Since we'd need a visitor that's allowed to mutate the AST nodes (which doesn't currently exist).

@konstin

konstin commented Nov 13, 2023

Copy link
Copy Markdown
Member

That's true, the mutability ruins this

@charliermarsh

Copy link
Copy Markdown
Member Author

I could do it but we’d need a new visitor, I think. I can at least try.

@charliermarsh
charliermarsh enabled auto-merge (squash) November 13, 2023 17:37
@charliermarsh
charliermarsh merged commit d574fcd into main Nov 13, 2023
@charliermarsh
charliermarsh deleted the charlie/norm branch November 13, 2023 17:43
WithItem,
};

/// A trait for transforming ASTs. Visits all nodes in the AST recursively in evaluation-order.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it important that this modifies the tree in evaluation order or could we use pre-order instead (I was very surprised when I learned that our default visitor visits in evaluation and not pre-order)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

formatter Related to the Ruff formatter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Does the Ruff formatter validate the final AST in a similar manner to Black?

3 participants