-
Notifications
You must be signed in to change notification settings - Fork 17.8k
Remove unnecessary call to keys() method on dictionaries #34260
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
Changes from all commits
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 | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -258,7 +258,7 @@ def _validate_dict(self, children_validation_specs: dict, full_field_path: str, | |||||||||||||||||
| validation_spec=child_validation_spec, dictionary_to_validate=value, parent=full_field_path | ||||||||||||||||||
| ) | ||||||||||||||||||
| all_dict_keys = {spec["name"] for spec in children_validation_specs} | ||||||||||||||||||
| for field_name in value.keys(): | ||||||||||||||||||
| for field_name in value: | ||||||||||||||||||
| if field_name not in all_dict_keys: | ||||||||||||||||||
| self.log.warning( | ||||||||||||||||||
| "The field '%s' is in the body, but is not specified in the " | ||||||||||||||||||
|
|
@@ -421,6 +421,8 @@ def validate(self, body_to_validate: dict) -> None: | |||||||||||||||||
| :param body_to_validate: body that must follow the specification | ||||||||||||||||||
| :return: None | ||||||||||||||||||
| """ | ||||||||||||||||||
| if body_to_validate is None: | ||||||||||||||||||
| raise RuntimeError("The body to validate is `None`. Please provide a dictionary to validate.") | ||||||||||||||||||
| try: | ||||||||||||||||||
| for validation_spec in self._validation_specs: | ||||||||||||||||||
| self._validate_field(validation_spec=validation_spec, dictionary_to_validate=body_to_validate) | ||||||||||||||||||
|
|
@@ -441,7 +443,7 @@ def validate(self, body_to_validate: dict) -> None: | |||||||||||||||||
| if nested_union_spec.get("type") != "union" | ||||||||||||||||||
| and nested_union_spec.get("api_version") != self._api_version | ||||||||||||||||||
| ) | ||||||||||||||||||
| for field_name in body_to_validate.keys(): | ||||||||||||||||||
| for field_name in body_to_validate: | ||||||||||||||||||
|
Contributor
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. Everything fine in refactoring itself, for me a bit confusing original test which was written years ago airflow/tests/providers/google/cloud/utils/test_field_validator.py Lines 37 to 44 in dd4804e
I not familiar with such body in GCP, but I guess if we expect that only dict is valid here then we need to fix a bit of logic from raise TypeError in case if got not a dict, because it also might confuse end users if they got Attribute error
Member
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. I understand that it fails because None is not iterable, but the question is how None.keys() didn't fail before 🤔
Member
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.
Member
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. I think you will never get I think the right solution is: Add this: and expect the RuntimeError in this test. |
||||||||||||||||||
| if field_name not in all_field_names: | ||||||||||||||||||
| self.log.warning( | ||||||||||||||||||
| "The field '%s' is in the body, but is not specified in the " | ||||||||||||||||||
|
|
||||||||||||||||||
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.
The function’s type annotation should probably be updated to cover the None case.
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 am slightly confused if it would make sense to update the type annotation to include None as an acceptable type only to raise an error. 🤔
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.
Yeah that’s a good point