unionai-oss / pandera

A light-weight, flexible, and expressive statistical data testing library
https://www.union.ai/pandera
MIT License
3.38k stars 310 forks source link

Static type hint error on class pandera DataFrame #1205

Closed manel-ab closed 1 year ago

manel-ab commented 1 year ago

Currently, the type hint for the static method of the class pandera DataFrame in pandera.typing.pandas.DataFrame is as follows:

@staticmethod
def from_records(  # type: ignore
    schema: T,
    data: Union[  # type: ignore
        np.ndarray, List[Tuple[Any, ...]], Dict[Any, Any], pd.DataFrame
    ],
    **kwargs,
) -> "DataFrame[T]":

giving pyright the following error: Expression of type "DataFrame[Type[Schema]]" cannot be assigned to return type "DataFrame[Schema]" when:

class Schema(SchemaModel):
    timestamp: Series[datetime]

DataFrame.from_records(
        schema=Schema, data={"data": []}
    )

because models of dataframes must no be instantiated and the type hint of schema suggests that the value must be an instance of a generic model schema.

Environment:

Expected code

from typing import Type
...
@staticmethod
def from_records(  # type: ignore
    schema: Type[T],
    data: Union[  # type: ignore
        np.ndarray, List[Tuple[Any, ...]], Dict[Any, Any], pd.DataFrame
    ],
    **kwargs,
) -> "DataFrame[T]":
    """
    Convert structured or record ndarray to pandera-validated DataFrame.

    Creates a DataFrame object from a structured ndarray, sequence of tuples
    or dicts, or DataFrame.

    See :doc:`pandas:reference/api/pandas.DataFrame.from_records` for
    more details.
    """
    schema = schema.to_schema()  # type: ignore[attr-defined]
    schema_index = schema.index.names if schema.index is not None else None
    if "index" not in kwargs:
        kwargs["index"] = schema_index
    return DataFrame[T](
        pd.DataFrame.from_records(data=data, **kwargs,)[
            schema.columns.keys()
        ]  # set the column order according to schema
    )

notice the schema: Type[T].

cosmicBboy commented 1 year ago

hi @manel-ab feel free to contribute a PR for this!

manel-ab commented 1 year ago

hi @manel-ab feel free to contribute a PR for this!

hi! I created the following PR: https://github.com/unionai-oss/pandera/pull/1207

cosmicBboy commented 1 year ago

fixed by #1207