Auto-generate AST boilerplate (#15544)

This PR replaces most of the hard-coded AST definitions with a
generation script, similar to what happens in `rust_python_formatter`.
I've replaced every "rote" definition that I could find, where the
content is entirely boilerplate and only depends on what syntax nodes
there are and which groups they belong to.

This is a pretty massive diff, but it's entirely a refactoring. It
should make absolutely no changes to the API or implementation. In
particular, this required adding some configuration knobs that let us
override default auto-generated names where they don't line up with
types that we created previously by hand.

## Test plan

There should be no changes outside of the `rust_python_ast` crate, which
verifies that there were no API changes as a result of the
auto-generation. Aggressive `cargo clippy` and `uvx pre-commit` runs
after each commit in the branch.

---------

Co-authored-by: Micha Reiser <micha@reiser.io>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
Douglas Creager 2025-01-17 14:23:02 -05:00 committed by GitHub
parent 4351d85d24
commit 8e3633f55a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 10074 additions and 8513 deletions

View file

@ -24,7 +24,7 @@ nodes_file = (
root.joinpath("crates")
.joinpath("ruff_python_ast")
.joinpath("src")
.joinpath("node.rs")
.joinpath("generated.rs")
.read_text()
)
node_lines = (

View file

@ -2392,6 +2392,114 @@ impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::PatternMatchOr {
}
}
impl FormatRule<ast::TypeParamTypeVar, PyFormatContext<'_>>
for crate::type_param::type_param_type_var::FormatTypeParamTypeVar
{
#[inline]
fn fmt(&self, node: &ast::TypeParamTypeVar, f: &mut PyFormatter) -> FormatResult<()> {
FormatNodeRule::<ast::TypeParamTypeVar>::fmt(self, node, f)
}
}
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::TypeParamTypeVar {
type Format<'a> = FormatRefWithRule<
'a,
ast::TypeParamTypeVar,
crate::type_param::type_param_type_var::FormatTypeParamTypeVar,
PyFormatContext<'ast>,
>;
fn format(&self) -> Self::Format<'_> {
FormatRefWithRule::new(
self,
crate::type_param::type_param_type_var::FormatTypeParamTypeVar::default(),
)
}
}
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::TypeParamTypeVar {
type Format = FormatOwnedWithRule<
ast::TypeParamTypeVar,
crate::type_param::type_param_type_var::FormatTypeParamTypeVar,
PyFormatContext<'ast>,
>;
fn into_format(self) -> Self::Format {
FormatOwnedWithRule::new(
self,
crate::type_param::type_param_type_var::FormatTypeParamTypeVar::default(),
)
}
}
impl FormatRule<ast::TypeParamTypeVarTuple, PyFormatContext<'_>>
for crate::type_param::type_param_type_var_tuple::FormatTypeParamTypeVarTuple
{
#[inline]
fn fmt(&self, node: &ast::TypeParamTypeVarTuple, f: &mut PyFormatter) -> FormatResult<()> {
FormatNodeRule::<ast::TypeParamTypeVarTuple>::fmt(self, node, f)
}
}
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::TypeParamTypeVarTuple {
type Format<'a> = FormatRefWithRule<
'a,
ast::TypeParamTypeVarTuple,
crate::type_param::type_param_type_var_tuple::FormatTypeParamTypeVarTuple,
PyFormatContext<'ast>,
>;
fn format(&self) -> Self::Format<'_> {
FormatRefWithRule::new(
self,
crate::type_param::type_param_type_var_tuple::FormatTypeParamTypeVarTuple::default(),
)
}
}
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::TypeParamTypeVarTuple {
type Format = FormatOwnedWithRule<
ast::TypeParamTypeVarTuple,
crate::type_param::type_param_type_var_tuple::FormatTypeParamTypeVarTuple,
PyFormatContext<'ast>,
>;
fn into_format(self) -> Self::Format {
FormatOwnedWithRule::new(
self,
crate::type_param::type_param_type_var_tuple::FormatTypeParamTypeVarTuple::default(),
)
}
}
impl FormatRule<ast::TypeParamParamSpec, PyFormatContext<'_>>
for crate::type_param::type_param_param_spec::FormatTypeParamParamSpec
{
#[inline]
fn fmt(&self, node: &ast::TypeParamParamSpec, f: &mut PyFormatter) -> FormatResult<()> {
FormatNodeRule::<ast::TypeParamParamSpec>::fmt(self, node, f)
}
}
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::TypeParamParamSpec {
type Format<'a> = FormatRefWithRule<
'a,
ast::TypeParamParamSpec,
crate::type_param::type_param_param_spec::FormatTypeParamParamSpec,
PyFormatContext<'ast>,
>;
fn format(&self) -> Self::Format<'_> {
FormatRefWithRule::new(
self,
crate::type_param::type_param_param_spec::FormatTypeParamParamSpec::default(),
)
}
}
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::TypeParamParamSpec {
type Format = FormatOwnedWithRule<
ast::TypeParamParamSpec,
crate::type_param::type_param_param_spec::FormatTypeParamParamSpec,
PyFormatContext<'ast>,
>;
fn into_format(self) -> Self::Format {
FormatOwnedWithRule::new(
self,
crate::type_param::type_param_param_spec::FormatTypeParamParamSpec::default(),
)
}
}
impl FormatRule<ast::PatternArguments, PyFormatContext<'_>>
for crate::pattern::pattern_arguments::FormatPatternArguments
{
@ -2827,114 +2935,6 @@ impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::TypeParams {
}
}
impl FormatRule<ast::TypeParamTypeVar, PyFormatContext<'_>>
for crate::type_param::type_param_type_var::FormatTypeParamTypeVar
{
#[inline]
fn fmt(&self, node: &ast::TypeParamTypeVar, f: &mut PyFormatter) -> FormatResult<()> {
FormatNodeRule::<ast::TypeParamTypeVar>::fmt(self, node, f)
}
}
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::TypeParamTypeVar {
type Format<'a> = FormatRefWithRule<
'a,
ast::TypeParamTypeVar,
crate::type_param::type_param_type_var::FormatTypeParamTypeVar,
PyFormatContext<'ast>,
>;
fn format(&self) -> Self::Format<'_> {
FormatRefWithRule::new(
self,
crate::type_param::type_param_type_var::FormatTypeParamTypeVar::default(),
)
}
}
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::TypeParamTypeVar {
type Format = FormatOwnedWithRule<
ast::TypeParamTypeVar,
crate::type_param::type_param_type_var::FormatTypeParamTypeVar,
PyFormatContext<'ast>,
>;
fn into_format(self) -> Self::Format {
FormatOwnedWithRule::new(
self,
crate::type_param::type_param_type_var::FormatTypeParamTypeVar::default(),
)
}
}
impl FormatRule<ast::TypeParamTypeVarTuple, PyFormatContext<'_>>
for crate::type_param::type_param_type_var_tuple::FormatTypeParamTypeVarTuple
{
#[inline]
fn fmt(&self, node: &ast::TypeParamTypeVarTuple, f: &mut PyFormatter) -> FormatResult<()> {
FormatNodeRule::<ast::TypeParamTypeVarTuple>::fmt(self, node, f)
}
}
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::TypeParamTypeVarTuple {
type Format<'a> = FormatRefWithRule<
'a,
ast::TypeParamTypeVarTuple,
crate::type_param::type_param_type_var_tuple::FormatTypeParamTypeVarTuple,
PyFormatContext<'ast>,
>;
fn format(&self) -> Self::Format<'_> {
FormatRefWithRule::new(
self,
crate::type_param::type_param_type_var_tuple::FormatTypeParamTypeVarTuple::default(),
)
}
}
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::TypeParamTypeVarTuple {
type Format = FormatOwnedWithRule<
ast::TypeParamTypeVarTuple,
crate::type_param::type_param_type_var_tuple::FormatTypeParamTypeVarTuple,
PyFormatContext<'ast>,
>;
fn into_format(self) -> Self::Format {
FormatOwnedWithRule::new(
self,
crate::type_param::type_param_type_var_tuple::FormatTypeParamTypeVarTuple::default(),
)
}
}
impl FormatRule<ast::TypeParamParamSpec, PyFormatContext<'_>>
for crate::type_param::type_param_param_spec::FormatTypeParamParamSpec
{
#[inline]
fn fmt(&self, node: &ast::TypeParamParamSpec, f: &mut PyFormatter) -> FormatResult<()> {
FormatNodeRule::<ast::TypeParamParamSpec>::fmt(self, node, f)
}
}
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::TypeParamParamSpec {
type Format<'a> = FormatRefWithRule<
'a,
ast::TypeParamParamSpec,
crate::type_param::type_param_param_spec::FormatTypeParamParamSpec,
PyFormatContext<'ast>,
>;
fn format(&self) -> Self::Format<'_> {
FormatRefWithRule::new(
self,
crate::type_param::type_param_param_spec::FormatTypeParamParamSpec::default(),
)
}
}
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::TypeParamParamSpec {
type Format = FormatOwnedWithRule<
ast::TypeParamParamSpec,
crate::type_param::type_param_param_spec::FormatTypeParamParamSpec,
PyFormatContext<'ast>,
>;
fn into_format(self) -> Self::Format {
FormatOwnedWithRule::new(
self,
crate::type_param::type_param_param_spec::FormatTypeParamParamSpec::default(),
)
}
}
impl FormatRule<ast::FString, PyFormatContext<'_>> for crate::other::f_string::FormatFString {
#[inline]
fn fmt(&self, node: &ast::FString, f: &mut PyFormatter) -> FormatResult<()> {