python / python/mypy

meet.is_overlapping_types should be more relaxed for isinstance checks

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

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

needs discussion priority-1-normal refactoring topic-reachability topic-type-narrowing
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

Currently, the implementation of meet.is_overlapping_types(...) defaults to returning False when it encounters two unexpected types, and doesn't attempt to handle multiple inheritance at all. The current implementation also considers two types like List[A] and List[B] to be non-overlapping, even though they may overlap when both lists are empty.

This behavior makes sense when is_overlapping_types is used to detect unsafe overload variants, but may be too restrictive/cause branches to be unexpectedly unreachable when used for isinstance checks and if x is None checks and the like.

For more context, see https://github.com/python/mypy/pull/5476#discussion_r211030040 and https://github.com/python/mypy/issues/3603.

Some possible solutions include:

  1. Add a flag that makes is_overlapping_types default to returning True, not False. The main disadvantage here is that this would then make is_overlapping_types consider two unrelated types like Type[A] and TypedDict[...] to be overlapping, which seems wrong.
  2. Add a flag named allow_multiple_inheritance which makes the "if left and right are Instances" case just always return True. The main disadvantage here is that this may not capture every edge case we want to relax.

It may also be the case that neither solutions work and a more careful rearrangement of is_overlapping_types is necessary. The existing implementation of is_overlapping_types is designed to rule out bad combinations/normalize all types to Instance whenever possible, which may or may not be the correct thing to do for reachability checks.

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

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

はじめの一歩

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

調査の方向性

まず meet.is_overlapping_types の実装と、PR 5476 および issue 3603 の議論を読んでください。予期しない型、多重継承、List[A] と List[B] の場合における現在の動作を比較し、どの到達可能性の動作が意図されているのかを判断してください。isinstance と None のチェックを緩和しつつ、overload の安全性チェックを誤って広げない方針が決定されていれば完了です。

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

評価

技術スタック
python
領域
compilers
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
25/100

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

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