I know what I am asking is rather niche, but it has been bugging me for quite a while. Suppose I have the following function:
def foo(return_more: bool):
....
if return_more:
return data, more_data
return data
You can imagine it is a function that may return more data if given a flag.
How should I typehint this function? When I use the function in both ways
data = foo(False)
data, more_data = foo(True)
either the first or the 2nd statement would say that the function cannot be assigned due to wrong size of return tuple.
Is having variable signature an anti-pattern? Is Python’s typehinting mechanism not powerful enough and thus I am forced to ignore this error?
Edit:
Thanks for all the suggestions.
I was enlightened by this suggestion about the existence of overload
and this solution fit my requirements perfectly
from typing import overload, Literal
@overload
def foo(return_more: Literal[False]) -> Data: ...
@overload
def foo(return_more: Literal[True]) -> tuple[Data, OtherData]: ...
def foo(return_more: bool) -> Data | tuple[Data, OtherData]:
....
if return_more:
return data, more_data
return data
a = foo(False)
a,b = foo(True)
a,b = foo(False) # correctly identified as illegal
I think there’s a spectrum here, and I’ll clarify the stances.
The spectrum ranges from “Data shouldn’t cause the function to do (something wildly) different” to “It should be allowed, even to the point of variable returns”
I think you stand on the former while I stand on the latter. Correct me if I’m wrong though, but that’s the vibe I’m getting from the tone in your example.
Data shouldn’t drive the program in this way.
Suppose we have a function that calculates a price of an object.
I feel it is agreeable for us to have compute_price(with_discount: bool)
, over compute_price_with_discount() + compute_price_without_discount()
You’ve basically spelled:
I feel your point your making in the example is a bit exaggerated.
Again, coming back to my above example, I don’t think we would construe it as compute_price('with_discount')
.
Maybe this is bandwagoning, but one of the reason for my stance is that there are quite a few examples of variable returns.
eg:
getattr
may return a different type base on the key given- quite a few functions in
numpy
returns different things based on flags. SVD will returnS
ifcompute_uv=False
andS,U,V
otherwise
I think you stand on the former
Absolutely.
Suppose we have a function that calculates a price of an object. I feel it is agreeable for us to have
compute_price(with_discount: bool)
, overcompute_price_with_discount() + compute_price_without_discount()
Well, presumably you’d also actually have some other inputs to a price compute function. In which case, I’d suggest bundling all that information into an Invoice type or something that includes whether or not discounts are applied…
Maybe this is bandwagoning, but one of the reason for my stance is that there are quite a few examples of variable returns.
getattr
is really special, it’s basically a reflection operator, it shouldn’t be a model for how a normal function should behave.
I’m not familiar with numpy. The linked function though looks like a true case of generic behavior where an input changes an output in a specified way for any number of values that meet its requirements. A boolean flag is never generic.