python / python/mypy

Rather confusing error message when argument name is wrong

Open
#9,668 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-calls topic-error-reporting topic-protocols topic-usability
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

If you run Mypy on this code:

from typing import Any, Awaitable, Callable, Protocol, Union, TypeVar, Generic


class IRequest:
    pass


KleinSynchronousRenderable = Union[str, bytes]
KleinRenderable = Union[
    KleinSynchronousRenderable, Awaitable[KleinSynchronousRenderable]
]


T_co = TypeVar('T_co', contravariant=True)

class KleinRoute(Protocol[T_co]):
    def __call__(v, /, self: T_co, request: IRequest) -> KleinRenderable:
        """
        Function that, when decorated by L{Klein.route}, handles a Klein
        request.
        """


class Klein(Generic[T_co]):
    def route(self, url: str, *args: Any, **kwargs: Any) -> Callable[[KleinRoute[T_co]], KleinRoute[T_co]]:
        """
        Decorator for endpoint routing.
        """


class Application:
    klein: Klein[Application] = Klein()

    @klein.route("/foo")
    def foo(self, resource: IRequest) -> KleinRenderable:
        pass

You get this error:

main.py:34: error: Argument 1 has incompatible type "Callable[[Application, IRequest], Union[Union[str, bytes], Awaitable[Union[str, bytes]]]]"; expected "KleinRoute[Application]"

Using mypy 0.790 on Python 3.9.

If you change the resource argument to route (second to last line), this passes. The above error suggests a different problem entirely, so it had us chasing our tails a bit.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided mypy 0.790 reproduction on Python 3.9 and compare the diagnostic when the parameter is named resource versus route. Trace the code that formats this incompatible-argument error; done means the wrong argument name produces a diagnostic that points to the actual parameter-name problem rather than only reporting an incompatible type.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.