Skip to content

Upload in-place edits of nested metadata - #454

Draft
shcheklein wants to merge 2 commits into
mainfrom
nested-metadata-edits
Draft

shcheklein wants to merge 2 commits into
mainfrom
nested-metadata-edits

Conversation

@shcheklein

@shcheklein shcheklein commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Changing a nested metadata value in place and then calling Upload() sent nothing, and nothing said so.

Example

file = drive.CreateFile({"id": file_id})
file.FetchMetadata(fields="appProperties,parents")

file["appProperties"]["stage"] = "done"   # not uploaded
file["parents"][0] = archive_folder_id    # file not moved
file.Upload()                             # succeeds, sends an empty update

Assigning a new value always worked, for example file["appProperties"] = {**file["appProperties"], "stage": "done"}.

Cause

To send only what changed, PyDrive2 compares the file with a copy taken when Drive last answered. The copy was shallow: a nested dict or list was the same object in both places. An edit in place changed both sides, so the comparison found no difference.

Fix

The copy now has its own copy of the nested fields that an upload can change: appProperties, properties and parents (lists and dicts of strings, copied one level deep), and contentHints, contentRestrictions and downloadRestrictions (dicts inside, copied in full). Other nested fields, such as owners or permissions, stay shared: an upload cannot change them, so copying them would only cost memory.

A default listing returns parents with every file, so each listed file keeps one more small list. Measured over 2,000 listed files: about 90 bytes more per file, from about 2,070 to 2,160 bytes.

Appending a second parent in place (file["parents"].append(...)) used to be ignored too. It now raises the error that assigning two parents already raised: a Drive v3 file has one parent.

Validation

  • Live, on real Drive: a new object reads the file's appProperties, properties or parents, edits the value in place and uploads. Drive then stores the new value, and for parents the file is in the new folder. The same holds for parents of a file that came from ListFile(), and for unlocking a file by setting readOnly to false in its fetched contentRestrictions. These fail on main.
  • Live: appending a second parent in place raises ValueError before any request.
  • Offline, because Drive never returns contentHints: after an in-place edit of contentHints, the next upload sends them. It fails on main.
  • The offline suite passes with the current google-auth and with 2.15.0 (537 passed).

🤖 Generated with Claude Code

PyDrive2 finds edits by comparing the file with a copy taken when Drive
last answered. The copy shared nested values with the file, so an edit
made in place, such as file["appProperties"]["stage"] = "done" or a
changed entry in file["parents"], changed both, and Upload() sent
nothing. The copy now has its own appProperties, properties,
contentHints and parents.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread pydrive2/files.py Resolved
Comment thread pydrive2/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: clean root-cause fix for shallow-copy aliasing. Minor gaps to check: contentRestrictions, avoiding copy.deepcopy on every listed file's parents, and covering parents.append(...) + ListFile() in tests.

Comment thread pydrive2/files.py Resolved
Comment thread pydrive2/files.py Resolved
Comment thread pydrive2/test/test_file.py Resolved
contentRestrictions and downloadRestrictions are writable nested fields,
so the saved copy gets its own copy of them. parents, properties and
appProperties hold only strings, so a shallow copy is enough and keeps
listings fast.

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 nested-metadata change-tracking update.

No open issues 🎉

What changed in this PR

Fixes PyDrive2’s metadata change tracking so Upload() detects in-place edits to writable nested fields without copying read-only fields.

Changes:

  • Copies editable nested metadata when saving the clean state.
  • Adds live regression tests for metadata edits, folder moves, and parent validation.
  • Adds an offline test for uploading edited contentHints.
File Description
pydrive2/​test/​test_files_unit.py Tests uploads after in-place contentHints edits.
pydrive2/​test/​test_file.py Tests nested edits, parent changes, and content unlocking against Drive.
pydrive2/​files.py Isolates editable nested fields in saved metadata snapshots.

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