geopandas / pyogrio

Vectorized vector I/O using OGR
https://pyogrio.readthedocs.io
MIT License
259 stars 22 forks source link

FIX: Raise NotImplementedError if user passes an open file handle to write #442

Open brendan-ward opened 2 days ago

brendan-ward commented 2 days ago

As indicated in #430 and geopandas #3369, if a user passed an open file handle to write, we were failing with confusing error messages. While the docstrings in pyogrio specify the allowed inputs, we weren't strictly verifying those, and so any path that was not BytesIO was first getting cast to a string, and then failing badly from there.

These usages worked for users with the Fiona engine (previous default) in GeoPandas, so it is proving to be a point of confusion now that Pyogrio is the default engine and things are no longer working as expected.

In most cases, writing the dataset to BytesIO and then writing those bytes to the file handle (regular file handle via open, an fsspec opened file handle, or an open handle within a ZipFile), so the error message includes a brief instruction to do that instead. That also helps defer catching issues around supported drivers and lack of append support to the BytesIO handler.

In the mid term, we may be able to detect that the user passed a writeable file handle, and wrap in BytesIO ourselves, but this is more involved than I had time to take on at the moment.