feat: auto-register Entity subclasses at class definition time#7
Merged
deucalioncodes merged 2 commits intomainfrom Apr 6, 2026
Merged
feat: auto-register Entity subclasses at class definition time#7deucalioncodes merged 2 commits intomainfrom
deucalioncodes merged 2 commits intomainfrom
Conversation
Add __init_subclass__ to Entity so that every subclass is automatically registered in Database._entity_types when the class is defined, not just when instances are created. Types defined before Database.init() are collected in _deferred_types and flushed when the Database is first initialized. This eliminates the need for manual register_entity_type() boilerplate in every canister's startup code. Closes #6
90d43ba to
7799e36
Compare
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.
Summary
Add
__init_subclass__toEntityso that every subclass is automatically registered inDatabase._entity_typeswhen the class is defined, not just when instances are created.Problem
Entity types were only registered in
_entity_typeswhen:Entity.__init__).instances()was calledAfter a canister upgrade,
_entity_typesstarted empty. Types that existed in storage but weren't touched during init were invisible — e.g.%db typeswouldn't show them.Every canister needed manual registration boilerplate at startup (see Realms' ~15-line loop).
Solution
__init_subclass__onEntitychecksDatabase._instancedirectly (no auto-create)Entity._deferred_typesDatabase.init()flushes deferred types after creating the instanceregister_entity_type()is idempotent, so existing manual registration code is unaffectedTests
8 new tests in
test_auto_register.py:Database.clear()preserves type registration_flush_deferred_typesidempotencyDatabase.init()Full suite: 122 passed (8 pre-existing failures in audit/database tests unchanged).
Closes #6