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

Rather confusing error message when argument name is wrong #9668

Open
wsanchez opened this issue Oct 30, 2020 · 0 comments
Open

Rather confusing error message when argument name is wrong #9668

wsanchez opened this issue Oct 30, 2020 · 0 comments
Labels
bug mypy got something wrong topic-calls Function calls, *args, **kwargs, defaults topic-error-reporting How we report errors topic-protocols topic-usability

Comments

@wsanchez
Copy link

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.

@wsanchez wsanchez added the bug mypy got something wrong label Oct 30, 2020
@AlexWaygood AlexWaygood added topic-protocols topic-calls Function calls, *args, **kwargs, defaults topic-usability topic-error-reporting How we report errors labels Apr 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-calls Function calls, *args, **kwargs, defaults topic-error-reporting How we report errors topic-protocols topic-usability
Projects
None yet
Development

No branches or pull requests

2 participants