jsonargparse
jsonargparse copied to clipboard
`MappingProxyType` not accepted as `Mapping`
🐛 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
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.
Support for MappingProxyType has been added with #540.