-
Notifications
You must be signed in to change notification settings - Fork 974
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
New Singleton
enum for PatternMatchSingleton
node
#8063
Conversation
Current dependencies on/for this PR:
This stack of pull requests is managed by Graphite. |
71c6d5a
to
2d4925c
Compare
07dce4c
to
db9d767
Compare
#[inline] | ||
fn visit_singleton(&mut self, _singleton: &'a Singleton) {} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this should be defined 🤔
PR Check ResultsEcosystem✅ ecosystem check detected no linter changes. ✅ ecosystem check detected no format changes. |
@@ -326,6 +326,23 @@ impl<'a> From<&'a ast::Decorator> for ComparableDecorator<'a> { | |||
} | |||
} | |||
|
|||
#[derive(Debug, PartialEq, Eq, Hash)] | |||
pub enum ComparableSingleton { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I find Singleton
a confusing name. I assume it comes from Python's AST definition?
Or should this match the literal case? https://peps.python.org/pep-0622/#literal-patterns
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume it comes from Python's AST definition?
Yes, and in turn ours as both uses the "Singleton" suffix.
It is for the literal pattern but only a subset of it - None
, True
, False
. The string and number literal are represented using PatternMatchValue
while the singletons are using PatternMatchSingleton
.
@@ -3139,7 +3139,7 @@ impl AstNode for ast::PatternMatchSingleton { | |||
V: PreorderVisitor<'a> + ?Sized, | |||
{ | |||
let ast::PatternMatchSingleton { value, range: _ } = self; | |||
visitor.visit_constant(value); | |||
visitor.visit_singleton(value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about constant's in other positions? e.g. do we call this function when visiting a ExprConstant
too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we do:
ruff/crates/ruff_python_ast/src/node.rs
Lines 2681 to 2687 in db9d767
fn visit_preorder<'a, V>(&'a self, visitor: &mut V) | |
where | |
V: PreorderVisitor<'a> + ?Sized, | |
{ | |
let ast::ExprConstant { value, range: _ } = self; | |
visitor.visit_constant(value); | |
} |
I'm not sure if there should be a visit_singleton
or not. Do we define visitor methods for the enum values as well? Wouldn't they be accounted for when visiting the enclosing node? Looking at Constant
, we do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure. I guess it can be useful when the same non-nodes may have different parent nodes but I would be okay only having visitors for nodes. @charliermarsh should have more context on why visit_constant
exists.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a huge fan of the Singleton
terminology because it only makes me thing about the singleton pattern but it is in line with Python's terminology. It also seem to make sense from a technical perspective (I didn't know that) because None
, True
, and False
are indeed all singleton values 🤯
The one alternative I could think of is a KeywordLiteral
.
db9d767
to
666eb09
Compare
Ah yes, I should've written that in the PR description.
This might be confusing as other keywords are not part of it. |
666eb09
to
9321710
Compare
Summary
This PR adds a new
Singleton
enum for thePatternMatchSingleton
node.Earlier the node was using the
Constant
enum but the value for this pattern can only be eitherNone
,True
orFalse
. With the coming PR to remove theConstant
, this node required a new type to fill in.This also has the benefit of narrowing the type down to only the possible values for the node as evident by the removal of
unreachable
.Test Plan
Update the AST snapshots and run
cargo test
.