Skip to content

MappingProxyType can not be pickled #75392

Description

@AlexHayes
mannequin
BPO 31209
Nosy @rhettinger

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2017-08-28.09:56:42.114>
created_at = <Date 2017-08-15.04:49:11.844>
labels = ['invalid', 'type-bug', 'library']
title = 'MappingProxyType can not be pickled'
updated_at = <Date 2017-08-28.09:56:42.112>
user = 'https://bugs.python.org/AlexHayes'

bugs.python.org fields:

activity = <Date 2017-08-28.09:56:42.112>
actor = 'Alex Hayes'
assignee = 'none'
closed = True
closed_date = <Date 2017-08-28.09:56:42.114>
closer = 'Alex Hayes'
components = ['Library (Lib)']
creation = <Date 2017-08-15.04:49:11.844>
creator = 'Alex Hayes'
dependencies = []
files = []
hgrepos = []
issue_num = 31209
keywords = []
message_count = 5.0
messages = ['300284', '300324', '300735', '300738', '300947']
nosy_count = 2.0
nosy_names = ['rhettinger', 'Alex Hayes']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue31209'
versions = ['Python 3.6']

Activity

  1. AlexHayes commented on Aug 15, 2017

    AlexHayesmannequin
    MannequinAuthor

    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?

  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    type-bugAn unexpected behavior, bug, or error
    on Aug 15, 2017
  3. rhettinger commented on Aug 16, 2017

    @rhettinger
    Contributor

    What would be the point of pickling a MappingProxy? Do you have use case or is it just a curiosity?

  4. AlexHayes commented on Aug 23, 2017

    AlexHayesmannequin
    MannequinAuthor

    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] schematics/schematics#510
    [3] schematics/schematics#511
    [4] https://docs.python.org/3/library/types.html#types.MappingProxyType
    [5] https://docs.python.org/3/library/stdtypes.html#dict

  5. rhettinger commented on Aug 23, 2017

    @rhettinger
    Contributor

    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?

  6. AlexHayes commented on Aug 28, 2017

    AlexHayesmannequin
    MannequinAuthor

    Closing issue - thanks rhettinger

  7. transferred this issue fromon Apr 10, 2022
  8. eroller commented on Dec 13, 2023

    @eroller

    I think some clever python developers may inadvertently use it as a default value for an argument with mypy annotation of Mapping hoping to workaround the age-old issue with having mutable default arguments in python. A workaround for default arguments with mypy annotation of Sequence exists by using a tuple which is inherently immutable.

    For symmetry and to enable an immutable default argument for Mappings it would be nice if pickle could support MappingProxyType

    See related: https://peps.python.org/pep-0416/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions