Skip to content

Warnings/pydantic v2 cleanup#22463

Open
mvdbeek wants to merge 16 commits intogalaxyproject:devfrom
mvdbeek:warnings/pydantic-v2-mechanical
Open

Warnings/pydantic v2 cleanup#22463
mvdbeek wants to merge 16 commits intogalaxyproject:devfrom
mvdbeek:warnings/pydantic-v2-mechanical

Conversation

@mvdbeek
Copy link
Copy Markdown
Member

@mvdbeek mvdbeek commented Apr 13, 2026

These are straightforward refactors to use non-deprecated methods

How to test the changes?

(Select all options that apply)

  • I've included appropriate automated tests.
  • This is a refactoring of components with existing test coverage.
  • Instructions for manual testing are as follows:
    1. [add testing steps and prerequisites here if you didn't write automated tests covering all your changes]

License

  • I agree to license these and all my past contributions to the core galaxy codebase under the MIT license.

@mvdbeek mvdbeek force-pushed the warnings/pydantic-v2-mechanical branch from d00e8dd to 5a49127 Compare April 13, 2026 08:57
@github-actions github-actions bot added area/API area/database Galaxy's database or data access layer area/tool-framework labels Apr 13, 2026
@github-actions github-actions bot added this to the 26.1 milestone Apr 13, 2026
@mvdbeek mvdbeek closed this Apr 13, 2026
@github-project-automation github-project-automation bot moved this from Needs Review to Done in Galaxy Dev - weeklies Apr 13, 2026
@mvdbeek mvdbeek reopened this Apr 13, 2026
@github-project-automation github-project-automation bot moved this from Done to In Progress in Galaxy Dev - weeklies Apr 13, 2026
@mvdbeek mvdbeek force-pushed the warnings/pydantic-v2-mechanical branch from 1acd354 to 2f001fe Compare April 13, 2026 17:47
mvdbeek added 16 commits April 14, 2026 15:53
The .dict() method is deprecated in Pydantic V2 (PydanticDeprecatedSince20)
and slated for removal in V3. The identically-behaving replacement is
.model_dump() — all existing keyword arguments (exclude_unset,
exclude, by_alias) are supported unchanged.

Galaxy has already completed the bulk of the Pydantic v2 migration; these
are the straggling call sites still emitting deprecation warnings in CI.
.json() is deprecated in Pydantic V2; .model_dump_json() is the
identically-behaving replacement.
Pydantic V2 deprecates Model.parse_obj() (use Model.model_validate())
and the top-level parse_obj_as() helper (use
TypeAdapter(T).validate_python()).
Pydantic V2 deprecates Model.__fields__ in favour of the
Model.model_fields class property.
ShareableService._get_sharing_status was handing SharingStatus a
list of raw {'id': ..., 'email': ...} dicts for the
users_shared_with field, which is declared as list[UserEmail].
Pydantic model construction does not coerce the list in this code
path, so at serialization time the field holds dicts and Pydantic
emits PydanticSerializationUnexpectedValue warnings.

Build UserEmail instances directly — the EncodedDatabaseIdField
BeforeValidator handles id encoding.
The ToolDataField schema declares base_dir: List[str], but
TabularToolDataField.to_dict was producing a single-element tuple,
which trips PydanticSerializationUnexpectedValue for tuple-vs-list
mismatch on /api/tool_data responses.
Dictifiable.to_dict() stringifies any datetime attribute via
isoformat() before returning. UserModel.last_password_change is
declared as Optional[datetime], so model_construct() stashes the
string as-is and Pydantic emits
PydanticSerializationUnexpectedValue on whoami responses.

Add a no-op value_mapper entry to preserve the datetime object
through to_dict, so model_construct sees the correct type.
FastAPIExports._parse_export_metadata was passing the raw payload
dict straight into
ExportObjectRequestMetadata.model_construct(payload=...). The
field is declared as Union[WriteStoreToPayload,
ShortTermStoreExportPayload], so the stashed dict triggers
PydanticSerializationUnexpectedValue on any /api/exports call
that surfaces historical export metadata.

Mirror the discrimination logic already in
HistoryExportsManager.get_record_metadata: pick WriteStoreToPayload
when target_uri is present, otherwise ShortTermStoreExportPayload.
RefactorResponse.workflow uses WrapSerializer(safe_wraps) to wrap
the inner dict serialization in a fallback that calls
safe_dumps() on failure. The helper was annotated '-> str', but
the happy path (nxt(v) on a dict field) returns a nested dict
— not a string — so Pydantic emits
PydanticSerializationUnexpectedValue on /api/workflows/{id}/refactor
responses, expecting str and receiving dict.

The actual return type is Union[str, Any] (Any from nxt's inner
serialization). Annotate the helper as -> Any so Pydantic stops
enforcing a str output type on the happy path.
managers/datasets.py, managers/folders.py, and managers/libraries.py
each carried an identical inner closure (named make_tuples) that
returned a list of 2-element Python tuples. The corresponding
response schemas (DatasetAssociationRoles,
LibraryCurrentPermissions, LibraryFolderCurrentPermissions)
declare those fields as list[RoleNameIdTuple] where
RoleNameIdTuple = list[str], so every call emitted
PydanticSerializationUnexpectedValue at serialization time — the
field stored tuples but the type alias is list[str].

Extract a single role_name_id_pairs(roles, private_role_emails,
encode_id) helper in galaxy.model.db.role, where its companion
get_private_role_user_emails_dict already lives. The helper is
pure data shaping: it builds [displayed_name, encoded_id] inner
*lists* (not tuples) so the shape matches the schema alias, and
keeps the private-role -> user email rendering consistent across
the three call sites.

No behaviour change on the wire: RoleNameIdTuple is still
serialized as a 2-element JSON array.
Prevent regressions by turning PydanticDeprecatedSince20 warnings
into errors, now that all V1-style API usage has been replaced.
gravity still uses class-based Config which triggers
PydanticDeprecatedSince20; ignore it for that module only.
TPV still uses class-based Config which triggers
PydanticDeprecatedSince20; ignore it for that module only.
@mvdbeek mvdbeek force-pushed the warnings/pydantic-v2-mechanical branch from 2f001fe to bd74100 Compare April 14, 2026 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/API area/database Galaxy's database or data access layer area/tool-framework

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

1 participant