Mailing List Archive

[issue44368] Invalid mapping patterns give confusing SyntaxErrors
New submission from Brandt Bucher <brandtbucher@gmail.com>:

Here are a few that I found. Not sure when they were introduced:

match ...:
case {**rest, "key": value}:
pass

match ...:
case {"first": first, **rest, "last": last}:
pass

match ...:
case {**_}:
pass

These all give the following error while parsing the second line:

File "<stdin>", line 1
match ...:
^^^^^^^^^
SyntaxError: invalid syntax. Perhaps you forgot a comma?

----------
components: Parser
messages: 395453
nosy: brandtbucher, lys.nikolaou, pablogsal
priority: normal
severity: normal
status: open
title: Invalid mapping patterns give confusing SyntaxErrors
type: behavior
versions: Python 3.11

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue44368>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue44368] Invalid mapping patterns give confusing SyntaxErrors [ In reply to ]
Brandt Bucher <brandtbucher@gmail.com> added the comment:

Perhaps we need something like invalid_mapping_pattern or invalid_mapping_pattern_double_star rules?

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue44368>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue44368] Invalid mapping patterns give confusing SyntaxErrors [ In reply to ]
Pablo Galindo Salgado <pablogsal@gmail.com> added the comment:

Probably, otherwise is going to hurt because the syntax errors that you describe trigger when two names are place together, which in general is a missing comma.

The error message also doesn't say: "you are missing a comma" it says that the most typical reason is a missing comma, which is important to distinguish :)

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue44368>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue44368] Invalid mapping patterns give confusing SyntaxErrors [ In reply to ]
Pablo Galindo Salgado <pablogsal@gmail.com> added the comment:

The backtracking with the soft keyword may make this very annoying, by the way.

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue44368>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue44368] Invalid mapping patterns give confusing SyntaxErrors [ In reply to ]
Pablo Galindo Salgado <pablogsal@gmail.com> added the comment:

Oh, wait, I think I misunderstood the problem. The problem is that the parser is backtracking and identifying match as a name.

Indeed, the soft keyword is a pain :(

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue44368>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue44368] Invalid mapping patterns give confusing SyntaxErrors [ In reply to ]
Pablo Galindo Salgado <pablogsal@gmail.com> added the comment:

I think it actually will be very useful to explain that these cases are invalid in the Syntax error, which will also solve this problem.

This on the other hand shows a bigger problem: any generic syntax error that happens inside "match" will probably end identifying the keyword as a name, even if we don't have specific errors, the parser will point to it and complain about "match".

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue44368>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue44368] Invalid mapping patterns give confusing SyntaxErrors [ In reply to ]
Pablo Galindo Salgado <pablogsal@gmail.com> added the comment:

Oh, turns out I already added machinery to solved this but I was missing a piece!

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue44368>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue44368] Invalid mapping patterns give confusing SyntaxErrors [ In reply to ]
Change by Pablo Galindo Salgado <pablogsal@gmail.com>:


----------
keywords: +patch
pull_requests: +25216
stage: -> patch review
pull_request: https://github.com/python/cpython/pull/26630

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue44368>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue44368] Invalid mapping patterns give confusing SyntaxErrors [ In reply to ]
Brandt Bucher <brandtbucher@gmail.com> added the comment:

Wow, that was quite a roller-coaster ride. Thanks Pablo. :)

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue44368>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue44368] Invalid mapping patterns give confusing SyntaxErrors [ In reply to ]
Brandt Bucher <brandtbucher@gmail.com> added the comment:

I found a similar one, by the way (not related to mapping patterns):

match ...:
case 42 as _:
pass

File "<stdin>", line 2
case 42 as _:
^
SyntaxError: expected ':'

Is this covered by your fix?

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue44368>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue44368] Invalid mapping patterns give confusing SyntaxErrors [ In reply to ]
Pablo Galindo Salgado <pablogsal@gmail.com> added the comment:

> Is this covered by your fix?

No, that is not a backtracking error (is not going all the way to the "match" expression).

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue44368>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue44368] Invalid mapping patterns give confusing SyntaxErrors [ In reply to ]
Pablo Galindo Salgado <pablogsal@gmail.com> added the comment:

I will fix that one in another PR

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue44368>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue44368] Invalid mapping patterns give confusing SyntaxErrors [ In reply to ]
Change by miss-islington <mariatta.wijaya+miss-islington@gmail.com>:


----------
nosy: +miss-islington
nosy_count: 3.0 -> 4.0
pull_requests: +25217
pull_request: https://github.com/python/cpython/pull/26631

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue44368>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue44368] Invalid mapping patterns give confusing SyntaxErrors [ In reply to ]
Pablo Galindo Salgado <pablogsal@gmail.com> added the comment:


New changeset 457ce60fc70f1c9290023f46fb82b6a490dff32e by Pablo Galindo in branch 'main':
bpo-44368: Ensure we don't raise incorrect custom syntax errors with soft keywords (GH-26630)
https://github.com/python/cpython/commit/457ce60fc70f1c9290023f46fb82b6a490dff32e


----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue44368>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue44368] Invalid mapping patterns give confusing SyntaxErrors [ In reply to ]
Pablo Galindo Salgado <pablogsal@gmail.com> added the comment:

Oh, this one is actually correct:

match ...:
case 42 as _:
pass

File "<stdin>", line 2
case 42 as _:
^
SyntaxError: expected ':'

That is literally expecting a ":" and that's the error. It has identified "case 42" correctly and it now expects a ":". Is just that the error marker is wrong because it has reached the "as and _" as part of the parsing so the error is there.

That is going to be a bit tricky to "fix"

----------
nosy: -miss-islington

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue44368>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue44368] Invalid mapping patterns give confusing SyntaxErrors [ In reply to ]
Brandt Bucher <brandtbucher@gmail.com> added the comment:

Could we just try parsing "as _" and raise if so? That wouldn't conflict with any existing rules, and that way we could actually have a helpful error message.

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue44368>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue44368] Invalid mapping patterns give confusing SyntaxErrors [ In reply to ]
Brandt Bucher <brandtbucher@gmail.com> added the comment:

Like "SyntaxError: can't capture into a wildcard (consider removing)".

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue44368>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue44368] Invalid mapping patterns give confusing SyntaxErrors [ In reply to ]
miss-islington <mariatta.wijaya+miss-islington@gmail.com> added the comment:


New changeset f807a4fad4da8f629ea7fe1f00719e817c77b63c by Miss Islington (bot) in branch '3.10':
bpo-44368: Ensure we don't raise incorrect custom syntax errors with soft keywords (GH-26630)
https://github.com/python/cpython/commit/f807a4fad4da8f629ea7fe1f00719e817c77b63c


----------
nosy: +miss-islington

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue44368>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue44368] Invalid mapping patterns give confusing SyntaxErrors [ In reply to ]
Pablo Galindo Salgado <pablogsal@gmail.com> added the comment:

> Could we just try parsing "as _" and raise if so? That wouldn't conflict with any existing rules, and that way we could actually have a helpful error message.

No, the same happens with other targets such as:

File "/home/pablogsal/github/python/master/lel.py", line 2
case 42 as 1+1:
^
SyntaxError: expected ':'

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue44368>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue44368] Invalid mapping patterns give confusing SyntaxErrors [ In reply to ]
Change by Pablo Galindo Salgado <pablogsal@gmail.com>:


----------
pull_requests: +25218
pull_request: https://github.com/python/cpython/pull/26632

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue44368>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue44368] Invalid mapping patterns give confusing SyntaxErrors [ In reply to ]
Pablo Galindo Salgado <pablogsal@gmail.com> added the comment:

Checkout PR 26632, and see if this works for you

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue44368>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue44368] Invalid mapping patterns give confusing SyntaxErrors [ In reply to ]
Pablo Galindo Salgado <pablogsal@gmail.com> added the comment:


New changeset 05073036dcecefc00b0c3e7397601809da41e2f1 by Pablo Galindo in branch 'main':
bpo-44368: Improve syntax errors with invalid as pattern targets (GH-26632)
https://github.com/python/cpython/commit/05073036dcecefc00b0c3e7397601809da41e2f1


----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue44368>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue44368] Invalid mapping patterns give confusing SyntaxErrors [ In reply to ]
Change by Pablo Galindo Salgado <pablogsal@gmail.com>:


----------
pull_requests: +25376
pull_request: https://github.com/python/cpython/pull/26792

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue44368>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue44368] Invalid mapping patterns give confusing SyntaxErrors [ In reply to ]
Pablo Galindo Salgado <pablogsal@gmail.com> added the comment:


New changeset a8c418d5ed1103106db547aa576b82c6031985a4 by Pablo Galindo in branch '3.10':
[3.10] bpo-44368: Improve syntax errors with invalid as pattern targets (GH-26632) (GH-26792)
https://github.com/python/cpython/commit/a8c418d5ed1103106db547aa576b82c6031985a4


----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue44368>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com