[Python-ideas] Re: re.match(pattern, string, require=True)

2023-10-22 Thread David Mertz, Ph.D.
It's hard to overstate how "normal" a non-match is. A typical program might examine thousands of strings to identify the ten that match a pattern. Exceptions shouldn't be used for cases that are in no way exceptional. On Sun, Oct 22, 2023, 7:27 PM Greg Ewing wrote: > On 23/10/23 1:36 am,

[Python-ideas] Re: re.match(pattern, string, require=True)

2023-10-22 Thread Greg Ewing
On 23/10/23 1:36 am, Juancarlo Añez wrote: The *re* module is a black swan, because most of stdlib raises exceptions on invalid arguments or not being able to deliver. Most of the time, failure to match an re is not a programming error. Often it's perfectly normal. Sometimes it's the result of

[Python-ideas] Re: re.match(pattern, string, require=True)

2023-10-22 Thread Eric V. Smith via Python-ideas
On 10/21/2023 8:31 PM, Chris Angelico wrote: On Sun, 22 Oct 2023 at 11:29, MRAB wrote: I think what the OP wants is to have re.match either return a match or raise an exception. Yes, and my point is that simply attempting to access an attribute will do exactly that. It's not a silent failure.

[Python-ideas] Re: re.match(pattern, string, require=True)

2023-10-22 Thread Chris Angelico
On Mon, 23 Oct 2023 at 01:14, Juancarlo Añez wrote: > > The re module is a black swan, because most of stdlib raises exceptions on > invalid arguments or not being able to deliver. > This is a comparison function, and like every other comparison function, it signals its results with a return

[Python-ideas] Re: re.match(pattern, string, require=True)

2023-10-22 Thread Juancarlo Añez
The *re* module is a black swan, because most of stdlib raises exceptions on invalid arguments or not being able to deliver. It's impossible to change *re* now, so wrapping the calls should be the right solution. -- Juancarlo Añez mailto:apal...@gmail.com On Sun, Oct 22, 2023 at 5:19 AM

[Python-ideas] Re: re.match(pattern, string, require=True)

2023-10-22 Thread Paul Moore
Just as a further note, it's perfectly possible to write a helper: def ensure_match(pattern, string): m = re.match(pattern, string) if m is None: raise ValueError(f"Provided string did not match {pattern}") return m If the project is concerned about failures to check the

[Python-ideas] Re: re.match(pattern, string, require=True)

2023-10-22 Thread Stephen J. Turnbull
Chris Angelico writes: > Why create a new argument, then mandate that you use it everywhere, > just to achieve what's already happening? "Newbies don't read code backwards very well" seems to be the point. While I'm not of the school that "I learned this painfully, so newbies should learn

[Python-ideas] Re: re.match(pattern, string, require=True)

2023-10-21 Thread Chris Angelico
On Sun, 22 Oct 2023 at 11:29, MRAB wrote: > I think what the OP wants is to have re.match either return a match or > raise an exception. Yes, and my point is that simply attempting to access an attribute will do exactly that. It's not a silent failure. Why create a new argument, then mandate

[Python-ideas] Re: re.match(pattern, string, require=True)

2023-10-21 Thread MRAB
On 2023-10-21 21:15, Chris Angelico wrote: On Sun, 22 Oct 2023 at 06:37, Ram Rachum wrote: On Sat, Oct 21, 2023 at 10:30 PM Chris Angelico wrote: > I love that, but it mostly makes sense for "if there's a match do this, otherwise do that" where most cases fall into "I'm absolutely sure

[Python-ideas] Re: re.match(pattern, string, require=True)

2023-10-21 Thread Chris Angelico
On Sun, 22 Oct 2023 at 06:37, Ram Rachum wrote: > > On Sat, Oct 21, 2023 at 10:30 PM Chris Angelico wrote: >> >> >> > I love that, but it mostly makes sense for "if there's a match do this, >> > otherwise do that" where most cases fall into "I'm absolutely sure there's >> > a match here and

[Python-ideas] Re: re.match(pattern, string, require=True)

2023-10-21 Thread Ram Rachum
On Sat, Oct 21, 2023 at 10:30 PM Chris Angelico wrote: > > > I love that, but it mostly makes sense for "if there's a match do this, > otherwise do that" where most cases fall into "I'm absolutely sure there's > a match here and here's what we should do with that match", and when that >

[Python-ideas] Re: re.match(pattern, string, require=True)

2023-10-21 Thread Chris Angelico
On Sun, 22 Oct 2023 at 06:11, Ram Rachum wrote: > > On Sat, Oct 21, 2023 at 10:01 PM Chris Angelico wrote: >> >> On Sat, 21 Oct 2023 at 21:57, Ram Rachum wrote: >> >> What about an if with the match inside it? >> >> if m := re.match(...): >> ... >> >> That's one of the motivating examples

[Python-ideas] Re: re.match(pattern, string, require=True)

2023-10-21 Thread Ram Rachum
On Sat, Oct 21, 2023 at 10:01 PM Chris Angelico wrote: > On Sat, 21 Oct 2023 at 21:57, Ram Rachum wrote: > > What about an if with the match inside it? > > if m := re.match(...): > ... > > That's one of the motivating examples behind the walrus after all. > I love that, but it mostly makes

[Python-ideas] Re: re.match(pattern, string, require=True)

2023-10-21 Thread Chris Angelico
On Sat, 21 Oct 2023 at 21:57, Ram Rachum wrote: > > It's a little similar to the reasoning behind PEP 618 (adding the `strict` > argument to `zip`). Not quite, since without strict, zip will truncate - it doesn't have a different return value. > A keyword argument is easier to add, and makes

[Python-ideas] Re: re.match(pattern, string, require=True)

2023-10-21 Thread Ram Rachum
It's a little similar to the reasoning behind PEP 618 (adding the `strict` argument to `zip`). A keyword argument is easier to add, and makes the code less ugly, then an `if` clause. When I don't have that `if` clause you mentioned in my code, it's not because I forgot, it's because I don't want

[Python-ideas] Re: re.match(pattern, string, require=True)

2023-10-21 Thread David Mertz, Ph.D.
I feel like this is all example of "not every one line function needs to be in the standard library." You can easily write your own 'match_or_raise()'... I guess it would take two lines, actually. On Sat, Oct 21, 2023, 2:42 PM Ram Rachum wrote: > Hey, > > I bet this has been discussed before

[Python-ideas] Re: re.match(pattern, string, require=True)

2023-10-21 Thread Paul Moore
I don't see how it's more likely that people would remember to add a `require=True` flag than to add `if m: raise RuntimeError("No match")`. The problem here is people forgetting that a match can fail, not lack of a means to handle that problem. Paul On Sat, 21 Oct 2023 at 11:38, Ram Rachum