Skip to content
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

Merged
merged 4 commits into from Oct 8, 2016

Conversation

phobologic
Copy link
Member

This validates properties & sub-properties when using from_dict.

This validates properties & sub-properties when using from_dict.
props = {}
for prop_name, value in kwargs.items():
try:
prop_attrs = cls.props[prop_name]
Copy link
Contributor

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

Copy link
Member Author

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.

Copy link
Contributor

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
Copy link
Contributor

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?

Copy link
Member Author

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))
Copy link
Contributor

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])
Copy link
Contributor

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?

@phobologic
Copy link
Member Author

Updated to address @mhahn 's review, with another test.

Copy link
Contributor

@mwildehahn mwildehahn left a 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]
Copy link
Contributor

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):
Copy link
Contributor

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

@markpeek markpeek merged commit 44882b4 into master Oct 8, 2016
new_value = []
for v in value:
if not isinstance(v, collections.Mapping):
raise ValueError(
Copy link
Contributor

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?

amosshapira pushed a commit to amosshapira/troposphere that referenced this pull request Oct 24, 2016
* 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
@markpeek markpeek deleted the better_type_checking_from_dict branch December 30, 2021 19:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants