jsonargparse icon indicating copy to clipboard operation
jsonargparse copied to clipboard

`MappingProxyType` not accepted as `Mapping`

Open falckt opened this issue 6 days ago • 1 comments

🐛 Bug report

Using a MappingProxyType as a read only default value for a Mapping parameter fails with

Expected a <class 'collections.abc.Mapping'>. Got value: {'parameter': 1}

To reproduce

# /// script
# dependencies = [
#   "jsonargparse[ruyaml,signatures]"
# ]
# ///


from collections.abc import Mapping
from types import MappingProxyType
from typing import Any

from jsonargparse import CLI

DEFAULTS = MappingProxyType({"parameter": 1})


def func(arg: Mapping[str, Any] = DEFAULTS):
    print(arg)


if __name__ == "__main__":
    assert isinstance(DEFAULTS, Mapping)

    CLI([func])

Running this script fails with

$> python mwe.py                                                                   
usage: mwe.py [-h] [--config CONFIG] [--print_config[=flags]] [--arg ARG]
error: Validation failed: Parser key "arg":
  Expected a <class 'collections.abc.Mapping'>. Got value: {'parameter': 1}

Expected behavior

Running as normal, which jsonargparse does once the MappingProxyType is replaced by a normal dict.

Environment

  • jsonargparse version: 4.31.0
  • Python version: 3.10.12
  • How jsonargparse was installed: in a fresh isolated environment (hatch run <script name>)
  • OS: Ubuntu 22.04.4 LTS

falckt avatar Jun 27 '24 08:06 falckt