Re: [Python-ideas] warn/error when using a method as boolean in ifs/whiles

2016-10-11 Thread Paul Moore
On 11 October 2016 at 17:21, David Navarro wrote: > Something like this > > In [1]: def a(): pass > In [2]: def r(): raise RuntimeError('Do not forget to call this') > In [3]: a.__bool__ = r > In [4]: if a: pass > > I don't have an environment to test if this is possible. This would allow > markin

Re: [Python-ideas] warn/error when using a method as boolean in ifs/whiles

2016-10-11 Thread Nick Coghlan
On 11 October 2016 at 23:59, Paul Moore wrote: > Certainly. But the whole point of the warning is to catch people who > do the wrong thing. All I'm saying is that the new warning would cause > problems for people who omit a (currently unnecessary) "is None" check > in the process of protecting peo

Re: [Python-ideas] warn/error when using a method as boolean in ifs/whiles

2016-10-11 Thread David Navarro
One option would be to decorate those functions and provide an implementation to __bool__ or __nonzero__ which raises an exception. Something like this In [1]: def a(): pass In [2]: def r(): raise RuntimeError('Do not forget to call this') In [3]: a.__bool__ = r In [4]: if a: pass I don't have a

Re: [Python-ideas] warn/error when using a method as boolean in ifs/whiles

2016-10-11 Thread Ethan Furman
On 10/11/2016 07:00 AM, Steven D'Aprano wrote: On Tue, Oct 11, 2016 at 02:41:34PM +0200, Sven R. Kunze wrote: on django-developers, an intriguing idea appeared: https://groups.google.com/d/msg/django-developers/4bntzg1HwwY/HHHjbDnLBQAJ """ It seems to me that the default `method.__bool__` is

Re: [Python-ideas] warn/error when using a method as boolean in ifs/whiles

2016-10-11 Thread Steven D'Aprano
On Tue, Oct 11, 2016 at 02:41:34PM +0200, Sven R. Kunze wrote: > Hey python-ideas, > > on django-developers, an intriguing idea appeared: > https://groups.google.com/d/msg/django-developers/4bntzg1HwwY/HHHjbDnLBQAJ > > """ > It seems to me that the default `method.__bool__` is undesirable in >

Re: [Python-ideas] warn/error when using a method as boolean in ifs/whiles

2016-10-11 Thread Paul Moore
On 11 October 2016 at 14:38, Eric Snow wrote: > On Tue, Oct 11, 2016 at 7:02 AM, Paul Moore wrote: >> Interesting idea. There may be some issues - consider an object that >> may optionally have a handler method handle_event, and you want to >> call that method if it exists: >> >> handler = ge

Re: [Python-ideas] warn/error when using a method as boolean in ifs/whiles

2016-10-11 Thread Eric Snow
On Tue, Oct 11, 2016 at 7:02 AM, Paul Moore wrote: > Interesting idea. There may be some issues - consider an object that > may optionally have a handler method handle_event, and you want to > call that method if it exists: > > handler = getattr(obj, 'handle_event', None) > if handler: >

Re: [Python-ideas] warn/error when using a method as boolean in ifs/whiles

2016-10-11 Thread Paul Moore
On 11 October 2016 at 13:41, Sven R. Kunze wrote: > maybe they could benefit > from a patch where `if`-statements give a warning/error when the expression > is a callable (with the default `FunctionType.__bool__`? [...] > What do you think about that Python emitting an warning/error as described >

[Python-ideas] warn/error when using a method as boolean in ifs/whiles

2016-10-11 Thread Sven R. Kunze
Hey python-ideas, on django-developers, an intriguing idea appeared: https://groups.google.com/d/msg/django-developers/4bntzg1HwwY/HHHjbDnLBQAJ """ It seems to me that the default `method.__bool__` is undesirable in Jinja2 templates. I do not know Jinja2 well enough, but maybe they could ben