Skip to content

Commit

Permalink
Fixed Subclassing Dict Issues (Issue #101) (#102)
Browse files Browse the repository at this point in the history
* Fixed Subclassing Dict Issues (Issue #101)

Previously, when child classes were instantiated, all their nodes -- besides the top node -- remained instances of the parent (Dict) class.

Added tests appropriate for these changes
  • Loading branch information
MisterVladimir authored and mewwts committed Aug 23, 2018
1 parent 1a9b80b commit 3591a7f
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 75 deletions.
8 changes: 3 additions & 5 deletions addict/addict.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(__self, *args, **kwargs):
__self[key] = __self._hook(val)

def __setattr__(self, name, value):
if hasattr(Dict, name):
if hasattr(self.__class__, name):
raise AttributeError("'Dict' object attribute "
"'{0}' is read-only".format(name))
else:
Expand Down Expand Up @@ -61,10 +61,8 @@ def _hook(cls, item):
def __getattr__(self, item):
return self.__getitem__(item)

def __getitem__(self, name):
if name not in self:
return Dict(__parent=self, __key=name)
return super(Dict, self).__getitem__(name)
def __missing__(self, name):
return self.__class__(__parent=self, __key=name)

def __delattr__(self, name):
del self[name]
Expand Down
Loading

0 comments on commit 3591a7f

Please sign in to comment.