Skip to content

Commit

Permalink
Make KeyError to be an exact cause for AttributeError.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Koshkin committed Sep 29, 2018
1 parent 9379427 commit 6ab1fe2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
4 changes: 2 additions & 2 deletions watch/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,13 @@ class Just(BaseControlledValidator):
"""

test_against = And(
Container(HasAttr("__eq__"), container=list),
Container(HasAttr("__eq__"), container=tuple),
Predicate(lambda value: len(value) > 0),
)

def predicate(self, value):
return value in self.test_against

def __init__(self, *values):
self.test_against = list(values)
self.test_against = tuple(values)

20 changes: 12 additions & 8 deletions watch/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ def __get__(self, passed_instance, passed_type=None):
return self
try:
return passed_instance.__dict__[self.field_name]
except KeyError:
raise AttributeError(
"Object %s has no attribute '%s'." %
(passed_instance, self.field_name)
except KeyError as key_error:
attribute_error = AttributeError(
"%s object has no attribute '%s'." %
(
type(passed_instance).__qualname__, repr(self.field_name)
)
)
raise attribute_error from key_error


def __set__(self, passed_instance, value):
passed_instance.__dict__[self.field_name] = value
Expand Down Expand Up @@ -93,8 +97,8 @@ def __setattr__(self, attr_name, value):
value.field_name = attr_name
super().__setattr__(attr_name, value)

def __new__(cls, name, bases, attrs):
for name, value in attrs.items():
def __new__(cls, class_name, bases, attributes):
for name, value in attributes.items():
is_value_descriptor = (
isinstance(value, AttributeDescriptor) or
(
Expand All @@ -107,9 +111,9 @@ def __new__(cls, name, bases, attrs):
# descriptor instance
value_snapshot = copy.deepcopy(value())
value_snapshot.field_name = name
attrs[name] = value_snapshot
attributes[name] = value_snapshot

return super().__new__(cls, name, bases, attrs)
return super().__new__(cls, class_name, bases, attributes)


class WatchMe(metaclass=AttributeControllerMeta):
Expand Down

0 comments on commit 6ab1fe2

Please sign in to comment.