You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2022/11/14 19:02:45 UTC

[GitHub] [beam] robertwb commented on a diff in pull request #24022: Add error reporting for BatchConverter match failure

robertwb commented on code in PR #24022:
URL: https://github.com/apache/beam/pull/24022#discussion_r1021950368


##########
sdks/python/apache_beam/typehints/batch.py:
##########
@@ -72,26 +74,36 @@ def estimate_byte_size(self, batch):
     raise NotImplementedError
 
   @staticmethod
-  def register(
-      batch_converter_constructor: Callable[[type, type], 'BatchConverter']):
-    BATCH_CONVERTER_REGISTRY.append(batch_converter_constructor)
-    return batch_converter_constructor
+  def register(*, name: str):
+    def do_registration(
+        batch_converter_constructor: Callable[[type, type], 'BatchConverter']):
+      if name in BATCH_CONVERTER_REGISTRY:
+        raise AssertionError(
+            f"Attempted to register two batch converters with name {name}")
+
+      BATCH_CONVERTER_REGISTRY[name] = batch_converter_constructor
+      return batch_converter_constructor
+
+    return do_registration
 
   @staticmethod
   def from_typehints(*, element_type, batch_type) -> 'BatchConverter':
     element_type = typehints.normalize(element_type)
     batch_type = typehints.normalize(batch_type)
-    for constructor in BATCH_CONVERTER_REGISTRY:
-      result = constructor(element_type, batch_type)
-      if result is not None:
+    errors = {}
+    for name, constructor in BATCH_CONVERTER_REGISTRY.items():
+      try:
+        result = constructor(element_type, batch_type)
+      except TypeError as e:
+        errors[name] = e.args[0]
+      else:
         return result

Review Comment:
   It's fine to just `return constructor(element_type, batch_type)` in the try clause rather than have an else clause here, as the act of returning can't raise a TypeError.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org