pyo3 icon indicating copy to clipboard operation
pyo3 copied to clipboard

Add support for mappingproxy? (standard library read-only dict)

Open aldanor opened this issue 3 years ago • 1 comments

Quoting Python docs for vars:

Objects such as modules and instances have an updateable __dict__ attribute; however, other objects may have write restrictions on their __dict__ attributes (for example, classes use a types.MappingProxyType to prevent direct dictionary updates).

There's pyo3::ffi::PyDictProxy_Type and pyo3::ffi::PyDictProxy_New but they are currently unused.

It's almost a copy of PyDict, with only these methods present: copy, get, items, keys, values.

This has come up while implementing structured dtypes in rust-numpy, where dtype.fields is a read-only mappingproxy dict. It could be worked around via PyAny, but I figured maybe I should raise it here in case there's any interest.

aldanor avatar Jan 17 '22 13:01 aldanor

Yep I think having this is very reasonable. Anyone is welcome to pick this up - should be a case of copying the relevant parts of the PyDict implementation to create a new type. Perhaps the hardest bit is what to call it - the C API is PyDictProxy, but I wonder if PyMappingProxy is better to reflect it's name from Python?

davidhewitt avatar Jan 19 '22 22:01 davidhewitt