python / python/mypy

TypeVar in higher order decorator with type alias not resolved properly

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

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

bug topic-type-alias topic-type-variables
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

Bug Report

from collections.abc import Callable

type A1[**P, R] = Callable[[Callable[P, R]], Callable[P, R]]
type A2[**P, R] = Callable[P, R]


def decorator[**P, R]() -> A1[P, R]:
    def inner(func: Callable[P, R]) -> Callable[P, R]:
        return func
    return inner

def decorator_working[**P, R]() -> Callable[[A2[P, R]], A2[P, R]]:
    def inner(func: Callable[P, R]) -> Callable[P, R]:
        return func
    return inner


@decorator()  # error
def f1(x: str) -> int:
    return 1

@decorator_working()
def f2(x: str) -> int:
    return 1


reveal_type(f1)
reveal_type(f2)
Condensed version for debugging
# mypy: allow-empty-bodies
from collections.abc import Callable

type A1[**P, R] = Callable[[Callable[P, R]], Callable[P, R]]
type A2[**P, R] = Callable[P, R]

def decorator[**P, R]() -> A1[P, R]: ...
def decorator_working[**P, R]() -> Callable[[A2[P, R]], A2[P, R]]: ...

@decorator()
def f1(x: str) -> int: ...

@decorator_working()
def f2(x: str) -> int: ...

reveal_type(f1)
reveal_type(f2)

Expected Behavior
Mypy should be able to resolve the TypeVars for decorator just like it does for decorator_working.

Actual Behavior

error: Argument 1 has incompatible type "Callable[[str], int]"; expected "Callable[[VarArg(Never), KwArg(Never)], Never]"  [arg-type]
note: Revealed type is "def (*Never, **Never) -> Never"
note: Revealed type is "def (x: builtins.str) -> builtins.int"

Your Environment

  • Mypy version used: mypy 1.15.0+dev.68cffa7afe03d2b663aced9a70254e58704857db (compiled: no)
  • Python version: 3.13

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

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

はじめの一歩

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

調査の方向性

まず、Python 3.13 上で mypy を使って issue 内の簡略化された例を再現し、その後 decorator と decorator_working の診断結果および明らかになった型を比較します。高階デコレーターと型エイリアスの推論経路を追跡します。完了条件は、decorator が decorator_working と同様に TypeVars を解決し、f1 を受け入れ、エラーなしで期待される callable 型を明らかにすることです。

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

評価

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

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

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