Например, в MWE функция может выглядеть так:
Код: Выделить всё
import typing as tp
import polars as pl
u = pl.Series(range(5))
def f(L: tp.Sequence[int]) -> int:
return len(L) + sum(L)
print(f(u))
Код: Выделить всё
error: Argument 1 to "f" has incompatible type "Series"; expected "Sequence[int]" [arg-type]
Способ сделать это — расширить возможности набора текста:
Код: Выделить всё
import typing as tp
import polars as pl
u = pl.Series(range(5))
def f(L: tp.Sequence[int] | pl.Series) -> int:
return len(L) + sum(L)
print(f(u)) # should pass (u is a pl.Series, result is an int)
print(f(u+0.1)) # should fail (u is a pl.Series but result is not an int)
Последняя попытка, я попытался подписать тип pl.Series, поскольку у него есть атрибут dtype, который описывает данные внутри него ( в моем случае u.dtype — это Int64, внутренний тип Polars).
Код: Выделить всё
import typing as tp
import polars as pl
u = pl.Series(range(5))
def f(L: tp.Sequence[int] | pl.Series[int]) -> int:
return len(L) + sum(L)
Код: Выделить всё
error: "Series" expects no type arguments, but 1 given [type-arg]
Подробнее здесь: https://stackoverflow.com/questions/793 ... -in-python
Мобильная версия