Repository navigation
Fail clearly on shortcuts and refused exports; du from one listing - #460
shcheklein wants to merge 3 commits into
Conversation
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>
shcheklein
left a comment
There was a problem hiding this comment.
[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.
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>
There was a problem hiding this comment.
🟡 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
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>

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
Before:
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
Before:
ApiRequestError400badRequest. Drive cannot export a Sheet astext/plain, the default export type.After:
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) raiseFileNotDownloadableErrorwith Drive's message, asFetchContent()already did, and nowGetContentString(),GetContentFile()andGetContentIOBuffer()do too. Other errors stayApiRequestError, and so does a refused export of a file that is not a Google type.3.
du()of a folder with a shortcutBefore:
Drive gives a shortcut no size, and fsspec's
du()adds up the sizes from oneinfo()per file. Nowdu()takes the sizes from one detailedfind(), the listing it needs anyway, and counts a missing size as 0. That also removes theinfo()request per file: a folder of 1,000 files cost about 1,000 more requests before. Withwithdirs=True, directory names end in a slash, asinfo()spells them. The total comes from a listing without folders, which have no size, sowithdirscannot 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. Whetherget()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(itsworkspace_filesfixture gains a Sheet):cat_fileof the shortcut and of the Sheet raisesFileNotDownloadableError, 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, withtotal=False,withdirs=Trueanddetail=False;test_file.py:GetContentString()by ID of a shortcut and of a Sheet raisesFileNotDownloadableError; of the Sheet astext/csv, it returns the table;GetContentFile()of a Doc by ID asapplication/pdfwrites a PDF (the README example).Offline: the
du()total counts a file that shares its name with a folder of lower ID, with and withoutwithdirs(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