Skip to content

Conditional function variants result in warning despite having identical types (only varying in parameter names) #10575

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
posita opened this issue Jun 2, 2021 · 2 comments · Fixed by #13604
Labels
bug mypy got something wrong diagnostics

Comments

@posita
Copy link
Contributor

posita commented Jun 2, 2021

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).

@posita posita added the bug mypy got something wrong label Jun 2, 2021
@exarkun
Copy link

exarkun commented Mar 29, 2022

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.

kiritofeng added a commit to DMOJ/judge-server that referenced this issue Sep 4, 2022
Fix `All conditional function variants must have identical signatures`, see python/mypy#10575.
@Riolku
Copy link

Riolku commented Sep 4, 2022

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.

hauntsaninja added a commit to hauntsaninja/mypy that referenced this issue Sep 4, 2022
hauntsaninja added a commit to hauntsaninja/mypy that referenced this issue Sep 4, 2022
Xyene pushed a commit to DMOJ/judge-server that referenced this issue Oct 2, 2022
Fix `All conditional function variants must have identical signatures`, see python/mypy#10575.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong diagnostics
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants