python / python/mypy

More-helpful typing errors with Generics and Callables

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

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

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

説明

Feature

Taking into consideration the following code (using mypy==1.4.1), the error messages are not very useful in debugging the problem. I have included the Pyright error message for comparison:

"""A test pipeline"""
from __future__ import annotations

from typing import Callable, Generic, TypeVar

from pydantic import BaseModel

from filter_base.filters.base_filter import BaseFilter

ModelT = TypeVar("ModelT", bound=BaseModel)


class TestFilter(BaseFilter, Generic[ModelT]):
    """A filter for loading a JSON file."""

    def __init__(self, model_type: type[ModelT]):
        """Test filter."""
        self.model_type: type[ModelT] = model_type

    def output(self) -> ModelT:
        """Output the loaded model"""
        return self.model_type()

    def input(self, obj: ModelT) -> None:
        """Input the model"""
        pass


T = TypeVar("T")


def connect(
    connect_signal: Callable[[], T],
    connect_slot: Callable[[T], None],
) -> None:
    """Connect a signal to a slot"""

    # TODO: this is a bit silly - we're just immedicately calling the signal/slot
    # however - it'll do for the purposes of type checking
    connect_slot(connect_signal())


class SimpleModel(BaseModel):
    """A simple model"""

    a: int
    b: str


class SimpleModel2(BaseModel):
    """A simple model"""

    c: int
    d: str


def main() -> None:
    a = TestFilter(SimpleModel)
    b = TestFilter(SimpleModel2)

    connect(a.output, b.input)

The diagnostics from mypy and Pyright are as follows (error is on the final line of code):

Diagnostics:
1. mypy: Cannot infer type argument 1 of "connect" [misc]
2. Pyright: Argument of type "(obj: SimpleModel2) -> None" cannot be assigned to parameter "connect_slot" of type "(T@connect) -> None" in function "connect"
     Type "(obj: SimpleModel2) -> None" cannot be assigned to type "(SimpleModel) -> None"
       Parameter 1: type "SimpleModel" cannot be assigned to type "SimpleModel2"
         "SimpleModel" is incompatible with "SimpleModel2" [reportGeneralTypeIssues]

Pitch

Having the error messages of mypy more in-line with Pyright's reporting would mean that it is not essential to use Pyright's typechecking system when developing these sorts of generic systems.

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

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

はじめの一歩

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

調査の方向性

まず、提供された Python の例を mypy 1.4.1 で再現し、その最終診断を issue に示されている Pyright の出力と比較します。mypy が互換性のない Generic Callable 引数を、同程度に対処可能な型の詳細とともに説明できれば完了です。issue には対象とするソースファイルやテストの指定はありません。

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

評価

技術スタック
python
領域
devtools
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

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

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