python / python/mypy

improper inference of lambdas and dataclass constructors used as generic decorator arguments

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

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

bug topic-type-context
主要言語
Python
スター
20.6k
フォーク
3.3k
平均マージ
1日 18時間
マージ済み PR(30日)
54

説明

Bug Report

I'm attempting to make some registry-like decorators to be generic in my code and bumped into mypy complaining about my constructs.

The point is: concrete decorators should be defined with [HandlerT: Handler] type variable. It's to correctly decorate functions with non-exact (but matching) call signatures. Signatures should be preserved.
The rest registry functionality to be handled with broadly generic _decorate function.

I supposed decorate1, decorate2 and decorate3 should behave the same. Among them, lambda version preferred due to its conciseness and flexibility. Nevertheless, only decorate3 passes mypy checks. FTR, pyright passes them all.

To Reproduce

from collections.abc import Callable
from dataclasses import dataclass

type Handler = Callable[[int], str]

@dataclass
class Wrapper:
    handler: Handler

registry: dict[str, Wrapper] = {}

def _decorate[WrappedT](
        key: str, wrapper: Callable[[WrappedT], Wrapper]) -> Callable[[WrappedT], WrappedT]:
    def _wrap(handler: WrappedT) -> WrappedT:
        registry[key] = wrapper(handler)
        return handler
    return _wrap

def decorate1[HandlerT: Handler](key: str) -> Callable[[HandlerT], HandlerT]:
    return _decorate(key, Wrapper)   # line 27

def decorate2[HandlerT: Handler](key: str) -> Callable[[HandlerT], HandlerT]:
    return _decorate(key, lambda handler: Wrapper(handler))   # line 31

def decorate3[HandlerT: Handler](key: str) -> Callable[[HandlerT], HandlerT]:
    def _wrapper(handler: HandlerT) -> Wrapper:
        return Wrapper(handler)
    return _decorate(key, _wrapper)

@decorate1("add1")
@decorate2("add2")
@decorate3("add3")
def add_handler(x: int, y: int = 0) -> str:
    return str(x + y)

@decorate1("inc1")
@decorate2("inc2")
@decorate3("inc3")
def inc_handler(x: int) -> str:
    return add_handler(x, 1)

Expected Behavior

all of decorate1, decorate2 and decorate3 forms are correct, no errors shown

Actual Behavior

wrappers.py:27: error: Incompatible return value type (got "Callable[[Callable[[int], str]], Callable[[int], str]]", expected "Callable[[HandlerT], HandlerT]")  [return-value]
wrappers.py:31: error: Argument 1 to "Wrapper" has incompatible type "WrappedT"; expected "Callable[[int], str]"  [arg-type]

Your Environment

  • Mypy version used: 1.19.1
  • Mypy command-line flags: -
  • Mypy configuration options from mypy.ini (and other config files): -
  • Python version used: 3.12

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

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

はじめの一歩

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

調査の方向性

まず、報告された再現例を wrappers.py に配置し、提示された Python 3.12 コードで mypy 1.19.1 を実行します。decorate1、decorate2、decorate3 の診断結果を比較し、その後、dataclass コンストラクターと lambda に対するジェネリック callable の推論を追跡します。3 つすべてのデコレーター形式がエラーなしで型チェックを通過し、報告されたシグネチャを維持できれば完了です。

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

評価

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

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

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