Skip to content

Commit

Permalink
Merge pull request #301 from vnetserg/master
Browse files Browse the repository at this point in the history
Fix shadowed namedtuple bug
  • Loading branch information
mmckerns committed Feb 15, 2019
2 parents af2ffe6 + 5646867 commit 6ae26c3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
9 changes: 3 additions & 6 deletions dill/_dill.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,12 +779,9 @@ def _create_array(f, args, state, npdict=None):
return array

def _create_namedtuple(name, fieldnames, modulename):
mod = _import_module(modulename, safe=True)
if mod is not None:
try:
return getattr(mod, name)
except:
pass
class_ = _import_module(modulename + '.' + name, safe=True)
if class_ is not None:
return class_
import collections
t = collections.namedtuple(name, fieldnames)
t.__module__ = modulename
Expand Down
5 changes: 5 additions & 0 deletions tests/shadowed_namedtuple/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# This package is used by the `test_shadowed_namedtuple` test.
# Author: Sergei Fomin (se4min at yandex-team.ru)

from .shadowed import shadowed

11 changes: 11 additions & 0 deletions tests/shadowed_namedtuple/shadowed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This module is used by the `test_shadowed_namedtuple` test.
# Author: Sergei Fomin (se4min at yandex-team.ru)

from collections import namedtuple

class Shadowed(namedtuple("Shadowed", [])):
pass

def shadowed():
return Shadowed()

15 changes: 15 additions & 0 deletions tests/test_shadowed_namedtuple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env python
#
# Author: Sergei Fomin (se4min at yandex-team.ru)

from dill import dumps, loads
from shadowed_namedtuple import shadowed

def test_shadowed_namedtuple():
obj = shadowed()
obj_loaded = loads(dumps(obj))
assert obj.__class__ is obj_loaded.__class__

if __name__ == "__main__":
test_shadowed_namedtuple()

0 comments on commit 6ae26c3

Please sign in to comment.