Skip to content
4 changes: 2 additions & 2 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -555,12 +555,12 @@ complex_number[expr_ty]:

signed_number[expr_ty]:
| NUMBER
| '+' number=NUMBER { number }
| '+' number=NUMBER { _PyAST_UnaryOp(UAdd, number, EXTRA) }
| '-' number=NUMBER { _PyAST_UnaryOp(USub, number, EXTRA) }

signed_real_number[expr_ty]:
| real_number
| '+' real=real_number { real }
| '+' real=real_number { _PyAST_UnaryOp(UAdd, real, EXTRA) }
| '-' real=real_number { _PyAST_UnaryOp(USub, real, EXTRA) }

real_number[expr_ty]:
Expand Down
18 changes: 18 additions & 0 deletions Lib/test/test_unparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,24 @@ def test_unparse_interactive_integrity_3(self):

self.check_src_roundtrip(src, out, mode='single')

@test.support.subTests('case', [
"case 'a string':",
"case True:",
"case _:",
"case [*_]:",
"case tuple():",
"case cls(arg, more=arg2):",
"case tuple() | list():",
"case [tuple() as obj, _, {'a': a, **more}]:",
"case -2:",
"case +2:",
"case 2 + 3j:",
"case +2 - 3j:",
])
def test_unparse_match(self, case):
src = 'match x:\n ' + case + '\n pass'
self.check_src_roundtrip(src)


class CosmeticTestCase(ASTTestCase):
"""Test if there are cosmetic issues caused by unnecessary additions"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix AST generation asymmetry where unary positive ``+`` was implicitly dropped in pattern match expressions, restoring parsing parity with unary negative ``-``.
22 changes: 20 additions & 2 deletions Parser/parser.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions Python/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -426,11 +426,11 @@ ensure_literal_number(expr_ty exp, bool allow_real, bool allow_imaginary)
}

static int
ensure_literal_negative(expr_ty exp, bool allow_real, bool allow_imaginary)
ensure_literal_signed(expr_ty exp, bool allow_real, bool allow_imaginary)
{
assert(exp->kind == UnaryOp_kind);
// Must be negation ...
if (exp->v.UnaryOp.op != USub) {
// Must be negation or positive ...
if (exp->v.UnaryOp.op != USub && exp->v.UnaryOp.op != UAdd) {
return 0;
}
// ... of a constant ...
Expand Down Expand Up @@ -461,7 +461,7 @@ ensure_literal_complex(expr_ty exp)
}
break;
case UnaryOp_kind:
if (!ensure_literal_negative(left, /*real=*/true, /*imaginary=*/false)) {
if (!ensure_literal_signed(left, /*real=*/true, /*imaginary=*/false)) {
return 0;
}
break;
Expand Down Expand Up @@ -512,9 +512,9 @@ validate_pattern_match_value(expr_ty exp)
// Constants and attribute lookups are always permitted
return 1;
case UnaryOp_kind:
// Negated numbers are permitted (whether real or imaginary)
// Signed numbers are permitted (whether real or imaginary)
// Compiler will complain if AST folding doesn't create a constant
if (ensure_literal_negative(exp, /*real=*/true, /*imaginary=*/true)) {
if (ensure_literal_signed(exp, /*real=*/true, /*imaginary=*/true)) {
return 1;
}
break;
Expand Down
4 changes: 2 additions & 2 deletions Python/ast_preprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -867,11 +867,11 @@ fold_const_match_patterns(expr_ty node, PyArena *ctx_, _PyASTPreprocessState *st
{
case UnaryOp_kind:
{
if (node->v.UnaryOp.op == USub &&
if ((node->v.UnaryOp.op == USub || node->v.UnaryOp.op == UAdd) &&
node->v.UnaryOp.operand->kind == Constant_kind)
{
PyObject *operand = node->v.UnaryOp.operand->v.Constant.value;
PyObject *folded = PyNumber_Negative(operand);
PyObject *folded = node->v.UnaryOp.op == USub ? PyNumber_Negative(operand) : PyNumber_Positive(operand);
return make_const(node, folded, ctx_);
}
break;
Expand Down
Loading