`[Async]ContextDecorator.__call__` return type should depend on the underlying context manager's `_ExitT_co`
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 5.1k
- フォーク
- 2.1k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 82
説明
[!Important]
Please see https://discuss.python.org/t/support-suppressing-generator-context-managers/103615 first!
This is how ContextDecorator.__call__ is implemented in the Python standard library:
def __call__(self, func):
@wraps(func)
def inner(*args, **kwds):
with self._recreate_cm():
return func(*args, **kwds)
return inner
In typeshed, ContextDecorator.__call__ currently has a return type of the original callable (func).
However, if the return value of self._recreate_cm().__call__ is truthy, the return type of ContextDecorator.__call__ can as well be None, like in the silly example below:
from collections.abc import Generator
from contextlib import contextmanager, suppress
from typing import reveal_type
@contextmanager
def zero_division_to_none() -> Generator[None]:
with suppress(ZeroDivisionError):
yield
@zero_division_to_none()
def div(a: int, b: int) -> float:
return a / b
reveal_type(div(1, 0))
# mypy, pyright: Revealed type is "builtins.float"
# Runtime type is 'NoneType'
My first shot was to patch the proper __call__ functions to "extend" the return type with _ThrottledExcReturnT (a new type parameter of ContextDecorator), constrained by Never and None, with the default type Never for backward compatibility. To me, there's no way to declare the relationship symbolically, so it would have to be typed manually in classes implementing the context manager protocol.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
リンクされた Python の議論と、Lib/contextlib.py にある標準ライブラリの実装から始め、ContextDecorator.call と、基盤となるコンテキストマネージャーの _ExitT_co に焦点を当ててください。ラップされた呼び出し可能オブジェクトの戻り値の型について、サポートされる型レベルの関係を特定し、その後、アノテーションがコンテキストマネージャーによる抑制が None を返すケースを反映していることを確認してください。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- developer-experience
- issue の種類
- バグ
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- 説明が足りない
- 初心者へのやさしさ
- 35/100