Replies: 1 comment 4 replies
|
There is no good solution for this case, because it would require having a type that represents "all ints except 0", which we do not have. The problem is that 0 is an inhabitant of the type def other_func(x: int):
v = func(x)With your overload, the type checker will think that This is why your overlapping overload is an error. If we had a type |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I have a case where I have a function that accept general
inttype and returnstr, however0is an exception that cannot be handled, so Ireturn Nonein that case, the problem now is that I would have outputstr | None. While I have checks in some dynamic cases forNoneoutput, I also have some static cases where input always != 0, but because of output type hint I would have to add unnecessary checks which I want to avoid. I was trying to solve that with@overload, but IDE still complain about improper use of overloading.Some pseudo-code:
The error I get in this case on 1st overload:
Can you recommend me solution for better typing in this case?
All reactions