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
Title: typing.NamedTuple instances are not picklable Two
Type: Stage:
Components: Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Kurt, levkivskyi, mark.dickinson
Priority: normal Keywords:

Created on 2016-09-22 19:45 by Kurt, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg277236 - (view) Author: Kurt Dally (Kurt) Date: 2016-09-22 19:45
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 Issue25665.
msg277311 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2016-09-24 07:43
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'
msg277321 - (view) Author: Kurt Dally (Kurt) Date: 2016-09-24 17:07
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 <report@bugs.python.org>
 To: thedomesticdog@yahoo.com 
 Sent: Saturday, September 24, 2016 1:43 AM
 Subject: [issue28250] 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 <report@bugs.python.org>
<http://bugs.python.org/issue28250>
_______________________________________
History
Date User Action Args
2022-04-11 14:58:37adminsetgithub: 72437
2016-09-24 17:07:45Kurtsetmessages: + msg277321
2016-09-24 07:43:35mark.dickinsonsetstatus: open -> closed

nosy: + mark.dickinson
messages: + msg277311

resolution: not a bug
2016-09-23 22:07:59levkivskyisetnosy: + levkivskyi
2016-09-22 19:45:06Kurtcreate