Skip to content
This repository has been archived by the owner on Oct 3, 2019. It is now read-only.

Commit

Permalink
Merge pull request #140 from jacebrowning/attr-dict-init
Browse files Browse the repository at this point in the history
Call __init__ for AttributeDictionary types
  • Loading branch information
jacebrowning committed Mar 30, 2017
2 parents 8064397 + fa75dce commit 86b58f1
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .pycodestyle.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# E401 multiple imports on one line (checked by PyLint)
# E402 module level import not at top of file (checked by PyLint)
# E501: line too long (checked by PyLint)
# E701: multiple statements on one line (used to shorten test syntax)
# E711: comparison to None (used to improve test style)
# E712: comparison to True (used to improve test style)
ignore = E401,E402,E501,E711,E712
ignore = E401,E402,E501,E701,E711,E712
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Removed warnings about calling save/load unnecessarily.
- Allow keyword arguments to be passed to class construction via `new` and `find` utilities.
- Fixed missing attributes added in `__init__` on `AttributeDictionary`.

## 1.3 (2017/01/24)

Expand Down
2 changes: 1 addition & 1 deletion yorm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
from .mixins import ModelMixin

__project__ = 'YORM'
__version__ = '1.4dev4'
__version__ = '1.4dev5'
33 changes: 26 additions & 7 deletions yorm/tests/test_types_extended.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,42 @@ def it_converts_correctly(value, data):

def describe_attribute_dictionary():

@attr(var1=Integer)
@attr(var2=String)
class SampleAttributeDictionary(AttributeDictionary):
"""Sample attribute dictionary."""
@pytest.fixture
def cls():
@attr(var1=Integer)
@attr(var2=String)
class SampleAttributeDictionary(AttributeDictionary): pass
return SampleAttributeDictionary

@pytest.fixture
def converter():
return SampleAttributeDictionary()
def cls_with_init():
@attr(var1=Integer)
class SampleAttributeDictionaryWithInit(AttributeDictionary):
def __init__(self, *args, var2="2", **kwargs):
super().__init__(*args, **kwargs)
self.var2 = var2
return SampleAttributeDictionaryWithInit

def it_cannot_be_used_directly():
with expect.raises(NotImplementedError):
AttributeDictionary.to_value(None)
with expect.raises(NotImplementedError):
AttributeDictionary.to_data(None)

def it_has_keys_available_as_attributes(converter):
def it_has_keys_available_as_attributes(cls):
converter = cls()

value = converter.to_value({'var1': 1, 'var2': "2"})

expect(value.var1) == 1
expect(value.var2) == "2"

def it_adds_extra_attributes_from_init(cls_with_init):
converter = cls_with_init()

value = converter.to_value({'var1': 1})
print(value.__dict__)

expect(value.var1) == 1
expect(value.var2) == "2"

Expand Down
4 changes: 1 addition & 3 deletions yorm/types/extended.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,7 @@ def create_default(cls):
msg = "AttributeDictionary class must be subclassed to use"
raise NotImplementedError(msg)

obj = cls.__new__(cls)
obj.__dict__ = obj
return obj
return cls()


class SortedList(List):
Expand Down

0 comments on commit 86b58f1

Please sign in to comment.