Currently namedtuple(https://hg.python.org/cpython/file/3.5/Lib/collections/__init__.py#l418) sets the `__module__` attribute by looking up `__name__` in calling frame's globals. As in the case of `typing.NamedTuple` it is always going to be 'typing' pickle will raise an error. Instead of this `typing.NamedTuple` should override the `__module__` attribute itself because it has info about the actual caller frame. Something like this should work fine: ``` def NamedTuple(typename, fields): fields = [(n, t) for n, t in fields] cls = collections.namedtuple(typename, [n for n, t in fields]) cls._field_types = dict(fields) try: cls.__module__ = sys._getframe(1).f_globals.get('__name__', '__main__') except (AttributeError, ValueError): pass return cls ``` Related: http://stackoverflow.com/q/33796490/846892
The test tests pickling only with default protocol. It would be better to test pickling with all supported protocols. Why special TestCase methods to check for and report failures are not used in test_typing?
Serhiy, feel free to commit that patch. I was just being lazy. Also, I was using assert instead of self.assertEquals etc. out of laziness (and because at Dropbox people have got the py.test religion and prefer to use assert statements -- but I'm not keen to do this for the stdlib).