python / python/mypy

Rather confusing error message when argument name is wrong

オープン
#9,668 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

bug topic-calls topic-error-reporting topic-protocols topic-usability
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、提供されている mypy 0.790 の再現ケースを Python 3.9 で実行し、パラメーター名が route ではなく resource の場合の診断を比較します。この互換性のない引数エラーをフォーマットするコードを追跡します。完了条件は、誤った引数名によって、互換性のない型を報告するだけでなく、実際のパラメーター名の問題を指摘する診断が生成されることです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
tooling
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
45/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。