python / python/mypy

Support file-level assertions as inline configuration

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

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

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

説明

Feature

One would be able to do:

# mypy: assert=platform == "win32" and python_version >= "3.10"

and the file would only be checked if sys.platform is win32 and the tuple sys.version_info is greater than or equal to (3, 10). This is similar to the environment markers used in dependency definitions but with only support for the two static platform variables that are already used by Mypy.

Implementation-wise, there would be a pre-processing step that transforms the Python version string (if present) into a tuple. This way you can pass those two variables directly to eval.

Pitch

This would significantly help in situations such as https://github.com/python/mypy/issues/19009. Brief recap:

When you have the following provider module:

if sys.platform == "win32":
    from my_pkg._pty.windows import PtySession
else:
    from my_pkg._pty.unix import PtySession

and attempt importing this symbol from somewhere else, Mypy does not respect the platform condition.

For example, if you call pty.openpty() inside my_pkg._pty.unix and run Mypy on Windows it will show:

Module has no attribute "openpty"  [attr-defined]

The best solution available is adding a top-level assertion as introduced here. For example, on top of the module my_pkg._pty.unix you would put assert sys.platform != "win32".

This is suboptimal because it requires modifying runtime behavior. The sys import is required, perhaps only for the assertion, and if there are imports that only exist conditionally the assertion has to be in the middle of the standard import block on top.

Adding a file-level comment is the most pragmatic option at hand, in lieu of adding an option that automatically propagates conditional static assertions to enclosed imports.

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

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

はじめの一歩

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

調査の方向性

まず、Mypy が既存のトップレベルのプラットフォームおよび Python バージョンのアサーションをどのように処理しているかを追跡し、次に、インラインのファイルディレクティブを認識できる設定または preprocessing の経路を調べます。提案されている 2 変数の評価を、現在のプラットフォーム設定の動作と比較します。ファイルレベルのアサーションによって実行時の動作を変更せずにチェックを条件付きで有効化でき、サポート対象のプラットフォーム式およびバージョン式に対するカバレッジがあれば完了です。

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

評価

技術スタック
python
領域
compilers, devtools
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
42/100

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

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