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 |
Beta Was this translation helpful? Give feedback.
-
I have a case where I have a function that accept general
int
type and returnstr
, however0
is an exception that cannot be handled, so Ireturn None
in that case, the problem now is that I would have outputstr | None
. While I have checks in some dynamic cases forNone
output, 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?
Beta Was this translation helpful? Give feedback.
All reactions