omni-us / jsonargparse

Implement minimal boilerplate CLIs derived from type hints and parse from command line, config files and environment variables
https://jsonargparse.readthedocs.io
MIT License
302 stars 41 forks source link

`MappingProxyType` not accepted as `Mapping` #539

Closed falckt closed 3 weeks ago

falckt commented 3 weeks ago

šŸ› 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

mauvilsa commented 3 weeks ago

Thank you very much for reporting! Indeed MappingProxyType is not yet officially supported and can be added. But I wouldn't consider it a bug. Changing it to a feature request.

mauvilsa commented 3 weeks ago

Support for MappingProxyType has been added with #540.