Skip to content

Comments

Introduce AST nodes for PatternMatchClass arguments#6881

Merged
charliermarsh merged 1 commit intomainfrom
charlie/pattern-ast
Aug 26, 2023
Merged

Introduce AST nodes for PatternMatchClass arguments#6881
charliermarsh merged 1 commit intomainfrom
charlie/pattern-ast

Conversation

@charliermarsh
Copy link
Member

Summary

This PR introduces two new AST nodes to improve the representation of PatternMatchClass. As a reminder, PatternMatchClass looks like this:

case Point2D(0, 0, x=1, y=2):
  ...

Historically, this was represented as a vector of patterns (for the 0, 0 portion) and parallel vectors of keyword names (for x and y) and values (for 1 and 2). This introduces a bunch of challenges for the formatter, but importantly, it's also really different from how we represent similar nodes, like arguments (func(0, 0, x=1, y=2)) or parameters (def func(x, y)).

So, firstly, we now use a single node (PatternArguments) for the entire parenthesized region, making it much more consistent with our other nodes. So, above, PatternArguments would be (0, 0, x=1, y=2).

Secondly, we now have a PatternKeyword node for x=1 and y=2. This is much more similar to the how Keyword is represented within Arguments for call expressions.

Closes #6866.

Closes #6880.

CommentPlacement::Default(comment)
}

CommentPlacement::Default(comment)
Copy link
Member Author

Choose a reason for hiding this comment

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

This gets dramatically simplified.

Copy link
Member

Choose a reason for hiding this comment

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

...dramatically 😆

Copy link
Member Author

Choose a reason for hiding this comment

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

Anything that deletes code is a "dramatic simplification" for me

use crate::FormatNodeRule;

#[derive(Default)]
pub struct FormatPatternArguments;
Copy link
Member Author

Choose a reason for hiding this comment

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

This is very, very similar to arguments.rs, but it's difficult to share much code.

}
}
OptionalParentheses::Never
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This specific logic is way simpler now -- we only care if there are dangling comments, not where they are, since these are dangling comments between Point2D and its arguments.

@charliermarsh
Copy link
Member Author

Adding these AST nodes does require a bunch of boilerplate, but I think it's worth it for the improved representation.

@charliermarsh charliermarsh added the internal An internal refactor or improvement label Aug 25, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Aug 25, 2023

PR Check Results

Ecosystem

✅ ecosystem check detected no changes.

Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

Can you verify that the following continues to parse successfully (maybe add a test?)

match a:
   case Point2D((0), 0, x=(1), y=2):
        ...

CommentPlacement::Default(comment)
}

CommentPlacement::Default(comment)
Copy link
Member

Choose a reason for hiding this comment

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

...dramatically 😆

@charliermarsh charliermarsh enabled auto-merge (squash) August 26, 2023 14:35
@charliermarsh charliermarsh merged commit 15b73bd into main Aug 26, 2023
@charliermarsh charliermarsh deleted the charlie/pattern-ast branch August 26, 2023 14:45
@codspeed-hq
Copy link

codspeed-hq bot commented Aug 26, 2023

CodSpeed Performance Report

Merging #6881 will create unknown performance changes

Comparing charlie/pattern-ast (3b31526) with main (ed1b412)

Summary

🆕 16 new benchmarks

Benchmarks breakdown

Benchmark main charlie/pattern-ast Change
🆕 formatter[numpy/globals.py] N/A 1.4 ms N/A
🆕 linter/default-rules[large/dataset.py] N/A 91.9 ms N/A
🆕 formatter[large/dataset.py] N/A 72.7 ms N/A
🆕 linter/default-rules[numpy/ctypeslib.py] N/A 18.7 ms N/A
🆕 linter/all-rules[numpy/globals.py] N/A 4 ms N/A
🆕 formatter[numpy/ctypeslib.py] N/A 13.9 ms N/A
🆕 linter/default-rules[pydantic/types.py] N/A 39 ms N/A
🆕 parser[numpy/globals.py] N/A 1.4 ms N/A
🆕 parser[numpy/ctypeslib.py] N/A 12.5 ms N/A
🆕 parser[pydantic/types.py] N/A 26.8 ms N/A
🆕 parser[large/dataset.py] N/A 68.8 ms N/A
🆕 linter/all-rules[pydantic/types.py] N/A 71.8 ms N/A
🆕 linter/default-rules[numpy/globals.py] N/A 2.2 ms N/A
🆕 linter/all-rules[numpy/ctypeslib.py] N/A 34.2 ms N/A
🆕 linter/all-rules[large/dataset.py] N/A 156.1 ms N/A
🆕 formatter[pydantic/types.py] N/A 26.3 ms N/A

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

Labels

internal An internal refactor or improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refine edge-case comment handling for PatternMatchClass handle_parenthesized_comment can't deal with certain kinds of match comments

2 participants