Skip to content

feat: auto-register Entity subclasses at class definition time#7

Merged
deucalioncodes merged 2 commits intomainfrom
feature/auto-register-entity-types
Apr 6, 2026
Merged

feat: auto-register Entity subclasses at class definition time#7
deucalioncodes merged 2 commits intomainfrom
feature/auto-register-entity-types

Conversation

@deucalioncodes
Copy link
Copy Markdown
Member

Summary

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.

Problem

Entity types were only registered in _entity_types when:

  1. An instance was created (Entity.__init__)
  2. .instances() was called

After a canister upgrade, _entity_types started empty. Types that existed in storage but weren't touched during init were invisible — e.g. %db types wouldn't show them.

Every canister needed manual registration boilerplate at startup (see Realms' ~15-line loop).

Solution

  • __init_subclass__ on Entity checks Database._instance directly (no auto-create)
  • If DB exists → register immediately
  • If not → append to Entity._deferred_types
  • Database.init() flushes deferred types after creating the instance
  • register_entity_type() is idempotent, so existing manual registration code is unaffected

Tests

8 new tests in test_auto_register.py:

  • Type registered at definition time
  • Multiple types registered
  • Registration without creating instances
  • Registration survives instance creation
  • Namespaced entity registration
  • Database.clear() preserves type registration
  • _flush_deferred_types idempotency
  • Deferred types flushed on Database.init()

Full suite: 122 passed (8 pre-existing failures in audit/database tests unchanged).

Closes #6

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
@deucalioncodes deucalioncodes force-pushed the feature/auto-register-entity-types branch from 90d43ba to 7799e36 Compare April 6, 2026 16:24
@deucalioncodes deucalioncodes merged commit 119980c into main Apr 6, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Entity subclasses should auto-register with Database at class definition time

1 participant