`and` fails to refine `Optional[T]`

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

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

評価

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

調査の方向性

optional_refine_and.py の再現コードから始め、optional_refine_or.py の失敗する双対ケースとその挙動を比較します。例に対して静的コンパイラーを実行し、報告された型の絞り込みを調べます。and プログラムが受け入れられ、or プログラムが None を expect_int に渡すことを引き続き拒否すれば完了です。

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

説明

sp-correctness staticpython

a4a71ae
2021-12-29

What program did you run?
# optional_refine_and.py
# This should pass.
# This should terminate.

from typing import Optional

def expect_int(i: int) -> None:
    return

def f(x: Optional[int]) -> None:
    return x and expect_int(x)

assert f(None) is None
assert f(42) is None
What happened?
    return x and expect_int(x)
    ^
compiler.errors.TypedSyntaxError: return type must be None, not Optional[int]
What should have happened?

The program should be accepted by the type checker.

Note that a dual test fails as expected.

[root@0fcdc5077884 vol]# cat Playground/conformance_suite/optional_refine_or.py 
# optional_refine_or.py
# This should fail.

from typing import Optional

def expect_int(i: int) -> int:
    return 42

def f(x: Optional[int]) -> int:
    return x or expect_int(x)
[root@0fcdc5077884 vol]# ./python -m compiler --static Playground/conformance_suite/optional_refine_or.py 
Traceback (most recent call last):
  ....
  File "Playground/conformance_suite/optional_refine_or.py", line 10
    return x or expect_int(x)
                          ^
compiler.errors.TypedSyntaxError: type mismatch: None received for positional arg 'i', expected int
[root@0fcdc5077884 vol]# 
主要言語
Python
スター
3.8k
フォーク
139
PR マージ指標
30日以内にマージされた PR はありません

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

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

はじめの一歩

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

facebookincubator/MetaPython のほかの issue

facebookincubator/MetaPython の issue をすべて見る

似ている issue

Python の issue をもっと見る

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

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