Remove unnecessary call to keys() method on dictionaries - #34260
Conversation
dd8dcb1 to
0d29e89
Compare
| ] | ||
| ) | ||
| for field_name in body_to_validate.keys(): | ||
| for field_name in body_to_validate: |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
I understand that it fails because None is not iterable, but the question is how None.keys() didn't fail before 🤔
There was a problem hiding this comment.
>>> x = None
>>> x.keys()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'keys'
>>>
There was a problem hiding this comment.
I think you will never get None in reality - this was really a defensive test. And AttributeError was just side effect of using keys() on None.
I think the right solution is:
Add this:
if body_to_validate is None:
raise RuntimeError("The body passed here should never be `None`)
and expect the RuntimeError in this test.
| :param body_to_validate: body that must follow the specification | ||
| :return: None | ||
| """ | ||
| if body_to_validate is None: |
There was a problem hiding this comment.
The function’s type annotation should probably be updated to cover the None case.
There was a problem hiding this comment.
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. 🤔
^ Add meaningful description above
Read the Pull Request Guidelines for more information.
In case of fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in a newsfragment file, named
{pr_number}.significant.rstor{issue_number}.significant.rst, in newsfragments.