[Python-ideas] warn/error when using a method as boolean in ifs/whiles
David Navarro
davisein at gmail.com
Tue Oct 11 12:21:45 EDT 2016
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 an environment to test if this is possible. This would allow
marking with a decorator functions that might be misleading or that are a
common source of issues for new users.
--
David Navarro
On 11 October 2016 at 17:34, Ethan Furman <ethan at stoneleaf.us> wrote:
> 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/4bntzg1Hww
>>> Y/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
>>> benefit from a patch where `if`-statements give a warning/error when the
>>> expression is a callable (with the default `FunctionType.__bool__`?
>>> This would solve the issue not just for the methods you mention, but
>>> more in general.
>>>
>>
>> That should be easy enough to do as a custom descriptor.
>>
>> But I would not like to see the default function or method __bool__
>> raise a warning.
>>
>
> [...]
>
> So I think this is something that Django/Jinja2 should implement for its
>> own methods that need it, it should not be a general feature of all
>> Python functions/methods.
>>
>
> Agreed. Python is a /general/-purpose programming language. We should not
> make changes to help one subset of users when those changes will harm
> another subset (and being flooded with false positives is harmful) --
> particularly when easy customization is already available.
>
> --
> ~Ethan~
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
--
David Navarro Estruch
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20161011/8e524e84/attachment.html>
More information about the Python-ideas
mailing list