News for hackers. Stuff that matters.
Re:Namedtuple in a Post-Dataclasses World by wizzwizz4 on Wednesday July 21, 2021 @04:43PM (#27911913)
This breaks pickle, among other things.
Re:Namedtuple in a Post-Dataclasses World by Spivak on Wednesday July 21, 2021 @05:06PM (#27912174)
It really shouldn't. This is actually an example of pickle being broken.

The class exists, there is an existing valid reference to it, and Python itself knows about it and the class itself knows its own name and namedtuple generates the right __getnewargs__. Everything is there for this to "just work" but pickle expects that every class object will have a reference to it of the same name which is kinda weird when you think about it.

You can see it with this stupid little program.

    from collections import namedtuple
    import pickle

    def get_all_subclasses(cls):
        all_subclasses = []

        for subclass in cls.__subclasses__():
            if subclass != type:
                all_subclasses.append(subclass)
                all_subclasses.extend(get_all_subclasses(subclass))

        return all_subclasses

     E = namedtuple('EEEEEEEEEEEEEEEEEEE', 'x')
     e = E(x='hello')

     for cls in get_all_subclasses(object):
          print(cls)

     pickle.dumps(e)
You'll that the class it there and called the right thing! But pickle tries to look up a reference to it under __main__.
Re:Namedtuple in a Post-Dataclasses World by wizzwizz4 on Friday July 23, 2021 @03:10PM (#27934456)
> but pickle expects that every class object will have a reference to it of the same name

Yeah: its qualname. The qualname, as per PEP 3155, is defined as:

> a dotted path leading to the object from the module top-level

so of course pickle can't cope with this information being incorrect. (How would you make it work?)

Re:Namedtuple in a Post-Dataclasses World by Spivak on Sunday July 25, 2021 @06:19PM (#27953469)
There’s a difference between the classes __qualname__ and the requirement that there be reference to the class at, say, __main__.$qualname as the way for pickle to actually find the class object.

This is the part that, to me, is really odd because pickle knows (in theory) the class and module name of the thing it needs to instantiate and the class objects themselves know their name and module.

Up to the lack efficiency actually doing this you could just enumerate all class objects to find the one with the right name and module name.

You get nested classes for free this way and you break the requirement that there be references to class objects of the same name in the module.