How it works
1) Finder & loader
Section titled “1) Finder & loader”- Importing
wishfulinstalls aMagicFinderat the front ofsys.meta_path. - Imports under
wishful.static.*andwishful.dynamic.*get routed toMagicLoader. - Internal modules (
wishful.core,wishful.config, …) bypass the hook to avoid recursion.
2) Context discovery
Section titled “2) Context discovery”discover() looks around the call site:
- Parses the import to know which symbols are wanted.
- Scrapes nearby code/comments (radius configurable via
WISHFUL_CONTEXT_RADIUSorwishful.set_context_radius). - Pulls registered type schemas and output bindings from
@wishful.type.
That bundle becomes prompt seasoning for the LLM.
3) Generation + caching
Section titled “3) Generation + caching”- Static: check cache → use if present; otherwise generate once and write to
.wishful/<module>.py. - Dynamic: always regenerate (runtime-context-aware), and also write a snapshot under
.wishful/_dynamic/. - Safety gate:
validate_codeblocks dangerous imports/calls unlessallow_unsafe=True. - Missing symbols? Static loader regenerates with the expanded symbol set. Dynamic loader proxies
__getattr__and call-time regeneration.
4) Settings & singletons
Section titled “4) Settings & singletons”wishful.settingsis stored onbuiltinsso it survives module reloads (handy for tests).install_finder()is idempotent: if a finder fromwishful.core.finderis already present, it won’t stack duplicates.MagicLoaderresolvesgenerate_module_codeeach call, so monkeypatches work even after reloads.
5) Cache layout
Section titled “5) Cache layout”wishful.static.foo→.wishful/foo.pywishful.dynamic.foo→.wishful/foo.py(same cache file) plus dynamic snapshot.wishful/_dynamic/foo.py- CLI helpers (
wishful inspect/clear/regen) and Python helpers (inspect_cache,clear_cache,regenerate) manage these paths.
That’s the core loop. Everything else (CLI, types, logging) builds on this.
6) Explore: multi-variant generation
Section titled “6) Explore: multi-variant generation”wishful.explore() is a smarter way to populate the cache:
- Generates multiple implementations of a function
- Tests each one against your criteria
- Benchmarks them (optional) to find the fastest
- Caches the winner to
.wishful/like a regular import
# Generate 5 variants, test each, cache the winnerparser = wishful.explore( "wishful.static.text.extract_emails", variants=5, test=lambda fn: fn("test@example.com") == ["test@example.com"])
# Future imports use the proven winnerfrom wishful.static.text import extract_emails # ← No re-explorationUses async internally (litellm.acompletion()) for responsive UI with a beautiful Rich progress display. See Explore for the full tour.
For knobs you can turn, see Configuration.