You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "josh-fell (via GitHub)" <gi...@apache.org> on 2023/02/10 14:21:14 UTC

[GitHub] [airflow] josh-fell commented on a diff in pull request #29445: Better handle forward type definitions in `@task.python` for multiple output

josh-fell commented on code in PR #29445:
URL: https://github.com/apache/airflow/pull/29445#discussion_r1102809860


##########
airflow/decorators/base.py:
##########
@@ -296,7 +297,20 @@ class _TaskDecorator(ExpandableFactory, Generic[FParams, FReturn, OperatorSubcla
     @multiple_outputs.default
     def _infer_multiple_outputs(self):
         try:
-            return_type = typing_extensions.get_type_hints(self.function).get("return", Any)
+            # We only care about the return annotation, not anything about the parameters
+            def fake():
+                ...
+
+            fake.__annotations__ = {"return": self.function.__annotations__["return"]}
+
+            return_type = typing_extensions.get_type_hints(fake, self.function.__globals__).get("return", Any)
+        except NameError as e:
+            warnings.warn(
+                "Cannot infer multiple_outputs with forward type references that are not imported. "

Review Comment:
   The warning is nice. I think it would be helpful for the message to include the function name too. WDYT?
   
   Something like:
   ```suggestion
                   f"Cannot infer multiple_outputs for TaskFlow function {self.function.__name__!r} with forward type references that are not imported. "
   ```



##########
airflow/decorators/base.py:
##########
@@ -296,7 +297,20 @@ class _TaskDecorator(ExpandableFactory, Generic[FParams, FReturn, OperatorSubcla
     @multiple_outputs.default
     def _infer_multiple_outputs(self):
         try:
-            return_type = typing_extensions.get_type_hints(self.function).get("return", Any)
+            # We only care about the return annotation, not anything about the parameters
+            def fake():
+                ...
+
+            fake.__annotations__ = {"return": self.function.__annotations__["return"]}

Review Comment:
   ```suggestion
               fake.__annotations__ = {"return": self.function.__annotations__.get("return")}
   ```
   Just in case there isn't a return annotation.



-- 
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: commits-unsubscribe@airflow.apache.org

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