injectq 0.2.0__tar.gz → 0.2.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (180) hide show
  1. {injectq-0.2.0 → injectq-0.2.2}/PKG-INFO +66 -1
  2. {injectq-0.2.0 → injectq-0.2.2}/README.md +65 -0
  3. injectq-0.2.2/docs/advanced-features/enhanced-features.md +232 -0
  4. {injectq-0.2.0/docs/advanced → injectq-0.2.2/docs/advanced-features}/overview.md +12 -0
  5. {injectq-0.2.0 → injectq-0.2.2}/docs/api-reference/decorators.md +2 -2
  6. {injectq-0.2.0 → injectq-0.2.2}/docs/api-reference/providers.md +4 -4
  7. {injectq-0.2.0 → injectq-0.2.2}/docs/api-reference/scopes.md +3 -3
  8. {injectq-0.2.0 → injectq-0.2.2}/docs/changelog.md +13 -0
  9. injectq-0.2.2/docs/core-concepts/auto-registration-override-control.md +277 -0
  10. {injectq-0.2.0 → injectq-0.2.2}/docs/migration/other-frameworks.md +1 -1
  11. {injectq-0.2.0 → injectq-0.2.2}/docs/migration/python-injector.md +1 -1
  12. injectq-0.2.2/examples/compile.py +24 -0
  13. injectq-0.2.2/examples/enhanced_features_complete_demo.py +207 -0
  14. injectq-0.2.2/examples/enhanced_features_demo.py +138 -0
  15. injectq-0.2.2/examples/hiniting.py +15 -0
  16. injectq-0.2.2/examples/instance_checking.py +21 -0
  17. injectq-0.2.2/examples/null_checking.py +17 -0
  18. injectq-0.2.2/examples/subclass_checking.py +56 -0
  19. {injectq-0.2.0 → injectq-0.2.2}/injectq/__init__.py +1 -1
  20. {injectq-0.2.0 → injectq-0.2.2}/injectq/core/container.py +102 -19
  21. injectq-0.2.2/injectq/core/registry.py +302 -0
  22. {injectq-0.2.0 → injectq-0.2.2}/injectq/core/resolver.py +19 -0
  23. {injectq-0.2.0 → injectq-0.2.2}/injectq/decorators/inject.py +3 -5
  24. {injectq-0.2.0 → injectq-0.2.2}/injectq/utils/__init__.py +2 -0
  25. {injectq-0.2.0 → injectq-0.2.2}/injectq/utils/exceptions.py +8 -0
  26. injectq-0.2.2/tests/test_allow_concrete_override.py +361 -0
  27. {injectq-0.2.0 → injectq-0.2.2}/tests/test_comprehensive.py +3 -0
  28. {injectq-0.2.0 → injectq-0.2.2}/tests/test_comprehensive_threading.py +5 -5
  29. injectq-0.2.2/tests/test_enhanced_features.py +611 -0
  30. {injectq-0.2.0 → injectq-0.2.2}/tests/test_inject_container.py +13 -14
  31. injectq-0.2.0/injectq/core/registry.py +0 -170
  32. {injectq-0.2.0 → injectq-0.2.2}/.github/instructions/config.instructions.md +0 -0
  33. {injectq-0.2.0 → injectq-0.2.2}/.gitignore +0 -0
  34. {injectq-0.2.0 → injectq-0.2.2}/INJECTQ_SPECIFICATION.md +0 -0
  35. {injectq-0.2.0 → injectq-0.2.2}/LICENSE +0 -0
  36. {injectq-0.2.0 → injectq-0.2.2}/Makefile +0 -0
  37. {injectq-0.2.0 → injectq-0.2.2}/THREADING_TESTS_SUMMARY.md +0 -0
  38. {injectq-0.2.0 → injectq-0.2.2}/docs/advanced/async-support.md +0 -0
  39. {injectq-0.2.0 → injectq-0.2.2}/docs/advanced/circular-dependencies.md +0 -0
  40. {injectq-0.2.0 → injectq-0.2.2}/docs/advanced/diagnostics.md +0 -0
  41. {injectq-0.2.0 → injectq-0.2.2}/docs/advanced/middleware.md +0 -0
  42. {injectq-0.2.0/docs/advanced-features → injectq-0.2.2/docs/advanced}/overview.md +0 -0
  43. {injectq-0.2.0 → injectq-0.2.2}/docs/advanced/performance-optimization.md +0 -0
  44. {injectq-0.2.0 → injectq-0.2.2}/docs/advanced/plugins.md +0 -0
  45. {injectq-0.2.0 → injectq-0.2.2}/docs/advanced/profiling.md +0 -0
  46. {injectq-0.2.0 → injectq-0.2.2}/docs/advanced/resource-management.md +0 -0
  47. {injectq-0.2.0 → injectq-0.2.2}/docs/advanced/thread-safety.md +0 -0
  48. {injectq-0.2.0 → injectq-0.2.2}/docs/advanced-features/circular-dependencies.md +0 -0
  49. {injectq-0.2.0 → injectq-0.2.2}/docs/advanced-features/diagnostics.md +0 -0
  50. {injectq-0.2.0 → injectq-0.2.2}/docs/advanced-features/performance-optimization.md +0 -0
  51. {injectq-0.2.0 → injectq-0.2.2}/docs/advanced-features/profiling.md +0 -0
  52. {injectq-0.2.0 → injectq-0.2.2}/docs/advanced-features/resource-management.md +0 -0
  53. {injectq-0.2.0 → injectq-0.2.2}/docs/advanced-features/thread-safety.md +0 -0
  54. {injectq-0.2.0 → injectq-0.2.2}/docs/api-reference/container.md +0 -0
  55. {injectq-0.2.0 → injectq-0.2.2}/docs/api-reference/diagnostics.md +0 -0
  56. {injectq-0.2.0 → injectq-0.2.2}/docs/api-reference/exceptions.md +0 -0
  57. {injectq-0.2.0 → injectq-0.2.2}/docs/api-reference/index.md +0 -0
  58. {injectq-0.2.0 → injectq-0.2.2}/docs/api-reference/integrations.md +0 -0
  59. {injectq-0.2.0 → injectq-0.2.2}/docs/api-reference/module.md +0 -0
  60. {injectq-0.2.0 → injectq-0.2.2}/docs/api-reference/registry.md +0 -0
  61. {injectq-0.2.0 → injectq-0.2.2}/docs/api-reference/resolver.md +0 -0
  62. {injectq-0.2.0 → injectq-0.2.2}/docs/api-reference/testing.md +0 -0
  63. {injectq-0.2.0 → injectq-0.2.2}/docs/api-reference/types.md +0 -0
  64. {injectq-0.2.0 → injectq-0.2.2}/docs/api-reference/validation.md +0 -0
  65. {injectq-0.2.0 → injectq-0.2.2}/docs/best-practices/architectural-patterns.md +0 -0
  66. {injectq-0.2.0 → injectq-0.2.2}/docs/best-practices/performance.md +0 -0
  67. {injectq-0.2.0 → injectq-0.2.2}/docs/best-practices/security.md +0 -0
  68. {injectq-0.2.0 → injectq-0.2.2}/docs/best-practices/testing-strategies.md +0 -0
  69. {injectq-0.2.0 → injectq-0.2.2}/docs/contributing.md +0 -0
  70. {injectq-0.2.0 → injectq-0.2.2}/docs/core-concepts/container-pattern.md +0 -0
  71. {injectq-0.2.0 → injectq-0.2.2}/docs/core-concepts/service-lifetimes.md +0 -0
  72. {injectq-0.2.0 → injectq-0.2.2}/docs/core-concepts/type-safety.md +0 -0
  73. {injectq-0.2.0 → injectq-0.2.2}/docs/core-concepts/what-is-di.md +0 -0
  74. {injectq-0.2.0 → injectq-0.2.2}/docs/examples/architectural-examples.md +0 -0
  75. {injectq-0.2.0 → injectq-0.2.2}/docs/examples/design-patterns.md +0 -0
  76. {injectq-0.2.0 → injectq-0.2.2}/docs/examples/practical-examples.md +0 -0
  77. {injectq-0.2.0 → injectq-0.2.2}/docs/framework-integrations/fastapi-integration.md +0 -0
  78. {injectq-0.2.0 → injectq-0.2.2}/docs/framework-integrations/fastmcp-integration.md +0 -0
  79. {injectq-0.2.0 → injectq-0.2.2}/docs/framework-integrations/overview.md +0 -0
  80. {injectq-0.2.0 → injectq-0.2.2}/docs/framework-integrations/taskiq-integration.md +0 -0
  81. {injectq-0.2.0 → injectq-0.2.2}/docs/getting-started/first-app.md +0 -0
  82. {injectq-0.2.0 → injectq-0.2.2}/docs/getting-started/installation.md +0 -0
  83. {injectq-0.2.0 → injectq-0.2.2}/docs/getting-started/quick-start.md +0 -0
  84. {injectq-0.2.0 → injectq-0.2.2}/docs/index.md +0 -0
  85. {injectq-0.2.0 → injectq-0.2.2}/docs/injection-patterns/binding-patterns.md +0 -0
  86. {injectq-0.2.0 → injectq-0.2.2}/docs/injection-patterns/dict-interface.md +0 -0
  87. {injectq-0.2.0 → injectq-0.2.2}/docs/injection-patterns/inject-decorator.md +0 -0
  88. {injectq-0.2.0 → injectq-0.2.2}/docs/injection-patterns/inject-function.md +0 -0
  89. {injectq-0.2.0 → injectq-0.2.2}/docs/integrations/fastapi-integration.md +0 -0
  90. {injectq-0.2.0 → injectq-0.2.2}/docs/integrations/fastmcp-integration.md +0 -0
  91. {injectq-0.2.0 → injectq-0.2.2}/docs/integrations/overview.md +0 -0
  92. {injectq-0.2.0 → injectq-0.2.2}/docs/integrations/taskiq-integration.md +0 -0
  93. {injectq-0.2.0 → injectq-0.2.2}/docs/migration/from-old-versions.md +0 -0
  94. {injectq-0.2.0 → injectq-0.2.2}/docs/modules/configuration-modules.md +0 -0
  95. {injectq-0.2.0 → injectq-0.2.2}/docs/modules/module-best-practices.md +0 -0
  96. {injectq-0.2.0 → injectq-0.2.2}/docs/modules/module-composition.md +0 -0
  97. {injectq-0.2.0 → injectq-0.2.2}/docs/modules/overview.md +0 -0
  98. {injectq-0.2.0 → injectq-0.2.2}/docs/modules/provider-modules.md +0 -0
  99. {injectq-0.2.0 → injectq-0.2.2}/docs/modules-providers/configuration-modules.md +0 -0
  100. {injectq-0.2.0 → injectq-0.2.2}/docs/modules-providers/module-best-practices.md +0 -0
  101. {injectq-0.2.0 → injectq-0.2.2}/docs/modules-providers/module-composition.md +0 -0
  102. {injectq-0.2.0 → injectq-0.2.2}/docs/modules-providers/overview.md +0 -0
  103. {injectq-0.2.0 → injectq-0.2.2}/docs/modules-providers/provider-modules.md +0 -0
  104. {injectq-0.2.0 → injectq-0.2.2}/docs/scopes/custom-scopes.md +0 -0
  105. {injectq-0.2.0 → injectq-0.2.2}/docs/scopes/scope-best-practices.md +0 -0
  106. {injectq-0.2.0 → injectq-0.2.2}/docs/scopes/scoped-services.md +0 -0
  107. {injectq-0.2.0 → injectq-0.2.2}/docs/scopes/singleton-scope.md +0 -0
  108. {injectq-0.2.0 → injectq-0.2.2}/docs/scopes/transient-scope.md +0 -0
  109. {injectq-0.2.0 → injectq-0.2.2}/docs/scopes/understanding-scopes.md +0 -0
  110. {injectq-0.2.0 → injectq-0.2.2}/docs/testing/integration-testing.md +0 -0
  111. {injectq-0.2.0 → injectq-0.2.2}/docs/testing/mocking-strategies.md +0 -0
  112. {injectq-0.2.0 → injectq-0.2.2}/docs/testing/override-patterns.md +0 -0
  113. {injectq-0.2.0 → injectq-0.2.2}/docs/testing/overview.md +0 -0
  114. {injectq-0.2.0 → injectq-0.2.2}/docs/testing/test-scopes.md +0 -0
  115. {injectq-0.2.0 → injectq-0.2.2}/docs/testing/testing-best-practices.md +0 -0
  116. {injectq-0.2.0 → injectq-0.2.2}/docs/testing/testing-utilities.md +0 -0
  117. {injectq-0.2.0 → injectq-0.2.2}/examples/__init__.py +0 -0
  118. {injectq-0.2.0 → injectq-0.2.2}/examples/advanced_features_example.py +0 -0
  119. {injectq-0.2.0 → injectq-0.2.2}/examples/async_thread.py +0 -0
  120. {injectq-0.2.0 → injectq-0.2.2}/examples/comprehensive_example.py +0 -0
  121. {injectq-0.2.0 → injectq-0.2.2}/examples/ex.py +0 -0
  122. {injectq-0.2.0 → injectq-0.2.2}/examples/example.py +0 -0
  123. {injectq-0.2.0 → injectq-0.2.2}/examples/final_example.py +0 -0
  124. {injectq-0.2.0 → injectq-0.2.2}/examples/lazy_example.py +0 -0
  125. {injectq-0.2.0 → injectq-0.2.2}/examples/multi_container/__init__.py +0 -0
  126. {injectq-0.2.0 → injectq-0.2.2}/examples/multi_container/b.py +0 -0
  127. {injectq-0.2.0 → injectq-0.2.2}/examples/multi_container/test.py +0 -0
  128. {injectq-0.2.0 → injectq-0.2.2}/examples/multi_container/tester.py +0 -0
  129. {injectq-0.2.0 → injectq-0.2.2}/examples/multi_container.py +0 -0
  130. {injectq-0.2.0 → injectq-0.2.2}/examples/tt/__init__.py +0 -0
  131. {injectq-0.2.0 → injectq-0.2.2}/examples/tt/checkpointer.py +0 -0
  132. {injectq-0.2.0 → injectq-0.2.2}/examples/tt/compiled.py +0 -0
  133. {injectq-0.2.0 → injectq-0.2.2}/examples/tt/explanination.md +0 -0
  134. {injectq-0.2.0 → injectq-0.2.2}/examples/tt/graph.py +0 -0
  135. {injectq-0.2.0 → injectq-0.2.2}/examples/tt/handler.py +0 -0
  136. {injectq-0.2.0 → injectq-0.2.2}/examples/tt/test.py +0 -0
  137. {injectq-0.2.0 → injectq-0.2.2}/injectq/components/__init__.py +0 -0
  138. {injectq-0.2.0 → injectq-0.2.2}/injectq/core/__init__.py +0 -0
  139. {injectq-0.2.0 → injectq-0.2.2}/injectq/core/async_scopes.py +0 -0
  140. {injectq-0.2.0 → injectq-0.2.2}/injectq/core/base_scope_manager.py +0 -0
  141. {injectq-0.2.0 → injectq-0.2.2}/injectq/core/context.py +0 -0
  142. {injectq-0.2.0 → injectq-0.2.2}/injectq/core/scopes.py +0 -0
  143. {injectq-0.2.0 → injectq-0.2.2}/injectq/core/thread_safe_resolver.py +0 -0
  144. {injectq-0.2.0 → injectq-0.2.2}/injectq/core/thread_safety.py +0 -0
  145. {injectq-0.2.0 → injectq-0.2.2}/injectq/decorators/__init__.py +0 -0
  146. {injectq-0.2.0 → injectq-0.2.2}/injectq/decorators/resource.py +0 -0
  147. {injectq-0.2.0 → injectq-0.2.2}/injectq/decorators/singleton.py +0 -0
  148. {injectq-0.2.0 → injectq-0.2.2}/injectq/diagnostics/__init__.py +0 -0
  149. {injectq-0.2.0 → injectq-0.2.2}/injectq/diagnostics/profiling.py +0 -0
  150. {injectq-0.2.0 → injectq-0.2.2}/injectq/diagnostics/validation.py +0 -0
  151. {injectq-0.2.0 → injectq-0.2.2}/injectq/diagnostics/visualization.py +0 -0
  152. {injectq-0.2.0 → injectq-0.2.2}/injectq/integrations/__init__.py +0 -0
  153. {injectq-0.2.0 → injectq-0.2.2}/injectq/integrations/fastapi.py +0 -0
  154. {injectq-0.2.0 → injectq-0.2.2}/injectq/integrations/taskiq.py +0 -0
  155. {injectq-0.2.0 → injectq-0.2.2}/injectq/modules/__init__.py +0 -0
  156. {injectq-0.2.0 → injectq-0.2.2}/injectq/modules/base.py +0 -0
  157. {injectq-0.2.0 → injectq-0.2.2}/injectq/py.typed +0 -0
  158. {injectq-0.2.0 → injectq-0.2.2}/injectq/testing/__init__.py +0 -0
  159. {injectq-0.2.0 → injectq-0.2.2}/injectq/testing/utilities.py +0 -0
  160. {injectq-0.2.0 → injectq-0.2.2}/injectq/utils/helpers.py +0 -0
  161. {injectq-0.2.0 → injectq-0.2.2}/injectq/utils/protocols.py +0 -0
  162. {injectq-0.2.0 → injectq-0.2.2}/injectq/utils/types.py +0 -0
  163. {injectq-0.2.0 → injectq-0.2.2}/mkdocs.yml +0 -0
  164. {injectq-0.2.0 → injectq-0.2.2}/pyproject.toml +0 -0
  165. {injectq-0.2.0 → injectq-0.2.2}/smoke_test.py +0 -0
  166. {injectq-0.2.0 → injectq-0.2.2}/tests/__init__.py +0 -0
  167. {injectq-0.2.0 → injectq-0.2.2}/tests/test_async_factory.py +0 -0
  168. {injectq-0.2.0 → injectq-0.2.2}/tests/test_async_scopes.py +0 -0
  169. {injectq-0.2.0 → injectq-0.2.2}/tests/test_components.py +0 -0
  170. {injectq-0.2.0 → injectq-0.2.2}/tests/test_container.py +0 -0
  171. {injectq-0.2.0 → injectq-0.2.2}/tests/test_diagnostics.py +0 -0
  172. {injectq-0.2.0 → injectq-0.2.2}/tests/test_fastapi_integration.py +0 -0
  173. {injectq-0.2.0 → injectq-0.2.2}/tests/test_inject.py +0 -0
  174. {injectq-0.2.0 → injectq-0.2.2}/tests/test_modules.py +0 -0
  175. {injectq-0.2.0 → injectq-0.2.2}/tests/test_resource.py +0 -0
  176. {injectq-0.2.0 → injectq-0.2.2}/tests/test_taskiq_integration.py +0 -0
  177. {injectq-0.2.0 → injectq-0.2.2}/tests/test_thread_safety.py +0 -0
  178. {injectq-0.2.0 → injectq-0.2.2}/tests/test_type_checking.py +0 -0
  179. {injectq-0.2.0 → injectq-0.2.2}/tests/test_union_fix.py +0 -0
  180. {injectq-0.2.0 → injectq-0.2.2}/uv.lock +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: injectq
3
- Version: 0.2.0
3
+ Version: 0.2.2
4
4
  Summary: Modern Python dependency injection library combining simplicity, power, and performance
5
5
  Project-URL: Homepage, https://github.com/Iamsdt/injectq
6
6
  Project-URL: Documentation, https://github.com/Iamsdt/injectq
@@ -95,6 +95,71 @@ Notes:
95
95
  - Use `injectq[...]` for simple bindings and values.
96
96
  - Use `@inject` and `Inject[T]` for function/class injection.
97
97
 
98
+ ## Enhanced Features
99
+
100
+ ### Nullable Dependencies
101
+
102
+ InjectQ supports binding `None` values for optional dependencies using the `allow_none` parameter:
103
+
104
+ ```python
105
+ from injectq import InjectQ
106
+
107
+ container = InjectQ()
108
+
109
+ # Optional service - can be None
110
+ class EmailService:
111
+ def send_email(self, to: str, message: str) -> str:
112
+ return f"Email sent to {to}: {message}"
113
+
114
+ class NotificationService:
115
+ def __init__(self, email_service: EmailService | None = None):
116
+ self.email_service = email_service
117
+
118
+ def notify(self, message: str) -> str:
119
+ if self.email_service:
120
+ return self.email_service.send_email("user", message)
121
+ return f"Basic notification: {message}"
122
+
123
+ # Bind None for optional dependency
124
+ container.bind(EmailService, None, allow_none=True)
125
+ container.bind(NotificationService, NotificationService)
126
+
127
+ service = container.get(NotificationService)
128
+ print(service.notify("Hello")) # Prints: Basic notification: Hello
129
+ ```
130
+
131
+ ### Abstract Class Validation
132
+
133
+ InjectQ automatically prevents binding abstract classes and raises a `BindingError` during binding (not at resolution time):
134
+
135
+ ```python
136
+ from abc import ABC, abstractmethod
137
+ from injectq import InjectQ
138
+ from injectq.utils.exceptions import BindingError
139
+
140
+ class PaymentProcessor(ABC): # Abstract class
141
+ @abstractmethod
142
+ def process_payment(self, amount: float) -> str:
143
+ pass
144
+
145
+ class CreditCardProcessor(PaymentProcessor): # Concrete implementation
146
+ def process_payment(self, amount: float) -> str:
147
+ return f"Processing ${amount} via credit card"
148
+
149
+ container = InjectQ()
150
+
151
+ # This will raise BindingError immediately
152
+ try:
153
+ container.bind(PaymentProcessor, PaymentProcessor) # Error!
154
+ except BindingError:
155
+ print("Cannot bind abstract class")
156
+
157
+ # This works fine
158
+ container.bind(PaymentProcessor, CreditCardProcessor) # OK
159
+ ```
160
+
161
+ See `examples/enhanced_features_demo.py` for a complete demonstration.
162
+
98
163
  ## Installation
99
164
 
100
165
  Install from PyPI:
@@ -49,6 +49,71 @@ Notes:
49
49
  - Use `injectq[...]` for simple bindings and values.
50
50
  - Use `@inject` and `Inject[T]` for function/class injection.
51
51
 
52
+ ## Enhanced Features
53
+
54
+ ### Nullable Dependencies
55
+
56
+ InjectQ supports binding `None` values for optional dependencies using the `allow_none` parameter:
57
+
58
+ ```python
59
+ from injectq import InjectQ
60
+
61
+ container = InjectQ()
62
+
63
+ # Optional service - can be None
64
+ class EmailService:
65
+ def send_email(self, to: str, message: str) -> str:
66
+ return f"Email sent to {to}: {message}"
67
+
68
+ class NotificationService:
69
+ def __init__(self, email_service: EmailService | None = None):
70
+ self.email_service = email_service
71
+
72
+ def notify(self, message: str) -> str:
73
+ if self.email_service:
74
+ return self.email_service.send_email("user", message)
75
+ return f"Basic notification: {message}"
76
+
77
+ # Bind None for optional dependency
78
+ container.bind(EmailService, None, allow_none=True)
79
+ container.bind(NotificationService, NotificationService)
80
+
81
+ service = container.get(NotificationService)
82
+ print(service.notify("Hello")) # Prints: Basic notification: Hello
83
+ ```
84
+
85
+ ### Abstract Class Validation
86
+
87
+ InjectQ automatically prevents binding abstract classes and raises a `BindingError` during binding (not at resolution time):
88
+
89
+ ```python
90
+ from abc import ABC, abstractmethod
91
+ from injectq import InjectQ
92
+ from injectq.utils.exceptions import BindingError
93
+
94
+ class PaymentProcessor(ABC): # Abstract class
95
+ @abstractmethod
96
+ def process_payment(self, amount: float) -> str:
97
+ pass
98
+
99
+ class CreditCardProcessor(PaymentProcessor): # Concrete implementation
100
+ def process_payment(self, amount: float) -> str:
101
+ return f"Processing ${amount} via credit card"
102
+
103
+ container = InjectQ()
104
+
105
+ # This will raise BindingError immediately
106
+ try:
107
+ container.bind(PaymentProcessor, PaymentProcessor) # Error!
108
+ except BindingError:
109
+ print("Cannot bind abstract class")
110
+
111
+ # This works fine
112
+ container.bind(PaymentProcessor, CreditCardProcessor) # OK
113
+ ```
114
+
115
+ See `examples/enhanced_features_demo.py` for a complete demonstration.
116
+
52
117
  ## Installation
53
118
 
54
119
  Install from PyPI:
@@ -0,0 +1,232 @@
1
+ # Enhanced Features
2
+
3
+ InjectQ provides several advanced features to handle complex dependency injection scenarios.
4
+
5
+ ## Nullable Dependencies
6
+
7
+ Sometimes you need to inject dependencies that might be `None` or optional. InjectQ supports this through the `allow_none` parameter.
8
+
9
+ ### Basic Usage
10
+
11
+ ```python
12
+ from injectq import InjectQ
13
+
14
+ container = InjectQ()
15
+
16
+ # Bind None for an optional service
17
+ container.bind(EmailService, None, allow_none=True)
18
+ ```
19
+
20
+ ### Practical Example
21
+
22
+ ```python
23
+ class EmailService:
24
+ def send_email(self, to: str, message: str) -> str:
25
+ return f"Email sent to {to}: {message}"
26
+
27
+ class NotificationService:
28
+ def __init__(self, email_service: EmailService | None = None):
29
+ self.email_service = email_service
30
+
31
+ def notify(self, user: str, message: str) -> str:
32
+ result = f"Notification for {user}: {message}"
33
+
34
+ if self.email_service:
35
+ email_result = self.email_service.send_email(user, message)
36
+ result += f" | {email_result}"
37
+
38
+ return result
39
+
40
+ # Configure different scenarios
41
+ container = InjectQ()
42
+
43
+ # Scenario 1: Email service available
44
+ container.bind(EmailService, EmailService)
45
+ container.bind(NotificationService, NotificationService)
46
+
47
+ # Scenario 2: Email service disabled
48
+ container_disabled = InjectQ()
49
+ container_disabled.bind(EmailService, None, allow_none=True)
50
+ container_disabled.bind(NotificationService, NotificationService)
51
+
52
+ # Use the services
53
+ service1 = container.get(NotificationService)
54
+ service2 = container_disabled.get(NotificationService)
55
+
56
+ print(service1.notify("alice", "Welcome"))
57
+ # Output: Notification for alice: Welcome | Email sent to alice: Welcome
58
+
59
+ print(service2.notify("bob", "Hello"))
60
+ # Output: Notification for bob: Hello
61
+ ```
62
+
63
+ ### Important Notes
64
+
65
+ - **Explicit `allow_none=True` required**: You must explicitly set `allow_none=True` to bind `None` values
66
+ - **Without `allow_none`**: Binding `None` will raise a `BindingError`
67
+ - **Type safety**: Use union types (`Service | None`) in your type annotations
68
+ - **Default values**: Works well with default parameter values in constructors
69
+
70
+ ### Error Handling
71
+
72
+ ```python
73
+ # This will raise BindingError
74
+ try:
75
+ container.bind(EmailService, None) # Missing allow_none=True
76
+ except BindingError as e:
77
+ print(f"Error: {e}")
78
+ ```
79
+
80
+ ## Abstract Class Validation
81
+
82
+ InjectQ automatically prevents binding abstract classes, helping catch configuration errors early.
83
+
84
+ ### How It Works
85
+
86
+ ```python
87
+ from abc import ABC, abstractmethod
88
+ from injectq import InjectQ
89
+ from injectq.utils.exceptions import BindingError
90
+
91
+ class PaymentProcessor(ABC):
92
+ @abstractmethod
93
+ def process_payment(self, amount: float) -> str:
94
+ pass
95
+
96
+ class CreditCardProcessor(PaymentProcessor):
97
+ def process_payment(self, amount: float) -> str:
98
+ return f"Processing ${amount} via credit card"
99
+
100
+ container = InjectQ()
101
+
102
+ # This raises BindingError immediately during binding
103
+ try:
104
+ container.bind(PaymentProcessor, PaymentProcessor)
105
+ except BindingError as e:
106
+ print(f"Cannot bind abstract class: {e}")
107
+
108
+ # This works fine - concrete implementation
109
+ container.bind(PaymentProcessor, CreditCardProcessor)
110
+ processor = container.get(PaymentProcessor)
111
+ result = processor.process_payment(99.99)
112
+ ```
113
+
114
+ ### Benefits
115
+
116
+ - **Early error detection**: Problems are caught at binding time, not resolution time
117
+ - **Clear error messages**: Specific messages about which abstract class cannot be bound
118
+ - **Type safety**: Prevents runtime errors from trying to instantiate abstract classes
119
+ - **Development efficiency**: Faster feedback loop during development
120
+
121
+ ### Detection Rules
122
+
123
+ InjectQ uses Python's built-in `inspect.isabstract()` to detect abstract classes:
124
+
125
+ - Classes with `@abstractmethod` decorated methods
126
+ - Classes that inherit from `ABC` with unimplemented abstract methods
127
+ - Classes explicitly marked as abstract
128
+
129
+ ### Example Integration
130
+
131
+ ```python
132
+ from abc import ABC, abstractmethod
133
+ from injectq import InjectQ, singleton
134
+
135
+ # Define abstractions
136
+ class DatabaseService(ABC):
137
+ @abstractmethod
138
+ def save(self, data: dict) -> bool:
139
+ pass
140
+
141
+ class CacheService(ABC):
142
+ @abstractmethod
143
+ def get(self, key: str) -> str | None:
144
+ pass
145
+
146
+ # Concrete implementations
147
+ @singleton
148
+ class PostgreSQLService(DatabaseService):
149
+ def save(self, data: dict) -> bool:
150
+ print(f"Saving to PostgreSQL: {data}")
151
+ return True
152
+
153
+ @singleton
154
+ class RedisCache(CacheService):
155
+ def get(self, key: str) -> str | None:
156
+ print(f"Getting from Redis: {key}")
157
+ return "cached_value"
158
+
159
+ # Business service
160
+ class UserService:
161
+ def __init__(self, db: DatabaseService, cache: CacheService):
162
+ self.db = db
163
+ self.cache = cache
164
+
165
+ def create_user(self, user_data: dict) -> bool:
166
+ # Try cache first
167
+ cached = self.cache.get(f"user:{user_data['id']}")
168
+ if cached:
169
+ return True
170
+
171
+ # Save to database
172
+ return self.db.save(user_data)
173
+
174
+ # Configure container
175
+ container = InjectQ()
176
+ container.bind(DatabaseService, PostgreSQLService) # OK - concrete
177
+ container.bind(CacheService, RedisCache) # OK - concrete
178
+ container.bind(UserService, UserService)
179
+
180
+ # Use the service
181
+ user_service = container.get(UserService)
182
+ user_service.create_user({"id": 1, "name": "Alice"})
183
+ ```
184
+
185
+ ## Combining Both Features
186
+
187
+ You can use nullable dependencies and abstract class validation together:
188
+
189
+ ```python
190
+ from abc import ABC, abstractmethod
191
+ from injectq import InjectQ
192
+
193
+ class MetricsService(ABC):
194
+ @abstractmethod
195
+ def record_metric(self, name: str, value: float) -> None:
196
+ pass
197
+
198
+ class PrometheusMetrics(MetricsService):
199
+ def record_metric(self, name: str, value: float) -> None:
200
+ print(f"Prometheus: {name} = {value}")
201
+
202
+ class ApplicationService:
203
+ def __init__(self, metrics: MetricsService | None = None):
204
+ self.metrics = metrics
205
+
206
+ def do_work(self) -> str:
207
+ if self.metrics:
208
+ self.metrics.record_metric("work_done", 1.0)
209
+ return "Work completed"
210
+
211
+ container = InjectQ()
212
+
213
+ # Production: Use real metrics
214
+ container.bind(MetricsService, PrometheusMetrics)
215
+ container.bind(ApplicationService, ApplicationService)
216
+
217
+ # Testing: Disable metrics
218
+ test_container = InjectQ()
219
+ test_container.bind(MetricsService, None, allow_none=True)
220
+ test_container.bind(ApplicationService, ApplicationService)
221
+
222
+ # Both work correctly
223
+ prod_service = container.get(ApplicationService)
224
+ test_service = test_container.get(ApplicationService)
225
+
226
+ print(prod_service.do_work()) # With metrics
227
+ print(test_service.do_work()) # Without metrics
228
+ ```
229
+
230
+ ## Complete Example
231
+
232
+ See `examples/enhanced_features_demo.py` for a comprehensive demonstration of both nullable dependencies and abstract class validation working together in a realistic scenario.
@@ -6,6 +6,7 @@
6
6
 
7
7
  InjectQ's advanced features help you:
8
8
 
9
+ - **Enhanced bindings** - Nullable dependencies and abstract class validation
9
10
  - **Manage resources** - Automatic cleanup and lifecycle management
10
11
  - **Diagnose issues** - Debug dependency resolution and detect problems
11
12
  - **Optimize performance** - Lazy loading, caching, and efficient resolution
@@ -16,6 +17,16 @@ InjectQ's advanced features help you:
16
17
  ### Advanced Features Categories
17
18
 
18
19
  ```python
20
+ # Enhanced Bindings - Nullable Dependencies
21
+ container.bind(EmailService, None, allow_none=True) # Optional service
22
+
23
+ # Enhanced Bindings - Abstract Class Validation
24
+ from abc import ABC, abstractmethod
25
+
26
+ class PaymentProcessor(ABC): # Will be rejected during binding
27
+ @abstractmethod
28
+ def process(self): pass
29
+
19
30
  # Resource Management
20
31
  @injectq.resource
21
32
  class DatabaseConnection:
@@ -53,6 +64,7 @@ container = ThreadSafeContainer()
53
64
 
54
65
  This section covers:
55
66
 
67
+ - **[Enhanced Features](enhanced-features.md)** - Nullable dependencies and abstract class validation
56
68
  - **[Resource Management](resource-management.md)** - Automatic resource lifecycle management
57
69
  - **[Diagnostics](diagnostics.md)** - Debugging and dependency analysis tools
58
70
  - **[Performance Optimization](performance-optimization.md)** - Lazy loading, caching, and optimization techniques
@@ -278,9 +278,9 @@ def async_inject(func):
278
278
  if param_name not in bound_args.arguments:
279
279
  if hasattr(param.annotation, '__origin__'):
280
280
  # Handle generic types
281
- dependency = await container.get_async(param.annotation)
281
+ dependency = await container.aget(param.annotation)
282
282
  else:
283
- dependency = await container.get_async(param.annotation)
283
+ dependency = await container.aget(param.annotation)
284
284
  bound_args.arguments[param_name] = dependency
285
285
 
286
286
  return await func(**bound_args.arguments)
@@ -267,7 +267,7 @@ from typing import Awaitable
267
267
  class AsyncProvider:
268
268
  """Async provider interface."""
269
269
 
270
- async def get_async(self) -> Awaitable[Any]:
270
+ async def aget(self) -> Awaitable[Any]:
271
271
  """Asynchronously get service instance."""
272
272
  raise NotImplementedError
273
273
 
@@ -277,7 +277,7 @@ class AsyncFactoryProvider(AsyncProvider):
277
277
  def __init__(self, async_factory_func):
278
278
  self.async_factory_func = async_factory_func
279
279
 
280
- async def get_async(self):
280
+ async def aget(self):
281
281
  """Get instance from async factory."""
282
282
  return await self.async_factory_func()
283
283
 
@@ -306,7 +306,7 @@ class AsyncCachingProvider(AsyncProvider):
306
306
  self.cache_time = None
307
307
  self.lock = asyncio.Lock()
308
308
 
309
- async def get_async(self):
309
+ async def aget(self):
310
310
  """Get cached instance or create new one asynchronously."""
311
311
  async with self.lock:
312
312
  current_time = time.time()
@@ -318,7 +318,7 @@ class AsyncCachingProvider(AsyncProvider):
318
318
  return self.cached_instance
319
319
 
320
320
  # Create new instance
321
- self.cached_instance = await self.base_provider.get_async()
321
+ self.cached_instance = await self.base_provider.aget()
322
322
  self.cache_time = current_time
323
323
 
324
324
  return self.cached_instance
@@ -64,7 +64,7 @@ with container.create_scope() as scope2:
64
64
  ```python
65
65
  # Async scope context manager
66
66
  async with container.create_async_scope() as scope:
67
- service = await scope.get_async(AsyncService)
67
+ service = await scope.aget(AsyncService)
68
68
  await service.do_work()
69
69
  # Scope automatically disposed when exiting context
70
70
  ```
@@ -75,7 +75,7 @@ async with container.create_async_scope() as scope:
75
75
  # Manual async scope creation and disposal
76
76
  scope = container.create_async_scope()
77
77
  try:
78
- service = await scope.get_async(AsyncService)
78
+ service = await scope.aget(AsyncService)
79
79
  await service.do_work()
80
80
  finally:
81
81
  await scope.dispose()
@@ -96,7 +96,7 @@ class DatabaseConnection:
96
96
  # Usage in async scope
97
97
  async with container.create_async_scope() as scope:
98
98
  # Resource is automatically managed
99
- db = await scope.get_async(DatabaseConnection)
99
+ db = await scope.aget(DatabaseConnection)
100
100
  await db.execute("SELECT * FROM users")
101
101
  # Connection automatically closed when scope exits
102
102
  ```
@@ -8,6 +8,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
8
8
  ## [Unreleased]
9
9
 
10
10
  ### Added
11
+ - **Auto-registration of concrete types**: New `allow_concrete` parameter (default: True)
12
+ automatically registers concrete types when registering instances to base types
13
+ - **Registration override control**: New `allow_override` parameter (default: True)
14
+ controls whether existing service registrations can be overwritten
15
+ - Enhanced `bind_instance()` method with `allow_concrete` parameter
16
+ - Enhanced `bind_factory()` method with `allow_concrete` parameter
17
+ - Enhanced `bind()` method with `allow_concrete` parameter
18
+ - New `AlreadyRegisteredError` exception for override conflicts
11
19
  - Comprehensive documentation with MkDocs
12
20
  - Plugin system for extensibility
13
21
  - Advanced middleware support
@@ -18,11 +26,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
18
26
  - Migration guides from other DI libraries
19
27
 
20
28
  ### Changed
29
+ - **Breaking**: InjectQ constructor now accepts `allow_override` parameter
30
+ - **Breaking**: All binding methods now accept `allow_concrete` parameter
31
+ - Dict-like syntax (`container[Type] = instance`) now uses `allow_concrete=True` by default
32
+ - Improved subclass injection support - both base and concrete types can be resolved
21
33
  - Improved type safety and mypy compliance
22
34
  - Enhanced error messages and debugging information
23
35
  - Optimized performance for large dependency graphs
24
36
 
25
37
  ### Fixed
38
+ - Subclass injection issues where concrete types weren't accessible after registering to base type
26
39
  - Various bug fixes and stability improvements
27
40
 
28
41
  ## [0.1.0] - 2024-01-15