Skip to content
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

Error diagnostics incomplete on generic function redefinition #17791

Open
jolaf opened this issue Sep 19, 2024 · 1 comment
Open

Error diagnostics incomplete on generic function redefinition #17791

jolaf opened this issue Sep 19, 2024 · 1 comment
Labels
bug mypy got something wrong

Comments

@jolaf
Copy link
Contributor

jolaf commented Sep 19, 2024

Bug Report

The provided test defines the generic function f() in two ways which are incompatible with each other – one has an upper bound, and the other has not.

mypy reports the error, but in the error message only one of the mismatching function signatures is displayed, the other one is missing for some reason.

Note that there's nothing in the output after the word Redefinition:.

To Reproduce

https://mypy-play.net/?mypy=1.11.2&python=3.12&enable-incomplete-feature=NewGenericSyntax&gist=78806d2f754ba0e5ce9bdcb61909fbc6

@jolaf jolaf added the bug mypy got something wrong label Sep 19, 2024
@jolaf jolaf changed the title Error diagnostics incomplete on function redefinition Error diagnostics incomplete on generic function redefinition Sep 19, 2024
@brianschubert
Copy link

brianschubert commented Sep 20, 2024

Relevant snippet here is:

mypy/mypy/messages.py

Lines 1501 to 1511 in a646f33

def incompatible_conditional_function_def(
self, defn: FuncDef, old_type: FunctionLike, new_type: FunctionLike
) -> None:
self.fail("All conditional function variants must have identical signatures", defn)
if isinstance(old_type, (CallableType, Overloaded)) and isinstance(
new_type, (CallableType, Overloaded)
):
self.note("Original:", defn)
self.pretty_callable_or_overload(old_type, defn, offset=4)
self.note("Redefinition:", defn)
self.pretty_callable_or_overload(new_type, defn, offset=4)

The problem is that the second redefinition message is being suppressed here since it looks like a duplicate of the first definition and allow_dups is set to False in the ErrorInfo.

A possible fix would be to simply pass allow_dups=True so that the second message is never suppressed:

  self.note("Redefinition:", defn) 
- self.pretty_callable_or_overload(new_type, defn, offset=4) 
+ self.pretty_callable_or_overload(new_type, defn, offset=4, allow_dups=True) 

which results in the output:

main.py:11: error: All conditional function variants must have identical signatures  [misc]
main.py:11: note: Original:
main.py:11: note:     def [F] f(a: F) -> F
main.py:11: note: Redefinition:
main.py:11: note:     def [F] f(a: F) -> F
Found 1 error in 1 file (checked 1 source file)

A better fix would be to prevent the rendered definitions from being duplicates to begin with, which may come with the fix to #17792. But I think there would probably be no harm in setting allow_dups=True here anyway, since I can't think of a scenario where we'd want the message after Redefinition: to be suppressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants