Skip to content

Error diagnostics incomplete on generic function redefinition #17791

Closed

Description

@jolaf

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

Activity

  1. changed the title [-]Error diagnostics incomplete on function redefinition[/-] [+]Error diagnostics incomplete on generic function redefinition[/+] on Sep 19, 2024
  2. brianschubert commented on Sep 20, 2024

    @brianschubert
    Collaborator

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions