python / python/mypy

Incorrect type check for nested unions of dicts

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

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

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

説明

Bug Report

For all types X and Y, if a type checks correctly as X it should also check correctly as Union[X, Y].

This is not the case for at least one specific combination of unions and dicts. See reproduction below.

To Reproduce

from typing import Dict, Union

A = Dict[str, Union[str, Dict[str, str]]]
B = Union[A, Dict[str, str]]

example1: A = {"x": {"y": "z"}}
example2: B = {"x": {"y": "z"}}

Playground URL: https://mypy-play.net/?mypy=latest&python=3.11&gist=c72e66a8600324e7b1c1af24a0c533ca

Expected Behavior
example1 and example2 type check as valid.

Actual Behavior

example2 fails to type check with:

main.py:7: error: Incompatible types in assignment (expression has type "Dict[str, Dict[str, str]]", variable has type "Union[Dict[str, Union[str, Dict[str, str]]], Dict[str, str]]")  [assignment]
Found 1 error in 1 file (checked 1 source file)

Your Environment

Python 3.10 and 3.11.
Mypy 1.3.0

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

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

はじめの一歩

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

調査の方向性

まず、提供されている Python の再現コードを実行し、リンクされた mypy playground で 2 つの代入の動作を比較します。dicts を含むネストされた unions に対する型チェックの経路を追跡します。example1 と example2 の両方が、報告された代入エラーなしで型チェックを通れば完了です。

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

評価

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

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

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