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

New Singleton enum for PatternMatchSingleton node #8063

Merged
merged 1 commit into from
Oct 30, 2023

Conversation

dhruvmanila
Copy link
Member

@dhruvmanila dhruvmanila commented Oct 19, 2023

Summary

This PR adds a new Singleton enum for the PatternMatchSingleton node.

Earlier the node was using the Constant enum but the value for this pattern can only be either None, True or False. With the coming PR to remove the Constant, 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.

Base automatically changed from dhruv/tuple-constant to main October 19, 2023 17:50
@dhruvmanila dhruvmanila force-pushed the dhruv/match-pattern-singleton branch 2 times, most recently from 07dce4c to db9d767 Compare October 19, 2023 19:27
@dhruvmanila dhruvmanila added the internal An internal refactor or improvement label Oct 19, 2023
Comment on lines +47 to +49
#[inline]
fn visit_singleton(&mut self, _singleton: &'a Singleton) {}

Copy link
Member Author

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 🤔

@dhruvmanila dhruvmanila marked this pull request as ready for review October 19, 2023 19:39
@github-actions
Copy link
Contributor

github-actions bot commented Oct 19, 2023

PR Check Results

Ecosystem

✅ 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 {
Copy link
Member

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

Copy link
Member Author

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.

https://play.ruff.rs/2834cfa6-4d0d-4aaf-bbd2-1bd722fdd2ba

@@ -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);
Copy link
Member

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?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, we do:

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.

Copy link
Member

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.

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.

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.

@dhruvmanila
Copy link
Member Author

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 🤯

Ah yes, I should've written that in the PR description.

The one alternative I could think of is a KeywordLiteral.

This might be confusing as other keywords are not part of it.

@dhruvmanila dhruvmanila reopened this Oct 30, 2023
@dhruvmanila dhruvmanila enabled auto-merge (squash) October 30, 2023 05:42
@dhruvmanila dhruvmanila merged commit 78bbf6d into main Oct 30, 2023
31 of 54 checks passed
@dhruvmanila dhruvmanila deleted the dhruv/match-pattern-singleton branch October 30, 2023 05:48
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.

None yet

2 participants