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
Conversation
This validates properties & sub-properties when using from_dict.
props = {} | ||
for prop_name, value in kwargs.items(): | ||
try: | ||
prop_attrs = cls.props[prop_name] |
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 too
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.
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
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 comment
The reason will be displayed to describe this comment to others. Learn more.
maybe just prop_type = prop_attrs[0]
since you don't use required
?
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.
Good call, updating.
"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 comment
The reason will be displayed to describe this comment to others. Learn more.
what if v
isn't a dict? it seems like this needs the same check for aws object
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 comment
The reason will be displayed to describe this comment to others. Learn more.
what if kwargs[prop_name]
isn't a dict?
Updated to address @mhahn 's review, with another test. |
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.
sweet! excited to use this
except TypeError: | ||
pass | ||
if is_aws_object: | ||
v = kwargs[prop_name] |
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.
not a huge deal, but you already define value = kwargs[prop_name]
above, i would just reuse that here
value = prop_type._from_dict(**v) | ||
|
||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
this could just be value
that you pulled out aboe
new_value = [] | ||
for v in value: | ||
if not isinstance(v, collections.Mapping): | ||
raise ValueError( |
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.
how would this work if this was a list of role names? does this always have to be a list of mapping types?
* Provide better type checking for values in from_dict This validates properties & sub-properties when using from_dict. * Add more test for helperfn's and invalid props * test invalid sub-property definitions * More updates, thx for review @mhahn
This validates properties & sub-properties when using from_dict.