[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, 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 invalid
> user input, but that's the fault of the user, not the programmer.
>
> --
> Greg
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/4JNKSUTZ6ZDVHERYCNO35J2UDS5UO4CD/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/ATA4NWRU4MNUX427LZLNP3UMRVEOLVQE/
Code of Conduct: http://python.org/psf/codeofconduct/


[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 invalid
user input, but that's the fault of the user, not the programmer.

--
Greg
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/4JNKSUTZ6ZDVHERYCNO35J2UDS5UO4CD/
Code of Conduct: http://python.org/psf/codeofconduct/


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

Why create a new argument, then mandate that you use it everywhere,
just to achieve what's already happening?


Because the end user message would be much better, and the exception 
would point to the exact line where the match didn't occur, instead of 
some subsequent line.


Eric
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/5HRZYV54IUK43VX5WRULEZ6CH3IP35OO/
Code of Conduct: http://python.org/psf/codeofconduct/


[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 value. It returns a
truthy value if the regular expression matches, and a falsy value if
it does not. As such, it is perfectly in line with fnmatch.fnmatch(),
all of the str.is*() methods, math.isclose(), and pretty much
everything else.

So I guess by your logic, Python's standard library must have come
from Perth, as it is nothing but black swans.

ChrisA
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/EM4H4YIL5AIHEOZVA5RTOBFBQFDUIA5F/
Code of Conduct: http://python.org/psf/codeofconduct/


[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 Stephen J. Turnbull <
turnbull.stephen...@u.tsukuba.ac.jp> wrote:

> 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 this painfully", I do think that novice Python
> programmers should learn that
>
> 1.  "None has no .xxx attribute" means that some previous code (often
> but not always a regex match) was unable to perform some task
> and returned None to indicate failure.
> 2.  If the failure was expectable, your code is buggy because it
> didn't test for None, and if it was unexpected, some code
> somewhere is buggy because it allowed an invariant to fail.
>
> On the cost side, there are so many cases where a more finely divided
> Exception hierarchy would help novices quite a bit but experts very
> little that this case (easy to learn) would open the floodgates.  I
> believe Guido has specifically advised against such a hierarchy.  I'm
> against this change.
>
> Steve
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/MIE4OFPAG5CTNMUR7FYJSX66UMDHIH57/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/QCKOSYRIFF4O27CUFIUV76NGPQYI4FQP/
Code of Conduct: http://python.org/psf/codeofconduct/


[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 return value of
matches, then using a helper like this seems like a reasonable way of
addressing this (far more effective than living with the problem until a
flag gets added to the stdlib and the project can drop support for older
Python versions...)

If the intention here is simply to "make it easier for people to remember"
in the future, without being tied to any actual real world use case, then I
don't see how adding a (just as easily forgettable) boolean flag is any
significant improvement.

Paul

On Sun, 22 Oct 2023 at 10:19, Stephen J. Turnbull <
turnbull.stephen...@u.tsukuba.ac.jp> wrote:

> 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 this painfully", I do think that novice Python
> programmers should learn that
>
> 1.  "None has no .xxx attribute" means that some previous code (often
> but not always a regex match) was unable to perform some task
> and returned None to indicate failure.
> 2.  If the failure was expectable, your code is buggy because it
> didn't test for None, and if it was unexpected, some code
> somewhere is buggy because it allowed an invariant to fail.
>
> On the cost side, there are so many cases where a more finely divided
> Exception hierarchy would help novices quite a bit but experts very
> little that this case (easy to learn) would open the floodgates.  I
> believe Guido has specifically advised against such a hierarchy.  I'm
> against this change.
>
> Steve
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/MIE4OFPAG5CTNMUR7FYJSX66UMDHIH57/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/UTU4SQNQOJNBNYCW35ZP4OI4XTDKDJEN/
Code of Conduct: http://python.org/psf/codeofconduct/


[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 this painfully", I do think that novice Python
programmers should learn that

1.  "None has no .xxx attribute" means that some previous code (often
but not always a regex match) was unable to perform some task
and returned None to indicate failure.
2.  If the failure was expectable, your code is buggy because it
didn't test for None, and if it was unexpected, some code
somewhere is buggy because it allowed an invariant to fail.

On the cost side, there are so many cases where a more finely divided
Exception hierarchy would help novices quite a bit but experts very
little that this case (easy to learn) would open the floodgates.  I
believe Guido has specifically advised against such a hierarchy.  I'm
against this change.

Steve
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/MIE4OFPAG5CTNMUR7FYJSX66UMDHIH57/
Code of Conduct: http://python.org/psf/codeofconduct/