Skip to content

Commit

Permalink
Document intended AttributeError behavior of LooselyTypedDotDict in test
Browse files Browse the repository at this point in the history
  • Loading branch information
jcampbell committed Aug 17, 2019
1 parent 849c3b7 commit a9feee7
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions tests/types/test_base_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,23 @@ def test_LooselyTypedDotDict_raises_error():

with pytest.raises(KeyError):
d.x = "goodbye?"

assert d.x == None

# Note that we raise AttributeError instead of KeyError here since the
# intended semantics when using dot notation are class property style
with pytest.raises(AttributeError):
assert d.x is None

# In contrast, when we explicitly use the dictionary notation, we get
# the KeyError, also following the standard conventions
with pytest.raises(KeyError):
assert d["x"]
assert d["x"] is None


def test_LooselyTypedDotDict_subclass():
class MyLooselyTypedDotDict(LooselyTypedDotDict):
_allowed_keys = set([
_allowed_keys = {
"x", "y", "z"
])
}

d = MyLooselyTypedDotDict(**{
'x': 1,
Expand Down Expand Up @@ -108,10 +113,11 @@ class MyLooselyTypedDotDict(LooselyTypedDotDict):
with pytest.raises(KeyError):
d.w = 100

assert d.w == None
with pytest.raises(AttributeError):
assert d.w is None

with pytest.raises(KeyError):
assert d["w"]
assert d["w"] is None


def test_LooselyTypedDotDict_subclass_required_keys():
Expand Down

0 comments on commit a9feee7

Please sign in to comment.