Skip to content

Commit 2d49308

Browse files
authored
Configuration strict mode raise on non existing files (#375)
* Update from_yaml() * Refactor YAML environment variables interpolation * Update from_ini() * Refactor UNDEFINED * Update from_env() * Update from_dict() * Update docs * Update changelog
1 parent 5008558 commit 2d49308

File tree

5 files changed

+8833
-7955
lines changed

5 files changed

+8833
-7955
lines changed

docs/main/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Development version
1313
default YAML loader.
1414
- Make security improvement: change default YAML loader to the custom ``yaml.SafeLoader`` with a support
1515
of environment variables interpolation.
16+
- Update configuration provider ``.from_*()`` methods to raise an exception in strict mode if
17+
configuration file does not exist or configuration data is undefined.
1618
- Fix a bug with asynchronous injections: async providers do not work with async dependencies.
1719
See issue: `#368 <https://github.com/ets-labs/python-dependency-injector/issues/368>`_.
1820
Thanks `@kolypto <https://github.com/kolypto>`_ for the bug report.

docs/providers/configuration.rst

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,42 @@ on access to any undefined option.
168168
:lines: 3-
169169
:emphasize-lines: 12
170170

171-
You can also use ``.required()`` option modifier when making an injection.
171+
Methods ``.from_*()`` in strict mode raise an exception if configuration file does not exist or
172+
configuration data is undefined:
173+
174+
.. code-block:: python
175+
:emphasize-lines: 10,15,20,25
176+
177+
class Container(containers.DeclarativeContainer):
178+
179+
config = providers.Configuration(strict=True)
180+
181+
182+
if __name__ == '__main__':
183+
container = Container()
184+
185+
try:
186+
container.config.from_yaml('./does-not_exist.yml') # raise exception
187+
except FileNotFoundError:
188+
...
189+
190+
try:
191+
container.config.from_ini('./does-not_exist.ini') # raise exception
192+
except FileNotFoundError:
193+
...
194+
195+
try:
196+
container.config.from_env('UNDEFINED_ENV_VAR') # raise exception
197+
except ValueError:
198+
...
199+
200+
try:
201+
container.config.from_dict({}) # raise exception
202+
except ValueError:
203+
...
204+
205+
You can also use ``.required()`` option modifier when making an injection. It does not require to switch
206+
configuration provider to strict mode.
172207

173208
.. literalinclude:: ../../examples/providers/configuration/configuration_required.py
174209
:language: python

0 commit comments

Comments
 (0)