Refactor authentication document creation to use Pydantic models (PP-3675)#3066
Draft
jonathangreen wants to merge 5 commits intomainfrom
Draft
Refactor authentication document creation to use Pydantic models (PP-3675)#3066jonathangreen wants to merge 5 commits intomainfrom
jonathangreen wants to merge 5 commits intomainfrom
Conversation
Replace raw dict-based authentication document construction with structured Pydantic models (BaseOpdsModel subclasses), providing type safety, validation, and consistent serialization. - Add Palace-specific auth models in opds/palace_authentication.py (PalaceAuthentication, PalaceAuthenticationDocument, etc.) - Move MEDIA_TYPE/LINK_RELATION constants to AuthenticationDocument - Change OPDSAuthenticationFlow to return PalaceAuthentication models - Update all providers (basic, basic_token, SAML, OIDC) accordingly - Remove legacy AuthenticationForOPDSDocument class - Serialize via model_dump() instead of manual dict construction
…ments Make Authentication and AuthenticationDocument generic over their link and authentication types using covariant TypeVars with PEP 696 defaults. Since BaseOpdsModel is frozen (immutable), covariance is safe and allows Palace subclasses to parametrize with concrete types instead of overriding fields with incompatible types.
Add a type mismatch check in authentication_flow_document to ensure implementations set the correct type, and remove duplicate MEDIA_TYPE and LINK_RELATION from PalaceAuthenticationDocument since they are inherited from AuthenticationDocument.
Add None check for library() return in BasicTokenAuthenticationProvider and remove unused type: ignore[method-assign] comments in tests.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Refactor the authentication document creation pipeline to use structured Pydantic models (
BaseOpdsModelsubclasses) instead of building raw Python dicts. This brings the Authentication for OPDS document generation in line with the rest of the OPDS2 serialization in the codebase.Key changes:
opds/authentication.py, makingAuthenticationandAuthenticationDocumentgeneric over their link and auth types using covariant TypeVars with PEP 696 defaultsopds/palace_authentication.pywith Palace-specific extension models (PalaceAuthentication,PalaceAuthenticationDocument,PalaceAuthenticationLink, input descriptors, features, announcements, etc.)OPDSAuthenticationFlow._authentication_flow_document()return type fromdict[str, Any]toPalaceAuthenticationLibraryAuthenticator.create_authentication_document()to build aPalaceAuthenticationDocumentmodel and serialize viamodel_dump()AuthenticationForOPDSDocumentclassMotivation and Context
This lays groundwork for PP-3675. The previous dict-based approach lacked type safety, validation, and consistency with the rest of the OPDS2 feed serialization. By using Pydantic models, we get structured validation, consistent serialization via
model_dump(), and better IDE/type-checker support.Palace extensions are kept separate from pure OPDS spec models following the existing pattern (
opds/palace.pyfor OPDS2 feeds).How Has This Been Tested?
tests/manager/opds/test_authentication.py)tests/manager/opds/test_palace_authentication.py)Checklist