shahar1 commented on code in PR #75022:
URL: https://github.com/apache/airflow/pull/75022#discussion_r4494413992


##########
task-sdk/src/airflow/sdk/definitions/xcom_arg.py:
##########
@@ -400,7 +400,15 @@ class _MapResult(Sequence):
     callables: MapCallables
 
     def __getitem__(self, index: Any) -> Any:
-        value = self.value[index]
+        if isinstance(self.value, dict):
+            length = len(self.value)
+            if index < 0:

Review Comment:
   minor: This drops one path that works on `main`, though it is undocumented. 
A task that takes a mapped dict without `expand()`, such as 
`pull(value=push().map(f))`, can read `value["a"]` today and get `f(1)`. With 
this change, `"a" < 0` raises `TypeError: '<' not supported between instances 
of 'str' and 'int'`.
   
   Integer access and iteration over a dict never worked here, so I think this 
is an acceptable trade. `_MapResult` is a `Sequence`, and `expand()` is the 
documented use. Please mention the change in the PR description so that the 
decision is explicit.



##########
task-sdk/src/airflow/sdk/definitions/xcom_arg.py:
##########
@@ -400,7 +400,15 @@ class _MapResult(Sequence):
     callables: MapCallables
 
     def __getitem__(self, index: Any) -> Any:
-        value = self.value[index]
+        if isinstance(self.value, dict):

Review Comment:
   nit: `list` already provides the negative indexing and `IndexError` that the 
code here builds by hand, so the branch can shrink to this:
   
   ```python
   if isinstance(self.value, dict):
       value = list(self.value.items())[index]
   else:
       value = self.value[index]
   ```
   
   It builds the full list on each call, where `islice` can stop early. Both 
are O(n), and a mapped task instance makes one call. A string key would also 
fail with `list indices must be integers or slices, not str`, which is clearer 
than the comparison error.



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