@@ -326,9 +326,45 @@ to be called when this exception is raised:
326326
327327 If this function returns anything other than ``None ``, the exception will not be raised.
328328
329+ How to set temporary configuration?
330+ -----------------------------------
331+
332+ When writing unit tests, or in other scenarios when you need to change configuration briefly just to
333+ execute some particular part of your code, you can create an auto-resetting configuration context by
334+ calling your ``Config `` instance as a function.
335+
336+ .. code-block :: python
337+
338+ with config():
339+ config.greeting.value = ' Bon jour!'
340+
341+ # do some French things here
342+ pass
343+
344+ # French settings have been reset:
345+ assert config.greeting.get() == ' Hello, world!'
346+
347+ If you prefer to set the temporary configuration on initialisation of the context, you can do so
348+ by passing a values dictionary:
349+
350+ .. code-block :: python
351+
352+ with config({' greeting' : ' Bon jour!' }):
353+ # do some French things here
354+ pass
355+
356+ Note that you cannot pass keyword arguments there, just a dictionary.
357+
358+
329359How do I manage changesets of config values?
330360--------------------------------------------
331361
362+ The previous example used a special case of what we call a *changeset context *.
363+ You can explicitly create one with :meth: `.Config.changeset_context `.
364+
365+ Unlike the special context demonstrated above, a default changeset context does not
366+ reset changes made while program control was inside it.
367+
332368.. code-block :: python
333369 :emphasize- lines: 4 ,7 ,10 ,13 ,14
334370
@@ -353,3 +389,6 @@ How do I manage changesets of config values?
353389
354390 >> > config.greeting.get()
355391 ' Hello, world!'
392+
393+ A changeset context comes handy when you want to create a sub-context of changes which you want to be able
394+ to export or persist separately from the rest of configuration changes.
0 commit comments