python / python/mypy

overload and contextmanager doesn't work together

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

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

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

説明

Bug Report

contextmanager and overload doesn't seem to be compatbile together

To Reproduce

from contextlib import contextmanager
from typing import overload, ContextManager, Iterator
from uuid import UUID

@overload
def cactus() -> ContextManager[int]:
    ...

@overload
def cactus(_id: str) -> ContextManager[UUID]:
    ...

@contextmanager
def cactus(_id: str | None = None) -> Iterator[UUID] | Iterator[int]:
    if not _id:
        yield 1
    yield UUID(_id)

Expected Behavior

Mypy should find no issue

Actual Behavior

test.py: note: In function "cactus":
test.py:16:2: error: Overloaded function implementation cannot produce return type of signature 1  [misc]
    @contextmanager
     ^
test.py:16:2: error: Overloaded function implementation cannot produce return type of signature 2  [misc]
    @contextmanager
     ^
Found 2 errors in 1 file (checked 1 source file)

Your Environment

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

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

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

はじめの一歩

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

調査の方向性

まず、提供された Python 3.10.6 の例を mypy 1.0.0 で再現し、@overload と @contextmanager の相互作用に重点を置きます。オーバーロードされた実装の型チェックの経路を追跡し、回帰テストを追加します。完了の条件は、関連する overload チェックを後退させることなく、例がエラーを出さないことです。

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

評価

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

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

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