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 2021/06/07 23:19:39 UTC

[GitHub] [beam] pabloem commented on a change in pull request #14869: [BEAM-12357] improve WithKeys transform to take args, kwargs

pabloem commented on a change in pull request #14869:
URL: https://github.com/apache/beam/pull/14869#discussion_r647002255



##########
File path: sdks/python/apache_beam/transforms/util.py
##########
@@ -730,14 +731,30 @@ def from_runner_api_parameter(
     return Reshuffle()
 
 
+def fn_takes_side_inputs(fn):
+  try:
+    signature = get_signature(fn)
+  except TypeError:
+    # We can't tell; maybe it does.
+    return True
+
+  return (
+      len(signature.parameters) > 1 or any(
+          p.kind == p.VAR_POSITIONAL or p.kind == p.VAR_KEYWORD
+          for p in signature.parameters.values()))
+
+
 @ptransform_fn
-def WithKeys(pcoll, k):
+def WithKeys(pcoll, k, *args, **kwargs):
   """PTransform that takes a PCollection, and either a constant key or a
   callable, and returns a PCollection of (K, V), where each of the values in
   the input PCollection has been paired with either the constant key or a key
-  computed from the value.
+  computed from the value.  The callable may optionally accept positional or
+  keyword arguments, which should be passed to WithKeys directly.
   """
   if callable(k):
+    if fn_takes_side_inputs(k):
+      return pcoll | Map(lambda v: (k(v, *args, **kwargs), v))

Review comment:
       I think you need to pass the *args and **kwargs into the Map transform, not just into the lambda. Something like so:,
   ```suggestion
         return pcoll | Map(lambda v: (k(v, *args, **kwargs), v), *args, **kwargs)
   ```




-- 
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.

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