[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 tha

[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 th

[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 her

[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 > "absolute

[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 be

[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 th

[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 a

[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 bu

[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 wrot

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

2023-10-21 Thread Ram Rachum
Hey, I bet this has been discussed before but I couldn't find it. I'd appreciate it if anyone could point me to that thread. I'm sick of seeing "AttributeError: 'NoneType' object has no attribute 'foo'" whenever there's a `re.match` operation that fails while the code expects it to succeed. What