On this page

SpellMap๏ƒ

Use md.SpellMap from the public package namespace.

Implementation source

class SpellMap(spell: Any | None = None, *, spellframe: Any | None = None, binding_name: str | None = None, override: dict | list | tuple | None = None)[source]๏ƒ

Declarative DI descriptor for normal spellbook-local resolution.

SpellMap is the explicit descriptor form of ordinary Melder DI intent. It tells SpellCrafter and Meld which spell or frame/binding identity should be injected at a given parameter or attribute location, optionally along with an override payload that should be carried into runtime construction.

Unlike SpellContract, this descriptor does not declare a cross-conduit hole that will be satisfied later. It stays inside the current resolution world. SpellCrafter and the runtime planning path inspect it, derive the canonical lookup identity, and then resolve a local spell through the usual Spellbook, SpellIndex, and Existence machinery.

Supported shapes:

  • SpellMap(MyService) Direct type-based DI where the spell itself supplies the lookup identity. It is a class lookup (2026-11-07): it reaches only MyService bound bare - no spellframe and the default binding - as a MyService annotation and meld(MyService) do. A MyService bound under a spellframe is reached through the frame (SpellMap(spellframe=..., binding_name=...)).

  • SpellMap(MyService, binding_name="primary") The named class lookup: MyService bound bare under that binding name.

  • SpellMap(ILogic) with a Protocol as spell Matches by identity, as every spell does: it reaches only a spell whose bound object is ILogic itself - the Protocol's own definition, bound resolvable=False, whose value the meld supplies by override - and never an implementation, so with only implementations bound it waits as an unresolved input (it failed at conjure before 2026-11-16). To inject an implementation filed under the Protocol, name the frame: SpellMap(spellframe=ILogic, binding_name=...). (Corrected 2026-11-07: this shape was described as frame-based DI, which Phase 3 never did.)

  • SpellMap(MyService, spellframe=ILogic, binding_name="primary") Fully explicit spell + frame + binding declaration.

  • SpellMap(spell=None, spellframe=ILogic, binding_name="primary") Frame-only descriptor where the DI key is derived entirely from frame and binding.

Typical usage:

class MyService:
    def __init__(self, repo=SpellMap(MyRepo)):
        self.repo = repo

class UsesLogic:
    def __init__(self, logic=SpellMap(spellframe=ILogic, binding_name="primary")):
        self.logic = logic

class UsesConfig:
    def __init__(
        self,
        cfg=SpellMap(
            spell=None,
            spellframe=IAppConfig,
            binding_name="primary",
        ),
    ):
        self.cfg = cfg

Contract:

  • Pure intent object; it does not resolve providers by itself.

  • Used for normal in-graph DI, not late conduit linking.

  • Carries spell/frame/binding identity plus optional override payload.

  • Cleanable and invalid after cleanup.

  • Should not be subclassed or treated like a runtime-resolved object.

Threading:

Value-shaped and effectively immutable after construction: the four slots are set in __init__ and read thereafter. No lock is taken, because a descriptor written into a constructor default is shared read-only across every resolution that reads it.

Lifecycle / Cleanup:

Cleanable. The descriptor lives as long as the class default that declares it, which in practice is the lifetime of the defining module.

Registration:

MELDER KERNEL, USER-INSTANTIATED but NOT user-bindable. A user writes SpellMap(MyRepo) in their own constructor default constantly, so instances are authored outside melder - but binding the SpellMap CLASS itself is refused, which would be meaningless because a descriptor is a statement of intent, not a service to resolve.

Subsystem Context:

One of the two declarative DI descriptors, paired with SpellContract. The division is scope: SpellMap stays INSIDE the current resolution world and resolves through the ordinary Spellbook / SpellIndex / Existence machinery, while SpellContract declares a hole a future linked conduit may fill. Phase 1 classifies each parameter into a ParameterDIShape; a SpellMap default lands as SPELLMAP_DEFAULT, and Phase 3 performs the actual candidate resolution.

System Context:

The four supported shapes exist because DI identity in Melder is a (spell, frame, binding) triple rather than a single type key, and different call sites know different parts of it. Passing a concrete class supplies the whole identity (a class lookup: that class bound bare); a Protocol passed as spell is matched by identity too, so it names the Protocol's own definition, never an implementation; the frame-only form (spell=None) exists for the case where the caller deliberately knows NOTHING but the contract and the binding name, and is how an implementation filed under a Protocol is injected. Ambiguity is a build-time failure, not a runtime one: a SpellMap default that resolves to several candidates raises rather than silently picking. That is the whole reason the explicit binding_name form exists - it is the documented way to disambiguate when several spells legitimately satisfy the same frame. A SpellMap that resolves to none waits instead (2026-11-16, GitHub #329: the owner's rule that bind order must not matter; it raised before): Phase 3 records an unresolved input at its address, so a consumer bound before its provider binds, its meld raises UnresolvedInputError until a spell is bound there or the value is supplied, and the bind of that spell re-gates it.

AGENT_ACCESS: public

AGENT_PURPOSE:

access: public. Declarative DI descriptor for normal in-graph resolution. Write it as a constructor default: SpellMap(MyRepo) for MyRepo bound bare, SpellMap(MyRepo, binding_name='primary') for MyRepo bound bare under that name, or SpellMap(spellframe=ILogic, binding_name='primary') for a spell filed under a frame. Several matches raise at build time; no match waits as an unresolved input until a spell is bound at the address or the meld supplies the value.

cleanup() → None[source]๏ƒ

Release descriptor references and invalidate the object.

Contract:

  • Idempotent and safe to call multiple times.

  • Clears mutable override payloads before dropping references.

  • After cleanup the descriptor should be treated as unusable and callers should rely on check_cleaned() before reading it.

Returns:

None.

property lookup_triplet: tuple[Any, Any | None, str | None]๏ƒ

Return the raw descriptor identity tuple.

This is the shape SpellCrafter and the runtime planning path consume before they derive the normalized key or resolve the underlying spell.

Contract:

  • Returns the RAW, AS-SUPPLIED triplet - the spell, spellframe and binding name exactly as the caller gave them, WITHOUT normalization.

  • This is NOT the registry key. Use canonical_key when you need the normalized identity the spellbook actually indexes by; the two can differ for the same SpellMap.

Threading:

Pure computation over immutable fields; safe from any thread.

Lifecycle / Cleanup:

Carries no cleaned-state guard.

Returns:

(spell, spellframe, binding_name) exactly as stored on the descriptor.

Return type:

tuple[Any, Optional[Any], Optional[str]]

Notes

  • For frame-only SpellMaps (spell is None), only spellframe and binding_name contribute to the runtime identity.

  • For fully explicit SpellMaps, higher layers may use both spell and spellframe when enforcing resolution rules.

  • The binding name is the caller's text (0.2.8226); canonical_key carries the normalized form that matching uses.

property canonical_key: Tuple[str, str]๏ƒ

Return the normalized (frame_key, binding_key) pair.

This is the canonical Spellbook-style key derived through SpellInputUtils.normalize_spell_key(...). It is the stable string pair higher layers use when indexing or matching SpellMap intent.

Shape rules:

  • when spellframe is present, the frame key is derived from spellframe

  • when spellframe is absent, the frame key is derived from spell

  • binding defaults normalize to "__default__" inside the helper path

Contract:

  • NORMALIZED identity: it runs the raw triplet through the shared key normalizer, so equivalent-but-differently-spelled inputs collapse to the same key. This is what the spellbook indexes by.

  • Recomputed on every access rather than cached, so it always reflects the current field values.

Threading:

Pure computation over immutable fields; safe from any thread.

Lifecycle / Cleanup:

Carries no cleaned-state guard.

Returns:

Normalized (frame_key, binding_key) pair.

Return type:

Tuple[str, str]

property spell_key: Tuple[str, str]๏ƒ

Compatibility alias for canonical_key.

Contract:

  • ALIAS for canonical_key, kept for call-site readability. Identical behaviour - it is the NORMALIZED key, not the raw triplet.

Threading:

Pure computation over immutable fields; safe from any thread.

Lifecycle / Cleanup:

Carries no cleaned-state guard.

Returns:

The same normalized key pair returned by canonical_key.

Return type:

Tuple[str, str]

Topic reference ยท Full contents