[issue46692] match case does not support regex

2022-02-09 Thread Eric V. Smith


Eric V. Smith  added the comment:

Good catch, @xtreak. I think something like the discussed (but not implemented) 
custom matching protocol would be required here. __match_args__ won't work, 
because it's a special attribute only checked on classes, not instances.

Of course, I'm still not sure this is what the original poster is requesting!

But assuming so, this would require a PEP, and should be discussed with the PEP 
634 authors.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46692] match case does not support regex

2022-02-09 Thread Karthikeyan Singaravelan


Karthikeyan Singaravelan  added the comment:

> There were ideas for exotic matchers such as IsInstance(), InRange(), 
> RegexMatchingGroup() and so on.

https://www.python.org/dev/peps/pep-0622/#custom-matching-protocol

The PEP has some mention about a similar use case in custom match protocol 
section.

--
nosy: +xtreak

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46692] match case does not support regex

2022-02-09 Thread Eric V. Smith


Eric V. Smith  added the comment:

Looking at PEP 634, the obvious way to add support for this is to have the 
re.Match object specify Py_TPFLAGS_MAPPING. But I tried that, and then I get 
this error when using an re.Match object in a match statement:

case {'one': x, 'two': y}:
 
TypeError: object of type 're.Match' has no len()


Add len() to re.Match objects was rejected when __getitem__ was added to 
re.Match, in issue 24454.

I haven't explored other ways to support re.Match objects in the match 
statement.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46692] match case does not support regex

2022-02-09 Thread Eric V. Smith


Eric V. Smith  added the comment:

Oops, slight bug in my code. Use this:

import re

def f(map):
print(f'input={map["one"]} {map["two"]}')
match map:
case {'one': x, 'two': y}:
print(f"match {x} {y}")
case _:
print("no match")

d = {'one':0, 'two':1}
f(d)
m = re.match("(?Pa)b(?Pc)", "abc")
f(m)

With this output:
input=0 1
match 0 1
input=a c
no match

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46692] match case does not support regex

2022-02-09 Thread Eric V. Smith


Change by Eric V. Smith :


--
components: +Interpreter Core

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46692] match case does not support regex

2022-02-09 Thread Eric V. Smith


New submission from Eric V. Smith :

You need to provide more information.

Is your concern that re.match objects aren't matched like dicts are, despite 
looking like a mapping?

import re

def f(map):
print(f'input={m["one"]} {m["two"]}')
match map:
case {'one': x, 'two': y}:
print(f"match {x} {y}")
case _:
print("no match")

m = re.match("(?Pa)b(?Pc)", "abc")
d = {'one':0, 'two':1}
f(d)
f(m)

produces:
input=a c
match 0 1
input=a c
no match

I assume you're not reporting a bug, so I'm going to mark this as a feature 
request.

--
nosy: +eric.smith
type: behavior -> enhancement
versions:  -Python 3.10

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46692] match case does not support regex

2022-02-09 Thread Ali Rn


Change by Ali Rn :


--
components: Regular Expressions
nosy: AliRn, ezio.melotti, mrabarnett
priority: normal
severity: normal
status: open
title: match case does not support regex
type: behavior
versions: Python 3.10, Python 3.11

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com