python / python/mypy

Wrong test result for default values of Mapping arguments

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

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

bug
主要言語
Python
スター
20.6k
フォーク
3.3k
平均マージ
1日 18時間
マージ済み PR(30日)
54

説明

Bug Report

It seems that the validation of

def foo(x: Mapping[str, str] = some_default) -> None:
    pass 

yields strange results depending on the declared type of some_default.

To Reproduce

use mypy --strict on the following file

"""
This file is part of python-none-objects library.

python-none-objects is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

python-none-objects is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with python-none-objects.
If not, see <http://www.gnu.org/licenses/>.

©Copyright 2023 Laurent Lyaudet
"""
"""
The empty tuple is really useful.
But it is implementation dependent for it to be a constant:
https://docs.python.org/3.12/reference/expressions.html#parenthesized-forms
https://stackoverflow.com/questions/41983180/is-the-empty-tuple-in-python-a-constant
https://stackoverflow.com/questions/8185776/compare-object-to-empty-tuple-with-the-is-operator-in-python-2-x
https://stackoverflow.com/questions/38328857/why-does-is-return-true-when-is-and-is-return-false
https://stackoverflow.com/questions/14135542/how-is-tuple-implemented-in-cpython
I'm wondering if there would be additional efficiency gains
to treat the empty tuple and the constants here differently
at execution of Python scripts.
"""
# from typing import Iterable, Container, Collection, Mapping
from types import MappingProxyType
from typing import Any, Mapping, Never

NoneIterable = ()
NoneContainer = NoneIterable
NoneCollection = NoneIterable
NoneMapping1: Mapping[Any, str] = {123: "abc"}  # MappingProxyType({})
NoneMapping2: Mapping[str, Any] = {"abc": 123}  # MappingProxyType({})
NoneMapping3: Mapping[Never, Any] = MappingProxyType({})
NoneMapping4: Mapping[Any, Never] = MappingProxyType({})
NoneMapping5: Mapping[Any, Any] = {"abc": 123}  # MappingProxyType({})
NoneMapping6: Mapping[Never, Never] = MappingProxyType({})


def foo1(x: Mapping[str, str] = NoneMapping1) -> None:
    # Pass typing but {123: "abc"} is a Mapping[Any, "str"]
    # and it should not be valid.
    for y, z in x.items():
        print(f"foo {y} bar {z}")


def foo2(x: Mapping[str, str] = NoneMapping2) -> None:
    # Pass typing but {"abc": 123} is a Mapping[str, Any]
    # and it should not be valid.
    for y, z in x.items():
        print(f"foo {y} bar {z}")

def foo3(x: Mapping[str, str] = NoneMapping3) -> None:
    # Fails typing but only an empty mapping is a Mapping[Never, Any]
    # and it is also a Mapping[str, str].
    for y, z in x.items():
        print(f"foo {y} bar {z}")


def foo4(x: Mapping[str, str] = NoneMapping4) -> None:
    # Pass typing
    for y, z in x.items():
        print(f"foo {y} bar {z}")


def foo5(x: Mapping[str, str] = NoneMapping5) -> None:
    # Pass typing but {"abc": 123} is a Mapping[Any, Any]
    # and it should not be valid.
    for y, z in x.items():
        print(f"foo {y} bar {z}")


def foo6(x: Mapping[str, str] = NoneMapping6) -> None:
    # Fails typing but only an empty mapping is a Mapping[Never, Never]
    # and it is also a Mapping[str, str].
    for y, z in x.items():
        print(f"foo {y} bar {z}")

# Or even better, a reproducible playground link https://mypy-play.net/ (use the "Gist" button)

Expected Behavior

I thought that cases 3, 4, 6 would pass since the only possible value for the default is an empty mapping.
And I thought that cases 1, 2, 5 would fail.

Actual Behavior

$mypy --strict ./test_mypy_on_pno.py
test_mypy_on_pno.py:60: error: Incompatible default for argument "x" (default has type "Mapping[NoReturn, Any]", argument has type "Mapping[str, str]") [assignment]
test_mypy_on_pno.py:80: error: Incompatible default for argument "x" (default has type "Mapping[NoReturn, NoReturn]", argument has type "Mapping[str, str]") [assignment]

Only cases 3 and 6 fails, when both should pass.

Your Environment

  • Mypy version used: mypy 1.8.0 (compiled: yes)
  • Mypy command-line flags: --strict
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: Python 3.11

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

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

はじめの一歩

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

調査の方向性

まず、提供された再現プログラムを mypy 1.8.0 と --strict フラグで実行し、foo1 から foo6 までについて報告された結果を期待される動作と比較します。ケース 3、4、6 がパスし、ケース 1、2、5 が互換性のないデフォルト値として拒否されれば完了です。

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

評価

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

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

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