pandera icon indicating copy to clipboard operation
pandera copied to clipboard

Pandera Series generic argument does not allow the `typing.List` etc. types

Open sam-goodwin opened this issue 1 year ago • 4 comments

Code Sample, a copy-pastable example

import pandera as pa
import pandera.typing as pdt

class MySchema(pa.DataFrameModel):
    int32: pdt.Series[list[pdt.Int32]] = pa.Field()

Run mypy and get the following error:

error: Type argument "list[Int32]" of "Series" must be a subtype of "bool | int | str | float | ExtensionDtype | Bool | Date | Timestamp | Decimal | Timedelta | Category | Float | Float16 | Float32 | Float64 | Int | Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | UInt64 | INT8 | INT16 | INT32 | INT64 | UINT8 | UINT16 | UINT32 | UINT64 | Object | String | STRING | Geometry"  [type-var]

Expected behavior

The type checking should not fail.

sam-goodwin avatar Apr 04 '24 04:04 sam-goodwin

@sam-goodwin feel free to make a PR to add the generic types here and here.

Also, FYI you can use the type without pdt.Series

    int32: pdt.list[pdt.Int32] = pa.Field()

cosmicBboy avatar Apr 04 '24 15:04 cosmicBboy

My plan was to fix this today.

RE: dropping the series type, I'm waiting for this PR to make its way into Pypi:

https://github.com/unionai-oss/pandera/pull/1547

sam-goodwin avatar Apr 04 '24 15:04 sam-goodwin

Interesting, adding the types here is a new case as this is a recursive types. Would I add list[Any] or is there a better type for the parameter?

List[Any],
Dict[Any, Any],
Tuple[Any],
NamedTuple,

sam-goodwin avatar Apr 04 '24 17:04 sam-goodwin

@sam-goodwin Not sure but this might (in theory) catch it

T = String | Int | Bool | ... | List[ForwardRef('T')] | Dict[str, ForwardRef('T')]

kosmitive avatar Jun 04 '24 13:06 kosmitive