← 输入框|bugs.python.org|刷新

➜

This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
标题: MappingProxyType can not be pickled
类型: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.6
process
状态: closed Resolution: not a bug
Dependencies: 后续:
分配给: 抄送列表: Alex Hayes, rhettinger
优先级: normal 关键字:

Created on 2017-08-15 04:49 by Alex Hayes, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (5)
msg300284 - (view) Author: Alex Hayes (Alex Hayes) 日期: 2017-08-15 04:49
I imagine that this is by design (likely because it can't be implemented) however I wanted to check if this was the case.

Instances of types.MappingProxyType can't be pickled.

For example;

```
import pickle
from types import MappingProxyType
eggs = MappingProxyType(dict(sausage=True))
pickle.dumps(eggs)
```

Raises: TypeError: can't pickle mappingproxy objects

----

Is this the desired behaviour or is it a bug?
msg300324 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) 日期: 2017-08-16 03:33
What would be the point of pickling a MappingProxy?  Do you have use case or is it just a curiosity?
msg300735 - (view) Author: Alex Hayes (Alex Hayes) 日期: 2017-08-23 00:29
Some background. I'm not actually trying to pickle MappingProxyType, I'm using the schematics [1] library and recently it broke support for pickle [2] and the cause of that is because they are using MappingProxyType [3] internally (when they used to use dict).

The Python docs on MappingProxyType [4] provides an appropriate level of API documentation but what it doesn't cover is when you should or should not use MappingProxyType (understandably, it's most likely a complex answer). However, I imagine that it's being used in schematics because at the bottom of the Python dict documentation [5] it states;

> See also types.MappingProxyType can be used to create a read-only view of a dict.

To answer the question "What would be the point of pickling a MappingProxy?" I would probably have to ask, what is the intended use of MappingProxyType?

If one was unfamiliar with the way in which pickle and MappingProxyType works I imagine they would think there would be no reason why a "read-only view of a dict" can't be pickled.

So I guess my questions now are;

1. Should types.MappingProxyType be pickleable?
2. Should there be some documentation about the intended use of MappingProxyType?

I imagine the answer to the first question is "no" given how `__setstate__` works.


----

[1] https://github.com/schematics/schematics
[2] https://github.com/schematics/schematics/issues/510
[3] https://github.com/schematics/schematics/pull/511
[4] https://docs.python.org/3/library/types.html#types.MappingProxyType
[5] https://docs.python.org/3/library/stdtypes.html#dict
msg300738 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) 日期: 2017-08-23 02:43
> So I guess my questions now are;
> 
> 1. Should types.MappingProxyType be pickleable?
> 2. Should there be some documentation about the intended use of
> MappingProxyType?
>
> I imagine the answer to the first question is "no" given 
> how `__setstate__` works.

For the second question, I don't think so.  AFAICT the only intention was to support the actual mapping views for regular dicts.  Its likely that not much thought was given to other uses, so it doesn't make sense to document intention when none really exists ;-)

Given the answers above and that you have a PR solve your upstream issue with Schematics, can this bug report be closed?
msg300947 - (view) Author: Alex Hayes (Alex Hayes) 日期: 2017-08-28 09:56
Closing issue - thanks rhettinger
历史
日期 用户 动作 参数
2022-04-11 14:58:50admin修改github: 75392
2017-08-28 09:56:42Alex Hayes修改状态: open -> closed
resolution: not a bug
消息: + msg300947

stage: resolved
2017-08-23 02:43:39rhettinger修改消息: + msg300738
2017-08-23 00:29:33Alex Hayes修改消息: + msg300735
2017-08-16 03:33:07rhettinger修改抄送: + rhettinger
消息: + msg300324
2017-08-15 04:49:11Alex Hayes创建