fix(csv): read a CSV or TSV filename only from the local filesystem

Merged
CR
6d ago
+262-56

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 that partition(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 like https://.

The contract after this change

  • filename is always a local path. partition_csv() and partition_tsv() open it with Python's open() and give Pandas only the open file, so no filename is ever downloaded. A file whose name ends in .gz, .bz2, .xz, .zip, .zst or .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 as https:// or s3://, the message says to pass an HTTP(S) address as url to partition(), 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 contains example.com/x.csv, then filename="https://example.com/x.csv" reads that file, because the operating system treats // as /.
  • Remote documents go through partition(url=...). It fetches once through safe_get(), hands the response to the CSV or TSV partitioner as file, and sets metadata.url. No new network code is added.
  • Exactly one source. partition_csv() now requires exactly one of filename and file, as partition_tsv() and most other partitioners already do. partition() still requires exactly one of filename, file and url, and still rejects a filename that isn't an existing local file. So partition(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 unreleased main a CSV file with such a name was rejected with ValueError.

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 filename now raises FileNotFoundError. 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 pass file=;
    • for a compressed file such as x.tsv.gz, download it and pass the local path as filename, because partition(url=...) does not decompress;
    • for s3:// or another storage scheme, download the file with that storage's own client, because partition(url=...) only accepts http and https.
  • CSV with a URL as filename: nothing changes for users of released versions. They opened filename with open(), so a URL already raised FileNotFoundError, and it still does. Only unreleased main passed it to Pandas or raised ValueError.
  • partition_csv(filename=..., file=...) used to read filename and ignore file. It now raises ValueError("Exactly one of filename and file must be specified."), matching partition_tsv().

Testing

  • New CSV and TSV tests check that a URL-shaped filename (http, https, s3 and file schemes) raises FileNotFoundError without 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 on main.
  • New CSV and TSV tests read single-file .zip and .tar archives and compressed files by name, since Pandas now gets the open file rather than its name.

(authored by codex)