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:
python: 3.11
pandera: 0.15.1
pyright: 1.1.311
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
)
Currently, the type hint for the static method of the class pandera DataFrame in pandera.typing.pandas.DataFrame is as follows:
giving pyright the following error: Expression of type "DataFrame[Type[Schema]]" cannot be assigned to return type "DataFrame[Schema]" when:
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
notice the
schema: Type[T]
.