Skip to content

Fail clearly on shortcuts and refused exports; du from one listing - #460

Draft
shcheklein wants to merge 3 commits into
mainfrom
workspace-files-fail-clearly
Draft

shcheklein wants to merge 3 commits into
mainfrom
workspace-files-fail-clearly

Conversation

@shcheklein

@shcheklein shcheklein commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

I ran the filesystem against a real Drive folder holding a Google Doc, a Google Sheet, a shortcut and a text file. Three things failed in a way the user could not act on.

1. Reading a shortcut

fs.cat_file("FOLDER_ID/link")    # a shortcut to a Doc

Before:

pydrive2.files.ApiRequestError: <HttpError 400 when requesting .../files/1Wq.../export?mimeType=text%2Fplain ...>

The download is refused, so PyDrive2 tried a text export, which Drive also refuses for a shortcut. The core API already said what is wrong (FileNotDownloadableError: Folders and shortcuts have no content), because it knew the file's type. Through the filesystem it did not.

After, the same FileNotDownloadableError. When Drive refuses the export of a file whose type is not known yet, PyDrive2 reads the type (one request) to tell a shortcut from a Workspace file. A Google Doc read by ID still takes two requests, the refused download and the export. A file already known to be a folder or a shortcut raises before any request.

2. Reading a Sheet as text

fs.cat_file("FOLDER_ID/sheet")
drive.CreateFile({"id": sheet_id}).GetContentString()

Before: ApiRequestError 400 badRequest. Drive cannot export a Sheet as text/plain, the default export type.

After:

FileNotDownloadableError: Drive cannot export this file as text/plain. Pass another mimetype to GetContentString(), GetContentFile() or GetContentIOBuffer(), such as text/csv for a Google Sheet.

GetContentString(mimetype="text/csv") returns the table, as before. The filesystem has no way to pass a mimetype yet; that is part of the open question below.

The export errors are handled in one place. Drive's answers for a type it cannot export to (badRequest, conversionUnsupported, exportFormatNotFound) give the message above. Its answers for a file it will not export at all (fileNotExportable, cannotExportFile) raise FileNotDownloadableError with Drive's message, as FetchContent() already did, and now GetContentString(), GetContentFile() and GetContentIOBuffer() do too. Other errors stay ApiRequestError, and so does a refused export of a file that is not a Google type.

3. du() of a folder with a shortcut

fs.du("FOLDER_ID")

Before:

TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

Drive gives a shortcut no size, and fsspec's du() adds up the sizes from one info() per file. Now du() takes the sizes from one detailed find(), the listing it needs anyway, and counts a missing size as 0. That also removes the info() request per file: a folder of 1,000 files cost about 1,000 more requests before. With withdirs=True, directory names end in a slash, as info() spells them. The total comes from a listing without folders, which have no size, so withdirs cannot change it: with folders listed, a folder with a lower ID hides a file of the same name.

Not changed, needs a decision

A recursive fs.get() of such a folder writes the Doc's text and then stops at the shortcut, with the error above now clearer. Whether get() should skip shortcuts, export Sheets as CSV, or refuse up front, and whether the filesystem should accept a mimetype, is a separate decision.

Validation

Live, on real Drive:

  • test_fs.py (its workspace_files fixture gains a Sheet): cat_file of the shortcut and of the Sheet raises FileNotDownloadableError, and of the Doc returns its text; du() of a folder with a shortcut, a text file and an empty folder counts the shortcut as 0, with total=False, withdirs=True and detail=False;
  • test_file.py: GetContentString() by ID of a shortcut and of a Sheet raises FileNotDownloadableError; of the Sheet as text/csv, it returns the table; GetContentFile() of a Doc by ID as application/pdf writes a PDF (the README example).

Offline: the du() total counts a file that shares its name with a folder of lower ID, with and without withdirs (only a mock can choose Drive's IDs). The existing offline tests are unchanged; one of them checks that a Doc read by ID takes two requests. The offline suite passes with the current google-auth and with 2.15.0 (538 passed).

🤖 Generated with Claude Code

On real Drive:
- reading a shortcut through the filesystem raised ApiRequestError 400
  from a text export Drive refuses; it now raises
  FileNotDownloadableError, as the core API already did;
- reading a Google Sheet as text raised ApiRequestError 400; an export
  Drive refuses now raises FileNotDownloadableError that names the
  type and suggests another mimetype;
- du() raised TypeError on a folder with a shortcut, which has no size.
  It now adds up the sizes from one detailed find(), counting a missing
  size as 0, instead of one info() request per file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/files.py Resolved
Comment thread pydrive2/test/test_workspace_files.py Resolved

@shcheklein shcheklein left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[AI generated — Copilot]

5-pass review summary: great usability improvements for shortcuts, refused Sheets exports, and du()'s N+1 info() calls — but introduces a +1 HTTP request regression on every Google Doc download without mimeType (including fs.cat_file), sends a wasted get_media request when mimeType is already known to be a folder/shortcut, splits export-error handling across two methods, and adds another standalone test file duplicating workspace_files.

Comment thread pydrive2/files.py Resolved
Comment thread pydrive2/files.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/test/test_workspace_files.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
A Google Doc read by ID stays at two requests: the refused download and
the export. The type is read only when the export is refused too, to
tell a shortcut from a Workspace file. A folder or shortcut of a known
type raises before any request, and FetchContent shares the export
error handling instead of repeating it.

du() keeps info()'s directory names with withdirs=True, sums without a
second dictionary, and accepts detail= from fsspec callers.

The live tests move into test_fs.py and test_file.py, next to the
fixtures they extend.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Same-name file and directory collisions are resolved inconsistently in find(), causing withdirs=True to drop a file and incorrectly reduce the reported size.

1 open issue
What changed in this PR

Improves Drive read errors and makes filesystem disk-usage calculations use listing metadata instead of per-file requests.

Changes:

  • Clarifies errors for shortcuts and refused Workspace exports.
  • Calculates du() from a detailed listing, treating missing sizes as zero.
  • Adds live tests for export behavior and disk usage.
File Description
pydrive2/​test/​test_fs.py Tests filesystem reads and shortcut disk usage.
pydrive2/​test/​test_file.py Tests Sheet, shortcut, and PDF export behavior.
pydrive2/​fs/​spec.py Adds listing-based du().
pydrive2/​files.py Centralizes export errors and rejects contentless types.

Review effort: Balanced

Comment thread pydrive2/fs/spec.py Resolved
Folders have no size, and with withdirs=True a folder with a lower ID
hides a file of the same name, so the total now comes from a listing
without folders.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

There are no active review issues for the download-error handling and listing-based disk-usage changes.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants