python / python/mypy

mypy fails to recognize functions as generators when `sys.platform` is used in conditions

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

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

bug priority-1-normal topic-runtime-semantics
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

mypy seems to be too thorough when interpreting if sys.platform == ... conditions. Consider the following file:

import sys
from typing import Generator

def f() -> Generator[int, None, None]:
    if sys.platform == "darwin":
        yield 1

I'm trying to run mypy for this file on Linux using mypy gen.py

Expected behavior:
mypy test passes for this file on Linux

Actual behavior:
I get this on Linux:

$ mypy gen.py 
gen.py:4: error: Missing return statement

At the same time, mypy on macOS does not complain. I think that this is related to #698

Platform information:

$ uname -sr; mypy --version; python --version
Linux 4.15.0-34-generic
mypy 0.630
Python 2.7.15rc1

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

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

はじめの一歩

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

調査の方向性

gen.py の例を使って報告を再現し、Linux で mypy を実行してから、その結果を macOS の挙動と比較します。checker が sys.platform の条件とジェネレーター関数をどのように処理するかを追跡します。Linux で例が return 文の欠落を報告しなくなり、macOS の結果にリグレッションが発生しなければ完了です。

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

評価

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

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

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