From f5d9be6a273cd27b9b0e23f1e846b596ee014826 Mon Sep 17 00:00:00 2001 From: Thomas Schultz Date: Tue, 24 Jan 2017 14:26:28 -0500 Subject: [PATCH 1/2] Fix KeyError for unsupported annotation types. --- vision/google/cloud/vision/annotations.py | 3 +++ vision/unit_tests/test_annotations.py | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/vision/google/cloud/vision/annotations.py b/vision/google/cloud/vision/annotations.py index a19527a96c11..8c59b035e5ac 100644 --- a/vision/google/cloud/vision/annotations.py +++ b/vision/google/cloud/vision/annotations.py @@ -87,6 +87,9 @@ def from_api_repr(cls, response): :rtype: :class:`~google.cloud.vision.annotations.Annotations` :returns: An instance of ``Annotations`` with detection types loaded. """ + for feature_type in response.keys(): + if feature_type not in _KEY_MAP: + del response[feature_type] annotations = { _KEY_MAP[feature_type]: _entity_from_response_type( feature_type, annotation) diff --git a/vision/unit_tests/test_annotations.py b/vision/unit_tests/test_annotations.py index 68a270e3122d..2a5fb5b8ca92 100644 --- a/vision/unit_tests/test_annotations.py +++ b/vision/unit_tests/test_annotations.py @@ -66,6 +66,15 @@ def test_ctor(self): self.assertEqual(annotations.safe_searches, [True]) self.assertEqual(annotations.texts, [True]) + def test_unsupported_http_annotation(self): + returned = { + 'responses': [ + {'someMadeUpAnnotation': None}, + ], + } + annotation = self._get_target_class().from_api_repr(returned) + self.assertIsInstance(annotation, self._get_target_class()) + def test_from_pb(self): from google.cloud.vision.likelihood import Likelihood from google.cloud.vision.safe_search import SafeSearchAnnotation From d958507e506964f18e647a04b5913ee6fb445f0a Mon Sep 17 00:00:00 2001 From: Thomas Schultz Date: Tue, 24 Jan 2017 15:59:47 -0500 Subject: [PATCH 2/2] Why not just filter in the comprehension. --- vision/google/cloud/vision/annotations.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/vision/google/cloud/vision/annotations.py b/vision/google/cloud/vision/annotations.py index 8c59b035e5ac..2348d2361731 100644 --- a/vision/google/cloud/vision/annotations.py +++ b/vision/google/cloud/vision/annotations.py @@ -87,13 +87,11 @@ def from_api_repr(cls, response): :rtype: :class:`~google.cloud.vision.annotations.Annotations` :returns: An instance of ``Annotations`` with detection types loaded. """ - for feature_type in response.keys(): - if feature_type not in _KEY_MAP: - del response[feature_type] annotations = { _KEY_MAP[feature_type]: _entity_from_response_type( feature_type, annotation) for feature_type, annotation in six.iteritems(response) + if feature_type in _KEY_MAP } return cls(**annotations)