python / python/typeshed

`AbstractContextManger.__enter__` should be abstract

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

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

stubs: improvement
主要言語
Python
スター
5.1k
フォーク
2.1k
平均マージ
1日 19時間
マージ済み PR(30日)
82

説明

Code sample in basedpyright playground

from contextlib import AbstractContextManager
from typing import override


class CM(AbstractContextManager[int]):
    @override
    def __exit__(self, *_): ...

with CM() as i:
    print(i + 1)

AbstractContextManger.__enter__ has a default implementation that returns self, so by making it abstract you would require implementations to provide the correct implementation. this would force a redundant implementation if the type is Self, but that is currently impossible to type anyway: https://discuss.python.org/t/self-as-typevar-default/90939

even without Self support, it is only one line of redundant code: def __enter__(self) -> Self: return self

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

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

はじめの一歩

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

調査の方向性

basedpyright playground example と、それが検証する AbstractContextManager stub entry point から始めます。現在のデフォルトの enter が抽象クラスのチェックにどのような影響を与えるかを確認し、その後、それを抽象化すると実装が必要になることを、意図されたコンテキストマネージャーの型付け動作を維持したまま検証します。

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

評価

技術スタック
python
領域
tooling
issue の種類
機能追加
難易度
2/5
見積もり時間
1〜3時間
活発さ
静か
明瞭さ
おおむね明確
初心者へのやさしさ
55/100

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

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