New Singleton enum for PatternMatchSingleton node (#8063)

## 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`.
This commit is contained in:
Dhruv Manilawala 2023-10-30 11:18:53 +05:30 committed by GitHub
parent ee7d445ef5
commit 78bbf6d403
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 70 additions and 28 deletions

View file

@ -5,8 +5,8 @@ use std::ops::Deref;
use ruff_python_ast::{
self as ast, Alias, ArgOrKeyword, BoolOp, CmpOp, Comprehension, Constant, ConversionFlag,
DebugText, ExceptHandler, Expr, Identifier, MatchCase, Operator, Parameter, Parameters,
Pattern, Stmt, Suite, TypeParam, TypeParamParamSpec, TypeParamTypeVar, TypeParamTypeVarTuple,
WithItem,
Pattern, Singleton, Stmt, Suite, TypeParam, TypeParamParamSpec, TypeParamTypeVar,
TypeParamTypeVarTuple, WithItem,
};
use ruff_python_ast::{ParameterWithDefault, TypeParams};
use ruff_python_literal::escape::{AsciiEscape, Escape, UnicodeEscape};
@ -672,7 +672,7 @@ impl<'a> Generator<'a> {
self.unparse_expr(value, precedence::MAX);
}
Pattern::MatchSingleton(ast::PatternMatchSingleton { value, range: _ }) => {
self.unparse_constant(value);
self.unparse_singleton(value);
}
Pattern::MatchSequence(ast::PatternMatchSequence { patterns, range: _ }) => {
self.p("[");
@ -1166,6 +1166,14 @@ impl<'a> Generator<'a> {
}
}
pub(crate) fn unparse_singleton(&mut self, singleton: &Singleton) {
match singleton {
Singleton::None => self.p("None"),
Singleton::True => self.p("True"),
Singleton::False => self.p("False"),
}
}
pub(crate) fn unparse_constant(&mut self, constant: &Constant) {
assert_eq!(f64::MAX_10_EXP, 308);
let inf_str = "1e309";