Skip to content

typing.NamedTuple instances are not picklable Two #72437

Closed

Description

@Kurt
mannequin
BPO 28250
Nosy @mdickinson, @ilevkivskyi

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 2016-09-24.07:43:35.706>
created_at = <Date 2016-09-22.19:45:06.272>
labels = ['invalid', 'library']
title = 'typing.NamedTuple instances are not picklable Two'
updated_at = <Date 2016-09-24.17:07:45.226>
user = 'https://bugs.python.org/Kurt'

bugs.python.org fields:

activity = <Date 2016-09-24.17:07:45.226>
actor = 'Kurt'
assignee = 'none'
closed = True
closed_date = <Date 2016-09-24.07:43:35.706>
closer = 'mark.dickinson'
components = ['Library (Lib)']
creation = <Date 2016-09-22.19:45:06.272>
creator = 'Kurt'
dependencies = []
files = []
hgrepos = []
issue_num = 28250
keywords = []
message_count = 3.0
messages = ['277236', '277311', '277321']
nosy_count = 3.0
nosy_names = ['mark.dickinson', 'levkivskyi', 'Kurt']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue28250'
versions = ['Python 3.5']

Activity

  1. Kurt commented on Sep 22, 2016

    Kurtmannequin
    MannequinAuthor

    Creating a namedtuple and an instance of the namedtuple in a function then returning the instance to the global namespace made the instance unpickleable, as in bpo-25665.

  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Sep 22, 2016
  3. mdickinson commented on Sep 24, 2016

    @mdickinson
    Member

    I don't think this has anything to do with namedtuple; it's true whenever you create a class in an inner scope (rather than at module level). This is by design, and these restrictions are documented: https://docs.python.org/3.6/library/pickle.html#what-can-be-pickled-and-unpickled

    For example, running this script:

        import pickle
    
        def my_func():
            class A:
                pass
    
            a = A()
            return a
    
        a = my_func()
        pickle.dumps(a)

    produces:

        Traceback (most recent call last):
          File "test.py", line 11, in <module>
            pickle.dumps(a)
        AttributeError: Can't pickle local object 'my_func.<locals>.A'
  4. Kurt commented on Sep 24, 2016

    Kurtmannequin
    MannequinAuthor

    My bad, I searched and found the issue, it very closely fit mine and the pickle module is new to me.  I hadn't yet got through  the details of  pickling.  Thanks for catching that.
    Kurt

      From: Mark Dickinson \<[email protected]\>
    

    To: [email protected]
    Sent: Saturday, September 24, 2016 1:43 AM
    Subject: [bpo-28250] typing.NamedTuple instances are not picklable Two

    Mark Dickinson added the comment:

    I don't think this has anything to do with namedtuple; it's true whenever you create a class in an inner scope (rather than at module level). This is by design, and these restrictions are documented: https://docs.python.org/3.6/library/pickle.html#what-can-be-pickled-and-unpickled

    For example, running this script:

        import pickle

        def my_func():
            class A:
                pass

            a = A()
            return a

        a = my_func()
        pickle.dumps(a)

    produces:

        Traceback (most recent call last):
          File "test.py", line 11, in <module>
            pickle.dumps(a)
        AttributeError: Can't pickle local object 'my_func.<locals>.A'

    nosy: +mark.dickinson
    resolution:  -> not a bug
    status: open -> closed


    Python tracker <[email protected]>
    <http://bugs.python.org/issue28250\>


  5. transferred this issue fromon Apr 10, 2022
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/ directory

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions