python / python/mypy

Optional types not being narrowed after a logical-and followed by a test of one of the operands

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

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

false-positive feature priority-2-low
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

(Sorry if the title is overly specific)

  • Are you reporting a bug, or opening a feature request?

Bug

  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.
from typing import Optional

def get_org_id(org_name: str) -> str:
    return 'some_org_id'

def create(org_name: Optional[str], org_id: Optional[str]):
    if not org_name and not org_id:
        raise ValueError('No org specified')
    if not org_id:
        org_id = get_org_id(org_name)
  • What is the actual behavior/output?

The inferred type of org_name at the last line is Optional[str] (technically Union[builtins.str, None]).

  • What is the behavior/output you expect?

The type should be narrowed to str.

  • What are the versions of mypy and Python you are using?
    Do you see the same issue after installing mypy from Git master?

Python 3.7.3
mypy: 0.701

  • What are the mypy flags you are using? (For example --strict-optional)

No flags

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

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

はじめの一歩

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

調査の方向性

issue にある最小限の Python 再現コードから始め、論理 AND 条件の後で org_name に推論される型を確認します。org_name と org_id の型の絞り込みの動作を追跡し、このケースのカバレッジを追加します。完了条件は、関連するオプショナル型の絞り込みを壊すことなく、get_org_id の呼び出し時に org_name が str として推論されることです。

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

評価

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

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

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