jrmccluskey commented on code in PR #25795:
URL: https://github.com/apache/beam/pull/25795#discussion_r1136068126


##########
sdks/python/apache_beam/typehints/trivial_inference.py:
##########
@@ -498,6 +518,39 @@ def infer_return_type_func(f, input_types, debug=False, 
depth=0):
       else:
         return_type = typehints.Any
       state.stack[-pop_count:] = [return_type]
+    elif opname == 'CALL':
+      pop_count = 1 + arg
+      # Keyword Args case
+      if state.kw_names is not None:
+        if isinstance(state.stack[-pop_count], Const):
+          from apache_beam.pvalue import Row
+          if state.stack[-pop_count].value == Row:
+            fields = state.kw_names
+            return_type = row_type.RowTypeConstraint.from_fields(
+                list(
+                    zip(fields,
+                        Const.unwrap_all(state.stack[-pop_count + 1:]))))
+          else:
+            return_type = Any
+        state.kw_names = None
+      else:
+        # Catch lambdas not passing in arg values properly

Review Comment:
   Okay so this will be a bit of a lengthy comment. For a typical function call 
the stack winds up being something like
   
   Keyword Args
   Positional Args
   Fully-Qualified Name of Function
   Callable
   
   and the arg to the `CALL` (or pre-3.11, `CALL_FUNCTION`) opcode is an 
integer representing the number of elements on the stack above the callable 
itself. This behavior was the same for defined functions and lambdas. In 3.11, 
however, lambdas now produce the following:
   
   Keyword Args
   Positional Args
   Callable
   
   with the opcode and arg always being `CALL 0`. I don't have a handle on why 
lambdas are getting a 0 arg from the CPython side yet, but this isn't 
documented anywhere I can find. So, as a result, I have to add a decent chunk 
of logic to iterate through the stack and find the callable in the event that 
we get a `CALL 0` op. 
   
   This gets used pretty extensively because a ton of the test cases pass 
lambdas into the type hinting functions rather than pre-defining a function 
(although we do use both, so we cover both cases in the unit tests.) I should 
probably update that comment to have a little bit more context and a link to an 
issue I filed to the CPython repo asking about this behavior since it goes 
against the documented behavior of `CALL`.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to