You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2021/10/22 18:18:56 UTC

[GitHub] [spark] ueshin commented on a change in pull request #34354: [WIP][SPARK-37085][PYTHON][SQL] Add list/tuple overloads to array, struct, create_map, map_concat

ueshin commented on a change in pull request #34354:
URL: https://github.com/apache/spark/pull/34354#discussion_r734744105



##########
File path: python/pyspark/sql/functions.py
##########
@@ -1652,7 +1652,19 @@ def expr(str: str) -> Column:
     return Column(sc._jvm.functions.expr(str))
 
 
+@overload
 def struct(*cols: "ColumnOrName") -> Column:
+    ...
+
+
+@overload
+def struct(__cols: Union[List["ColumnOrName"], Tuple["ColumnOrName", ...]]) -> Column:

Review comment:
       How about using more general type, like `Sequence` or `Iterable`?

##########
File path: python/pyspark/sql/functions.py
##########
@@ -1652,7 +1652,19 @@ def expr(str: str) -> Column:
     return Column(sc._jvm.functions.expr(str))
 
 
+@overload
 def struct(*cols: "ColumnOrName") -> Column:
+    ...
+
+
+@overload
+def struct(__cols: Union[List["ColumnOrName"], Tuple["ColumnOrName", ...]]) -> Column:
+    ...
+
+
+def struct(
+        *cols: Union["ColumnOrName", Union[List["ColumnOrName"], Tuple["ColumnOrName", ...]]]

Review comment:
       nit: style, 4-space indent?

##########
File path: python/pyspark/sql/functions.py
##########
@@ -1652,7 +1652,19 @@ def expr(str: str) -> Column:
     return Column(sc._jvm.functions.expr(str))
 
 
+@overload
 def struct(*cols: "ColumnOrName") -> Column:
+    ...
+
+
+@overload
+def struct(__cols: Union[List["ColumnOrName"], Tuple["ColumnOrName", ...]]) -> Column:

Review comment:
       Actually `List["ColumnOrName"]` is a bit tricky because `List` is not covariant and it doesn't accept `List[str]` or `List[Column]`.
   
   e.g.,
   
   ```
   % cat python/test.py
   from pyspark.sql import functions as F
   
   columns = ['a', 'b', 'c']
   reveal_type(columns)
   
   F.struct(columns)
   
   % mypy --config-file python/mypy.ini python/test.py
   python/test.py:4: note: Revealed type is "builtins.list[builtins.str*]"
   python/test.py:6: error: Argument 1 to "struct" has incompatible type "List[str]"; expected "Union[List[Union[Column, str]], Tuple[Union[Column, str], ...]]"  [arg-type]
   python/test.py:6: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
   python/test.py:6: note: Consider using "Sequence" instead, which is covariant
   ```
   
   whereas with `def struct(__cols: Sequence["ColumnOrName"]) -> Column:`:
   
   ```
   % mypy --config-file python/mypy.ini python/test.py
   python/test.py:4: note: Revealed type is "builtins.list[builtins.str*]"
   ```
   




-- 
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: reviews-unsubscribe@spark.apache.org

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



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org