This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Conflict between re.match and match keyword
Type: behavior Stage: resolved
Components: Library (Lib), Regular Expressions Versions: Python 3.11, Python 3.10
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ezio.melotti, mrabarnett, pablogsal, steven.daprano
Priority: normal Keywords:

Created on 2021-06-07 23:22 by finnjavier08, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg395295 - (view) Author: Finn Mason (finnjavier08) Date: 2021-06-07 23:22
>>> import re
>>> re.match('str', 'str').group()
'str'
>>> match 'str':
...     case 'str':
...         print('match!')
...
match!
>>> from re import match
>>> match
<function re.match ...>

As the above example demonstrates, while re.match doesn't raise an error despite having a keyword name, importing re.match directly into __main__ replaces the keyword with the function. The obvious solution is to rename re.match, but this would break many, many pieces of code.
msg395296 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2021-06-07 23:28
`match` is a soft keyword. Which means that the interpreter should still recognise `match 'str': ...` even if the *name* "match" is defined.
msg395298 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-06-08 00:06
I don't understand this example. 

> importing re.match directly into __main__ replaces the keyword with the function. 

It has not replaced anything, if you do a match statement it works and doesn't call your function. For example:

>>> x = [1,2]
>>>
>>> def match(*args):
...    print("Oh no")
...
>>> match x:
...    case [y,z]:
...       print(y,z)
...
1 2

Here "match" when used as a statement has not been replaced by the function that prints "oh no" so the match statement works as expected and so does the function:

>>> match
<function match at 0x7f7c173c5ff0>
msg395299 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-06-08 00:07
As Steven mentions, match is a soft keyword:

>>> import keyword
>>> keyword.softkwlist
['_', 'case', 'match']

And they don't follow the same rules as keywords. Check the pattern matching PEPs for more info.
History
Date User Action Args
2022-04-11 14:59:46adminsetgithub: 88507
2021-06-08 00:07:54pablogsalsetmessages: + msg395299
2021-06-08 00:06:39pablogsalsetstatus: open -> closed

nosy: + pablogsal
messages: + msg395298

resolution: not a bug
stage: resolved
2021-06-07 23:28:25steven.dapranosetnosy: + steven.daprano
messages: + msg395296
2021-06-07 23:22:35finnjavier08setnosy: - finnjavier08
2021-06-07 23:22:07finnjavier08create