Skip to content

Comments

New Singleton enum for PatternMatchSingleton node#8063

Merged
dhruvmanila merged 1 commit intomainfrom
dhruv/match-pattern-singleton
Oct 30, 2023
Merged

New Singleton enum for PatternMatchSingleton node#8063
dhruvmanila merged 1 commit intomainfrom
dhruv/match-pattern-singleton

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.

}

#[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

{
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 dhruvmanila force-pushed the dhruv/match-pattern-singleton branch from db9d767 to 666eb09 Compare October 20, 2023 10:46
@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.

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.

2 participants