I have a function that creates a pyspark WindowSpec partitioning on either a single column or a pair of columns in a list, depending on a boolean param. Mypy throws an error that I can't understand, because my parameter partition_cols
should be Union[str, List[str]]
which is acceptable for Window.partitionBy()
.
Example method and error:
from pyspark.sql import Window, WindowSpec
def get_window(single_column: bool) -> WindowSpec:
partition_cols = "key" if single_column else ["key", "name"]
return Window.partitionBy(partition_cols).orderBy("timestamp").rangeBetween(0, 10)
Then running mypy:
$ mypy tmp.py
tmp.py:8: error: Argument 1 to "partitionBy" of "Window" has incompatible type "Sequence[str]"; expected "Union[Union[Column, str], List[Union[Column, str]]]" [arg-type]