[Python-ideas] Re: PEP 572: Is it a good ideas that walrus operator can be used in assertions since it causes side effects?

2020-02-13 Thread jdveiga
Thank you for all your answers. They were all very instructive to me.

However, after further investigation, I feel that I have made a mistake when 
putting this topic on the table.

When walrus operator was discussed, it was proposed to restrict assignment 
expressions to if, elif, and while statements. That proposal was rejected.

I realised now that that also applies, by analogy, to assert statement. So, 
sorry for taking this issue back. My apologies. I was not my intention to 
re-start any superseded debate.

The walrus operator, as you know, has been —and will be— highly controversial 
and material on this matter is enormous. Reviewing all this material is a huge 
task that I should have done before speaking.

Sorry again and thank you.
___
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/MACCFXXNIL3EDUAFR7ZVGK36B4I6SATO/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: PEP 572: Is it a good ideas that walrus operator can be used in assertions since it causes side effects?

2020-02-12 Thread Ethan Furman

On 02/12/2020 10:00 AM, Andrew Barnert wrote:

On Feb 12, 2020, at 06:49, Ethan Furman  wrote:


On 02/11/2020 01:49 PM, Steven D'Aprano wrote:


Oh, definitely for the best. This now allows us to write complex
assertions much more easily and efficiently:
 assert log(flag := complex_expression) or flag


That doesn't seem very useful: for that assert to fail, `flag` would have to be 
falsey, and if falsey, then surely the log of it is not helpful?


You’ve never had some rare code path that’s missing a return, and wasted half 
an hour trying to figure out how you could possibly be getting 0 out of that 
calculation before realizing you’re actually getting None?


*sheepish grin*

Point taken.

--
~Ethan~
___
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/5UA4E5Q4CVPP3K5AA6DU53TA3QTODXC5/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: PEP 572: Is it a good ideas that walrus operator can be used in assertions since it causes side effects?

2020-02-12 Thread Andrew Barnert via Python-ideas
On Feb 12, 2020, at 06:49, Ethan Furman  wrote:
> 
> On 02/11/2020 01:49 PM, Steven D'Aprano wrote:
> 
>> Oh, definitely for the best. This now allows us to write complex
>> assertions much more easily and efficiently:
>> assert log(flag := complex_expression) or flag
> 
> That doesn't seem very useful: for that assert to fail, `flag` would have to 
> be falsey, and if falsey, then surely the log of it is not helpful?

You’ve never had some rare code path that’s missing a return, and wasted half 
an hour trying to figure out how you could possibly be getting 0 out of that 
calculation before realizing you’re actually getting None?
___
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/B6XX2XABDPUEUBC3KJWHGDHZIHC4K7OK/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: PEP 572: Is it a good ideas that walrus operator can be used in assertions since it causes side effects?

2020-02-12 Thread Chris Angelico
On Thu, Feb 13, 2020 at 1:50 AM Ethan Furman  wrote:
>
> On 02/11/2020 01:49 PM, Steven D'Aprano wrote:
>
> > Oh, definitely for the best. This now allows us to write complex
> > assertions much more easily and efficiently:
> >
> >  assert log(flag := complex_expression) or flag
>
> That doesn't seem very useful: for that assert to fail, `flag` would have to 
> be falsey, and if falsey, then surely the log of it is not helpful?
>

Maybe the logging is meant to be useful when the flag is truthy. If
the assertion fails, it's an immediate error, but if it passes, you
get an informational log that might help interpret future messages?

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/CH25Q7TJ7MSJH2A6O7GNIYA6KXDSPEPJ/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: PEP 572: Is it a good ideas that walrus operator can be used in assertions since it causes side effects?

2020-02-12 Thread Christopher Barker
>  assert log(flag := complex_expression) or flag

>
> That doesn't seem very useful: for that assert to fail, `flag` would have
> to be falsey, and if falsey, then surely the log of it is not helpful?


Unless complex_expression produces an object that has useful information
when Falsey.


But it’s a toy example that I think makes the point well that there may be
a good use of := in an assert.

In fact, it’s my impression (and we’ll see how it works out) that the
walrus operator is best used in a “local” manner such as this— if the
assignment is important later in the code, it may be better to stick with
regular assignment anyway.

-CHB




>
> --
> ~Ethan~
> ___
> 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/N5BMTKG6KHNRDFVCISQ2YFFXK33YR7I2/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-- 
Christopher Barker, PhD

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython
___
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/L4SQBJ6FVQCZRZZOHCS2KZWRFYE67T64/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: PEP 572: Is it a good ideas that walrus operator can be used in assertions since it causes side effects?

2020-02-12 Thread Ethan Furman

On 02/11/2020 01:49 PM, Steven D'Aprano wrote:


Oh, definitely for the best. This now allows us to write complex
assertions much more easily and efficiently:

 assert log(flag := complex_expression) or flag


That doesn't seem very useful: for that assert to fail, `flag` would have to be 
falsey, and if falsey, then surely the log of it is not helpful?

--
~Ethan~
___
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/N5BMTKG6KHNRDFVCISQ2YFFXK33YR7I2/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: PEP 572: Is it a good ideas that walrus operator can be used in assertions since it causes side effects?

2020-02-12 Thread Steven D'Aprano
On Tue, Feb 11, 2020 at 07:10:18PM -, jdve...@gmail.com wrote:

> I have been taught that assertions must not produce side effects at all.

That's good practice, but saying that they "must not" is too strong.

The risk here is not the side-effect, but relying on that side-effect 
(which may be disabled) outside of the assertion. Some side-effects are 
perfectly fine:

assert log(message) or condition


> This is a problem in languages such as C. But not in Python (until 
> 3.8) since it does not allow assignment expressions.

Assignment is not the only possible side-effect. These have been possible 
back to Python 2.4 and probably older:

assert mylist.pop()
assert globals().update({'flag': expression}) or flag
assert delattr(mymodule, 'spam') or condition

In Python 3, we can also do things like this:

assert print(message) or condition
assert exec("value = expression") or condition

Since assertions can call functions (and methods), and functions can 
have side-effects, Python has *always* been able to have side-effects 
inside assertions.



> But now, for best or worst, we have the walrus operator

Oh, definitely for the best. This now allows us to write complex 
assertions much more easily and efficiently:

assert log(flag := complex_expression) or flag

Previously we would have to write this:

if __debug__:
flag = complex_expression
assert flag

which is much less convenient, but is another way to get side-effects in 
an assertion.


> However, if assertions are disabled:
> 
> `python3.8 -Oc "assert (always := True); print(f'{always=}')"`
> 
> it raises an exception:

Indeed. So does this:

[steve@ando ~]$ python -c "if False:
always = 999
print(always)"
Traceback (most recent call last):
  File "", line 3, in 
NameError: name 'always' is not defined

and for roughly the same reason.


[...]
> Do not you think that disabling walrus operators in assertions would 
> be a good idea?

No.


-- 
Steven
___
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/H4YLYLRWLNUG6LNOTWSH4VVRT3MLKZGZ/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: PEP 572: Is it a good ideas that walrus operator can be used in assertions since it causes side effects?

2020-02-11 Thread Ethan Furman

On 02/11/2020 11:49 AM, jdve...@gmail.com wrote:


My point is: is it good to add a new way of causing side effects in assertions?


Good or not, it's not changing now.

It's probably safe to say that every new language feature adds one more way to 
break assertions.  It is our responsibility to have correct code.

--
~Ethan~
___
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/SNKY3IMOF5VINA3H7L7RAJB3TBTVKJ5H/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: PEP 572: Is it a good ideas that walrus operator can be used in assertions since it causes side effects?

2020-02-11 Thread Joao S. O. Bueno
Yes =

The Walrus operator, poorly used, can caused the problem in the OP, and
many, many others.

It does not make a difference - we discussed and got to the conclusion that
the benefits
of having it  surpass whatever bad uses people put it for.

It clearly does not make sense to try to modify the syntax to forbid
"walrus containing expressions inside assert statements", as even
the creation of  a variable inside a conditional `if` block could
have the same effect you are describing, when the code is broken.




On Tue, 11 Feb 2020 at 17:22, Soni L.  wrote:

> I should perhaps provide a link to the relevant discussion in the Lua
> mailing list. For what it's worth, you'll only be looking at C.
>
> https://marc.info/?l=lua-l=157312955532763=2
>
> On 2020-02-11 4:55 p.m., jdve...@gmail.com wrote:
> > Sorry, Soni, I do no speak Lua. Can you provide any documentation on
> that point? I have found a function called assert but I am not sure that it
> works like assertions in Python.
> > ___
> > 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/A3CYE66PHTP3PSFD6GPGFA2EJQRH4GQL/
> > 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/H3VTCEFIGKTHASUNBNDNCV2OMH33NPM2/
> 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/GFSNTMW2T4KIU6USCUXXLKWO5F6XXEZN/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: PEP 572: Is it a good ideas that walrus operator can be used in assertions since it causes side effects?

2020-02-11 Thread Soni L.
I should perhaps provide a link to the relevant discussion in the Lua 
mailing list. For what it's worth, you'll only be looking at C.


https://marc.info/?l=lua-l=157312955532763=2

On 2020-02-11 4:55 p.m., jdve...@gmail.com wrote:

Sorry, Soni, I do no speak Lua. Can you provide any documentation on that 
point? I have found a function called assert but I am not sure that it works 
like assertions in Python.
___
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/A3CYE66PHTP3PSFD6GPGFA2EJQRH4GQL/
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/H3VTCEFIGKTHASUNBNDNCV2OMH33NPM2/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: PEP 572: Is it a good ideas that walrus operator can be used in assertions since it causes side effects?

2020-02-11 Thread Chris Angelico
On Wed, Feb 12, 2020 at 6:51 AM  wrote:
>
> Good point, Brandt. However, `global` is not used in the assertion itself as 
> walrus operator can be.
>
> My point is: is it good to add a new way of causing side effects in 
> assertions?
>

My point is: Is it good to find extremely stupid things that can be
done with a code construct, and then blaming the code construct?

I can do way, WAY worse with assertions.

assert globals().__setitem__("len", lambda x: 5) is None

Obviously lambda is evil, because it can be used in this way.

This thread doesn't belong on python-ideas, and I don't see a lot of
value moving it to python-list either. Maybe it can move to Google
Groups instead?

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/6RVXMJBRYHO5UIFIUMVONDU23EIHVDYH/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: PEP 572: Is it a good ideas that walrus operator can be used in assertions since it causes side effects?

2020-02-11 Thread jdveiga
Sorry, Soni, I do no speak Lua. Can you provide any documentation on that 
point? I have found a function called assert but I am not sure that it works 
like assertions in Python.
___
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/A3CYE66PHTP3PSFD6GPGFA2EJQRH4GQL/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: PEP 572: Is it a good ideas that walrus operator can be used in assertions since it causes side effects?

2020-02-11 Thread jdveiga
Good point, Brandt. However, `global` is not used in the assertion itself as 
walrus operator can be.

My point is: is it good to add a new way of causing side effects in assertions?

Thank you.
___
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/NCQ7QNB7MGGJP5TSL2WRP2O2W4FXQP7G/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: PEP 572: Is it a good ideas that walrus operator can be used in assertions since it causes side effects?

2020-02-11 Thread Brandt Bucher
> However, in the case of assertions, a nice and secure feature of Python has 
> been lost without any apparent benefit.

It's always been possible to have side effects from an assertion:

>>> always
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'always' is not defined
>>> def f():
... global always
... always = True
... return True
... 
>>> assert f()
>>> always
True

The walrus operator just adds another way to do it.
___
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/OIOZTMFBSEBLA2P66BJTWH5MWRURE2EV/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: PEP 572: Is it a good ideas that walrus operator can be used in assertions since it causes side effects?

2020-02-11 Thread Soni L.

Lua is known for using assignments in asserts in C.

It can come in handy for more complex, multi-part assertions.

On 2020-02-11 4:10 p.m., jdve...@gmail.com wrote:

Hi,

Recently the idea has come to me that walrus operator can have some issues when 
use in assertions.

I have been taught that assertions must not produce side effects at all. This 
is a problem in languages such as C. But not in Python (until 3.8) since it 
does not allow assignment expressions. But now, for best or worst, we have the 
walrus operator and a code like this is possible:

`python3.8 -c "assert (always := True); print(f'{always=}')"`

Which outputs:

```
always=True
```

since the assertion is always true.


However, if assertions are disabled:

`python3.8 -Oc "assert (always := True); print(f'{always=}')"`

it raises an exception:

```
Traceback (most recent call last):
   File "", line 1, in 
NameError: name 'always' is not defined

```

because of the side effect caused by the walrus operator.

Do not you think that disabling walrus operators in assertions would be a good 
idea? Would be it possible?

Personally, I do not see many advantages in walrus operator (versus 
disadvantages, of course). However, in the case of assertions, a nice and 
secure feature of Python has been lost without any apparent benefit.

Thank you.
___
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/KJFG76RXMZ2YVNZ4WXYY4TTQWSDJSMGY/
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/UKJPOROMU4EUFJRH5JCL4VXHXJ4ZE7PL/
Code of Conduct: http://python.org/psf/codeofconduct/