Skip to content

chore(mongoengine): migrate off deprecated GridOut.contentType ahead of PyMongo 5.0 #2920

Description

@hasansezertasan

Context

PyMongo deprecated the GridOut.contentType property and will remove it in PyMongo 5.0:

GridOut property 'contentType' is deprecated and will be removed in PyMongo 5.0

Although flask-admin's code reads data.content_type (snake_case), the underlying GridFS GridOut object still resolves this through the deprecated contentType accessor, so the DeprecationWarning fires on every read.

Flask-admin's test suite runs pytest with filterwarnings = ["error", ...], which promotes every warning to an error. PR #2918 added a default:GridOut property 'contentType' is deprecated...:DeprecationWarning entry to pyproject.toml, which downgrades this specific warning back to the default (print-once) behavior so the new MongoEngine test could pass. The warning itself is not silenced — it is just exempted from the strict-errors policy. This issue tracks the actual migration off the deprecated property.

Affected call sites (read path — GridOut / GridFSProxy)

  • flask_admin/contrib/mongoengine/view.pyapi_file_view reads data.content_type where data = fs.get(...) is a raw GridOut
  • flask_admin/contrib/mongoengine/widgets.pyMongoFileInput reads field.data.content_type where field.data is a GridFSProxy
  • flask_admin/contrib/mongoengine/typefmt.pygrid_formatter reads value.content_type where value is a GridFSProxy/GridOut

Not affected

  • flask_admin/contrib/mongoengine/fields.py — this reads content_type from a Werkzeug FileStorage (the uploaded file) on the write path and passes it into GridFSProxy.put/replace. Not a GridOut attribute access; unrelated to the PyMongo deprecation.

Proposed approach

  1. Two of the three read sites go through MongoEngine's GridFSProxy rather than raw PyMongo. Investigate whether MongoEngine already exposes (or plans to expose) a non-deprecated accessor; if so, use it directly. If not, fall back to reading the content type from the GridFS document's metadata dict (PyMongo's recommended location in 5.0) with a mimetypes.guess_type(filename) fallback when missing.
  2. Centralize the lookup in a small helper (e.g. _gridfs_content_type(data)) so all three read-side call sites share one implementation and the raw-GridOut site and GridFSProxy sites stay consistent.
  3. Remove the default:GridOut property 'contentType'... entry from pyproject.toml once the migration lands and verify the suite still passes under filterwarnings = ["error", ...].
  4. Verify the test added in fix(mongoengine): set Content-Disposition on api_file_view downloads #2918 (test_api_file_view_sets_content_disposition) still asserts the correct Content-Type (text/plain) after the migration.

Out of scope

  • Behavioral changes to api_file_view beyond the content-type source.
  • Any unrelated PyMongo or MongoEngine deprecations.

References


Disclosure: I drafted this issue with help from Claude Code while reviewing feedback on PR #2918; the affected call sites were verified manually against the current master.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions