Warning: This is sort of a weird use case.
In our codebase, we have
classFalseWithAssociatedReason(object):
def__init__(self, reason):
self.reason=reasondef__nonzero__(self):
# This object is always false in a boolean context.returnFalse
The usecase for this is something like the following
defparameter_equals_one(param):
# type: (Any) -> ???ifparam==1:
returnTrueelse:
returnFalseWithAssociatedReason("Param {} is not 1.".format(param))
defmain(...):
result=parameter_equals_one("2345")
ifnotresult:
printresult.reasonThere's no return type we can use for parameter_equals_one that wouldn't be a union. I propose a SupportsBool abc that FalseWithAssociatedReason could inherit from.
(Only catch: I believe falseness is described by both __nonzero__ and __len__.)
And to those who say the above use case can be covered by exceptions: hey, you're not wrong, but this is a hyper-simplified usecase.
Warning: This is sort of a weird use case.
In our codebase, we have
The usecase for this is something like the following
There's no return type we can use for parameter_equals_one that wouldn't be a union. I propose a SupportsBool abc that FalseWithAssociatedReason could inherit from.
(Only catch: I believe falseness is described by both
__nonzero__and__len__.)And to those who say the above use case can be covered by exceptions: hey, you're not wrong, but this is a hyper-simplified usecase.