-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide better type checking for values in from_dict #587
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -162,10 +162,43 @@ def validate(self): | |
pass | ||
|
||
@classmethod | ||
def from_dict(cls, title, dict): | ||
obj = cls(title) | ||
obj.properties.update(dict) | ||
return obj | ||
def _from_dict(cls, title=None, **kwargs): | ||
props = {} | ||
for prop_name, value in kwargs.items(): | ||
try: | ||
prop_attrs = cls.props[prop_name] | ||
except KeyError: | ||
raise AttributeError("Object type %s does not have a " | ||
"%s property." % (cls.__name__, | ||
prop_name)) | ||
prop_type, required = prop_attrs | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call, updating. |
||
if prop_name in kwargs: | ||
value = kwargs[prop_name] | ||
is_aws_object = False | ||
try: | ||
is_aws_object = issubclass(prop_type, BaseAWSObject) | ||
# prop_type isn't a class | ||
except TypeError: | ||
pass | ||
if is_aws_object: | ||
value = prop_type._from_dict(**kwargs[prop_name]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if |
||
|
||
if isinstance(prop_type, list): | ||
if not isinstance(kwargs[prop_name], list): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this could just be |
||
raise TypeError("Attribute %s must be a " | ||
"list." % prop_name) | ||
new_value = [] | ||
for v in value: | ||
new_value.append(prop_type[0]._from_dict(**v)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if |
||
value = new_value | ||
props[prop_name] = value | ||
if title: | ||
return cls(title, **props) | ||
return cls(**props) | ||
|
||
@classmethod | ||
def from_dict(cls, title, d): | ||
return cls._from_dict(title, **d) | ||
|
||
def JSONrepr(self): | ||
for k, (_, required) in self.props.items(): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cls.props
could be missing tooThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it can - this is on AWSBaseObject, and AWSBaseObject sets up
self.props
in its init.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh you're right, this is the
cls
object. i was thinking of the function you had before that took any class