bpo-32299: Return patched dict when using patch.dict as a context man… · python/cpython@0453081 (original) (raw)

`@@ -1556,15 +1556,24 @@ patch.dict

`

1556

1556

`` decorator. When used as a class decorator :func:patch.dict honours

``

1557

1557

``` patch.TEST_PREFIX for choosing which methods to wrap.

```

1558

1558

``

``

1559

`+

.. versionchanged:: 3.8

`

``

1560

+

``

1561

`` +

:func:patch.dict now returns the patched dictionary when used as a context

``

``

1562

`+

manager.

`

``

1563

+

1559

1564

`` :func:patch.dict can be used to add members to a dictionary, or simply let a test

``

1560

1565

`change a dictionary, and ensure the dictionary is restored when the test

`

1561

1566

`ends.

`

1562

1567

``

1563

1568

` >>> foo = {}

`

1564

``

`-

with patch.dict(foo, {'newkey': 'newvalue'}):

`

``

1569

`+

with patch.dict(foo, {'newkey': 'newvalue'}) as patched_foo:

`

1565

1570

` ... assert foo == {'newkey': 'newvalue'}

`

``

1571

`+

... assert patched_foo == {'newkey': 'newvalue'}

`

``

1572

`+

... # You can add, update or delete keys of foo (or patched_foo, it's the same dict)

`

``

1573

`+

... patched_foo['spam'] = 'eggs'

`

1566

1574

` ...

`

1567

1575

` >>> assert foo == {}

`

``

1576

`+

assert patched_foo == {}

`

1568

1577

``

1569

1578

` >>> import os

`

1570

1579

` >>> with patch.dict('os.environ', {'newkey': 'newvalue'}):

`