Conditional function variants result in warning despite having identical types (only varying in parameter names)
These both result in the All conditional function variants must have identical signatures warning:
try:
from math import comb
except ImportError:
def comb(n: int, k: int) -> int: # All conditional function variants must have identical signatures
return int(factorial(n) / factorial(k) / factorial(n - k))
import sys
if sys.version_info >= (3, 8):
from math import comb
else:
def comb(n: int, k: int) -> int: # All conditional function variants must have identical signatures
return int(factorial(n) / factorial(k) / factorial(n - k))
Typeshed conditionally defines math.comb as def comb(__n: int, __k: int) -> int: .... Changing the parameter names in either of the above examples to match the typeshed signature silences the warning:
try:
from math import comb
except ImportError:
def comb(__n: int, __k: int) -> int: # <no more warning>
return int(factorial(__n) / factorial(__k) / factorial(__n - __k))
I think this came up before in #1168 (and via #1801, but, given the above, I don't think #698 addresses this).
Additionally, the cause of this error is not very discoverable. The error message doesn't tell you what the difference is, just that there is one. The documentation doesn't seem to cover this case. The only information I could find about what is happening here is this ticket itself.
Ok, I'll play devil's advocate. What should mypy do if you write:
try:
from math import comb
except ImportError:
def comb(n: int, k: int) -> int: # All conditional function variants must have identical signatures
return int(factorial(n) / factorial(k) / factorial(n - k))
And then call:
comb(n = 3, k = 5)
I agree that the error should be more descriptive. My question is: is this a bug? If the above situation comes up, what should happen? Should an error only be produced here? That seems like it would make type tracking much more painful.