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

GeoPandas type not compatible with FastApi #1108

Open mangecoeur opened 1 year ago

mangecoeur commented 1 year ago

Describe the bug A clear and concise description of what the bug is.

Code Sample, a copy-pastable example

import pandera as pa
from pandera.typing.geopandas import GeoDataFrame, GeoSeries
from fastapi import FastAPI

class Location(pa.SchemaModel):
    id: pa.typing.Series[int]
    point: GeoSeries

app = FastAPI()

@app.post("/", response_model=GeoDataFrame[Location])
def get_location(id: int):
    return None

Produces the error:

Fields of type "<class 'pandera.typing.geopandas.GeoDataFrame'>" are not supported.

Irrespective of whether to_format is defined.

Expected behavior

Similar to DataFrame can be used in annotation, would like to be able to use a GeoDataFrame. Conversion of the GeoDataFrame would be controlled by to_format (e.g. to produce a GeoJson). I assume that since FastAPI doesn't support DataFrame by default either somewhere the pandera types are registered with FastAPI.

Desktop (please complete the following information):

cosmicBboy commented 1 year ago

Hi @mangecoeur, relabelling this as an enhancement, as FastAPI support is only currently for pandera.typing.pandas.DataFrame (should probably document this if it isn't already)

Basically the GeoDataFrame generic class needs a __get_validators__ method where currently it's just a bare generic class: https://github.com/unionai-oss/pandera/blob/main/pandera/typing/geopandas.py#L34

Probably won't get to this for a few months, but would welcome a PR from you or the community!

derinwalters commented 1 year ago

I picked this up and will submit PR soon