Skip to content

Wrong "Overloaded function implementation does not accept all possible arguments" #11004

Description

@dvarrazzo

Bug Report

The following example (reduced from psycopg 3) seems to be a false positive:

To Reproduce

from typing import Any, Callable, Generic, overload, Optional, TypeVar, Tuple, Union

Row = TypeVar("Row")

class Connection(Generic[Row]):
    @overload
    def connect(
        self, *, row_factory: Callable[[], Row], **kwargs: Union[None, int, str]
    ) -> "Connection[Row]":
        ...

    @overload
    def connect(
        self, **kwargs: Union[None, int, str],
    ) -> "Connection[Tuple[Any, ...]]":
        ...

    def connect(
        self, *, row_factory: Optional[Callable[[], Row]] = None, **kwargs: Any,
    ) -> "Connection[Any]":
        raise NotImplementedError

The file is flagged with the error:

bug_mypy.py:18: error: Overloaded function implementation does not accept all possible arguments of signature 2  [misc]

Your Environment

  • Mypy version used: 0.910
  • Python version used: 3.8
  • Operating system and version: Linux

Activity

  1. erictraut commented on Aug 21, 2021

    @erictraut

    You may have already figured this out, but there is a simple workaround. If you change the second overload to include an explicit row_factory parameter, it will type check without errors.

        @overload
        def connect(
            self,
            *,
            row_factory: None = None,
            **kwargs: Union[None, int, str],
        ) -> "Connection[Tuple[Any, ...]]":
            ...
  2. dvarrazzo commented on Aug 21, 2021

    @dvarrazzo
    Author

    @erictraut No, I didn't notice that, thank you. It is interesting, however I prefer to use a type: ignore rather than a dummy parameter.

    Another thing that makes the error go is to use **kwargs: Any in the second signature.

  3. dlax commented on Aug 23, 2021

    @dlax
    Contributor

    I think that's the issue I tried to resolve in #10390.

  4. dvarrazzo commented on Aug 23, 2021

    @dvarrazzo
    Author

    Amazing @dlax, didn't know you had looked into it so much to propose a MR! 🙂

  5. adam-grant-hendry commented on Jul 5, 2022

    @adam-grant-hendry

    @dlax Do you plan to merge the change soon?

  6. dlax commented on Jul 6, 2022

    @dlax
    Contributor

    @adam-grant-hendry, the PR is still pending review; and then possible merge by a project member.

  7. sterliakov commented on Mar 26, 2025

    @sterliakov
    Collaborator

    This isn't a bug? As currently written, the second overload accepts Connection().connect(row_factory=10) and the implementation signature does not, so mypy warns about a real problem.

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions