File tree Expand file tree Collapse file tree
Misc/NEWS.d/next/Core_and_Builtins Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -536,6 +536,7 @@ literal_pattern[pattern_ty]:
536536 | 'None' { _PyAST_MatchSingleton(Py_None, EXTRA) }
537537 | 'True' { _PyAST_MatchSingleton(Py_True, EXTRA) }
538538 | 'False' { _PyAST_MatchSingleton(Py_False, EXTRA) }
539+ | invalid_literal_pattern
539540
540541# Literal expressions are used to restrict permitted mapping pattern keys
541542literal_expr[expr_ty]:
@@ -1519,6 +1520,8 @@ invalid_mapping_pattern:
15191520 "double star pattern must be the last (right-most) subpattern in the mapping pattern") }
15201521invalid_class_argument_pattern[asdl_pattern_seq*]:
15211522 | [positional_patterns ','] keyword_patterns ',' a=positional_patterns { a }
1523+ invalid_literal_pattern:
1524+ | a='+' NUMBER { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use unary '+' in a literal pattern") }
15221525invalid_if_stmt:
15231526 | 'if' named_expression NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
15241527 | a='if' a=named_expression ':' NEWLINE !INDENT {
Original file line number Diff line number Diff line change @@ -3230,6 +3230,41 @@ def test_mapping_pattern_duplicate_key_edge_case3(self):
32303230 pass
32313231 """ )
32323232
3233+ def test_unary_add_in_literal_pattern (self ):
3234+ self .assert_syntax_error ("""
3235+ match ...:
3236+ case +1:
3237+ pass
3238+ """ )
3239+
3240+ def test_unary_add_in_or_pattern (self ):
3241+ self .assert_syntax_error ("""
3242+ match ...:
3243+ case 1 | +2 | -3:
3244+ pass
3245+ """ )
3246+
3247+ def test_unary_add_in_sequence_pattern (self ):
3248+ self .assert_syntax_error ("""
3249+ match ...:
3250+ case [1, +2, -3]:
3251+ pass
3252+ """ )
3253+
3254+ def test_unary_add_in_class_pattern (self ):
3255+ self .assert_syntax_error ("""
3256+ match ...:
3257+ case Foo(x=+1, y=-2):
3258+ pass
3259+ """ )
3260+
3261+ def test_unary_add_in_mapping_pattern (self ):
3262+ self .assert_syntax_error ("""
3263+ match ...:
3264+ case {True: +1, False: -2}:
3265+ pass
3266+ """ )
3267+
32333268class TestTypeErrors (unittest .TestCase ):
32343269
32353270 def test_accepts_positional_subpatterns_0 (self ):
Original file line number Diff line number Diff line change 23992399 Traceback (most recent call last):
24002400 SyntaxError: double star pattern must be the last (right-most) subpattern in the mapping pattern
24012401
2402+ Unary '+' is not allowed in match patterns:
2403+
2404+ >>> match ...:
2405+ ... case +1:
2406+ ... ...
2407+ Traceback (most recent call last):
2408+ SyntaxError: cannot use unary '+' in a literal pattern
2409+
2410+ >>> match ...:
2411+ ... case 1 | +2 | -3:
2412+ ... ...
2413+ Traceback (most recent call last):
2414+ SyntaxError: cannot use unary '+' in a literal pattern
2415+
2416+ >>> match ...:
2417+ ... case [1, +2, -3]:
2418+ ... ...
2419+ Traceback (most recent call last):
2420+ SyntaxError: cannot use unary '+' in a literal pattern
2421+
2422+ >>> match ...:
2423+ ... case Foo(x=+1, y=-2):
2424+ ... ...
2425+ Traceback (most recent call last):
2426+ SyntaxError: cannot use unary '+' in a literal pattern
2427+
2428+ >>> match ...:
2429+ ... case {True: +1, False: -2}:
2430+ ... ...
2431+ Traceback (most recent call last):
2432+ SyntaxError: cannot use unary '+' in a literal pattern
2433+
24022434Uses of the star operator which should fail:
24032435
24042436A[:*b]
Original file line number Diff line number Diff line change 1+ Improved the error message when using unary ``+ `` in a :keyword: `match `
2+ pattern. Instead of a generic "invalid syntax", Python now reports "cannot
3+ use unary '+' in a literal pattern".
You can’t perform that action at this time.
0 commit comments