On this page

Deep spell override paths๏ƒ

๐ŸŸ  Advanced ยท Lesson 01

Deep override - the ">"-path form. A path of parameter names walks the dependency graph from the melded root and REPLACES the actual object at that socket: override={"transport>credentials": my_object} Untargeted sockets keep their DI-resolved defaults WITHIN the call; a path that matches nothing refuses. But read the second half of this lesson before you reach for it: under a singleton lifetime the overridden meld BUILDS the singleton, so the substitution outlives the call that made it. This is the surgical form - injecting a fixture or a variant into the MIDDLE of a real graph at meld time without rebinding anything. The simple top-level form (flat dict into the root's own constructor) is intermediate lesson 08.

Before you run๏ƒ

Use the Advanced guide for prerequisite concepts. Run from a checkout with Melder installed and Python 3.14 free-threading selected. The collection download includes the level's local helper modules.

Run the saved script๏ƒ

python UX_and_AIX_experiences/03_advanced/01_deep_spell_override_paths.py
py -3.14t UX_and_AIX_experiences/03_advanced/01_deep_spell_override_paths.py

Download this collection ยท Source on GitHub

Public surface๏ƒ

meld(override={"path>to>socket": object})

Code๏ƒ

 1"""
 2TIER: advanced (01)
 3GOAL: Deep override - the ">"-path form. A path of parameter
 4      names walks the dependency graph from the melded root and
 5      REPLACES the actual object at that socket:
 6          override={"transport>credentials": my_object}
 7      Untargeted sockets keep their DI-resolved defaults WITHIN the call; a
 8      path that matches nothing refuses. But read the second half of this
 9      lesson before you reach for it: under a singleton lifetime the
10      overridden meld BUILDS the singleton, so the substitution outlives the
11      call that made it. This is the surgical form - injecting a
12      fixture or a variant into the MIDDLE of a real graph at meld time
13      without rebinding anything. The simple top-level form (flat dict
14      into the root's own constructor) is intermediate lesson 08.
15SURFACE EXERCISED: meld(override={"path>to>socket": object})
16"""
17import melder as md
18
19
20class Credentials:
21    def __init__(self) -> None:
22        self.source = "vault"
23
24
25class Transport:
26    def __init__(self, credentials: Credentials) -> None:
27        self.credentials = credentials
28
29
30class MailPipeline:
31    def __init__(self, transport: Transport) -> None:
32        self.transport = transport
33
34
35def main() -> None:
36    book = md.Spellbook()
37    book.bind(spell=Credentials, existence="unique")
38    book.bind(spell=Transport, existence="unique")
39    book.bind(spell=MailPipeline, existence="unique")
40    conduit = book.conjure()
41
42    # The path walks parameter names from the melded root:
43    # MailPipeline(transport=...) -> Transport(credentials=...).
44    test_credentials = Credentials()
45    test_credentials.source = "test-fixture"
46    pipeline = conduit.meld(
47        spell=MailPipeline,
48        override={"transport>credentials": test_credentials},
49    )
50    assert pipeline.transport.credentials is test_credentials
51    print("replaced a spell two levels deep:",
52          pipeline.transport.credentials.source)
53
54    # THE SHARP EDGE - the half of this surface that will bite you.
55    # Every spell here is bound `unique`: one instance per frame. The meld
56    # above did not build a private throwaway graph. It CONSTRUCTED the
57    # singleton Transport - around your fixture - and registered it as the
58    # canonical one. The override therefore did not end when the call did.
59    plain = conduit.meld(spell=Transport)
60    assert plain is pipeline.transport
61    assert plain.credentials.source == "test-fixture"
62    print("the override BUILT the singleton; later melds reuse it:",
63          plain.credentials.source)
64
65    # THE RULE: an override is surgical in WHERE it reaches, not in HOW LONG
66    # it lasts. Its lifetime is the lifetime of whatever it helped build.
67    # Override into `unique` and you have changed the world; override into
68    # `many` and you have changed one call. If you want a fixture that cannot
69    # escape, bind the target `many` so every meld constructs its own.
70
71
72if __name__ == "__main__":
73    main()

Check the outcome๏ƒ

The script contains its own assertions or demonstrated refusal paths. Run it to evaluate those checks against your installed version. The code above is taken directly from the saved file; no run output is invented here.

More advanced examples ยท Level guide