[issue35712] Make NotImplemented unusable in boolean context

Serhiy Storchaka report at bugs.python.org
Wed Mar 11 17:41:19 EDT 2020


Serhiy Storchaka <storchaka+cpython at gmail.com> added the comment:

> How is -1 interpreted?  Does it become a TypeError?

It is interpreted as error. It requires an exception be set. If you return -1 without setting an exception you will usually get a SystemError or crash. Oh, and this may happen during executing other code, so you will search a bug in wrong place.

You cannot change this without breaking the C and Python API in hard way.

> So my thinking is that when the interpreter gets the `NotImplemented` returned by either `if a` or by `if not a` that it would be converted to a `TypeError`

> and the `and` machinery sees it has a `NotImplemented` and raises a `TypeError`.

So you literally want NotImplemented raising a TypeError in boolean context except the "not" operator, and redefine `not a` from

    False if a else True

to

    NotImplemented if a is NotImplemented else False if a else True

You can do this, but this is a different issue, and I doubt that it will solve many problems.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue35712>
_______________________________________


More information about the Python-bugs-list mailing list