API
doppy-di: minimal dependency injection container.
AsyncConfiguration
Bases: Configuration
Async variant of :class:Configuration resolved via aget().
Rules are marked async so they resolve through the async path; a sync
get() on one raises the same error as any other async rule.
Source code in src/doppy_di/configuration.py
262 263 264 265 266 267 268 269 270 271 272 273 | |
AsyncDependencyInSyncContextError
Bases: Exception
Raised when an async dependency is resolved via sync get().
Examples:
>>> raise AsyncDependencyInSyncContextError("a")
Traceback (most recent call last):
...
AsyncDependencyInSyncContextError: Async dependency 'a' cannot be resolved in sync context
Source code in src/doppy_di/container.py
178 179 180 181 182 183 184 185 186 187 188 189 190 | |
AsyncScope
Bases: Scope
Async scope-local cache with async yield provider support.
Examples:
>>> builder = ContainerBuilder()
>>> builder.value("x", 1)
>>> c = builder.build()
>>> async def main():
... async with c.ascope("s") as s:
... return await s.get("x")
>>> import asyncio
>>> asyncio.run(main())
1
Source code in src/doppy_di/container.py
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 | |
get(key)
async
Resolve key from scope cache or underlying container.
Source code in src/doppy_di/container.py
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 | |
ChildrenFirstPolicy
dataclass
Resolve nested children before the parent.
Examples:
>>> policy = ChildrenFirstPolicy(nested={"service": ["repo"]})
>>> isinstance(policy, ChildrenFirstPolicy)
True
>>> policy.nested
{'service': ['repo']}
Source code in src/doppy_di/devkit/policy.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | |
CompilePolicy
Bases: Enum
Strategy for resolving rules after a plan is compiled.
ALLOW_OVERRIDE (default): the compiled plan delegates to the live container,
so later override() calls are honoured.
STRICT: once compile() is called, any override() call on the
container raises RuntimeError because the plan was snapshot immutable.
Source code in src/doppy_di/container.py
557 558 559 560 561 562 563 564 565 566 567 | |
CompositeRuleSet
Rule storage that layers local rules over a parent rule set.
Reads delegate to the parent when a key is not overridden locally, so rules added to the parent after the child was created stay visible. Writes go only to the local layer, leaving the parent untouched.
The merged view is cached and invalidated when the parent version changes, so repeated reads are cheap.
Examples:
>>> parent = RuleSet()
>>> parent.add("db", Rule("db", lambda: "base-db"))
>>> child = CompositeRuleSet(parent)
>>> child.add("extra", Rule("extra", lambda: 1))
>>> child.find("db").key
'db'
>>> child.find("extra").key
'extra'
Source code in src/doppy_di/container.py
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 | |
graph
property
Return the merged dependency graph (own edges win).
map
property
Return the merged rule map (own rules win).
version
property
Return (parent_version, own_version) for cache invalidation.
__init__(parent, defer_cycle_check=False)
Initialize a composite rule set over parent.
Source code in src/doppy_di/container.py
859 860 861 862 863 864 865 866 867 868 869 870 871 872 | |
add(key, rule)
Add a rule to the local layer and validate graph cycles.
Source code in src/doppy_di/container.py
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 | |
deps_of(key)
Return direct dependencies for a key.
Source code in src/doppy_di/container.py
929 930 931 | |
find(key)
Return a rule by key, falling back to the parent.
Source code in src/doppy_di/container.py
918 919 920 921 922 923 | |
has(key)
Check whether a key is registered locally or in the parent.
Source code in src/doppy_di/container.py
925 926 927 | |
keys()
Return all registered keys (parent and local).
Source code in src/doppy_di/container.py
933 934 935 | |
Configuration
Bases: Provider
Declarative provider reading a configuration tree.
Sources are given as keyword arguments and merged in priority order:
dictionary, ini_path, json_path, yaml_path, settings,
env_prefix, then env (last wins).
The live flag controls env interpolation. With live=False (default)
the config resolves once at assignment time and env values are cached, so
later env changes are not observed until :meth:reload. With
live=True environment variables are re-read on every get so dynamic
changes are visible, at the cost of per-resolution closure reads.
Examples:
>>> import os
>>> from doppy_di import Container
>>> from doppy_di.providers import Configuration
>>> services = Container()
>>> services.config = Configuration(
... dictionary={"db": {"host": "${DB_HOST}", "port": 5432}}
... )
>>> os.environ["DB_HOST"] = "localhost"
>>> services.get("config.db.host")
'localhost'
>>> services.get("config")
{'db': {'host': 'localhost', 'port': 5432}}
Source code in src/doppy_di/configuration.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | |
pre_validate_registration(ruleset, name)
Raise :class:DuplicateKeyError if a namespaced child key is taken.
Source code in src/doppy_di/configuration.py
226 227 228 229 230 231 232 233 234 | |
reload()
Re-read all sources and rebuild the resolved snapshot.
Source code in src/doppy_di/configuration.py
257 258 259 | |
to_rules(name)
Register a parent rule plus one rule per dotted config path.
Source code in src/doppy_di/configuration.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | |
ConfigurationError
Bases: Exception
Raised when a configuration source cannot be loaded.
Source code in src/doppy_di/configuration.py
33 34 | |
Container
Runtime container with singleton cache.
Thread-safe singleton resolution with double-checked locking.
Examples:
>>> builder = ContainerBuilder()
>>> builder.service("answer", lambda: 42, lifetime="singleton")
>>> container = builder.build()
>>> container.get("answer")
42
Source code in src/doppy_di/container.py
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 | |
aget(key, qualifier=None, _stacks=None, _scope_name=None, _path=None, policy=None)
async
Resolve a service by key asynchronously.
Resolves dependencies concurrently with asyncio.gather and awaits
async factories. Sync factories are called directly, so there is no
overhead for sync dependencies. Singleton results are cached.
Async yield providers are supported; their resources are finalized when resolution is cancelled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Key
|
Service key. |
required |
qualifier
|
Optional[str]
|
Optional named qualifier. When given, the rule
registered as |
None
|
policy
|
Optional['ResolutionPolicy']
|
Optional per-call resolution policy. Overrides the container-wide policy for this call only. |
None
|
Examples:
>>> builder = ContainerBuilder()
>>> builder.service("answer", lambda: 42)
>>> container = builder.build()
>>> async def main():
... return await container.aget("answer")
>>> import asyncio
>>> asyncio.run(main())
42
Source code in src/doppy_di/container.py
1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 | |
ascope(name)
Return a named or unique async scope.
Examples:
>>> builder = ContainerBuilder()
>>> builder.value("x", 1)
>>> c = builder.build()
>>> s = c.ascope("req")
>>> isinstance(s, AsyncScope)
True
Source code in src/doppy_di/container.py
2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 | |
child(name=None)
Return a new container layered over this one.
The child inherits the parent rules and can add or override rules without mutating the parent. Rules added to the parent after the child was created stay visible to the child.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
Optional[str]
|
Optional profile name stored in |
None
|
Examples:
>>> builder = ContainerBuilder()
>>> builder.value("db", "base-db")
>>> parent = builder.build()
>>> child = parent.child()
>>> child.value("db", "child-db")
>>> child.get("db")
'child-db'
>>> parent.get("db")
'base-db'
Source code in src/doppy_di/container.py
2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 | |
compile(copy_parent_rules=True, allow_post_compile_overrides=True, guardless=False)
Compile the dependency graph into an immutable execution plan.
Computes a topological ordering of the registered rules, validates the
full graph (missing dependencies raise :class:MissingDependencyError;
cycles raise :class:DependencyCycleError) and returns an
:class:ExecutionPlan. The plan delegates resolution to this container,
so lifetimes, caches and scopes keep identical semantics.
The plan is immutable and can be cached or serialized. If
compile() is never called there is zero overhead.
When copy_parent_rules is True (default) and this container is a
child with a :class:CompositeRuleSet, a merged snapshot of the
parent and local rules is taken so the plan is stable even if the
parent is mutated later.
When allow_post_compile_overrides is False, the plan freezes the
graph at compile time: singletons are pre-resolved and the plan uses
lockless resolvers. Any later override() on this container raises
RuntimeError. This is a breaking behavioral change for callers
relying on override visibility through a compiled plan.
Raises:
| Type | Description |
|---|---|
MissingDependencyError
|
If a rule depends on an unregistered key. |
DependencyCycleError
|
If the graph contains a cycle. |
ContainerBuildError
|
If multiple missing dependencies are found. |
Examples:
>>> builder = ContainerBuilder()
>>> builder.service("b", lambda a: a + 1, deps=["a"])
>>> builder.value("a", 1)
>>> container = builder.build()
>>> plan = container.compile()
>>> plan.get("b")
2
Source code in src/doppy_di/container.py
2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 | |
diff(other)
Return rule differences between this container and other.
Added keys exist only in other, removed keys exist only in this
container, changed keys exist in both with different rule metadata
(lifetime, deps, scope, resource flags).
Examples:
>>> builder = ContainerBuilder()
>>> builder.value("a", 1)
>>> base = builder.build()
>>> modded = base.child()
>>> modded.value("b", 2)
>>> report = base.diff(modded)
>>> "b" in report.added
True
Source code in src/doppy_di/container.py
2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 | |
export_config(format='json')
Export the effective configuration as a JSON string.
Includes the container profile and a rule table with lifetime, deps,
scope, and resource flags. Non-string keys are rendered via repr.
Examples:
>>> builder = ContainerBuilder()
>>> builder.value("a", 1)
>>> container = builder.build()
>>> out = container.export_config()
>>> '"a"' in out
True
Source code in src/doppy_di/container.py
2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 | |
get(key, qualifier=None, _scope_name=None, policy=None)
Resolve a service by key.
Returns the cached singleton if already resolved, otherwise resolves the rule from the config, applies the scope policy, and stores the result for singleton lifetimes.
Double-checked locking provides thread safety.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Key
|
Service key. |
required |
qualifier
|
Optional[str]
|
Optional named qualifier. When given, resolves the
rule registered as |
None
|
policy
|
Optional['ResolutionPolicy']
|
Optional per-call resolution policy. Overrides the container-wide policy for this call only. |
None
|
Examples:
>>> builder = ContainerBuilder()
>>> builder.service("answer", lambda: 42)
>>> container = builder.build()
>>> container.get("answer")
42
Source code in src/doppy_di/container.py
1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 | |
get_many(keys, parallel=False)
async
Resolve multiple services, optionally in parallel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
List[Key]
|
Service keys to resolve. |
required |
parallel
|
bool
|
When True, resolve independent dependencies concurrently. Falls back to sequential resolution for small graphs (fewer than 5 nodes) where parallelism overhead outweighs the benefit. |
False
|
Examples:
>>> builder = ContainerBuilder()
>>> builder.value("a", 1)
>>> builder.value("b", 2)
>>> container = builder.build()
>>> async def main():
... return await container.get_many(["a", "b"])
>>> import asyncio
>>> asyncio.run(main())
[1, 2]
Source code in src/doppy_di/container.py
1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 | |
get_or_none(key, qualifier=None)
Return resolved service or None if key not registered.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Key
|
Service key. |
required |
qualifier
|
Optional[str]
|
Optional named qualifier. |
None
|
Examples:
>>> builder = ContainerBuilder()
>>> c = builder.build()
>>> c.get_or_none("missing") is None
True
Source code in src/doppy_di/container.py
2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 | |
graph()
Return a DependencyGraph representation of this container.
Source code in src/doppy_di/container.py
2361 2362 2363 2364 2365 | |
has(key, qualifier=None)
Return True if a rule for key is registered.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Key
|
Service key. |
required |
qualifier
|
Optional[str]
|
Optional named qualifier. |
None
|
Examples:
>>> builder = ContainerBuilder()
>>> builder.service("x", lambda: 1)
>>> c = builder.build()
>>> c.has("x")
True
>>> c.has("missing")
False
Source code in src/doppy_di/container.py
2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 | |
override(key, value=None, **overrides)
Create a temporary override context.
Supports both a single key/value pair and a dictionary of
overrides: container.override({"a": 1, "b": 2}).
Nested overrides stack LIFO: the last override() wins. On
context exit the previous state is restored.
Callable override values are treated as factories and invoked on every resolution.
Examples:
>>> builder = ContainerBuilder()
>>> builder.value("x", 1)
>>> c = builder.build()
>>> ctx = c.override("x", 2)
>>> isinstance(ctx, OverrideContext)
True
>>> with c.override({"x": 3}):
... c.get("x")
3
>>> c.get("x")
1
Source code in src/doppy_di/container.py
2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 | |
scan(*packages, recursive=True)
Register all injectable classes found in the given packages.
Explicitly registered rules are never overridden.
Examples:
>>> builder = ContainerBuilder()
>>> container = builder.build()
>>> container.scan(__name__)
Source code in src/doppy_di/container.py
2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 | |
scope(name)
Return a named or unique scope according to the active policy.
Examples:
>>> builder = ContainerBuilder()
>>> builder.value("x", 1)
>>> c = builder.build()
>>> s = c.scope("req")
>>> isinstance(s, Scope)
True
Source code in src/doppy_di/container.py
2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 | |
service(key, make, lifetime='transient', deps=None, qualifier=None, scope=None)
Register a factory service on this container.
Examples:
>>> builder = ContainerBuilder()
>>> container = builder.build()
>>> container.service("a", lambda: 1)
>>> container.get("a")
1
Source code in src/doppy_di/container.py
2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 | |
set_tracer(tracer_fn)
Set a tracer callback or disable tracing with None.
The callback receives (key, duration, cache_hit, scope) after
every successful resolution. When no tracer is set there is no
timing and no dispatch, so overhead is zero.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tracer_fn
|
Optional[TracerFn]
|
Callback receiving trace events, or |
required |
Examples:
>>> events = []
>>> builder = ContainerBuilder()
>>> builder.value("a", 1)
>>> container = builder.build()
>>> container.set_tracer(lambda *args: events.append(args))
>>> container.get("a")
1
>>> len(events)
1
>>> container.set_tracer(None)
>>> container.get("a")
1
>>> len(events)
1
Source code in src/doppy_di/container.py
1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 | |
validate(strict=True)
Validate the whole dependency graph statically.
Checks every registered rule for missing dependencies, dependency cycles, and factory arity mismatches. Validation is explicit and never runs automatically, so there is zero overhead unless called.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strict
|
bool
|
When True, raise ValidationError on the first error. When False, collect all errors and return them as a list. |
True
|
Returns:
| Type | Description |
|---|---|
Optional[List[ValidationError]]
|
None when strict=True and the graph is valid. |
Optional[List[ValidationError]]
|
List of ValidationError when strict=False. |
Examples:
>>> builder = ContainerBuilder()
>>> builder.value("x", 1)
>>> c = builder.build()
>>> c.validate() is None
True
Source code in src/doppy_di/container.py
2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 | |
value(key, value)
Register a constant value on this container.
Child containers inherit this rule; overriding it on a child does not touch the parent.
Examples:
>>> builder = ContainerBuilder()
>>> container = builder.build()
>>> container.value("env", "base")
>>> container.get("env")
'base'
Source code in src/doppy_di/container.py
2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 | |
visualize(format='mermaid')
Return a textual representation of the dependency graph.
Supported formats
"mermaid"— mermaidgraph TDfor embedding in Markdown."graphviz"— Graphvizdigraphfor PNG/SVG generation."json"— structured dict for programmatic processing.
Rendering applies only to registered rules. Lifetime and optional
scope are encoded as node color and shape. Edges participating in a
cycle are marked [CYCLE]. The result is cached until the rule set
changes, so repeated calls are free.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
format
|
str
|
Output format among |
'mermaid'
|
Returns:
| Type | Description |
|---|---|
Any
|
str for |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> builder = ContainerBuilder()
>>> builder.value("db", object())
>>> builder.service("service", lambda db: db, deps=["db"])
>>> c = builder.build()
>>> out = c.visualize()
>>> "graph TD" in out
True
Source code in src/doppy_di/container.py
2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 | |
with_profile(name, overrides=None)
Return a derived child container with the given overrides applied.
Each override is registered as a singleton value on the child. Keys
must already exist in the effective rule set; unknown keys raise
:class:UnregisteredTypeError.
Examples:
>>> builder = ContainerBuilder()
>>> builder.value("env", "base")
>>> container = builder.build()
>>> prod = container.with_profile("prod", {"env": "prod"})
>>> container.get("env")
'base'
>>> prod.get("env")
'prod'
>>> prod.config.profile
'prod'
Source code in src/doppy_di/container.py
2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 | |
ContainerBuilder
Builder for a container.
Provides methods to register services, values, and aliases, then produce a ready-to-use Container.
Examples:
>>> builder = ContainerBuilder()
>>> builder.value("x", 1)
>>> builder.service("y", lambda x: x + 1, deps=["x"])
>>> c = builder.build()
>>> c.get("y")
2
Source code in src/doppy_di/container.py
2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 | |
__init__(duplicate_policy=DuplicateKeyPolicy.OVERWRITE, scope_policy=ScopePolicy.NAMED, compile_policy=CompilePolicy.ALLOW_OVERRIDE, track_sources=False, wrap_factory_errors=False, finalization_errors=False, check_cycles_on_register=True)
Initialize builder with optional policies.
Examples:
>>> b = ContainerBuilder(
... DuplicateKeyPolicy.FAIL, ScopePolicy.UNIQUE
... )
>>> b.duplicate_policy
<DuplicateKeyPolicy.FAIL: 'fail'>
Source code in src/doppy_di/container.py
2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 | |
alias(key, target)
Register an alias pointing to another key.
Examples:
>>> b = ContainerBuilder()
>>> b.value("x", 42)
>>> b.alias("answer", "x")
>>> c = b.build()
>>> c.get("answer")
42
Source code in src/doppy_di/container.py
2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 | |
build(validate=False, policy=None)
Build and return a Container.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
validate
|
bool
|
When True, raises ContainerBuildError if any dependency key is not registered. |
False
|
policy
|
Optional['ResolutionPolicy']
|
Optional resolution policy applied to the container. |
None
|
Examples:
>>> b = ContainerBuilder()
>>> b.value("x", 1)
>>> c = b.build()
>>> c.get("x")
1
Example with validation
b = ContainerBuilder() b.service("a", lambda b: b, deps=["b"]) b.build(validate=True) # doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... ContainerBuildError
Source code in src/doppy_di/container.py
2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 | |
service(key, make, lifetime='transient', deps=None, qualifier=None, scope=None)
Register a factory service.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Key
|
Registration key. |
required |
make
|
Callable[..., Any]
|
Factory callable. |
required |
lifetime
|
Lifetime
|
Service lifetime. |
'transient'
|
deps
|
Optional[List[Key]]
|
Dependency keys. |
None
|
qualifier
|
Optional[str]
|
Optional named qualifier. When given, the rule is
stored under the key |
None
|
Examples:
>>> b = ContainerBuilder()
>>> b.service("greet", lambda name: f"Hello {name}", deps=["name"])
>>> b.value("name", "World")
>>> c = b.build()
>>> c.get("greet")
'Hello World'
Source code in src/doppy_di/container.py
2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 | |
value(key, value)
Register a constant value as a singleton.
Examples:
>>> b = ContainerBuilder()
>>> b.value("pi", 3.14)
>>> c = b.build()
>>> c.get("pi")
3.14
Source code in src/doppy_di/container.py
2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 | |
ContainerConfig
dataclass
Immutable container configuration.
Examples:
>>> rules = RuleSet()
>>> config = ContainerConfig(rules)
>>> isinstance(config.ruleset, RuleSet)
True
>>> config.scope_policy
<ScopePolicy.NAMED: 'named'>
Source code in src/doppy_di/container.py
2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 | |
ContextValueMissingError
Bases: Exception
Raised when a from_context key is absent in the active scope.
Examples:
>>> from doppy_di import Container, Scope
>>> from doppy_di.providers import from_context
>>> services = Container()
>>> services.user = from_context("user")
>>> with services.scope("req") as s:
... try:
... s.get("user")
... except ContextValueMissingError as exc:
... str(exc)
"Context value 'user' missing in 'request' scope"
Source code in src/doppy_di/container.py
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 | |
CycleError
Bases: Exception
Raised when the rule graph contains a dependency cycle.
Examples:
>>> raise CycleError(["a", "b", "a"])
Traceback (most recent call last):
...
CycleError: Cycle detected: 'a' -> 'b' -> 'a'
Source code in src/doppy_di/container.py
148 149 150 151 152 153 154 155 156 157 158 159 160 | |
CyclicDependencyError
Bases: ValidationError
Raised when the rule graph contains a dependency cycle.
Examples:
>>> raise CyclicDependencyError(["a", "b", "a"])
Traceback (most recent call last):
...
CyclicDependencyError: Cycle detected: 'a' -> 'b' -> 'a'
Source code in src/doppy_di/container.py
276 277 278 279 280 281 282 283 284 285 286 287 288 | |
DefaultResolutionPolicy
dataclass
Default resolution order: resolve only the requested key.
Matches the historical container behaviour exactly. Dependencies are resolved recursively on demand; nothing is pre-resolved.
Examples:
>>> policy = DefaultResolutionPolicy()
>>> list(policy.order({}, "a"))
['a']
Source code in src/doppy_di/resolution.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | |
DependencyCycleError
Bases: CycleError
Raised when a dependency cycle is detected.
Subclasses CycleError for backward compatibility.
Examples:
>>> err = DependencyCycleError(["a", "b", "a"])
>>> "a" in err.cycle
True
Source code in src/doppy_di/container.py
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 | |
DependencyGraph
DependencyGraph for container introspection.
Source code in src/doppy_di/graph.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | |
dependencies_of(key)
Return registered direct dependencies of key.
Source code in src/doppy_di/graph.py
33 34 35 36 37 38 | |
dependents_of(key)
Return registered direct dependents of key.
Source code in src/doppy_di/graph.py
40 41 42 43 44 45 46 47 48 | |
edges()
Return all directed edges (key, dependency).
Only returns edges pointing to registered dependencies.
Source code in src/doppy_di/graph.py
21 22 23 24 25 26 27 28 29 30 31 | |
nodes()
Return all registered keys in the graph.
Source code in src/doppy_di/graph.py
17 18 19 | |
to_dot()
Render graph as graphviz dot string.
Source code in src/doppy_di/graph.py
54 55 56 | |
to_json()
Render graph as JSON dictionary.
Source code in src/doppy_di/graph.py
58 59 60 | |
to_mermaid()
Render graph as mermaid graph.
Source code in src/doppy_di/graph.py
50 51 52 | |
to_text()
Render graph as text tree.
Source code in src/doppy_di/graph.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | |
DiffReport
dataclass
Differences between two containers' effective rule sets.
Attributes:
| Name | Type | Description |
|---|---|---|
added |
Tuple[Key, ...]
|
Keys present in |
removed |
Tuple[Key, ...]
|
Keys present in |
changed |
Tuple[Key, ...]
|
Keys present in both with different rules. |
Examples:
>>> report = DiffReport(added=("c",), changed=("a",))
>>> "a" in report
True
>>> "c" in report
True
>>> "b" in report
False
Source code in src/doppy_di/container.py
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 | |
DuplicateKeyError
Bases: KeyError
Raised when a duplicate key is registered under the FAIL policy.
Examples:
>>> raise DuplicateKeyError("x")
Traceback (most recent call last):
...
DuplicateKeyError: Duplicate key: 'x'
Source code in src/doppy_di/container.py
307 308 309 310 311 312 313 314 315 316 317 318 319 | |
DuplicateKeyPolicy
Bases: Enum
Strategy for handling duplicate key registration.
OVERWRITE: replace existing rule (current default behavior). FAIL: raise DuplicateKeyError on duplicate. WARN: log a warning and overwrite.
Examples:
>>> DuplicateKeyPolicy.FAIL.name
'FAIL'
>>> DuplicateKeyPolicy.OVERWRITE.value
'overwrite'
Source code in src/doppy_di/container.py
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 | |
DuplicateRegistrationError
Bases: DuplicateKeyError
Raised when a duplicate key is registered with source information.
Subclasses DuplicateKeyError for backward compatibility.
Source code in src/doppy_di/container.py
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | |
EagerPolicy
dataclass
Resolve the entire graph eagerly.
All registered keys are resolved up front. When used with build(),
every sync rule is resolved at build time. Async rules cannot be resolved
in a sync context and raise :class:AsyncDependencyInSyncContextError.
Examples:
>>> builder = ContainerBuilder()
>>> builder.value("a", 1)
>>> container = builder.build(policy=EagerPolicy())
>>> container.get("a")
1
Source code in src/doppy_di/resolution.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | |
ExecutionPlan
dataclass
Immutable, pre-compiled execution plan for a container.
The plan holds a topological ordering of the registered rules plus the
resolved dependency edges. get walks the precomputed order and
resolves directly without re-entering Container.get, so lifetimes,
caches and scopes keep the same semantics as Container.get.
The plan is immutable: once built it cannot be changed. It may be
serialized for caching or cross-process reuse via :meth:serialize.
Source code in src/doppy_di/plan.py
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 | |
aget(key, qualifier=None)
Async resolution using the precomputed order.
Source code in src/doppy_di/plan.py
1087 1088 1089 1090 1091 1092 1093 | |
bind(key, qualifier=None)
Return a bound resolver for key without repeated root lookup.
The returned callable validates the key at bind time and preserves
the same semantics as :meth:get. It exposes the selected
execution mode through :attr:BoundResolver.kind.
Source code in src/doppy_di/plan.py
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 | |
deserialize(data)
classmethod
Rebuild an :class:ExecutionPlan from serialized data.
Source code in src/doppy_di/plan.py
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 | |
from_container(container, copy_parent_rules=True, allow_post_compile_overrides=True, guardless=False)
classmethod
Build an :class:ExecutionPlan from a container.
Source code in src/doppy_di/plan.py
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 | |
get(key, qualifier=None)
Resolve key using the precomputed order.
Source code in src/doppy_di/plan.py
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 | |
serialize(format='json')
Serialize the plan to a string for caching or cross-process use.
Source code in src/doppy_di/plan.py
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 | |
Factory
Bases: Protocol[P, T]
A factory callable with parameter specification.
Examples:
>>> def make(host: str) -> Database:
... return Database(host)
>>> isinstance(make, Factory)
True
Source code in src/doppy_di/container.py
89 90 91 92 93 94 95 96 97 98 99 100 | |
FactoryExecutionError
Bases: Exception
Wraps an exception raised by a factory body.
Only raised when wrap_factory_errors=True is set on the builder.
Source code in src/doppy_di/container.py
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 | |
InvalidFactoryError
Bases: ValidationError
Raised when a factory is incompatible with its declared deps.
Examples:
>>> raise InvalidFactoryError("a", "arity mismatch")
Traceback (most recent call last):
...
InvalidFactoryError: Invalid factory for 'a': arity mismatch
Source code in src/doppy_di/container.py
291 292 293 294 295 296 297 298 299 300 301 302 303 304 | |
InvalidLifetimeError
Bases: ValueError
Raised when an unknown lifetime is used.
Subclasses ValueError for backward compatibility.
Examples:
>>> raise InvalidLifetimeError("per_request")
Traceback (most recent call last):
...
InvalidLifetimeError: Unknown lifetime: 'per_request'
Source code in src/doppy_di/container.py
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 | |
KeyProtocol
Bases: Protocol
A protocol for custom hashable keys.
Examples:
>>> class MyKey:
... def __hash__(self): return 1
... def __eq__(self, other): return isinstance(other, MyKey)
>>> isinstance(MyKey(), KeyProtocol)
True
Source code in src/doppy_di/container.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 | |
LazyPolicy
dataclass
Resolve only the requested key on demand.
This is the same semantics as the default behaviour: nothing is resolved
until get() is called, and only the requested key plus its transitive
dependencies are resolved.
Examples:
>>> policy = LazyPolicy()
>>> list(policy.order({}, "a"))
['a']
Source code in src/doppy_di/resolution.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
LifetimePolicy
Validation policy for service lifetimes.
Centralizes the set of known lifetime identifiers and provides an extension point for registering custom lifetimes.
Examples:
>>> LifetimePolicy.validate("singleton")
>>> LifetimePolicy.validate("transient")
>>> LifetimePolicy.validate("per_request") # raises ValueError
Traceback (most recent call last):
...
ValueError: Unknown lifetime: 'per_request'
Source code in src/doppy_di/container.py
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 | |
LoggingContainer
dataclass
Log container operations while preserving the same API.
Wraps a Container and calls a log function on every operation.
Examples:
>>> events = []
>>> def log(msg):
... events.append(msg)
>>> from doppy_di.container import ContainerBuilder
>>> builder = ContainerBuilder()
>>> builder.value("x", 1)
>>> base = builder.build()
>>> wrapped = LoggingContainer(base, log)
>>> wrapped.get("x")
1
>>> events
["get('x')", "get('x') -> ok"]
Source code in src/doppy_di/devkit/logging.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | |
get(key)
Resolve key and log the operation.
Examples:
>>> wrapped.get("x")
1
Source code in src/doppy_di/devkit/logging.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | |
has(key)
Log presence check and delegate.
Examples:
>>> wrapped.has("x")
True
Source code in src/doppy_di/devkit/logging.py
61 62 63 64 65 66 67 68 69 | |
override(key, value)
Log override creation and delegate.
Examples:
>>> ctx = wrapped.override("x", 2)
>>> isinstance(ctx, OverrideContext)
True
Source code in src/doppy_di/devkit/logging.py
82 83 84 85 86 87 88 89 90 91 | |
scope(name)
Log scope creation and delegate.
Examples:
>>> s = wrapped.scope("req")
>>> isinstance(s, Scope)
True
Source code in src/doppy_di/devkit/logging.py
71 72 73 74 75 76 77 78 79 80 | |
MissingAnnotationError
Bases: TypeError
Raised when an injectable class has an unannotated dependency.
Source code in src/doppy_di/auto_wiring.py
28 29 30 31 32 33 34 | |
MissingDependencyError
Bases: ServiceNotFoundError
Raised when a deep dependency chain fails to resolve.
Subclasses ServiceNotFoundError so existing KeyError handling
keeps working.
Examples:
>>> err = MissingDependencyError("c", ["a", "b", "c"])
>>> err.key
'c'
>>> err.resolution_path
['a', 'b', 'c']
Source code in src/doppy_di/container.py
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 | |
MissingExternalArgumentError
Bases: TypeError
Raised when a declared External() argument is not supplied.
Source code in src/doppy_di/inject.py
120 121 122 123 124 125 126 | |
NestedPolicy
Bases: Protocol
Policy used to compare nested objects.
Implementations compare the resolved nested attribute against the object stored under the nested rule key.
Examples:
>>> isinstance(SameObjectPolicy(), NestedPolicy)
True
Source code in src/doppy_di/devkit/nested.py
21 22 23 24 25 26 27 28 29 30 31 32 33 | |
check(nested, resolved)
Return True if nested object is valid.
Source code in src/doppy_di/devkit/nested.py
32 33 | |
NestedRules
Track nested relations and validate resolved objects.
Source code in src/doppy_di/devkit/nested.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
add_nested(parent, child, rule, ruleset)
Register a nested dependency for a parent key.
The nested key (parent, child) is added to the shared ruleset and
tracked for validation.
Examples:
>>> nested = NestedRules()
>>> rule = Rule(("db", "conn"), lambda: object())
>>> rs = RuleSet()
>>> nested.add_nested("db", "conn", rule, rs)
>>> rs.has(("db", "conn"))
True
Source code in src/doppy_di/devkit/nested.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | |
children_of(parent)
Return the registered nested child names for a parent.
Source code in src/doppy_di/devkit/nested.py
135 136 137 | |
validate_nested(parent, container, parent_obj=None)
Validate nested rules for a resolved parent object.
Source code in src/doppy_di/devkit/nested.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
OrderPolicy
Bases: Protocol
A strategy for controlling resolution order.
Examples:
>>> isinstance(UnorderedPolicy(), OrderPolicy)
True
Source code in src/doppy_di/devkit/policy.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |
after_resolve(key, obj, ruleset, ctx)
Run after object resolution.
Source code in src/doppy_di/devkit/policy.py
30 31 32 33 | |
before_resolve(key, ruleset, ctx)
Run before object resolution.
Source code in src/doppy_di/devkit/policy.py
27 28 | |
OverrideContext
Context manager for temporary stack-based overrides.
Entering pushes a validated :class:OverrideLayer onto the container's
layer stack. Lookups walk the stack LIFO, so the last override()
wins. Exiting pops the layer and restores the original rules — even when
the with block raises.
Callable override values are treated as factories and invoked on every resolution, mirroring a transient service.
Examples:
>>> builder = ContainerBuilder()
>>> builder.value("x", 1)
>>> c = builder.build()
>>> with c.override("x", 2):
... c.get("x")
2
>>> c.get("x")
1
>>> with c.override({"x": 3}):
... c.get("x")
3
>>> c.get("x")
1
Source code in src/doppy_di/container.py
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 | |
ParallelPolicy
dataclass
Resolve independent branches concurrently (async only).
The policy orders keys by dependency level. In aget() each level is
resolved concurrently with asyncio.gather. In sync get() the
order is honoured sequentially, because there is no event loop.
Examples:
>>> policy = ParallelPolicy()
>>> list(policy.order({}, "a"))
['a']
Source code in src/doppy_di/resolution.py
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | |
ParentFirstPolicy
dataclass
Resolve parent first, then optionally inspect children.
Examples:
>>> policy = ParentFirstPolicy(nested={"service": ["repo"]})
>>> isinstance(policy, ParentFirstPolicy)
True
Source code in src/doppy_di/devkit/policy.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | |
Qualifier
dataclass
Marker for named dependencies via typing.Annotated.
Examples:
>>> Qualifier("read")
Qualifier(name='read')
Source code in src/doppy_di/container.py
106 107 108 109 110 111 112 113 114 115 | |
RegistrationSource
dataclass
Source location where a rule was registered.
Examples:
>>> src = RegistrationSource("app/container.py", 42, "setup")
>>> str(src)
'app/container.py:42'
Source code in src/doppy_di/container.py
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | |
ResolutionCancelledError
Bases: CancelledError
Raised when aget() is cancelled after partially creating resources.
Source code in src/doppy_di/container.py
208 209 210 211 212 213 | |
ResolutionChildrenFirstPolicy
dataclass
Resolve children before parents.
Transitive dependencies (children) are resolved before the requested root (parent).
Examples:
>>> builder = ContainerBuilder()
>>> builder.value("b", 1)
>>> builder.service("a", lambda b: b, deps=["b"])
>>> container = builder.build(policy=ChildrenFirstPolicy())
>>> container.get("a")
1
Source code in src/doppy_di/resolution.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |
ResolutionParentFirstPolicy
dataclass
Resolve parents before children.
The requested root (parent) is attempted first; its transitive dependencies (children) follow.
Examples:
>>> builder = ContainerBuilder()
>>> builder.value("b", 1)
>>> builder.service("a", lambda b: b, deps=["b"])
>>> container = builder.build(policy=ParentFirstPolicy())
>>> container.get("a")
1
Source code in src/doppy_di/resolution.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | |
ResolutionPolicy
Bases: Protocol
A strategy for ordering dependency resolution.
Implementations receive the rule graph and the requested root key and return an iteration order of keys. The container resolves keys in that order.
Examples:
>>> class ReversePolicy:
... def order(self, graph, root):
... return reversed(list(graph.keys()))
>>> isinstance(ReversePolicy(), ResolutionPolicy)
True
Source code in src/doppy_di/resolution.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
order(graph, root)
Return the resolution order of keys for root.
Source code in src/doppy_di/resolution.py
45 46 47 48 49 50 51 | |
ResolveContext
Resolution context used during object creation.
Provides access to the container and scope for dependency resolution.
Examples:
>>> builder = ContainerBuilder()
>>> builder.service("x", lambda: 1)
>>> c = builder.build()
>>> ctx = ResolveContext(c)
>>> ctx.get("x")
1
Source code in src/doppy_di/container.py
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 | |
ResourceFinalizationError
Bases: Exception
Raised when one or more yield providers fail to finalize.
Only raised when finalization_errors=True is set on the builder.
Source code in src/doppy_di/container.py
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 | |
Rule
dataclass
Immutable service rule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Key
|
Registration key. |
required |
make
|
Callable[..., Any]
|
Factory callable. |
required |
lifetime
|
Lifetime
|
Service lifetime. |
'transient'
|
deps
|
Tuple[Key, ...]
|
Dependency keys. |
()
|
Examples:
>>> rule = Rule("answer", lambda: 42, "singleton", ())
>>> rule.key
'answer'
>>> rule.lifetime
'singleton'
Source code in src/doppy_di/container.py
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 | |
RuleSet
Immutable-by-convention rule storage and dependency graph.
Examples:
>>> rules = RuleSet()
>>> rules.add("x", Rule("x", lambda: 1))
>>> rules.find("x").key
'x'
>>> rules.has("x")
True
Source code in src/doppy_di/container.py
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 | |
__init__(rules_map=None, graph=None, defer_cycle_check=False)
Initialize storage from optional existing map and graph.
Source code in src/doppy_di/container.py
669 670 671 672 673 674 675 676 677 678 679 | |
add(key, rule)
Add a rule and validate graph cycles.
Raises:
| Type | Description |
|---|---|
CycleError
|
If adding the rule creates a dependency cycle. |
Examples:
>>> rules = RuleSet()
>>> rules.add("a", Rule("a", lambda: 1, deps=("b",)))
>>> rules.has("a")
True
Source code in src/doppy_di/container.py
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 | |
copy()
Return a deep copy of this RuleSet.
Source code in src/doppy_di/container.py
681 682 683 684 685 686 687 | |
deps_of(key)
Return direct dependencies for a key.
Examples:
>>> rules = RuleSet()
>>> rules.add("a", Rule("a", lambda: 1, deps=("b", "c")))
>>> rules.deps_of("a")
('b', 'c')
Source code in src/doppy_di/container.py
748 749 750 751 752 753 754 755 756 757 | |
find(key)
Return a rule by key.
Raises:
| Type | Description |
|---|---|
ServiceNotFoundError
|
If key is not registered. |
Examples:
>>> rules = RuleSet()
>>> rules.add("x", Rule("x", lambda: 1))
>>> rules.find("x")
Rule(key='x', make=..., lifetime='transient', deps=())
>>> rules.find("missing")
Traceback (most recent call last):
...
ServiceNotFoundError
Source code in src/doppy_di/container.py
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 | |
has(key)
Check whether a key is registered.
Examples:
>>> rules = RuleSet()
>>> rules.add("x", Rule("x", lambda: 1))
>>> rules.has("x")
True
>>> rules.has("y")
False
Source code in src/doppy_di/container.py
735 736 737 738 739 740 741 742 743 744 745 746 | |
keys()
Return registered keys.
Examples:
>>> rules = RuleSet()
>>> rules.add("x", Rule("x", lambda: 1))
>>> rules.add("y", Rule("y", lambda: 2))
>>> sorted(rules.keys())
['x', 'y']
Source code in src/doppy_di/container.py
759 760 761 762 763 764 765 766 767 768 769 | |
SameObjectPolicy
dataclass
Check object identity for nested values.
Examples:
>>> policy = SameObjectPolicy()
>>> obj = object()
>>> policy.check(obj, obj)
True
Source code in src/doppy_di/devkit/nested.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 | |
check(nested, resolved)
Return True only when both references are the same object.
Source code in src/doppy_di/devkit/nested.py
47 48 49 | |
SameValuePolicy
dataclass
Check value equality for nested values.
Attributes:
| Name | Type | Description |
|---|---|---|
strict |
bool
|
When True, propagate comparison exceptions instead of returning False. |
Examples:
>>> policy = SameValuePolicy()
>>> policy.check(1, 1)
True
>>> policy.check(1, 2)
False
Source code in src/doppy_di/devkit/nested.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
check(nested, resolved)
Return True when both values compare equal.
When strict is False, exceptions raised during comparison are
logged and treated as inequality.
Source code in src/doppy_di/devkit/nested.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
Scope
Scope-local cache over a container.
Scopes allow caching within a with block. All resolved values are
cached until the scope exits.
Attributes:
| Name | Type | Description |
|---|---|---|
APP |
Application-wide scope name. |
|
REQUEST |
Per-request scope name. |
|
SESSION |
Per-session scope name. |
Examples:
>>> builder = ContainerBuilder()
>>> builder.service("x", lambda: object(), lifetime="transient")
>>> c = builder.build()
>>> with c.scope("req") as s:
... a = s.get("x")
... b = s.get("x")
... a is b
True
Source code in src/doppy_di/container.py
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 | |
__init__(container, name)
Create a named scope with an empty local cache.
Source code in src/doppy_di/container.py
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 | |
get(key)
Resolve key from scope cache or underlying container.
Examples:
>>> builder = ContainerBuilder()
>>> builder.value("x", 1)
>>> c = builder.build()
>>> with c.scope("s") as s:
... s.get("x")
1
Source code in src/doppy_di/container.py
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 | |
get_context(key, scope=None)
Read a context value stored via :meth:set_context.
Raises:
| Type | Description |
|---|---|
ContextValueMissingError
|
When the key is absent in the scope. |
Source code in src/doppy_di/container.py
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 | |
set_context(key, value, scope=None)
Store a context value available to from_context providers.
Request-scope values are cleared when the scope exits; session-scope values persist for the scope lifetime.
Examples:
>>> from doppy_di import Container
>>> from doppy_di.providers import from_context
>>> services = Container()
>>> services.user = from_context("user")
>>> with services.scope("req") as s:
... s.set_context("user", "alice")
... s.get("user")
'alice'
Source code in src/doppy_di/container.py
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 | |
ScopePolicy
Bases: Enum
Strategy for resolving scopes by name.
NAMED: reuse the same Scope object for the same name (current default).
return a fresh Scope per call, stored under a unique internal
key so its cache never leaks across calls even if exit is forgotten.
Examples:
>>> ScopePolicy.NAMED.value
'named'
>>> ScopePolicy.UNIQUE.name
'UNIQUE'
Source code in src/doppy_di/container.py
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 | |
ScopeViolationError
Bases: Exception
Raised when a scope/lifetime rule is violated.
Defined for API completeness; not raised by the current container.
Source code in src/doppy_di/container.py
460 461 462 463 464 465 466 467 468 469 470 | |
ServiceNotFoundError
Bases: KeyError
Raised when a service key is not registered.
Examples:
>>> raise ServiceNotFoundError("missing")
Traceback (most recent call last):
...
ServiceNotFoundError: Service not found: 'missing'
Source code in src/doppy_di/container.py
118 119 120 121 122 123 124 125 126 127 128 129 130 | |
SyncFactoryReturningAwaitableError
Bases: Exception
Raised when a sync factory returns an awaitable.
Examples:
>>> raise SyncFactoryReturningAwaitableError("a")
Traceback (most recent call last):
...
SyncFactoryReturningAwaitableError: Sync factory 'a' returned an awaitable
Source code in src/doppy_di/container.py
193 194 195 196 197 198 199 200 201 202 203 204 205 | |
UnorderedPolicy
dataclass
Policy with no extra resolution ordering.
Examples:
>>> policy = UnorderedPolicy()
>>> isinstance(policy, UnorderedPolicy)
True
>>> policy.before_resolve("x", None, ResolveContext(container))
Source code in src/doppy_di/devkit/policy.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | |
UnregisteredDependencyError
Bases: ValidationError
Raised when a rule depends on an unregistered key.
Examples:
>>> raise UnregisteredDependencyError("a", "b")
Traceback (most recent call last):
...
UnregisteredDependencyError: Unregistered dependency: 'a' -> 'b'
Source code in src/doppy_di/container.py
260 261 262 263 264 265 266 267 268 269 270 271 272 273 | |
UnregisteredTypeError
Bases: KeyError
Raised when an override targets an unregistered key.
Examples:
>>> raise UnregisteredTypeError("missing")
Traceback (most recent call last):
...
UnregisteredTypeError: Unregistered type: 'missing'
Source code in src/doppy_di/container.py
133 134 135 136 137 138 139 140 141 142 143 144 145 | |
UnresolvableDependencyError
Bases: Exception
Raised when an auto-wired dependency cannot be resolved.
Source code in src/doppy_di/auto_wiring.py
37 38 39 40 41 42 43 | |
ValidatingContainer
Container view that applies order policy and validation.
Wraps a base Container with resolution ordering and optional validation rules.
Examples:
>>> from doppy_di.container import ContainerBuilder
>>> builder = ContainerBuilder()
>>> builder.value("x", 1)
>>> base = builder.build()
>>> wrapped = ValidatingContainer(
... base, UnorderedPolicy(), ValidationRunner()
... )
>>> wrapped.get("x")
1
Source code in src/doppy_di/devkit/validation.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
__init__(wrapped, policy, validator=None, nested=None)
Wrap container with validation and resolution guard.
Source code in src/doppy_di/devkit/validation.py
102 103 104 105 106 107 108 109 110 111 112 113 114 | |
get(key)
Resolve key with ordering and validation.
Examples:
>>> wrapped = ValidatingContainer(base, UnorderedPolicy())
>>> wrapped.get("x")
1
Source code in src/doppy_di/devkit/validation.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | |
has(key)
Check if key is registered.
Examples:
>>> wrapped.has("x")
True
Source code in src/doppy_di/devkit/validation.py
144 145 146 147 148 149 150 151 | |
override(key, value)
Override a value in the wrapped container.
Examples:
>>> with wrapped.override("x", 2):
... wrapped.get("x")
2
Source code in src/doppy_di/devkit/validation.py
163 164 165 166 167 168 169 170 171 | |
scope(name)
Return a scope from the wrapped container.
Examples:
>>> s = wrapped.scope("req")
>>> isinstance(s, Scope)
True
Source code in src/doppy_di/devkit/validation.py
153 154 155 156 157 158 159 160 161 | |
ValidationError
Bases: Exception
Base class for static graph validation errors.
Examples:
>>> raise ValidationError("bad graph")
Traceback (most recent call last):
...
ValidationError: bad graph
Source code in src/doppy_di/container.py
249 250 251 252 253 254 255 256 257 | |
ValidationRule
Bases: Protocol
A validation rule executed after resolution.
Examples:
>>> class MyRule:
... def check(self, container, key, obj):
... assert obj is not None
>>> isinstance(MyRule(), ValidationRule)
True
Source code in src/doppy_di/devkit/validation.py
27 28 29 30 31 32 33 34 35 36 37 38 39 | |
check(container, key, obj)
Validate resolved object.
Source code in src/doppy_di/devkit/validation.py
38 39 | |
ValidationRunner
dataclass
Run a list of validation rules.
Examples:
>>> runner = ValidationRunner()
>>> len(runner.rules)
0
>>> runner.add(MyRule())
>>> len(runner.rules)
1
Source code in src/doppy_di/devkit/validation.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
add(rule)
Append a validation rule.
Examples:
>>> runner = ValidationRunner()
>>> runner.add(MyRule())
>>> len(runner.rules)
1
Source code in src/doppy_di/devkit/validation.py
60 61 62 63 64 65 66 67 68 69 | |
run(container, key, obj)
Execute all registered rules.
Examples:
>>> runner = ValidationRunner()
>>> runner.run(container, "x", 42)
Source code in src/doppy_di/devkit/validation.py
71 72 73 74 75 76 77 78 79 | |
YieldNotCalledError
Bases: Exception
Raised when a yield provider generator does not yield.
Examples:
>>> raise YieldNotCalledError("session")
Traceback (most recent call last):
...
YieldNotCalledError: Yield provider 'session' did not yield a value
Source code in src/doppy_di/container.py
163 164 165 166 167 168 169 170 171 172 173 174 175 | |
Depends(dependency=None)
Declare a dependency for injection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dependency
|
Optional[Union[Type[Any], Callable[..., Any]]]
|
Type to resolve from container, callable to invoke, or
|
None
|
Examples:
>>> Depends()
<doppy_di.inject._DependsMarker object at ...>
Source code in src/doppy_di/inject.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
External()
Declare a runtime parameter supplied at build time (assisted injection).
Parameters whose default is an External() marker are not resolved
from the container; the caller passes them as keyword arguments when
calling build(**kwargs) (or through @inject's reflected call).
Examples:
>>> External()
<doppy_di.inject._ExternalMarker object at ...>
Source code in src/doppy_di/inject.py
79 80 81 82 83 84 85 86 87 88 89 90 | |
assisted(func, container=None)
Build an assisted-injection factory.
Injected (annotated) parameters resolve from container (or the active
scope); parameters declared with External() are supplied at
build(**kwargs) / abuild(**kwargs) time.
Examples:
>>> from doppy_di.container import ContainerBuilder
>>> builder = ContainerBuilder()
>>> builder.value("repo", object())
>>> container = builder.build()
>>> def make(repo, user_id: int = External()):
... return user_id
>>> assisted(make, container=container).build(user_id=7)
7
Source code in src/doppy_di/inject.py
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 | |
injectable(cls=None, *, scope=None, qualifier=None)
Mark a class as a candidate for auto-registration.
Usable bare (@injectable) or with options
(@injectable(scope=..., qualifier=...)).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
Optional[Type[Any]]
|
Class being decorated (bare usage). |
None
|
scope
|
Optional[str]
|
Default lifetime for the class. |
None
|
qualifier
|
Optional[str]
|
Named qualifier. |
None
|
Examples:
>>> @injectable(scope="singleton")
... class Service:
... pass
>>> Service.__doppy_injectable__
True
Source code in src/doppy_di/auto_wiring.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
is_injectable(cls)
Return True when cls is marked with @injectable.
Examples:
>>> @injectable
... class Service:
... pass
>>> is_injectable(Service)
True
>>> is_injectable(int)
False
Source code in src/doppy_di/auto_wiring.py
50 51 52 53 54 55 56 57 58 59 60 61 62 | |
Rich diagnostic errors
Errors carry resolution paths, scope names, and optional registration sources.
Error classes
MissingDependencyError— deep dependency chain failure. Carrieskey,resolution_path,scope,registration_source. SubclassesServiceNotFoundError.DependencyCycleError— dependency cycle. Carriescycle. SubclassesCycleError.InvalidLifetimeError— unknown lifetime. Carrieslifetime. SubclassesValueError.ScopeViolationError— scope/lifetime violation. Carrieskey,scope,violation_type.FactoryExecutionError— wraps a factory-body exception. Carrieskey,original_exception,resolution_path. Raised only whenwrap_factory_errors=True.DuplicateRegistrationError— duplicate key under the FAIL policy. Carrieskey,existing_source,new_source. SubclassesKeyError.ResourceFinalizationError— yield-provider cleanup failure. Carrieserrors: list[(key, exception)]. Raised only whenfinalization_errors=True.
Builder flags
builder = ContainerBuilder(
track_sources=True, # capture filename/lineno on registration
wrap_factory_errors=True, # wrap factory exceptions
finalization_errors=True, # raise on yield-provider cleanup failure
check_cycles_on_register=False, # defer cycle detection to resolve
)
All flags default to False (or True for cycle checking), preserving
existing behavior.
Modern typing support
The public API supports modern typing features for improved static checking (mypy, pyright). All features are annotation-only — zero runtime overhead.
TypeAlias
TypeAlias can be used as a service key:
from typing import TypeAlias
DatabaseService: TypeAlias = Database
builder = ContainerBuilder()
builder.service(DatabaseService, make=lambda: Database())
container = builder.build()
db = container.get(DatabaseService)
TypedDict
TypedDict classes are resolvable as dependencies:
from typing import TypedDict
class DBConfig(TypedDict):
host: str
port: int
builder = ContainerBuilder()
builder.service(DBConfig, make=lambda: {"host": "localhost", "port": 5432})
container = builder.build()
config = container.get(DBConfig)
ParamSpec factories
Factory protocol and Provider alias accept ParamSpec-typed callables:
from typing import Callable, ParamSpec, TypeVar
from doppy_di import Factory, Provider
P = ParamSpec("P")
T = TypeVar("T")
def provider(factory: Callable[P, T]) -> Callable[P, T]:
return factory
builder = ContainerBuilder()
builder.service(Database, make=provider(lambda: Database()))
TypeGuard detection
is_injectable() narrows types at runtime:
from doppy_di import injectable, is_injectable
@injectable
class Service:
pass
if is_injectable(Service):
# Service is narrowed to type here
...
Self fluent builder
ContainerBuilder.service(), value(), and alias() return Self for
chaining:
builder = ContainerBuilder()
builder.value("x", 1).service("y", lambda: 2).alias("z", "x")
container = builder.build()
Compile / plan mode
Container.compile() returns an immutable ExecutionPlan that captures a
topological ordering of the registered rules. The plan validates the graph up
front (raising MissingDependencyError for unregistered dependencies and
DependencyCycleError for cycles), then resolves through the live container so
lifetimes, singleton caches and scopes keep identical semantics. The feature is
fully opt-in: if compile() is never called there is zero overhead.
from doppy_di import CompilePolicy, ContainerBuilder
builder = ContainerBuilder(compile_policy=CompilePolicy.ALLOW_OVERRIDE)
builder.value("a", 1)
builder.service("b", lambda a: a + 1, deps=["a"])
container = builder.build()
plan = container.compile()
plan.get("b") # 2
# ALLOW_OVERRIDE: overrides still apply through the live container
with container.override("a", 10):
plan.get("b") # 11
# STRICT: after compile() the container rejects further overrides
strict = ContainerBuilder(compile_policy=CompilePolicy.STRICT).build()
strict.compile()
# strict.override("a", 1) # raises RuntimeError
ExecutionPlan.serialize() / ExecutionPlan.deserialize() persist the graph
topology, rule metadata and resolved singletons to JSON for caching or
cross-process reuse.
When a dependency subtree is fully sync (no yield/async/nested rules) and its
transients only depend on singletons or values, the plan builds a flattened
resolver at compile time (issue #40): the root closure evaluates each shared
singleton once into local bindings, then calls the transient factories directly
with those bindings. This removes the per-node loop and the intermediate
transient closure frames from the hot path, while preserving singleton
identity, transient freshness, override visibility and thread safety.
Fallback composed resolvers (kind == "composed") and the generic
_resolve_fast walk are used for everything else.
Provider facade
Declarative providers convert to container rules on attribute assignment.
They are inert data objects: no resolution logic, zero overhead until
assigned. Import from doppy_di.providers; the package-level Factory
protocol is untouched.
from doppy_di import Container, Scope
from doppy_di.providers import Factory, Singleton, Value, Resource
services = Container()
services.config = Value({"debug": True})
services.db = Resource(create_db, Scope.APP)
services.repo = Factory(UserRepository, db=services.db)
services.service = Singleton(UserService, repo=services.repo)
Assigning a class factory registers both the named key and a type key. Dependencies may reference providers before assignment; unbound placeholders resolve by name at rule registration.
Provider classes
Factory— transient factory.Singleton— singleton factory.Scoped— factory cached per scope.Value— constant value.Resource— yield-based resource finalized on scope exit.Coroutine— async factory.Alias— points at another key.Dependency— mandatory key; raises on a missing target. Strict alias.Selector— picks one provider at resolution time.ListOf— aggregates providers into a list.DictOf— aggregates named providers into a dict.
Async resolution
Container.aget() resolves sync and async factories, sync and async
resources, and resolves independent dependency branches concurrently. Sync
factories are called directly with no await overhead. Container.ascope()
provides an async scope; async yield providers are finalized on scope exit
and on cancellation.
async def make_db():
return Database("async")
builder.service("db", make_db)
container = builder.build()
db = await container.aget("db")
async with container.ascope("req") as scope:
session = await scope.aget("session")
# async resources finalized on scope exit
Container.get_many(keys, parallel=True) resolves independent keys
concurrently.
Async errors
AsyncDependencyInSyncContextError— async dependency resolved via syncget().SyncFactoryReturningAwaitableError— sync factory returned an awaitable.ResolutionCancelledError—aget()cancelled after partially creating resources; partially-created resources are finalized automatically.
Resolution policies
Pluggable policies control the order of dependency resolution. Policies are opt-in; default behaviour is unchanged when none is specified.
from doppy_di import (
ResolutionChildrenFirstPolicy,
DefaultResolutionPolicy,
EagerPolicy,
ParallelPolicy,
)
# container-wide policy
container = builder.build(policy=ResolutionChildrenFirstPolicy())
# per-call policy
container.get("a", policy=EagerPolicy())
Built-in policies
DefaultResolutionPolicy— resolve only the requested key (historical behaviour).LazyPolicy— same as default; nothing resolved untilget().ParentFirstPolicy— parents before children.ChildrenFirstPolicy— children before parents.EagerPolicy— resolve the entire graph up front.ParallelPolicy— resolve dependency levels concurrently inaget()(sequential in syncget()).
Implement the ResolutionPolicy protocol (order(graph, root)) for custom
strategies.
Name disambiguation
The top-level ChildrenFirstPolicy/ParentFirstPolicy names belong to the
devkit nested-field ordering. Resolution policies are exported under the
aliases ResolutionChildrenFirstPolicy/ResolutionParentFirstPolicy.
Import resolution policies from doppy_di using the aliases, or directly
from doppy_di.resolution.
Graph introspection
Container.graph() returns a DependencyGraph for programmatic querying.
g = container.graph()
g.nodes() # all registered keys
g.edges() # (key, dependency) pairs
g.dependencies_of("a") # direct deps
g.dependents_of("a") # direct dependents
g.to_mermaid() # mermaid
g.to_dot() # graphviz
g.to_json() # dict
g.to_text() # text tree
DependencyGraph is exported from the package root and
doppy_di.graph.
Profiles and child containers
Derive environment-specific containers without mutating the base.
builder.value("env", "base")
container = builder.build()
prod = container.with_profile("prod", {"env": "prod"})
with_profile(name, overrides)— layered container with profile overrides.child(name=None)— container layered over the parent; parent rules added later stay visible.diff(other)— returnsDiffReportofadded/removed/changedkeys.export_config(format="json")— serialize the effective configuration to JSON.
Tracing
Container.set_tracer() registers a callback invoked after each successful
resolution.
def tracer(key, duration, cache_hit, scope):
...
container.set_tracer(tracer)
container.set_tracer(None) # disable
The callback receives (key, duration, cache_hit, scope). With no tracer
set there is no timing and no dispatch — zero overhead. Child containers
inherit the parent tracer.
OpenTelemetry
pip install "doppy-di[otel]"
from doppy_di.ext.otel import otel_adapter
container.set_tracer(otel_adapter())
container.get("a") # emits doppy.resolve:'a' span
TracerFn is the callback protocol:
Callable[[Key, float, bool, Optional[str]], None].