Repository navigation
Conversation
…kage stays importable
…o pydantic-style objects serialise correctly
…erminusEnum and PydanticSchema
…b loading and codegen
…bpy check drift gate
…sdb-client-python into pydantic-native
This branch has not been deployed
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.
This adds a first-class pydantic interface for schema management and typed documents, so database schemas and Python domain models can evolve together without drift.
TerminusModelsubclasses declare the same schemaDocumentTemplatedeclares and emit identical class documents, so both syntaxes coexist in oneSchemaand one commit. The legacy path is untouched in behaviour — it remains the fully supported compatibility interface.AI-assisted PR writing.
What is included
terminusdb_client/schema/pydantic.pycarries the whole integration:TerminusModel,TerminusUnion(tagged unions, includingsys:Unitmembers viaNoneannotations),TerminusEnum,PydanticSchema, and theUnfold/Arrayannotation markers. Class-level flags cover the schema constructs the legacy client never mapped —_shared,_foreign,_metadata,_unfoldable,_abstract,_base— andField(min_length/max_length)maps to@cardinality/@min_cardinality/@max_cardinalityonSetproperties.Instances serialise through the existing
_obj_to_dict/_embedded_rep/@capturecontract, soinsert_document,update_document,replace_documentand capture/reference graphs work unchanged. Documents parse back into validated models viaPydanticSchema.parse_document(client.get_document(...)); folded links materialise as id-carrying stubs with a private_backend_id.PydanticSchema.from_dbrebuilds models from the schema graph in dependency order (inheritance included), resolves forward references withmodel_rebuild, and restores class/property documentation as docstrings andField(description=...).tdbpy sync --format pydanticgenerates aschema.pyofTerminusModelclasses whose emitted schema round-trips against the database;model_backendinconfig.jsonselects the emitter.tdbpy checkdiffsschema.pyagainst the database schema and exits 1 on drift (with--ignore-docsfor documentation-only differences), giving CI a real drift gate.Compatibility changes
The only edits to existing code are four
hasattr→getattr(..., None) is not Noneguards inClient.py— needed because pydanticPrivateAttrs andClassVardefaults are always present even when unset (_backend_idstamp-back,_capture,_subdocument).schema.pygainsUuidV7Key(engine-supported, previously missing), an optionalmetadatacontext argument, andSchema.diff.woqlschema/__init__.pyno longer leaks theschemamodule name, which shadowed theschemapackage. Pydantic is an optional extra (terminusdb[pydantic],pydantic>=2.7,<3); the client imports and works unchanged without it.Verified behaviour
Unit suite: 1410 tests pass including 50 new pydantic tests covering golden parity with
DocumentTemplatetwins, all annotation mappings, stub/capture behaviour,from_dbround-trips, generated-module imports, anddiffsemantics. Integration suite against a live server: schema push, legacy reload, insert→parse→replace, folded-link stubs,@shared/@unfoldable/@unfold, mixedDocumentTemplate+TerminusModelcommits, anddiffin both directions — all passing.Engine-verified semantics worth noting:
Optional[Set]/Optional[List]raiseTypeErrorat declaration (the engine rejects them),@keyis repeated by the server on subclasses so models always emit the effective key, and@inheritsscalar-vs-list shape is normalised insidediffcomparisons.