Skip to content

Rather confusing error message when argument name is wrong #9668

Open
@wsanchez

Description

@wsanchez

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions