fix(csv): read a CSV or TSV filename only from the local filesystem
Merged
CR
6d ago+262+262+012345678901234567890123456789-56-56-01234567890123456789
Follow-up to #4712.
Problem
partition_tsv(filename=...) passed the filename string straight to Pandas, and since #4712 (merged, not yet released) partition_csv(filename=...) did too. Pandas treats some strings as remote addresses and fetches them itself:
partition_tsv("https://host/x.tsv")downloaded the file, once for the size check and again for the read. It skipped the scheme, redirect and private-address checks thatpartition(url=...)applies. Strings such as"s3://bucket/x.tsv"were read through fsspec (an optional library for remote file systems) if it was installed.- #4712 added a check that rejected a URL-shaped CSV filename with
ValueError, but TSV kept fetching. The check also rejected a real local file whose path happens to start with something likehttps://.
The contract after this change
filenameis always a local path.partition_csv()andpartition_tsv()open it with Python'sopen()and give Pandas only the open file, so no filename is ever downloaded. A file whose name ends in.gz,.bz2,.xz,.zip,.zstor.tar(alone or with a compression suffix) is still decompressed, and a leading~is still expanded, as before.- A missing file raises
FileNotFoundError, whatever the name looks like. When the name starts with a scheme such ashttps://ors3://, the message says to pass an HTTP(S) address asurltopartition(), or to download the file first. - A local file whose name looks like a URL is read. For example, if the current directory has a folder named
https:that containsexample.com/x.csv, thenfilename="https://example.com/x.csv"reads that file, because the operating system treats//as/. - Remote documents go through
partition(url=...). It fetches once throughsafe_get(), hands the response to the CSV or TSV partitioner asfile, and setsmetadata.url. No new network code is added. - Exactly one source.
partition_csv()now requires exactly one offilenameandfile, aspartition_tsv()and most other partitioners already do.partition()still requires exactly one offilename,fileandurl, and still rejects afilenamethat isn't an existing local file. Sopartition(filename=...)is unchanged, except in one rare case: a local file whose name looks like a URL is now read. Before, a TSV file with such a name was fetched, and on unreleasedmaina CSV file with such a name was rejected withValueError.
CSV and TSV now follow the same rules. Encoding, header handling, cell-count limits, HTML, text and metadata are unchanged.
Compatibility and migration
- TSV with a URL as
filenamenow raisesFileNotFoundError. Released versions fetched it through Pandas. To migrate:- for an HTTP(S) address, use
partition(url="https://host/x.tsv", content_type="text/tsv"); - if you call
partition_tsv()directly, download the file and passfile=; - for a compressed file such as
x.tsv.gz, download it and pass the local path asfilename, becausepartition(url=...)does not decompress; - for
s3://or another storage scheme, download the file with that storage's own client, becausepartition(url=...)only acceptshttpandhttps.
- for an HTTP(S) address, use
- CSV with a URL as
filename: nothing changes for users of released versions. They openedfilenamewithopen(), so a URL already raisedFileNotFoundError, and it still does. Only unreleasedmainpassed it to Pandas or raisedValueError. partition_csv(filename=..., file=...)used to readfilenameand ignorefile. It now raisesValueError("Exactly one of filename and file must be specified."), matchingpartition_tsv().
Testing
- New CSV and TSV tests check that a URL-shaped
filename(http,https,s3andfileschemes) raisesFileNotFoundErrorwithout resolving a host or reaching Pandas, and that a local file whose name looks like a URL is read. Without this change, these tests fail onmain. - New CSV and TSV tests read single-file
.zipand.tararchives and compressed files by name, since Pandas now gets the open file rather than its name.
(authored by codex)