MCPcopy Index your code
hub / github.com/RustPython/RustPython / validate_pattern_match_value

Function validate_pattern_match_value

crates/vm/src/stdlib/_ast/validate.rs:127–150  ·  view source on GitHub ↗
(vm: &VirtualMachine, expr: &ast::Expr)

Source from the content-addressed store, hash-verified

125}
126
127fn validate_pattern_match_value(vm: &VirtualMachine, expr: &ast::Expr) -> PyResult<()> {
128 validate_expr(vm, expr, ast::ExprContext::Load)?;
129 match expr {
130 ast::Expr::NumberLiteral(_) | ast::Expr::StringLiteral(_) | ast::Expr::BytesLiteral(_) => {
131 Ok(())
132 }
133 ast::Expr::Attribute(_) => Ok(()),
134 ast::Expr::UnaryOp(op) => match &*op.operand {
135 ast::Expr::NumberLiteral(_) => Ok(()),
136 _ => Err(vm.new_value_error("patterns may only match literals and attribute lookups")),
137 },
138 ast::Expr::BinOp(bin) => match (&*bin.left, &*bin.right) {
139 (ast::Expr::NumberLiteral(_), ast::Expr::NumberLiteral(_)) => Ok(()),
140 _ => Err(vm.new_value_error("patterns may only match literals and attribute lookups")),
141 },
142 ast::Expr::FString(_) | ast::Expr::TString(_) => Ok(()),
143 ast::Expr::BooleanLiteral(_)
144 | ast::Expr::NoneLiteral(_)
145 | ast::Expr::EllipsisLiteral(_) => {
146 Err(vm.new_value_error("unexpected constant inside of a literal pattern"))
147 }
148 _ => Err(vm.new_value_error("patterns may only match literals and attribute lookups")),
149 }
150}
151
152fn validate_capture(vm: &VirtualMachine, name: &ast::Identifier) -> PyResult<()> {
153 if name.as_str() == "_" {

Callers 1

validate_patternFunction · 0.85

Calls 2

validate_exprFunction · 0.85
ErrClass · 0.50

Tested by

no test coverage detected