chunk_id stringlengths 36 36 | source stringclasses 35
values | source_url stringlengths 0 290 | upstream_license stringclasses 1
value | document_id stringlengths 36 36 | chunk_index int64 0 324k | retrieved_at stringclasses 2
values | chunker_version stringclasses 4
values | content_hash stringlengths 15 64 | content stringlengths 50 44.7k | namespace stringclasses 9
values | source_name stringclasses 35
values | raw_text stringlengths 50 44.7k | cleaned_text stringlengths 50 44.7k | tags stringclasses 49
values | collection_name stringclasses 11
values |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
cae42d91-890b-4296-ae36-b6d58d79ec31 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,449 | supabase-export-v2 | 1c2b1cba9c0d7f3f | *Python* order that decorators are applied). This means from the bottom up, so in the example above the mock for ``module.ClassName1`` is passed in first.
With :func:`patch` it matters that you patch objects in the namespace where they
are looked up. This is normally straightforward, but for a quick guide
read :ref:`... | trusted_official_docs | CPython Docs | *Python* order that decorators are applied). This means from the bottom up, so in the example above the mock for ``module.ClassName1`` is passed in first.
With :func:`patch` it matters that you patch objects in the namespace where they
are looked up. This is normally straightforward, but for a quick guide
read :ref:`... | *Python* order that decorators are applied). This means from the bottom up, so in the example above the mock for ``module.ClassName1`` is passed in first.
With :func:`patch` it matters that you patch objects in the namespace where they
are looked up. This is normally straightforward, but for a quick guide
read :ref:`... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
cba9bf6f-ef34-49c4-b315-ae2495c0683c | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,431 | supabase-export-v2 | b367a77ac96f06f1 | in Python. It allows you to replace parts of your system under test with mock objects and make assertions about how they have been used.
:mod:`!unittest.mock` provides a core :class:`Mock` class removing the need to
create a host of stubs throughout your test suite. After performing an
action, you can make assertions a... | trusted_official_docs | CPython Docs | in Python. It allows you to replace parts of your system under test with mock objects and make assertions about how they have been used.
:mod:`!unittest.mock` provides a core :class:`Mock` class removing the need to
create a host of stubs throughout your test suite. After performing an
action, you can make assertions a... | in Python. It allows you to replace parts of your system under test with mock objects and make assertions about how they have been used.
:mod:`!unittest.mock` provides a core :class:`Mock` class removing the need to
create a host of stubs throughout your test suite. After performing an
action, you can make assertions a... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
cc7e57b3-0182-437e-94d2-be50dffb56f8 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,857 | supabase-export-v2 | 59be229a9a0c5796 | The following methods exist but are *not* supported as they are either in use by mock, can't be set dynamically, or can cause problems:
* ``__getattr__``, ``__setattr__``, ``__init__`` and ``__new__``
* ``__prepare__``, ``__instancecheck__``, ``__subclasscheck__``, ``__del__`` | trusted_official_docs | CPython Docs | The following methods exist but are *not* supported as they are either in use by mock, can't be set dynamically, or can cause problems:
* ``__getattr__``, ``__setattr__``, ``__init__`` and ``__new__``
* ``__prepare__``, ``__instancecheck__``, ``__subclasscheck__``, ``__del__`` | The following methods exist but are *not* supported as they are either in use by mock, can't be set dynamically, or can cause problems:
* ``__getattr__``, ``__setattr__``, ``__init__`` and ``__new__``
* ``__prepare__``, ``__instancecheck__``, ``__subclasscheck__``, ``__del__`` | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
cc80c63d-cce9-4366-b39e-96d3602d2f49 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,581 | supabase-export-v2 | 0c61b30549a16e72 | As well as tracking calls to themselves, mocks also track calls to methods and attributes, and *their* methods and attributes:
>>> mock = Mock()
>>> mock.method()
<Mock name='mock.method()' id='...'>
>>> mock.property.method.attribute()
<Mock name='mock.property.method.attribute()' id='...'>
>>> mock.method_calls
... | trusted_official_docs | CPython Docs | As well as tracking calls to themselves, mocks also track calls to methods and attributes, and *their* methods and attributes:
>>> mock = Mock()
>>> mock.method()
<Mock name='mock.method()' id='...'>
>>> mock.property.method.attribute()
<Mock name='mock.property.method.attribute()' id='...'>
>>> mock.method_calls
... | As well as tracking calls to themselves, mocks also track calls to methods and attributes, and *their* methods and attributes:
>>> mock = Mock()
>>> mock.method()
<Mock name='mock.method()' id='...'>
>>> mock.property.method.attribute()
<Mock name='mock.property.method.attribute()' id='...'>
>>> mock.method_calls
... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
ccec7ea3-72b2-4b2d-8765-4dade229b3c5 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,723 | supabase-export-v2 | 02045688ff6f7a1b | signature. For mocks replacing a class, their return value (the 'instance') will have the same spec as the class. See the :func:`create_autospec` function and :ref:`auto-speccing`.
Instead of ``autospec=True`` you can pass ``autospec=some_object`` to use an
arbitrary object as the spec instead of the one being replace... | trusted_official_docs | CPython Docs | signature. For mocks replacing a class, their return value (the 'instance') will have the same spec as the class. See the :func:`create_autospec` function and :ref:`auto-speccing`.
Instead of ``autospec=True`` you can pass ``autospec=some_object`` to use an
arbitrary object as the spec instead of the one being replace... | signature. For mocks replacing a class, their return value (the 'instance') will have the same spec as the class. See the :func:`create_autospec` function and :ref:`auto-speccing`.
Instead of ``autospec=True`` you can pass ``autospec=some_object`` to use an
arbitrary object as the spec instead of the one being replace... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
cd1501b0-fc9b-45c2-8e18-7997e119e746 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,669 | supabase-export-v2 | 2e06e5aa1b50b301 | the mock or if a timeout argument is passed to this function, the function raises an :exc:`AssertionError` if the call is not performed in time.
>>> mock = ThreadingMock()
>>> thread = threading.Thread(target=mock)
>>> thread.start()
>>> mock.wait_until_called(timeout=1)
>>> thread.join() | trusted_official_docs | CPython Docs | the mock or if a timeout argument is passed to this function, the function raises an :exc:`AssertionError` if the call is not performed in time.
>>> mock = ThreadingMock()
>>> thread = threading.Thread(target=mock)
>>> thread.start()
>>> mock.wait_until_called(timeout=1)
>>> thread.join() | the mock or if a timeout argument is passed to this function, the function raises an :exc:`AssertionError` if the call is not performed in time.
>>> mock = ThreadingMock()
>>> thread = threading.Thread(target=mock)
>>> thread.start()
>>> mock.wait_until_called(timeout=1)
>>> thread.join() | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
cdcf97d6-8593-4571-bbf0-50b4c6dad350 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,886 | supabase-export-v2 | 6d9bcdc12c21c680 | The ``sentinel`` object provides a convenient way of providing unique objects for your tests.
Attributes are created on demand when you access them by name. Accessing
the same attribute will always return the same object. The objects
returned have a sensible repr so that test failure messages are readable. | trusted_official_docs | CPython Docs | The ``sentinel`` object provides a convenient way of providing unique objects for your tests.
Attributes are created on demand when you access them by name. Accessing
the same attribute will always return the same object. The objects
returned have a sensible repr so that test failure messages are readable. | The ``sentinel`` object provides a convenient way of providing unique objects for your tests.
Attributes are created on demand when you access them by name. Accessing
the same attribute will always return the same object. The objects
returned have a sensible repr so that test failure messages are readable. | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
ce5bc181-9427-4c30-aee5-cf7fa6fe817f | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,981 | supabase-export-v2 | fbc24338d725e206 | patch('__main__.Something', autospec=True, spec_set=True): ... thing = Something() ... thing.a = 33 ... Traceback (most recent call last): ... AttributeError: Mock object has no attribute 'a'
Probably the best way of solving the problem is to add class attributes as
default values for instance members initialised in :m... | trusted_official_docs | CPython Docs | patch('__main__.Something', autospec=True, spec_set=True): ... thing = Something() ... thing.a = 33 ... Traceback (most recent call last): ... AttributeError: Mock object has no attribute 'a'
Probably the best way of solving the problem is to add class attributes as
default values for instance members initialised in :m... | patch('__main__.Something', autospec=True, spec_set=True): ... thing = Something() ... thing.a = 33 ... Traceback (most recent call last): ... AttributeError: Mock object has no attribute 'a'
Probably the best way of solving the problem is to add class attributes as
default values for instance members initialised in :m... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
cec028af-3695-4195-ba70-9af8db9f55a5 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,740 | supabase-export-v2 | 27b8ac73ac5b13f0 | you want to use an alternative class to the default :class:`MagicMock` for the created mock. For example, if you wanted a :class:`NonCallableMock` to be used::
>>> thing = object()
>>> with patch('__main__.thing', new_callable=NonCallableMock) as mock_thing:
... assert thing is mock_thing
... thing()
... Traceback ... | trusted_official_docs | CPython Docs | you want to use an alternative class to the default :class:`MagicMock` for the created mock. For example, if you wanted a :class:`NonCallableMock` to be used::
>>> thing = object()
>>> with patch('__main__.thing', new_callable=NonCallableMock) as mock_thing:
... assert thing is mock_thing
... thing()
... Traceback ... | you want to use an alternative class to the default :class:`MagicMock` for the created mock. For example, if you wanted a :class:`NonCallableMock` to be used::
>>> thing = object()
>>> with patch('__main__.thing', new_callable=NonCallableMock) as mock_thing:
... assert thing is mock_thing
... thing()
... Traceback ... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
cef051a9-7a1a-48d3-b526-bb6da8ad2d61 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,951 | supabase-export-v2 | 8aacc762e2b74cd0 | Autospeccing ~~~~~~~~~~~~
Autospeccing is based on the existing :attr:`!spec` feature of mock. It limits the
api of mocks to the api of an original object (the spec), but it is recursive
(implemented lazily) so that attributes of mocks only have the same api as
the attributes of the spec. In addition mocked functions /... | trusted_official_docs | CPython Docs | Autospeccing ~~~~~~~~~~~~
Autospeccing is based on the existing :attr:`!spec` feature of mock. It limits the
api of mocks to the api of an original object (the spec), but it is recursive
(implemented lazily) so that attributes of mocks only have the same api as
the attributes of the spec. In addition mocked functions /... | Autospeccing ~~~~~~~~~~~~
Autospeccing is based on the existing :attr:`!spec` feature of mock. It limits the
api of mocks to the api of an original object (the spec), but it is recursive
(implemented lazily) so that attributes of mocks only have the same api as
the attributes of the spec. In addition mocked functions /... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
cf0d9a13-3be4-4b36-af66-0f751edb2875 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,889 | supabase-export-v2 | e4da8f27f4f20769 | be common to create named sentinel objects to test this. :data:`sentinel` provides a convenient way of creating and testing the identity of objects like this.
In this example we monkey patch ``method`` to return ``sentinel.some_object``: | trusted_official_docs | CPython Docs | be common to create named sentinel objects to test this. :data:`sentinel` provides a convenient way of creating and testing the identity of objects like this.
In this example we monkey patch ``method`` to return ``sentinel.some_object``: | be common to create named sentinel objects to test this. :data:`sentinel` provides a convenient way of creating and testing the identity of objects like this.
In this example we monkey patch ``method`` to return ``sentinel.some_object``: | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
cf3c658b-9ea7-4c0e-ae09-eaeb63499d34 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,900 | supabase-export-v2 | 275585b9603b95be | :meth:`~call.call_list` can construct the sequence of calls from the same chained call:
>>> m = MagicMock()
>>> m(1).method(arg='foo').other('bar')(2.0)
<MagicMock name='mock().method().other()()' id='...'>
>>> kall = call(1).method(arg='foo').other('bar')(2.0)
>>> kall.call_list()
[call(1),
call().method(arg='fo... | trusted_official_docs | CPython Docs | :meth:`~call.call_list` can construct the sequence of calls from the same chained call:
>>> m = MagicMock()
>>> m(1).method(arg='foo').other('bar')(2.0)
<MagicMock name='mock().method().other()()' id='...'>
>>> kall = call(1).method(arg='foo').other('bar')(2.0)
>>> kall.call_list()
[call(1),
call().method(arg='fo... | :meth:`~call.call_list` can construct the sequence of calls from the same chained call:
>>> m = MagicMock()
>>> m(1).method(arg='foo').other('bar')(2.0)
<MagicMock name='mock().method().other()()' id='...'>
>>> kall = call(1).method(arg='foo').other('bar')(2.0)
>>> kall.call_list()
[call(1),
call().method(arg='fo... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
cfdf35d0-615c-4ed9-9520-71307065d5e6 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,747 | supabase-export-v2 | 40e007c22201b518 | {'method.return_value': 3, 'other.side_effect': KeyError} >>> patcher = patch('__main__.thing', **config) >>> mock_thing = patcher.start() >>> mock_thing.method() 3 >>> mock_thing.other() Traceback (most recent call last): ... KeyError
By default, attempting to patch a function in a module (or a method or an
attribute ... | trusted_official_docs | CPython Docs | {'method.return_value': 3, 'other.side_effect': KeyError} >>> patcher = patch('__main__.thing', **config) >>> mock_thing = patcher.start() >>> mock_thing.method() 3 >>> mock_thing.other() Traceback (most recent call last): ... KeyError
By default, attempting to patch a function in a module (or a method or an
attribute ... | {'method.return_value': 3, 'other.side_effect': KeyError} >>> patcher = patch('__main__.thing', **config) >>> mock_thing = patcher.start() >>> mock_thing.method() 3 >>> mock_thing.other() Traceback (most recent call last): ... KeyError
By default, attempting to patch a function in a module (or a method or an
attribute ... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
cff0c0b0-3126-4ada-859b-187f6243191b | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,766 | supabase-export-v2 | 2fc6135ff6b76785 | or a mapping like container. If it is a mapping then it must at least support getting, setting and deleting items plus iterating over keys.
*in_dict* can also be a string specifying the name of the dictionary, which
will then be fetched by importing it. | trusted_official_docs | CPython Docs | or a mapping like container. If it is a mapping then it must at least support getting, setting and deleting items plus iterating over keys.
*in_dict* can also be a string specifying the name of the dictionary, which
will then be fetched by importing it. | or a mapping like container. If it is a mapping then it must at least support getting, setting and deleting items plus iterating over keys.
*in_dict* can also be a string specifying the name of the dictionary, which
will then be fetched by importing it. | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
d159a43a-8324-4fff-adf7-56879223820d | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,942 | supabase-export-v2 | 77094ecf66b00724 | .. versionchanged:: 3.5 *read_data* is now reset on each call to the *mock*.
.. versionchanged:: 3.8
Added :meth:`~container.__iter__` to implementation so that iteration
(such as in for loops) correctly consumes *read_data*. | trusted_official_docs | CPython Docs | .. versionchanged:: 3.5 *read_data* is now reset on each call to the *mock*.
.. versionchanged:: 3.8
Added :meth:`~container.__iter__` to implementation so that iteration
(such as in for loops) correctly consumes *read_data*. | .. versionchanged:: 3.5 *read_data* is now reset on each call to the *mock*.
.. versionchanged:: 3.8
Added :meth:`~container.__iter__` to implementation so that iteration
(such as in for loops) correctly consumes *read_data*. | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
d1646901-aeed-4146-a8a9-5a3f62165f62 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,645 | supabase-export-v2 | ee9bfa42927ba12e | Assert the mock has been awaited with the specified calls. The :attr:`await_args_list` list is checked for the awaits.
If *any_order* is false then the awaits must be
sequential. There can be extra calls before or after the
specified awaits. | trusted_official_docs | CPython Docs | Assert the mock has been awaited with the specified calls. The :attr:`await_args_list` list is checked for the awaits.
If *any_order* is false then the awaits must be
sequential. There can be extra calls before or after the
specified awaits. | Assert the mock has been awaited with the specified calls. The :attr:`await_args_list` list is checked for the awaits.
If *any_order* is false then the awaits must be
sequential. There can be extra calls before or after the
specified awaits. | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
d2a18490-aad1-49f7-b557-1c82e288c0e0 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,733 | supabase-export-v2 | d50ac5c209db48a1 | >>> @patch('__main__.SomeClass') ... def function(normal_argument, mock_class): ... print(mock_class is SomeClass) ... >>> function(None) True
Patching a class replaces the class with a :class:`MagicMock` *instance*. If the
class is instantiated in the code under test then it will be the
:attr:`~Mock.return_value` of t... | trusted_official_docs | CPython Docs | >>> @patch('__main__.SomeClass') ... def function(normal_argument, mock_class): ... print(mock_class is SomeClass) ... >>> function(None) True
Patching a class replaces the class with a :class:`MagicMock` *instance*. If the
class is instantiated in the code under test then it will be the
:attr:`~Mock.return_value` of t... | >>> @patch('__main__.SomeClass') ... def function(normal_argument, mock_class): ... print(mock_class is SomeClass) ... >>> function(None) True
Patching a class replaces the class with a :class:`MagicMock` *instance*. If the
class is instantiated in the code under test then it will be the
:attr:`~Mock.return_value` of t... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
d2c7e77d-f01f-4aee-8f77-2b2ade7a06af | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,797 | supabase-export-v2 | 2c1b07481730b8a7 | If :func:`patch.multiple` is used as a context manager, the value returned by the context manager is a dictionary where created mocks are keyed by name::
>>> with patch.multiple('__main__', thing=DEFAULT, other=DEFAULT) as values:
... assert 'other' in repr(values['other'])
... assert 'thing' in repr(values['thing'])... | trusted_official_docs | CPython Docs | If :func:`patch.multiple` is used as a context manager, the value returned by the context manager is a dictionary where created mocks are keyed by name::
>>> with patch.multiple('__main__', thing=DEFAULT, other=DEFAULT) as values:
... assert 'other' in repr(values['other'])
... assert 'thing' in repr(values['thing'])... | If :func:`patch.multiple` is used as a context manager, the value returned by the context manager is a dictionary where created mocks are keyed by name::
>>> with patch.multiple('__main__', thing=DEFAULT, other=DEFAULT) as values:
... assert 'other' in repr(values['other'])
... assert 'thing' in repr(values['thing'])... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
d43201d1-9b66-4026-878e-70e23c577e01 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,606 | supabase-export-v2 | cdb7176c5af08991 | def f(a, b, c): pass ... >>> mock = Mock(spec=f) >>> mock(1, 2, c=3) <Mock name='mock()' id='140161580456576'> >>> mock.assert_called_with(1, 2, 3) >>> mock.assert_called_with(a=1, b=2, c=3)
This applies to :meth:`~Mock.assert_called_with`,
:meth:`~Mock.assert_called_once_with`, :meth:`~Mock.assert_has_calls` and
:meth... | trusted_official_docs | CPython Docs | def f(a, b, c): pass ... >>> mock = Mock(spec=f) >>> mock(1, 2, c=3) <Mock name='mock()' id='140161580456576'> >>> mock.assert_called_with(1, 2, 3) >>> mock.assert_called_with(a=1, b=2, c=3)
This applies to :meth:`~Mock.assert_called_with`,
:meth:`~Mock.assert_called_once_with`, :meth:`~Mock.assert_has_calls` and
:meth... | def f(a, b, c): pass ... >>> mock = Mock(spec=f) >>> mock(1, 2, c=3) <Mock name='mock()' id='140161580456576'> >>> mock.assert_called_with(1, 2, 3) >>> mock.assert_called_with(a=1, b=2, c=3)
This applies to :meth:`~Mock.assert_called_with`,
:meth:`~Mock.assert_called_once_with`, :meth:`~Mock.assert_has_calls` and
:meth... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
d448866c-806f-4fed-a66c-c6d655449d77 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,677 | supabase-export-v2 | dc0612731314bd48 | Calling ~~~~~~~
Mock objects are callable. The call will return the value set as the
:attr:`~Mock.return_value` attribute. The default return value is a new Mock
object; it is created the first time the return value is accessed (either
explicitly or by calling the Mock) - but it is stored and the same one
returned each... | trusted_official_docs | CPython Docs | Calling ~~~~~~~
Mock objects are callable. The call will return the value set as the
:attr:`~Mock.return_value` attribute. The default return value is a new Mock
object; it is created the first time the return value is accessed (either
explicitly or by calling the Mock) - but it is stored and the same one
returned each... | Calling ~~~~~~~
Mock objects are callable. The call will return the value set as the
:attr:`~Mock.return_value` attribute. The default return value is a new Mock
object; it is created the first time the return value is accessed (either
explicitly or by calling the Mock) - but it is stored and the same one
returned each... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
d4f4b643-9a3f-43de-a6d0-2ca52bf3fc7f | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,862 | supabase-export-v2 | 50420b34ec4548f5 | of :class:`Mock` with default implementations of most of the :term:`magic methods <magic method>`. You can use ``MagicMock`` without having to configure the magic methods yourself.
The constructor parameters have the same meaning as for :class:`Mock`. | trusted_official_docs | CPython Docs | of :class:`Mock` with default implementations of most of the :term:`magic methods <magic method>`. You can use ``MagicMock`` without having to configure the magic methods yourself.
The constructor parameters have the same meaning as for :class:`Mock`. | of :class:`Mock` with default implementations of most of the :term:`magic methods <magic method>`. You can use ``MagicMock`` without having to configure the magic methods yourself.
The constructor parameters have the same meaning as for :class:`Mock`. | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
d6c1edf3-c9e7-448d-8a8c-f25947571a4a | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,756 | supabase-export-v2 | 16b52599be85ee40 | patch the named member (*attribute*) on an object (*target*) with a mock object.
:func:`patch.object` can be used as a decorator, class decorator or a context
manager. Arguments *new*, *spec*, *create*, *spec_set*, *autospec* and
*new_callable* have the same meaning as for :func:`patch`. Like :func:`patch`,
:func:`p... | trusted_official_docs | CPython Docs | patch the named member (*attribute*) on an object (*target*) with a mock object.
:func:`patch.object` can be used as a decorator, class decorator or a context
manager. Arguments *new*, *spec*, *create*, *spec_set*, *autospec* and
*new_callable* have the same meaning as for :func:`patch`. Like :func:`patch`,
:func:`p... | patch the named member (*attribute*) on an object (*target*) with a mock object.
:func:`patch.object` can be used as a decorator, class decorator or a context
manager. Arguments *new*, *spec*, *create*, *spec_set*, *autospec* and
*new_callable* have the same meaning as for :func:`patch`. Like :func:`patch`,
:func:`p... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
dac83713-a9fa-4e8f-b858-2274955d1f2d | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,504 | supabase-export-v2 | e1127d6fb6fd7775 | assert the mock has been called with the specified calls. The :attr:`mock_calls` list is checked for the calls.
If *any_order* is false then the calls must be
sequential. There can be extra calls before or after the
specified calls. | trusted_official_docs | CPython Docs | assert the mock has been called with the specified calls. The :attr:`mock_calls` list is checked for the calls.
If *any_order* is false then the calls must be
sequential. There can be extra calls before or after the
specified calls. | assert the mock has been called with the specified calls. The :attr:`mock_calls` list is checked for the calls.
If *any_order* is false then the calls must be
sequential. There can be extra calls before or after the
specified calls. | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
dbe7bc1e-692a-4522-9c48-ff65d4fc6883 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,605 | supabase-export-v2 | b6aec58ed8a15386 | signature when matching calls to the mock. Therefore, it can match the actual call's arguments regardless of whether they were passed positionally or by name::
>>> def f(a, b, c): pass
... >>> mock = Mock(spec=f)
>>> mock(1, 2, c=3)
<Mock name='mock()' id='140161580456576'>
>>> mock.assert_called_with(1, 2, 3)
>>>... | trusted_official_docs | CPython Docs | signature when matching calls to the mock. Therefore, it can match the actual call's arguments regardless of whether they were passed positionally or by name::
>>> def f(a, b, c): pass
... >>> mock = Mock(spec=f)
>>> mock(1, 2, c=3)
<Mock name='mock()' id='140161580456576'>
>>> mock.assert_called_with(1, 2, 3)
>>>... | signature when matching calls to the mock. Therefore, it can match the actual call's arguments regardless of whether they were passed positionally or by name::
>>> def f(a, b, c): pass
... >>> mock = Mock(spec=f)
>>> mock(1, 2, c=3)
<Mock name='mock()' id='140161580456576'>
>>> mock.assert_called_with(1, 2, 3)
>>>... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
dc09aea1-08f1-4ed7-9f24-f8c39c819d49 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,867 | supabase-export-v2 | e52e1a20ba421127 | The constructor parameters have the same meaning as for :class:`MagicMock`, with the exception of *return_value* and *side_effect* which have no meaning on a non-callable mock.
The magic methods are setup with :class:`MagicMock` objects, so you can configure them
and use them in the usual way: | trusted_official_docs | CPython Docs | The constructor parameters have the same meaning as for :class:`MagicMock`, with the exception of *return_value* and *side_effect* which have no meaning on a non-callable mock.
The magic methods are setup with :class:`MagicMock` objects, so you can configure them
and use them in the usual way: | The constructor parameters have the same meaning as for :class:`MagicMock`, with the exception of *return_value* and *side_effect* which have no meaning on a non-callable mock.
The magic methods are setup with :class:`MagicMock` objects, so you can configure them
and use them in the usual way: | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
dc0e0373-3c68-437a-8868-21faca4f8c36 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,698 | supabase-export-v2 | 1f4ee145f3b96e6b | MagicMock() >>> hasattr(mock, 'm') True >>> del mock.m >>> hasattr(mock, 'm') False >>> del mock.f >>> mock.f Traceback (most recent call last): ... AttributeError: f
Mock names and the name attribute
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | trusted_official_docs | CPython Docs | MagicMock() >>> hasattr(mock, 'm') True >>> del mock.m >>> hasattr(mock, 'm') False >>> del mock.f >>> mock.f Traceback (most recent call last): ... AttributeError: f
Mock names and the name attribute
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | MagicMock() >>> hasattr(mock, 'm') True >>> del mock.m >>> hasattr(mock, 'm') False >>> del mock.f >>> mock.f Traceback (most recent call last): ... AttributeError: f
Mock names and the name attribute
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
dc45e654-d700-4b2e-a0e0-b7d33366b752 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,600 | supabase-export-v2 | 356ad897ec869f8d | The :class:`Mock` classes have support for mocking magic methods. See :ref:`magic methods <magic-methods>` for the full details.
The mock classes and the :func:`patch` decorators all take arbitrary keyword
arguments for configuration. For the :func:`patch` decorators the keywords are
passed to the constructor of the mo... | trusted_official_docs | CPython Docs | The :class:`Mock` classes have support for mocking magic methods. See :ref:`magic methods <magic-methods>` for the full details.
The mock classes and the :func:`patch` decorators all take arbitrary keyword
arguments for configuration. For the :func:`patch` decorators the keywords are
passed to the constructor of the mo... | The :class:`Mock` classes have support for mocking magic methods. See :ref:`magic methods <magic-methods>` for the full details.
The mock classes and the :func:`patch` decorators all take arbitrary keyword
arguments for configuration. For the :func:`patch` decorators the keywords are
passed to the constructor of the mo... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
dcb27856-e0db-487d-bcd2-fc5dfdbc2e7f | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,662 | supabase-export-v2 | a02e7a9e9dd9a59e | mock = AsyncMock() >>> async def main(*args): ... await mock(*args) ... >>> mock.await_args_list [] >>> asyncio.run(main('foo')) >>> mock.await_args_list [call('foo')] >>> asyncio.run(main('bar')) >>> mock.await_args_list [call('foo'), call('bar')]
.. class:: ThreadingMock(spec=None, side_effect=None, return_value=DEFA... | trusted_official_docs | CPython Docs | mock = AsyncMock() >>> async def main(*args): ... await mock(*args) ... >>> mock.await_args_list [] >>> asyncio.run(main('foo')) >>> mock.await_args_list [call('foo')] >>> asyncio.run(main('bar')) >>> mock.await_args_list [call('foo'), call('bar')]
.. class:: ThreadingMock(spec=None, side_effect=None, return_value=DEFA... | mock = AsyncMock() >>> async def main(*args): ... await mock(*args) ... >>> mock.await_args_list [] >>> asyncio.run(main('foo')) >>> mock.await_args_list [call('foo')] >>> asyncio.run(main('bar')) >>> mock.await_args_list [call('foo'), call('bar')]
.. class:: ThreadingMock(spec=None, side_effect=None, return_value=DEFA... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
dd5806ef-01c6-482f-a786-74451efd739c | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,708 | supabase-export-v2 | ae34df363831e9ac | >>> mock = MagicMock() >>> not_a_child = MagicMock(name='not-a-child') >>> mock.attribute = not_a_child >>> mock.attribute() <MagicMock name='not-a-child()' id='...'> >>> mock.mock_calls []
Mocks created for you by :func:`patch` are automatically given names. To
attach mocks that have names to a parent you use the :met... | trusted_official_docs | CPython Docs | >>> mock = MagicMock() >>> not_a_child = MagicMock(name='not-a-child') >>> mock.attribute = not_a_child >>> mock.attribute() <MagicMock name='not-a-child()' id='...'> >>> mock.mock_calls []
Mocks created for you by :func:`patch` are automatically given names. To
attach mocks that have names to a parent you use the :met... | >>> mock = MagicMock() >>> not_a_child = MagicMock(name='not-a-child') >>> mock.attribute = not_a_child >>> mock.attribute() <MagicMock name='not-a-child()' id='...'> >>> mock.mock_calls []
Mocks created for you by :func:`patch` are automatically given names. To
attach mocks that have names to a parent you use the :met... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
ddbc8420-0357-4623-8e16-e81c27070cab | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,623 | supabase-export-v2 | 9a08d29176e277bc | ``side_effect`` is not defined, the async function will return the value defined by ``return_value``, hence, by default, the async function returns a new :class:`AsyncMock` object.
Setting the *spec* of a :class:`Mock` or :class:`MagicMock` to an async function
will result in a coroutine object being returned after ca... | trusted_official_docs | CPython Docs | ``side_effect`` is not defined, the async function will return the value defined by ``return_value``, hence, by default, the async function returns a new :class:`AsyncMock` object.
Setting the *spec* of a :class:`Mock` or :class:`MagicMock` to an async function
will result in a coroutine object being returned after ca... | ``side_effect`` is not defined, the async function will return the value defined by ``return_value``, hence, by default, the async function returns a new :class:`AsyncMock` object.
Setting the *spec* of a :class:`Mock` or :class:`MagicMock` to an async function
will result in a coroutine object being returned after ca... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
de7c3fce-53ef-4efc-8c5a-6bd575ffdd89 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,607 | supabase-export-v2 | 22ddf342c4cb7436 | This applies to :meth:`~Mock.assert_called_with`, :meth:`~Mock.assert_called_once_with`, :meth:`~Mock.assert_has_calls` and :meth:`~Mock.assert_any_call`. When :ref:`auto-speccing`, it will also apply to method calls on the mock object.
.. versionchanged:: 3.4
Added signature introspection on specced and autospecced m... | trusted_official_docs | CPython Docs | This applies to :meth:`~Mock.assert_called_with`, :meth:`~Mock.assert_called_once_with`, :meth:`~Mock.assert_has_calls` and :meth:`~Mock.assert_any_call`. When :ref:`auto-speccing`, it will also apply to method calls on the mock object.
.. versionchanged:: 3.4
Added signature introspection on specced and autospecced m... | This applies to :meth:`~Mock.assert_called_with`, :meth:`~Mock.assert_called_once_with`, :meth:`~Mock.assert_has_calls` and :meth:`~Mock.assert_any_call`. When :ref:`auto-speccing`, it will also apply to method calls on the mock object.
.. versionchanged:: 3.4
Added signature introspection on specced and autospecced m... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
dede8a1f-3bc1-46c1-ae89-20f8f84b4f1e | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,686 | supabase-export-v2 | f76b5a8a0cf5ea8d | m.side_effect = side_effect >>> m.return_value = 3 >>> m() 3 >>> def side_effect(*args, **kwargs): ... return DEFAULT ... >>> m.side_effect = side_effect >>> m() 3
To remove a :attr:`~Mock.side_effect`, and return to the default behaviour, set the
:attr:`!side_effect` to ``None``: | trusted_official_docs | CPython Docs | m.side_effect = side_effect >>> m.return_value = 3 >>> m() 3 >>> def side_effect(*args, **kwargs): ... return DEFAULT ... >>> m.side_effect = side_effect >>> m() 3
To remove a :attr:`~Mock.side_effect`, and return to the default behaviour, set the
:attr:`!side_effect` to ``None``: | m.side_effect = side_effect >>> m.return_value = 3 >>> m() 3 >>> def side_effect(*args, **kwargs): ... return DEFAULT ... >>> m.side_effect = side_effect >>> m() 3
To remove a :attr:`~Mock.side_effect`, and return to the default behaviour, set the
:attr:`!side_effect` to ``None``: | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
df1aec5a-d193-4450-b149-04c55bbf3ae9 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,668 | supabase-export-v2 | 73f697c6fc98f9d9 | Waits until the mock is called.
If a timeout was passed at the creation of the mock or if a timeout
argument is passed to this function, the function raises an
:exc:`AssertionError` if the call is not performed in time. | trusted_official_docs | CPython Docs | Waits until the mock is called.
If a timeout was passed at the creation of the mock or if a timeout
argument is passed to this function, the function raises an
:exc:`AssertionError` if the call is not performed in time. | Waits until the mock is called.
If a timeout was passed at the creation of the mock or if a timeout
argument is passed to this function, the function raises an
:exc:`AssertionError` if the call is not performed in time. | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
dfb018e2-afd2-4238-a01e-4d64bf4fba7f | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 27,016 | supabase-export-v2 | 39fcc4cb4d31fbea | If :attr:`~Mock.side_effect` is exhausted, the order of precedence will not cause a value to be obtained from the successors. Instead, ``StopIteration`` exception is raised.
>>> order_mock = Mock(spec=Order, wraps=Order)
>>> order_mock.get_value.side_effect = ["first side effect value",
... "another side effect value... | trusted_official_docs | CPython Docs | If :attr:`~Mock.side_effect` is exhausted, the order of precedence will not cause a value to be obtained from the successors. Instead, ``StopIteration`` exception is raised.
>>> order_mock = Mock(spec=Order, wraps=Order)
>>> order_mock.get_value.side_effect = ["first side effect value",
... "another side effect value... | If :attr:`~Mock.side_effect` is exhausted, the order of precedence will not cause a value to be obtained from the successors. Instead, ``StopIteration`` exception is raised.
>>> order_mock = Mock(spec=Order, wraps=Order)
>>> order_mock.get_value.side_effect = ["first side effect value",
... "another side effect value... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
dfebfced-ccf0-4814-84a4-534762d6826b | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,781 | supabase-export-v2 | 6dd17a422616870d | Keywords can be used in the :func:`patch.dict` call to set values in the dictionary:
>>> mymodule = MagicMock()
>>> mymodule.function.return_value = 'fish'
>>> with patch.dict('sys.modules', mymodule=mymodule):
... import mymodule
... mymodule.function('some', 'args')
... 'fish' | trusted_official_docs | CPython Docs | Keywords can be used in the :func:`patch.dict` call to set values in the dictionary:
>>> mymodule = MagicMock()
>>> mymodule.function.return_value = 'fish'
>>> with patch.dict('sys.modules', mymodule=mymodule):
... import mymodule
... mymodule.function('some', 'args')
... 'fish' | Keywords can be used in the :func:`patch.dict` call to set values in the dictionary:
>>> mymodule = MagicMock()
>>> mymodule.function.return_value = 'fish'
>>> with patch.dict('sys.modules', mymodule=mymodule):
... import mymodule
... mymodule.function('some', 'args')
... 'fish' | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
e0f93f90-337f-4003-b7d4-5ab50cda8d63 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,687 | supabase-export-v2 | d01f610ed401576e | To remove a :attr:`~Mock.side_effect`, and return to the default behaviour, set the :attr:`!side_effect` to ``None``:
>>> m = MagicMock(return_value=6)
>>> def side_effect(*args, **kwargs):
... return 3
... >>> m.side_effect = side_effect
>>> m()
3
>>> m.side_effect = None
>>> m()
6 | trusted_official_docs | CPython Docs | To remove a :attr:`~Mock.side_effect`, and return to the default behaviour, set the :attr:`!side_effect` to ``None``:
>>> m = MagicMock(return_value=6)
>>> def side_effect(*args, **kwargs):
... return 3
... >>> m.side_effect = side_effect
>>> m()
3
>>> m.side_effect = None
>>> m()
6 | To remove a :attr:`~Mock.side_effect`, and return to the default behaviour, set the :attr:`!side_effect` to ``None``:
>>> m = MagicMock(return_value=6)
>>> def side_effect(*args, **kwargs):
... return 3
... >>> m.side_effect = side_effect
>>> m()
3
>>> m.side_effect = None
>>> m()
6 | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
e1c1f9a7-be1b-4b87-85d4-91253e03a8b9 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,943 | supabase-export-v2 | be389acbbb1e64f9 | .. versionchanged:: 3.8 Added :meth:`~container.__iter__` to implementation so that iteration (such as in for loops) correctly consumes *read_data*.
Using :func:`open` as a context manager is a great way to ensure your file handles
are closed properly and is becoming common:: | trusted_official_docs | CPython Docs | .. versionchanged:: 3.8 Added :meth:`~container.__iter__` to implementation so that iteration (such as in for loops) correctly consumes *read_data*.
Using :func:`open` as a context manager is a great way to ensure your file handles
are closed properly and is becoming common:: | .. versionchanged:: 3.8 Added :meth:`~container.__iter__` to implementation so that iteration (such as in for loops) correctly consumes *read_data*.
Using :func:`open` as a context manager is a great way to ensure your file handles
are closed properly and is becoming common:: | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
e25f712a-6ff8-4650-bda9-a4c3be33e82d | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,818 | supabase-export-v2 | 4e8792902ae09cb5 | The patchers recognise methods that start with ``'test'`` as being test methods. This is the same way that the :class:`unittest.TestLoader` finds test methods by default.
It is possible that you want to use a different prefix for your tests. You can
inform the patchers of the different prefix by setting ``patch.TEST_PR... | trusted_official_docs | CPython Docs | The patchers recognise methods that start with ``'test'`` as being test methods. This is the same way that the :class:`unittest.TestLoader` finds test methods by default.
It is possible that you want to use a different prefix for your tests. You can
inform the patchers of the different prefix by setting ``patch.TEST_PR... | The patchers recognise methods that start with ``'test'`` as being test methods. This is the same way that the :class:`unittest.TestLoader` finds test methods by default.
It is possible that you want to use a different prefix for your tests. You can
inform the patchers of the different prefix by setting ``patch.TEST_PR... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
e2b35108-f0e0-461d-9285-d9aac90c9898 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,807 | supabase-export-v2 | faa7a3b0befb1390 | .. caution::
If you use this technique you must ensure that the patching is "undone" by
calling ``stop``. This can be fiddlier than you might think, because if an
exception is raised in the ``setUp`` then ``tearDown`` is not called. :meth:`unittest.TestCase.addCleanup` makes this easier:: | trusted_official_docs | CPython Docs | .. caution::
If you use this technique you must ensure that the patching is "undone" by
calling ``stop``. This can be fiddlier than you might think, because if an
exception is raised in the ``setUp`` then ``tearDown`` is not called. :meth:`unittest.TestCase.addCleanup` makes this easier:: | .. caution::
If you use this technique you must ensure that the patching is "undone" by
calling ``stop``. This can be fiddlier than you might think, because if an
exception is raised in the ``setUp`` then ``tearDown`` is not called. :meth:`unittest.TestCase.addCleanup` makes this easier:: | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
e305e623-48dd-4f8b-a9ff-efa406998edb | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,599 | supabase-export-v2 | 9808803b153d389d | >>> mock = Mock(spec=SomeClass) >>> isinstance(mock, SomeClass) True >>> mock = Mock(spec_set=SomeClass()) >>> isinstance(mock, SomeClass) True
The :class:`Mock` classes have support for mocking magic methods. See :ref:`magic
methods <magic-methods>` for the full details. | trusted_official_docs | CPython Docs | >>> mock = Mock(spec=SomeClass) >>> isinstance(mock, SomeClass) True >>> mock = Mock(spec_set=SomeClass()) >>> isinstance(mock, SomeClass) True
The :class:`Mock` classes have support for mocking magic methods. See :ref:`magic
methods <magic-methods>` for the full details. | >>> mock = Mock(spec=SomeClass) >>> isinstance(mock, SomeClass) True >>> mock = Mock(spec_set=SomeClass()) >>> isinstance(mock, SomeClass) True
The :class:`Mock` classes have support for mocking magic methods. See :ref:`magic
methods <magic-methods>` for the full details. | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
e3c2bf20-8d27-46fb-9c21-ef1d8dbf9745 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,802 | supabase-export-v2 | 5ce2f8c5c8855cbf | and keep a reference to the returned ``patcher`` object. You can then call :meth:`!start` to put the patch in place and :meth:`!stop` to undo it.
If you are using :func:`patch` to create a mock for you then it will be returned by
the call to ``patcher.start``. :: | trusted_official_docs | CPython Docs | and keep a reference to the returned ``patcher`` object. You can then call :meth:`!start` to put the patch in place and :meth:`!stop` to undo it.
If you are using :func:`patch` to create a mock for you then it will be returned by
the call to ``patcher.start``. :: | and keep a reference to the returned ``patcher`` object. You can then call :meth:`!start` to put the patch in place and :meth:`!stop` to undo it.
If you are using :func:`patch` to create a mock for you then it will be returned by
the call to ``patcher.start``. :: | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
e4040227-e911-4da5-871e-205d6bdd4b5a | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,611 | supabase-export-v2 | a71bb92680207b14 | Fetching a :class:`PropertyMock` instance from an object calls the mock, with no args. Setting it calls the mock with the value being set. ::
>>> class Foo:
... @property
... def foo(self):
... return 'something'
... @foo.setter
... def foo(self, value):
... pass
... >>> with patch('__main__.Foo.foo', new_callab... | trusted_official_docs | CPython Docs | Fetching a :class:`PropertyMock` instance from an object calls the mock, with no args. Setting it calls the mock with the value being set. ::
>>> class Foo:
... @property
... def foo(self):
... return 'something'
... @foo.setter
... def foo(self, value):
... pass
... >>> with patch('__main__.Foo.foo', new_callab... | Fetching a :class:`PropertyMock` instance from an object calls the mock, with no args. Setting it calls the mock with the value being set. ::
>>> class Foo:
... @property
... def foo(self):
... return 'something'
... @foo.setter
... def foo(self, value):
... pass
... >>> with patch('__main__.Foo.foo', new_callab... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
e56798f1-e94a-4696-bc9b-ac5644fdce75 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,952 | supabase-export-v2 | 623d272ac6753d05 | spec. In addition mocked functions / methods have the same call signature as the original so they raise a :exc:`TypeError` if they are called incorrectly.
Before I explain how auto-speccing works, here's why it is needed. | trusted_official_docs | CPython Docs | spec. In addition mocked functions / methods have the same call signature as the original so they raise a :exc:`TypeError` if they are called incorrectly.
Before I explain how auto-speccing works, here's why it is needed. | spec. In addition mocked functions / methods have the same call signature as the original so they raise a :exc:`TypeError` if they are called incorrectly.
Before I explain how auto-speccing works, here's why it is needed. | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
e76a8fd6-44be-4e36-a9cd-54ec4330674c | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,910 | supabase-export-v2 | 58345531156e6009 | a mock object using another object as a spec. Attributes on the mock will use the corresponding attribute on the *spec* object as their spec.
Functions or methods being mocked will have their arguments checked to
ensure that they are called with the correct signature. | trusted_official_docs | CPython Docs | a mock object using another object as a spec. Attributes on the mock will use the corresponding attribute on the *spec* object as their spec.
Functions or methods being mocked will have their arguments checked to
ensure that they are called with the correct signature. | a mock object using another object as a spec. Attributes on the mock will use the corresponding attribute on the *spec* object as their spec.
Functions or methods being mocked will have their arguments checked to
ensure that they are called with the correct signature. | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
e76e1d3e-5297-4d80-be58-dc55d7d8bb2d | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,456 | supabase-export-v2 | 7e49beefe78abd61 | >>> mock = MagicMock() >>> mock.__str__.return_value = 'foobarbaz' >>> str(mock) 'foobarbaz' >>> mock.__str__.assert_called_with()
Mock allows you to assign functions (or other Mock instances) to magic methods
and they will be called appropriately. The :class:`MagicMock` class is just a Mock
variant that has all of the... | trusted_official_docs | CPython Docs | >>> mock = MagicMock() >>> mock.__str__.return_value = 'foobarbaz' >>> str(mock) 'foobarbaz' >>> mock.__str__.assert_called_with()
Mock allows you to assign functions (or other Mock instances) to magic methods
and they will be called appropriately. The :class:`MagicMock` class is just a Mock
variant that has all of the... | >>> mock = MagicMock() >>> mock.__str__.return_value = 'foobarbaz' >>> str(mock) 'foobarbaz' >>> mock.__str__.assert_called_with()
Mock allows you to assign functions (or other Mock instances) to magic methods
and they will be called appropriately. The :class:`MagicMock` class is just a Mock
variant that has all of the... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
e80988ed-0f8c-4911-b4b6-ac3fe707e2b1 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,582 | supabase-export-v2 | 2c2c37c43bb8d043 | >>> mock = Mock() >>> mock.method() <Mock name='mock.method()' id='...'> >>> mock.property.method.attribute() <Mock name='mock.property.method.attribute()' id='...'> >>> mock.method_calls [call.method(), call.property.method.attribute()]
Members of :attr:`method_calls` are :data:`call` objects. These can be
unpacked a... | trusted_official_docs | CPython Docs | >>> mock = Mock() >>> mock.method() <Mock name='mock.method()' id='...'> >>> mock.property.method.attribute() <Mock name='mock.property.method.attribute()' id='...'> >>> mock.method_calls [call.method(), call.property.method.attribute()]
Members of :attr:`method_calls` are :data:`call` objects. These can be
unpacked a... | >>> mock = Mock() >>> mock.method() <Mock name='mock.method()' id='...'> >>> mock.property.method.attribute() <Mock name='mock.property.method.attribute()' id='...'> >>> mock.method_calls [call.method(), call.property.method.attribute()]
Members of :attr:`method_calls` are :data:`call` objects. These can be
unpacked a... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
e8b7acca-bede-4152-9a54-31fb8deede40 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,646 | supabase-export-v2 | ec9738e0ff296ecd | If *any_order* is false then the awaits must be sequential. There can be extra calls before or after the specified awaits.
If *any_order* is true then the awaits can be in any order, but
they must all appear in :attr:`await_args_list`. | trusted_official_docs | CPython Docs | If *any_order* is false then the awaits must be sequential. There can be extra calls before or after the specified awaits.
If *any_order* is true then the awaits can be in any order, but
they must all appear in :attr:`await_args_list`. | If *any_order* is false then the awaits must be sequential. There can be extra calls before or after the specified awaits.
If *any_order* is true then the awaits can be in any order, but
they must all appear in :attr:`await_args_list`. | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
ea242766-47da-4028-95fd-101fbac1ca3e | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,861 | supabase-export-v2 | ab5dd98e51339967 | .. class:: MagicMock(*args, **kw)
``MagicMock`` is a subclass of :class:`Mock` with default implementations
of most of the :term:`magic methods <magic method>`. You can use
``MagicMock`` without having to configure the magic methods yourself. | trusted_official_docs | CPython Docs | .. class:: MagicMock(*args, **kw)
``MagicMock`` is a subclass of :class:`Mock` with default implementations
of most of the :term:`magic methods <magic method>`. You can use
``MagicMock`` without having to configure the magic methods yourself. | .. class:: MagicMock(*args, **kw)
``MagicMock`` is a subclass of :class:`Mock` with default implementations
of most of the :term:`magic methods <magic method>`. You can use
``MagicMock`` without having to configure the magic methods yourself. | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
ea63c9ba-9874-4e22-bee0-88d3982704f1 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,672 | supabase-export-v2 | 97b9d9c6008f104a | Waits until the mock is called with the specified arguments.
If a timeout was passed at the creation of the mock
the function raises an :exc:`AssertionError` if the call is not performed in time. | trusted_official_docs | CPython Docs | Waits until the mock is called with the specified arguments.
If a timeout was passed at the creation of the mock
the function raises an :exc:`AssertionError` if the call is not performed in time. | Waits until the mock is called with the specified arguments.
If a timeout was passed at the creation of the mock
the function raises an :exc:`AssertionError` if the call is not performed in time. | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
eab500ef-758f-452f-9e96-c9de6a296a45 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 27,014 | supabase-export-v2 | 8b2830f08592f421 | Even if you set all three at once when initializing the mock, the order of precedence remains the same:
>>> order_mock = Mock(spec=Order, wraps=Order,
... **{"get_value.side_effect": ["first"],
... "get_value.return_value": "second"}
... )
... >>> order_mock.get_value()
'first'
>>> order_mock.get_value.side_effec... | trusted_official_docs | CPython Docs | Even if you set all three at once when initializing the mock, the order of precedence remains the same:
>>> order_mock = Mock(spec=Order, wraps=Order,
... **{"get_value.side_effect": ["first"],
... "get_value.return_value": "second"}
... )
... >>> order_mock.get_value()
'first'
>>> order_mock.get_value.side_effec... | Even if you set all three at once when initializing the mock, the order of precedence remains the same:
>>> order_mock = Mock(spec=Order, wraps=Order,
... **{"get_value.side_effect": ["first"],
... "get_value.return_value": "second"}
... )
... >>> order_mock.get_value()
'first'
>>> order_mock.get_value.side_effec... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
eb53570f-9e6a-415d-9675-70c3b8af5250 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,494 | supabase-export-v2 | 485e5a719c4b5eab | This method is a convenient way of asserting that the last call has been made in a particular way:
>>> mock = Mock()
>>> mock.method(1, 2, 3, test='wow')
<Mock name='mock.method()' id='...'>
>>> mock.method.assert_called_with(1, 2, 3, test='wow') | trusted_official_docs | CPython Docs | This method is a convenient way of asserting that the last call has been made in a particular way:
>>> mock = Mock()
>>> mock.method(1, 2, 3, test='wow')
<Mock name='mock.method()' id='...'>
>>> mock.method.assert_called_with(1, 2, 3, test='wow') | This method is a convenient way of asserting that the last call has been made in a particular way:
>>> mock = Mock()
>>> mock.method(1, 2, 3, test='wow')
<Mock name='mock.method()' id='...'>
>>> mock.method.assert_called_with(1, 2, 3, test='wow') | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
ebeaf97a-75aa-4de2-930a-071dba00f884 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,984 | supabase-export-v2 | 42dbe921dd00839e | class Something: a = 33
This brings up another issue. It is relatively common to provide a default
value of ``None`` for members that will later be an object of a different type. ``None`` would be useless as a spec because it wouldn't let you access *any*
attributes or methods on it. As ``None`` is *never* going to be ... | trusted_official_docs | CPython Docs | class Something: a = 33
This brings up another issue. It is relatively common to provide a default
value of ``None`` for members that will later be an object of a different type. ``None`` would be useless as a spec because it wouldn't let you access *any*
attributes or methods on it. As ``None`` is *never* going to be ... | class Something: a = 33
This brings up another issue. It is relatively common to provide a default
value of ``None`` for members that will later be an object of a different type. ``None`` would be useless as a spec because it wouldn't let you access *any*
attributes or methods on it. As ``None`` is *never* going to be ... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
ec1b0967-cecc-4f2b-a14c-e37b9e2eb8f3 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 27,002 | supabase-export-v2 | 37be05c0c374b93f | >>> order_mock.get_value.side_effect = None >>> order_mock.get_value() 'second'
If the value being returned by :attr:`~Mock.side_effect` is :data:`DEFAULT`,
it is ignored and the order of precedence moves to the successor to obtain the
value to return. | trusted_official_docs | CPython Docs | >>> order_mock.get_value.side_effect = None >>> order_mock.get_value() 'second'
If the value being returned by :attr:`~Mock.side_effect` is :data:`DEFAULT`,
it is ignored and the order of precedence moves to the successor to obtain the
value to return. | >>> order_mock.get_value.side_effect = None >>> order_mock.get_value() 'second'
If the value being returned by :attr:`~Mock.side_effect` is :data:`DEFAULT`,
it is ignored and the order of precedence moves to the successor to obtain the
value to return. | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
ec5bbee8-aafe-4d94-b5ca-f4574d0ecdb2 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,444 | supabase-export-v2 | 34334ebdc47c268d | mock.side_effect = side_effect >>> mock('a'), mock('b'), mock('c') (1, 2, 3) >>> mock.side_effect = [5, 4, 3, 2, 1] >>> mock(), mock(), mock() (5, 4, 3)
Mock has many other ways you can configure it and control its behaviour. For
example the *spec* argument configures the mock to take its specification
from another obj... | trusted_official_docs | CPython Docs | mock.side_effect = side_effect >>> mock('a'), mock('b'), mock('c') (1, 2, 3) >>> mock.side_effect = [5, 4, 3, 2, 1] >>> mock(), mock(), mock() (5, 4, 3)
Mock has many other ways you can configure it and control its behaviour. For
example the *spec* argument configures the mock to take its specification
from another obj... | mock.side_effect = side_effect >>> mock('a'), mock('b'), mock('c') (1, 2, 3) >>> mock.side_effect = [5, 4, 3, 2, 1] >>> mock(), mock(), mock() (5, 4, 3)
Mock has many other ways you can configure it and control its behaviour. For
example the *spec* argument configures the mock to take its specification
from another obj... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
ecda5158-413f-439b-8eb3-2e57644896bd | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,472 | supabase-export-v2 | f34de95044020866 | is an object (rather than a list of strings) then :attr:`~object.__class__` returns the class of the spec object. This allows mocks to pass :func:`isinstance` tests.
* *spec_set*: A stricter variant of *spec*. If used, attempting to *set*
or get an attribute on the mock that isn't on the object passed as
*spec_set* w... | trusted_official_docs | CPython Docs | is an object (rather than a list of strings) then :attr:`~object.__class__` returns the class of the spec object. This allows mocks to pass :func:`isinstance` tests.
* *spec_set*: A stricter variant of *spec*. If used, attempting to *set*
or get an attribute on the mock that isn't on the object passed as
*spec_set* w... | is an object (rather than a list of strings) then :attr:`~object.__class__` returns the class of the spec object. This allows mocks to pass :func:`isinstance` tests.
* *spec_set*: A stricter variant of *spec*. If used, attempting to *set*
or get an attribute on the mock that isn't on the object passed as
*spec_set* w... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
ed009a9b-e715-46bd-a111-37c4e731bd24 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,746 | supabase-export-v2 | b60510e9bd315feb | valid to pass in directly as keyword arguments, but a dictionary with these as keys can still be expanded into a :func:`patch` call using ``**``::
>>> config = {'method.return_value': 3, 'other.side_effect': KeyError}
>>> patcher = patch('__main__.thing', **config)
>>> mock_thing = patcher.start()
>>> mock_thing.met... | trusted_official_docs | CPython Docs | valid to pass in directly as keyword arguments, but a dictionary with these as keys can still be expanded into a :func:`patch` call using ``**``::
>>> config = {'method.return_value': 3, 'other.side_effect': KeyError}
>>> patcher = patch('__main__.thing', **config)
>>> mock_thing = patcher.start()
>>> mock_thing.met... | valid to pass in directly as keyword arguments, but a dictionary with these as keys can still be expanded into a :func:`patch` call using ``**``::
>>> config = {'method.return_value': 3, 'other.side_effect': KeyError}
>>> patcher = patch('__main__.thing', **config)
>>> mock_thing = patcher.start()
>>> mock_thing.met... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
ed72e97e-75f0-4dd6-b93b-28e014493d62 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,815 | supabase-export-v2 | a2fbd6aa35f4bf6a | patch builtins ~~~~~~~~~~~~~~ You can patch any builtins within a module. The following example patches builtin :func:`ord`::
>>> @patch('__main__.ord')
... def test(mock_ord):
... mock_ord.return_value = 101
... print(ord('c'))
... >>> test()
101 | trusted_official_docs | CPython Docs | patch builtins ~~~~~~~~~~~~~~ You can patch any builtins within a module. The following example patches builtin :func:`ord`::
>>> @patch('__main__.ord')
... def test(mock_ord):
... mock_ord.return_value = 101
... print(ord('c'))
... >>> test()
101 | patch builtins ~~~~~~~~~~~~~~ You can patch any builtins within a module. The following example patches builtin :func:`ord`::
>>> @patch('__main__.ord')
... def test(mock_ord):
... mock_ord.return_value = 101
... print(ord('c'))
... >>> test()
101 | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
ee3cf979-c0a4-4dbb-bbfa-8b44721e8e3b | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,985 | supabase-export-v2 | cd507816bc1a0c9b | of some other type, autospec doesn't use a spec for members that are set to ``None``. These will just be ordinary mocks (well - MagicMocks):
>>> class Something:
... member = None
... >>> mock = create_autospec(Something)
>>> mock.member.foo.bar.baz()
<MagicMock name='mock.member.foo.bar.baz()' id='...'> | trusted_official_docs | CPython Docs | of some other type, autospec doesn't use a spec for members that are set to ``None``. These will just be ordinary mocks (well - MagicMocks):
>>> class Something:
... member = None
... >>> mock = create_autospec(Something)
>>> mock.member.foo.bar.baz()
<MagicMock name='mock.member.foo.bar.baz()' id='...'> | of some other type, autospec doesn't use a spec for members that are set to ``None``. These will just be ordinary mocks (well - MagicMocks):
>>> class Something:
... member = None
... >>> mock = create_autospec(Something)
>>> mock.member.foo.bar.baz()
<MagicMock name='mock.member.foo.bar.baz()' id='...'> | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
eed37be6-70eb-4ec9-9016-5b8cc0b05f18 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,838 | supabase-export-v2 | fd9cafd0042ad634 | patch these on the *class* rather than an instance. They also work with *some* objects that proxy attribute access, like the `django settings object <https://web.archive.org/web/20200603181648/http://www.voidspace.org.uk/python/weblog/arch_d7_2010_12_04.shtml#e1198>`_.
MagicMock and magic method support
---------------... | trusted_official_docs | CPython Docs | patch these on the *class* rather than an instance. They also work with *some* objects that proxy attribute access, like the `django settings object <https://web.archive.org/web/20200603181648/http://www.voidspace.org.uk/python/weblog/arch_d7_2010_12_04.shtml#e1198>`_.
MagicMock and magic method support
---------------... | patch these on the *class* rather than an instance. They also work with *some* objects that proxy attribute access, like the `django settings object <https://web.archive.org/web/20200603181648/http://www.voidspace.org.uk/python/weblog/arch_d7_2010_12_04.shtml#e1198>`_.
MagicMock and magic method support
---------------... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
f0f9c282-c400-41bd-b77e-fc10d94f081d | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,962 | supabase-export-v2 | 6c5c7cb402497382 | >>> mock.header_items() <mock.Mock object at 0x...> >>> mock.header_items.assret_called_with() # Intentional typo!
Auto-speccing solves this problem. You can either pass ``autospec=True`` to
:func:`patch` / :func:`patch.object` or use the :func:`create_autospec` function to create a
mock with a spec. If you use the ``a... | trusted_official_docs | CPython Docs | >>> mock.header_items() <mock.Mock object at 0x...> >>> mock.header_items.assret_called_with() # Intentional typo!
Auto-speccing solves this problem. You can either pass ``autospec=True`` to
:func:`patch` / :func:`patch.object` or use the :func:`create_autospec` function to create a
mock with a spec. If you use the ``a... | >>> mock.header_items() <mock.Mock object at 0x...> >>> mock.header_items.assret_called_with() # Intentional typo!
Auto-speccing solves this problem. You can either pass ``autospec=True`` to
:func:`patch` / :func:`patch.object` or use the :func:`create_autospec` function to create a
mock with a spec. If you use the ``a... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
f128645c-3f64-4bb1-b7c1-50c8a05e7a4e | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,728 | supabase-export-v2 | 77542b9d6ba25a54 | that start with ``patch.TEST_PREFIX``. By default this is ``'test'``, which matches the way :mod:`unittest` finds tests. You can specify an alternative prefix by setting ``patch.TEST_PREFIX``.
Patch can be used as a context manager, with the with statement. Here the
patching applies to the indented block after the wit... | trusted_official_docs | CPython Docs | that start with ``patch.TEST_PREFIX``. By default this is ``'test'``, which matches the way :mod:`unittest` finds tests. You can specify an alternative prefix by setting ``patch.TEST_PREFIX``.
Patch can be used as a context manager, with the with statement. Here the
patching applies to the indented block after the wit... | that start with ``patch.TEST_PREFIX``. By default this is ``'test'``, which matches the way :mod:`unittest` finds tests. You can specify an alternative prefix by setting ``patch.TEST_PREFIX``.
Patch can be used as a context manager, with the with statement. Here the
patching applies to the indented block after the wit... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
f190329a-a5d7-430c-bd26-06abec94d342 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,790 | supabase-export-v2 | 9b8458ba5530d250 | arguments *spec*, *spec_set*, *create*, *autospec* and *new_callable* have the same meaning as for :func:`patch`. These arguments will be applied to *all* patches done by :func:`patch.multiple`.
When used as a class decorator :func:`patch.multiple` honours ``patch.TEST_PREFIX``
for choosing which methods to wrap. | trusted_official_docs | CPython Docs | arguments *spec*, *spec_set*, *create*, *autospec* and *new_callable* have the same meaning as for :func:`patch`. These arguments will be applied to *all* patches done by :func:`patch.multiple`.
When used as a class decorator :func:`patch.multiple` honours ``patch.TEST_PREFIX``
for choosing which methods to wrap. | arguments *spec*, *spec_set*, *create*, *autospec* and *new_callable* have the same meaning as for :func:`patch`. These arguments will be applied to *all* patches done by :func:`patch.multiple`.
When used as a class decorator :func:`patch.multiple` honours ``patch.TEST_PREFIX``
for choosing which methods to wrap. | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
f267544e-c018-4577-8446-e53e263d4303 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,842 | supabase-export-v2 | a05fc543c6cec063 | the Python protocol methods, also known as :term:`"magic methods" <magic method>`. This allows mock objects to replace containers or other objects that implement Python protocols.
Because magic methods are looked up differently from normal methods [#]_, this
support has been specially implemented. This means that only ... | trusted_official_docs | CPython Docs | the Python protocol methods, also known as :term:`"magic methods" <magic method>`. This allows mock objects to replace containers or other objects that implement Python protocols.
Because magic methods are looked up differently from normal methods [#]_, this
support has been specially implemented. This means that only ... | the Python protocol methods, also known as :term:`"magic methods" <magic method>`. This allows mock objects to replace containers or other objects that implement Python protocols.
Because magic methods are looked up differently from normal methods [#]_, this
support has been specially implemented. This means that only ... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
f2aaa2cd-b5b9-4019-8ff4-83282925830d | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,558 | supabase-export-v2 | c430f7010e9435eb | This can either be a function to be called when the mock is called, an iterable or an exception (class or instance) to be raised.
If you pass in a function it will be called with same arguments as the
mock and unless the function returns the :data:`DEFAULT` singleton the
call to the mock will then return whatever the... | trusted_official_docs | CPython Docs | This can either be a function to be called when the mock is called, an iterable or an exception (class or instance) to be raised.
If you pass in a function it will be called with same arguments as the
mock and unless the function returns the :data:`DEFAULT` singleton the
call to the mock will then return whatever the... | This can either be a function to be called when the mock is called, an iterable or an exception (class or instance) to be raised.
If you pass in a function it will be called with same arguments as the
mock and unless the function returns the :data:`DEFAULT` singleton the
call to the mock will then return whatever the... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
f34a3717-929a-4f3b-9415-a53de32a5bfb | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,700 | supabase-export-v2 | 62d64c346e8d9376 | object to have a "name" attribute you can't just pass it in at creation time. There are two alternatives. One option is to use :meth:`~Mock.configure_mock`::
>>> mock = MagicMock()
>>> mock.configure_mock(name='my_name')
>>> mock.name
'my_name' | trusted_official_docs | CPython Docs | object to have a "name" attribute you can't just pass it in at creation time. There are two alternatives. One option is to use :meth:`~Mock.configure_mock`::
>>> mock = MagicMock()
>>> mock.configure_mock(name='my_name')
>>> mock.name
'my_name' | object to have a "name" attribute you can't just pass it in at creation time. There are two alternatives. One option is to use :meth:`~Mock.configure_mock`::
>>> mock = MagicMock()
>>> mock.configure_mock(name='my_name')
>>> mock.name
'my_name' | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
f4493d1c-a0e5-4201-9f16-c9a4732fd171 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,481 | supabase-export-v2 | 018422d72025c88a | a Mock object that wraps the corresponding attribute of the wrapped object (so attempting to access an attribute that doesn't exist will raise an :exc:`AttributeError`).
If the mock has an explicit *return_value* set then calls are not passed
to the wrapped object and the *return_value* is returned instead. | trusted_official_docs | CPython Docs | a Mock object that wraps the corresponding attribute of the wrapped object (so attempting to access an attribute that doesn't exist will raise an :exc:`AttributeError`).
If the mock has an explicit *return_value* set then calls are not passed
to the wrapped object and the *return_value* is returned instead. | a Mock object that wraps the corresponding attribute of the wrapped object (so attempting to access an attribute that doesn't exist will raise an :exc:`AttributeError`).
If the mock has an explicit *return_value* set then calls are not passed
to the wrapped object and the *return_value* is returned instead. | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
f48295fd-02a2-4778-b944-920a2c27267b | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,505 | supabase-export-v2 | be518e98e87efcb3 | If *any_order* is false then the calls must be sequential. There can be extra calls before or after the specified calls.
If *any_order* is true then the calls can be in any order, but
they must all appear in :attr:`mock_calls`. | trusted_official_docs | CPython Docs | If *any_order* is false then the calls must be sequential. There can be extra calls before or after the specified calls.
If *any_order* is true then the calls can be in any order, but
they must all appear in :attr:`mock_calls`. | If *any_order* is false then the calls must be sequential. There can be extra calls before or after the specified calls.
If *any_order* is true then the calls can be in any order, but
they must all appear in :attr:`mock_calls`. | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
f4f75267-c6a5-47d6-a1f9-2143d24e05de | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,567 | supabase-export-v2 | 7b2cfc0e7b0c64c9 | :attr:`side_effect` can be set in the constructor. Here's an example that adds one to the value the mock is called with and returns it:
>>> side_effect = lambda value: value + 1
>>> mock = Mock(side_effect=side_effect)
>>> mock(3)
4
>>> mock(-8)
-7 | trusted_official_docs | CPython Docs | :attr:`side_effect` can be set in the constructor. Here's an example that adds one to the value the mock is called with and returns it:
>>> side_effect = lambda value: value + 1
>>> mock = Mock(side_effect=side_effect)
>>> mock(3)
4
>>> mock(-8)
-7 | :attr:`side_effect` can be set in the constructor. Here's an example that adds one to the value the mock is called with and returns it:
>>> side_effect = lambda value: value + 1
>>> mock = Mock(side_effect=side_effect)
>>> mock(3)
4
>>> mock(-8)
-7 | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
f4fde103-1324-4fd9-bc6a-4ebc19d7ee8b | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,998 | supabase-export-v2 | a11ecd3d6d7f326e | 1. :attr:`~Mock.side_effect` 2. :attr:`~Mock.return_value` 3. *wraps*
If all three are set, mock will return the value from :attr:`~Mock.side_effect`,
ignoring :attr:`~Mock.return_value` and the wrapped object altogether. If any
two are set, the one with the higher precedence will return the value. Regardless of the or... | trusted_official_docs | CPython Docs | 1. :attr:`~Mock.side_effect` 2. :attr:`~Mock.return_value` 3. *wraps*
If all three are set, mock will return the value from :attr:`~Mock.side_effect`,
ignoring :attr:`~Mock.return_value` and the wrapped object altogether. If any
two are set, the one with the higher precedence will return the value. Regardless of the or... | 1. :attr:`~Mock.side_effect` 2. :attr:`~Mock.return_value` 3. *wraps*
If all three are set, mock will return the value from :attr:`~Mock.side_effect`,
ignoring :attr:`~Mock.return_value` and the wrapped object altogether. If any
two are set, the one with the higher precedence will return the value. Regardless of the or... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
f549122f-82d2-43e0-b3ee-f985ff4ef26a | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,561 | supabase-export-v2 | 1635a4b0b144c373 | An example of a mock that raises an exception (to test exception handling of an API):
>>> mock = Mock()
>>> mock.side_effect = Exception('Boom!')
>>> mock()
Traceback (most recent call last):
... Exception: Boom! | trusted_official_docs | CPython Docs | An example of a mock that raises an exception (to test exception handling of an API):
>>> mock = Mock()
>>> mock.side_effect = Exception('Boom!')
>>> mock()
Traceback (most recent call last):
... Exception: Boom! | An example of a mock that raises an exception (to test exception handling of an API):
>>> mock = Mock()
>>> mock.side_effect = Exception('Boom!')
>>> mock()
Traceback (most recent call last):
... Exception: Boom! | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
f5bfa86d-81ac-40aa-a9f3-7d947427014c | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,824 | supabase-export-v2 | ac84e387ff503deb | is mock1 ... assert SomeClass.class_method is mock2 ... SomeClass.static_method('foo') ... SomeClass.class_method('bar') ... return mock1, mock2 ... >>> mock1, mock2 = test() >>> mock1.assert_called_once_with('foo') >>> mock2.assert_called_once_with('bar')
Note that the decorators are applied from the bottom upwards. T... | trusted_official_docs | CPython Docs | is mock1 ... assert SomeClass.class_method is mock2 ... SomeClass.static_method('foo') ... SomeClass.class_method('bar') ... return mock1, mock2 ... >>> mock1, mock2 = test() >>> mock1.assert_called_once_with('foo') >>> mock2.assert_called_once_with('bar')
Note that the decorators are applied from the bottom upwards. T... | is mock1 ... assert SomeClass.class_method is mock2 ... SomeClass.static_method('foo') ... SomeClass.class_method('bar') ... return mock1, mock2 ... >>> mock1, mock2 = test() >>> mock1.assert_called_once_with('foo') >>> mock2.assert_called_once_with('bar')
Note that the decorators are applied from the bottom upwards. T... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
f639f513-9e03-4737-9bf4-0beef07e50cf | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,794 | supabase-export-v2 | 299cbef703c47563 | >>> @patch.multiple('__main__', thing=DEFAULT, other=DEFAULT) ... def test_function(thing, other): ... assert isinstance(thing, MagicMock) ... assert isinstance(other, MagicMock) ... >>> test_function()
:func:`patch.multiple` can be nested with other ``patch`` decorators, but put arguments
passed by keyword *after* any... | trusted_official_docs | CPython Docs | >>> @patch.multiple('__main__', thing=DEFAULT, other=DEFAULT) ... def test_function(thing, other): ... assert isinstance(thing, MagicMock) ... assert isinstance(other, MagicMock) ... >>> test_function()
:func:`patch.multiple` can be nested with other ``patch`` decorators, but put arguments
passed by keyword *after* any... | >>> @patch.multiple('__main__', thing=DEFAULT, other=DEFAULT) ... def test_function(thing, other): ... assert isinstance(thing, MagicMock) ... assert isinstance(other, MagicMock) ... >>> test_function()
:func:`patch.multiple` can be nested with other ``patch`` decorators, but put arguments
passed by keyword *after* any... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
f647a726-9080-478a-afde-ea08ef35ef10 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,462 | supabase-export-v2 | 0d9972ee2262e108 | return_value='fishy') >>> mock_function(1, 2, 3) 'fishy' >>> mock_function.assert_called_once_with(1, 2, 3) >>> mock_function('wrong arguments') Traceback (most recent call last): ... TypeError: missing a required argument: 'b'
:func:`create_autospec` can also be used on classes, where it copies the signature of
the ``... | trusted_official_docs | CPython Docs | return_value='fishy') >>> mock_function(1, 2, 3) 'fishy' >>> mock_function.assert_called_once_with(1, 2, 3) >>> mock_function('wrong arguments') Traceback (most recent call last): ... TypeError: missing a required argument: 'b'
:func:`create_autospec` can also be used on classes, where it copies the signature of
the ``... | return_value='fishy') >>> mock_function(1, 2, 3) 'fishy' >>> mock_function.assert_called_once_with(1, 2, 3) >>> mock_function('wrong arguments') Traceback (most recent call last): ... TypeError: missing a required argument: 'b'
:func:`create_autospec` can also be used on classes, where it copies the signature of
the ``... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
f6aedecc-7eba-4b0b-83f6-62d400e4cdc4 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,883 | supabase-export-v2 | 045bd678071609a3 | * ``__subclasses__`` * ``__dir__`` * ``__format__`` * ``__get__``, ``__set__`` and ``__delete__`` * ``__reversed__`` and ``__missing__`` * ``__reduce__``, ``__reduce_ex__``, ``__getinitargs__``, ``__getnewargs__``, ``__getstate__`` and ``__setstate__`` * ``__getformat__``
.. [#] Magic methods *should* be looked up on t... | trusted_official_docs | CPython Docs | * ``__subclasses__`` * ``__dir__`` * ``__format__`` * ``__get__``, ``__set__`` and ``__delete__`` * ``__reversed__`` and ``__missing__`` * ``__reduce__``, ``__reduce_ex__``, ``__getinitargs__``, ``__getnewargs__``, ``__getstate__`` and ``__setstate__`` * ``__getformat__``
.. [#] Magic methods *should* be looked up on t... | * ``__subclasses__`` * ``__dir__`` * ``__format__`` * ``__get__``, ``__set__`` and ``__delete__`` * ``__reversed__`` and ``__missing__`` * ``__reduce__``, ``__reduce_ex__``, ``__getinitargs__``, ``__getnewargs__``, ``__getstate__`` and ``__setstate__`` * ``__getformat__``
.. [#] Magic methods *should* be looked up on t... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
f80c8b5e-df1f-4fa9-818b-992644ab5e2b | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,690 | supabase-export-v2 | 9136ee4c5b2e3683 | >>> m = MagicMock(side_effect=[1, 2, 3]) >>> m() 1 >>> m() 2 >>> m() 3 >>> m() Traceback (most recent call last): ... StopIteration
If any members of the iterable are exceptions they will be raised instead of
returned:: | trusted_official_docs | CPython Docs | >>> m = MagicMock(side_effect=[1, 2, 3]) >>> m() 1 >>> m() 2 >>> m() 3 >>> m() Traceback (most recent call last): ... StopIteration
If any members of the iterable are exceptions they will be raised instead of
returned:: | >>> m = MagicMock(side_effect=[1, 2, 3]) >>> m() 1 >>> m() 2 >>> m() 3 >>> m() Traceback (most recent call last): ... StopIteration
If any members of the iterable are exceptions they will be raised instead of
returned:: | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
f82036d1-e3dd-4856-839b-50f67206885c | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,722 | supabase-export-v2 | 00c90fedfba2ff4c | or callable object, that will be called to create the *new* object. By default :class:`AsyncMock` is used for async functions and :class:`MagicMock` for the rest.
A more powerful form of *spec* is *autospec*. If you set ``autospec=True``
then the mock will be created with a spec from the object being replaced. All att... | trusted_official_docs | CPython Docs | or callable object, that will be called to create the *new* object. By default :class:`AsyncMock` is used for async functions and :class:`MagicMock` for the rest.
A more powerful form of *spec* is *autospec*. If you set ``autospec=True``
then the mock will be created with a spec from the object being replaced. All att... | or callable object, that will be called to create the *new* object. By default :class:`AsyncMock` is used for async functions and :class:`MagicMock` for the rest.
A more powerful form of *spec* is *autospec*. If you set ``autospec=True``
then the mock will be created with a spec from the object being replaced. All att... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
f8ef3d9f-d64d-4725-a8d7-3ef25ca02fde | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,913 | supabase-export-v2 | 9de7cdbe192c186b | class as the spec for an instance object by passing ``instance=True``. The returned mock will only be callable if instances of the mock are callable.
:func:`create_autospec` also takes arbitrary keyword arguments that are passed to
the constructor of the created mock. | trusted_official_docs | CPython Docs | class as the spec for an instance object by passing ``instance=True``. The returned mock will only be callable if instances of the mock are callable.
:func:`create_autospec` also takes arbitrary keyword arguments that are passed to
the constructor of the created mock. | class as the spec for an instance object by passing ``instance=True``. The returned mock will only be callable if instances of the mock are callable.
:func:`create_autospec` also takes arbitrary keyword arguments that are passed to
the constructor of the created mock. | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
f9584135-81ca-4450-9a0b-73e4f0e33882 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,709 | supabase-export-v2 | 7940c21475d04a1a | Mocks created for you by :func:`patch` are automatically given names. To attach mocks that have names to a parent you use the :meth:`~Mock.attach_mock` method::
>>> thing1 = object()
>>> thing2 = object()
>>> parent = MagicMock()
>>> with patch('__main__.thing1', return_value=None) as child1:
... with patch('__main... | trusted_official_docs | CPython Docs | Mocks created for you by :func:`patch` are automatically given names. To attach mocks that have names to a parent you use the :meth:`~Mock.attach_mock` method::
>>> thing1 = object()
>>> thing2 = object()
>>> parent = MagicMock()
>>> with patch('__main__.thing1', return_value=None) as child1:
... with patch('__main... | Mocks created for you by :func:`patch` are automatically given names. To attach mocks that have names to a parent you use the :meth:`~Mock.attach_mock` method::
>>> thing1 = object()
>>> thing2 = object()
>>> parent = MagicMock()
>>> with patch('__main__.thing1', return_value=None) as child1:
... with patch('__main... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
fa47c2b3-d4c5-4dc4-a678-eceb4bc83771 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,712 | supabase-export-v2 | 156056c966b23381 | The patchers ------------
The patch decorators are used for patching objects only within the scope of
the function they decorate. They automatically handle the unpatching for you,
even if exceptions are raised. All of these functions can also be used in with
statements or as class decorators. | trusted_official_docs | CPython Docs | The patchers ------------
The patch decorators are used for patching objects only within the scope of
the function they decorate. They automatically handle the unpatching for you,
even if exceptions are raised. All of these functions can also be used in with
statements or as class decorators. | The patchers ------------
The patch decorators are used for patching objects only within the scope of
the function they decorate. They automatically handle the unpatching for you,
even if exceptions are raised. All of these functions can also be used in with
statements or as class decorators. | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
fbc5bd64-effe-422a-bace-cfacf1c2b618 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,459 | supabase-export-v2 | 0e975297277bb8f8 | >>> mock = Mock() >>> mock.__str__ = Mock(return_value='wheeeeee') >>> str(mock) 'wheeeeee'
For ensuring that the mock objects in your tests have the same api as the
objects they are replacing, you can use :ref:`auto-speccing <auto-speccing>`. Auto-speccing can be done through the *autospec* argument to patch, or the
:... | trusted_official_docs | CPython Docs | >>> mock = Mock() >>> mock.__str__ = Mock(return_value='wheeeeee') >>> str(mock) 'wheeeeee'
For ensuring that the mock objects in your tests have the same api as the
objects they are replacing, you can use :ref:`auto-speccing <auto-speccing>`. Auto-speccing can be done through the *autospec* argument to patch, or the
:... | >>> mock = Mock() >>> mock.__str__ = Mock(return_value='wheeeeee') >>> str(mock) 'wheeeeee'
For ensuring that the mock objects in your tests have the same api as the
objects they are replacing, you can use :ref:`auto-speccing <auto-speccing>`. Auto-speccing can be done through the *autospec* argument to patch, or the
:... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
fbe0a729-7dc1-4e2d-9db2-16ccc7a7c7a4 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,980 | supabase-export-v2 | 9b3285fa488da9a2 | setting non-existent attributes. This is useful if you want to ensure your code only *sets* valid attributes too, but obviously it prevents this particular scenario:
>>> with patch('__main__.Something', autospec=True, spec_set=True):
... thing = Something()
... thing.a = 33
... Traceback (most recent call last):
..... | trusted_official_docs | CPython Docs | setting non-existent attributes. This is useful if you want to ensure your code only *sets* valid attributes too, but obviously it prevents this particular scenario:
>>> with patch('__main__.Something', autospec=True, spec_set=True):
... thing = Something()
... thing.a = 33
... Traceback (most recent call last):
..... | setting non-existent attributes. This is useful if you want to ensure your code only *sets* valid attributes too, but obviously it prevents this particular scenario:
>>> with patch('__main__.Something', autospec=True, spec_set=True):
... thing = Something()
... thing.a = 33
... Traceback (most recent call last):
..... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
fc1d483b-cf77-49c8-8be9-a791fd103b66 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,782 | supabase-export-v2 | 2c1b534435a0c1eb | >>> mymodule = MagicMock() >>> mymodule.function.return_value = 'fish' >>> with patch.dict('sys.modules', mymodule=mymodule): ... import mymodule ... mymodule.function('some', 'args') ... 'fish'
:func:`patch.dict` can be used with dictionary like objects that aren't actually
dictionaries. At the very minimum they must ... | trusted_official_docs | CPython Docs | >>> mymodule = MagicMock() >>> mymodule.function.return_value = 'fish' >>> with patch.dict('sys.modules', mymodule=mymodule): ... import mymodule ... mymodule.function('some', 'args') ... 'fish'
:func:`patch.dict` can be used with dictionary like objects that aren't actually
dictionaries. At the very minimum they must ... | >>> mymodule = MagicMock() >>> mymodule.function.return_value = 'fish' >>> with patch.dict('sys.modules', mymodule=mymodule): ... import mymodule ... mymodule.function('some', 'args') ... 'fish'
:func:`patch.dict` can be used with dictionary like objects that aren't actually
dictionaries. At the very minimum they must ... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
fc2b048c-0d3d-46c7-9b03-e6949255ad71 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,993 | supabase-export-v2 | bf653147d04a5fe2 | to an attribute it won't be considered in the sealing chain. This allows one to prevent seal from fixing part of the mock object. ::
>>> mock = Mock()
>>> mock.submock.attribute1 = 2
>>> mock.not_submock = mock.Mock(name="sample_name")
>>> seal(mock)
>>> mock.new_attribute # This will raise AttributeError. >>> mock... | trusted_official_docs | CPython Docs | to an attribute it won't be considered in the sealing chain. This allows one to prevent seal from fixing part of the mock object. ::
>>> mock = Mock()
>>> mock.submock.attribute1 = 2
>>> mock.not_submock = mock.Mock(name="sample_name")
>>> seal(mock)
>>> mock.new_attribute # This will raise AttributeError. >>> mock... | to an attribute it won't be considered in the sealing chain. This allows one to prevent seal from fixing part of the mock object. ::
>>> mock = Mock()
>>> mock.submock.attribute1 = 2
>>> mock.not_submock = mock.Mock(name="sample_name")
>>> seal(mock)
>>> mock.new_attribute # This will raise AttributeError. >>> mock... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
fc7066e8-247f-4c94-b8ff-0faa86042e47 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,939 | supabase-export-v2 | 23591ffb631a0c00 | If ``None`` (the default) then a :class:`MagicMock` will be created for you, with the API limited to methods or attributes available on standard file handles.
*read_data* is a string for the :meth:`~io.RawIOBase.read`,
:meth:`~io.IOBase.readline`, and :meth:`~io.IOBase.readlines` methods
of the file handle to return.... | trusted_official_docs | CPython Docs | If ``None`` (the default) then a :class:`MagicMock` will be created for you, with the API limited to methods or attributes available on standard file handles.
*read_data* is a string for the :meth:`~io.RawIOBase.read`,
:meth:`~io.IOBase.readline`, and :meth:`~io.IOBase.readlines` methods
of the file handle to return.... | If ``None`` (the default) then a :class:`MagicMock` will be created for you, with the API limited to methods or attributes available on standard file handles.
*read_data* is a string for the :meth:`~io.RawIOBase.read`,
:meth:`~io.IOBase.readline`, and :meth:`~io.IOBase.readlines` methods
of the file handle to return.... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
fd320fa7-8e4b-4bf3-9646-f88586a57c36 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,761 | supabase-export-v2 | 6c25d422ffa3caa0 | >>> @patch.object(SomeClass, 'class_method') ... def test(mock_method): ... SomeClass.class_method(3) ... mock_method.assert_called_with(3) ... >>> test()
*spec*, *create* and the other arguments to :func:`patch.object` have the same
meaning as they do for :func:`patch`. | trusted_official_docs | CPython Docs | >>> @patch.object(SomeClass, 'class_method') ... def test(mock_method): ... SomeClass.class_method(3) ... mock_method.assert_called_with(3) ... >>> test()
*spec*, *create* and the other arguments to :func:`patch.object` have the same
meaning as they do for :func:`patch`. | >>> @patch.object(SomeClass, 'class_method') ... def test(mock_method): ... SomeClass.class_method(3) ... mock_method.assert_called_with(3) ... >>> test()
*spec*, *create* and the other arguments to :func:`patch.object` have the same
meaning as they do for :func:`patch`. | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
fd4e01b7-bf34-4705-aee6-6c45d17b44b4 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,471 | supabase-export-v2 | 8b7f8f6e883656f0 | is formed by calling dir on the object (excluding unsupported magic attributes and methods). Accessing any attribute not in this list will raise an :exc:`AttributeError`.
If *spec* is an object (rather than a list of strings) then
:attr:`~object.__class__` returns the class of the spec object. This
allows mocks to pa... | trusted_official_docs | CPython Docs | is formed by calling dir on the object (excluding unsupported magic attributes and methods). Accessing any attribute not in this list will raise an :exc:`AttributeError`.
If *spec* is an object (rather than a list of strings) then
:attr:`~object.__class__` returns the class of the spec object. This
allows mocks to pa... | is formed by calling dir on the object (excluding unsupported magic attributes and methods). Accessing any attribute not in this list will raise an :exc:`AttributeError`.
If *spec* is an object (rather than a list of strings) then
:attr:`~object.__class__` returns the class of the spec object. This
allows mocks to pa... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
fe9b40c5-25af-45ab-a009-0b93bf903e1f | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,953 | supabase-export-v2 | 831edc66a7c90020 | Before I explain how auto-speccing works, here's why it is needed.
:class:`Mock` is a very powerful and flexible object, but it suffers from a flaw which
is general to mocking. If you refactor some of your code, rename members and so on, any
tests for code that is still using the *old api* but uses mocks instead of the... | trusted_official_docs | CPython Docs | Before I explain how auto-speccing works, here's why it is needed.
:class:`Mock` is a very powerful and flexible object, but it suffers from a flaw which
is general to mocking. If you refactor some of your code, rename members and so on, any
tests for code that is still using the *old api* but uses mocks instead of the... | Before I explain how auto-speccing works, here's why it is needed.
:class:`Mock` is a very powerful and flexible object, but it suffers from a flaw which
is general to mocking. If you refactor some of your code, rename members and so on, any
tests for code that is still using the *old api* but uses mocks instead of the... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
fecfe1b5-2767-4881-a1b8-c379b15f8db4 | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,922 | supabase-export-v2 | 8da92dd8dea34abe | :data:`ANY` can also be used in comparisons with call lists like :attr:`~Mock.mock_calls`:
>>> m = MagicMock(return_value=None)
>>> m(1)
>>> m(1, 2)
>>> m(object())
>>> m.mock_calls == [call(1), call(1, 2), ANY]
True | trusted_official_docs | CPython Docs | :data:`ANY` can also be used in comparisons with call lists like :attr:`~Mock.mock_calls`:
>>> m = MagicMock(return_value=None)
>>> m(1)
>>> m(1, 2)
>>> m(object())
>>> m.mock_calls == [call(1), call(1, 2), ANY]
True | :data:`ANY` can also be used in comparisons with call lists like :attr:`~Mock.mock_calls`:
>>> m = MagicMock(return_value=None)
>>> m(1)
>>> m(1, 2)
>>> m(object())
>>> m.mock_calls == [call(1), call(1, 2), ANY]
True | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
ff8e42f9-3840-486a-a37d-b0b9a67a713a | CPython Docs | file://datasets/cpython/Doc/library/unittest.mock.rst | unknown | bce24593-a692-4bea-816a-2b489f4af17b | 26,553 | supabase-export-v2 | 8d97fd382f533433 | The default return value is a mock object and you can configure it in the normal way:
>>> mock = Mock()
>>> mock.return_value.attribute = sentinel.Attribute
>>> mock.return_value()
<Mock name='mock()()' id='...'>
>>> mock.return_value.assert_called_with() | trusted_official_docs | CPython Docs | The default return value is a mock object and you can configure it in the normal way:
>>> mock = Mock()
>>> mock.return_value.attribute = sentinel.Attribute
>>> mock.return_value()
<Mock name='mock()()' id='...'>
>>> mock.return_value.assert_called_with() | The default return value is a mock object and you can configure it in the normal way:
>>> mock = Mock()
>>> mock.return_value.attribute = sentinel.Attribute
>>> mock.return_value()
<Mock name='mock()()' id='...'>
>>> mock.return_value.assert_called_with() | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
f4c69d25-8568-4832-8f36-2470dcc466b7 | CPython Docs | file://datasets/cpython/Doc/library/imp.rst | unknown | 6767b2eb-3038-406e-9995-45c88ec82b8c | 27,023 | supabase-export-v2 | 31f2b5431c06d423 | This module is no longer part of the Python standard library. It was :ref:`removed in Python 3.12 <whatsnew312-removed-imp>` after being deprecated in Python 3.4.
The :ref:`removal notice <whatsnew312-removed-imp>` includes guidance for
migrating code from :mod:`!imp` to :mod:`importlib`. | trusted_official_docs | CPython Docs | This module is no longer part of the Python standard library. It was :ref:`removed in Python 3.12 <whatsnew312-removed-imp>` after being deprecated in Python 3.4.
The :ref:`removal notice <whatsnew312-removed-imp>` includes guidance for
migrating code from :mod:`!imp` to :mod:`importlib`. | This module is no longer part of the Python standard library. It was :ref:`removed in Python 3.12 <whatsnew312-removed-imp>` after being deprecated in Python 3.4.
The :ref:`removal notice <whatsnew312-removed-imp>` includes guidance for
migrating code from :mod:`!imp` to :mod:`importlib`. | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
d53d7361-32db-440f-9b9d-e5ced50546af | CPython Docs | file://datasets/cpython/Doc/library/audioop.rst | unknown | 86d2bfbf-1669-4d6b-a1cc-516c09e10d30 | 27,029 | supabase-export-v2 | 9844ecbc07bdd86f | part of the Python standard library. It was :ref:`removed in Python 3.13 <whatsnew313-pep594>` after being deprecated in Python 3.11. The removal was decided in :pep:`594`.
The last version of Python that provided the :mod:`!audioop` module was
`Python 3.12 <https://docs.python.org/3.12/library/audioop.html>`_. | trusted_official_docs | CPython Docs | part of the Python standard library. It was :ref:`removed in Python 3.13 <whatsnew313-pep594>` after being deprecated in Python 3.11. The removal was decided in :pep:`594`.
The last version of Python that provided the :mod:`!audioop` module was
`Python 3.12 <https://docs.python.org/3.12/library/audioop.html>`_. | part of the Python standard library. It was :ref:`removed in Python 3.13 <whatsnew313-pep594>` after being deprecated in Python 3.11. The removal was decided in :pep:`594`.
The last version of Python that provided the :mod:`!audioop` module was
`Python 3.12 <https://docs.python.org/3.12/library/audioop.html>`_. | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
02a3e474-bca2-455b-9095-73b139c8b3a5 | CPython Docs | file://datasets/cpython/Doc/library/concurrent.interpreters.rst | unknown | 346dc6a4-67bd-4c03-81c6-10d33b9ff43f | 27,051 | supabase-export-v2 | 74e71c17641cf320 | only the main thread, has some extra runtime state, in addition to the current interpreter, related to the current exception and the bytecode eval loop.)
The concept and functionality of the interpreter have been a part of
Python since version 2.2, but the feature was only available through
the C-API and not well known... | trusted_official_docs | CPython Docs | only the main thread, has some extra runtime state, in addition to the current interpreter, related to the current exception and the bytecode eval loop.)
The concept and functionality of the interpreter have been a part of
Python since version 2.2, but the feature was only available through
the C-API and not well known... | only the main thread, has some extra runtime state, in addition to the current interpreter, related to the current exception and the bytecode eval loop.)
The concept and functionality of the interpreter have been a part of
Python since version 2.2, but the feature was only available through
the C-API and not well known... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
03ff699f-abba-4c32-99b4-22141d0b0971 | CPython Docs | file://datasets/cpython/Doc/library/concurrent.interpreters.rst | unknown | 346dc6a4-67bd-4c03-81c6-10d33b9ff43f | 27,037 | supabase-export-v2 | 8719e9698764b684 | "subinterpreters") and running things in them. Running mostly involves switching to an interpreter (in the current thread) and calling a function in that execution context.
For concurrency, interpreters themselves (and this module) don't
provide much more than isolation, which on its own isn't useful. Actual concurrenc... | trusted_official_docs | CPython Docs | "subinterpreters") and running things in them. Running mostly involves switching to an interpreter (in the current thread) and calling a function in that execution context.
For concurrency, interpreters themselves (and this module) don't
provide much more than isolation, which on its own isn't useful. Actual concurrenc... | "subinterpreters") and running things in them. Running mostly involves switching to an interpreter (in the current thread) and calling a function in that execution context.
For concurrency, interpreters themselves (and this module) don't
provide much more than isolation, which on its own isn't useful. Actual concurrenc... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
062e91ca-1ee0-4d97-8844-95a74690f020 | CPython Docs | file://datasets/cpython/Doc/library/concurrent.interpreters.rst | unknown | 346dc6a4-67bd-4c03-81c6-10d33b9ff43f | 27,047 | supabase-export-v2 | f66979d9e82f6508 | Before we dive in further, there are a small number of details to keep in mind about using multiple interpreters:
* `isolated <interp-isolation_>`_, by default
* no implicit threads
* not all PyPI packages support use in multiple interpreters yet | trusted_official_docs | CPython Docs | Before we dive in further, there are a small number of details to keep in mind about using multiple interpreters:
* `isolated <interp-isolation_>`_, by default
* no implicit threads
* not all PyPI packages support use in multiple interpreters yet | Before we dive in further, there are a small number of details to keep in mind about using multiple interpreters:
* `isolated <interp-isolation_>`_, by default
* no implicit threads
* not all PyPI packages support use in multiple interpreters yet | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
38d27500-67f5-4608-9aaf-8551cd8e296d | CPython Docs | file://datasets/cpython/Doc/library/concurrent.interpreters.rst | unknown | 346dc6a4-67bd-4c03-81c6-10d33b9ff43f | 27,069 | supabase-export-v2 | 07c1780258baa75c | At a high level, you can think of the combination of threads and interpreters as threads with opt-in sharing.
As a significant bonus, interpreters are sufficiently isolated that
they do not share the :term:`GIL`, which means combining threads with
multiple interpreters enables full multi-core parallelism. (This has bee... | trusted_official_docs | CPython Docs | At a high level, you can think of the combination of threads and interpreters as threads with opt-in sharing.
As a significant bonus, interpreters are sufficiently isolated that
they do not share the :term:`GIL`, which means combining threads with
multiple interpreters enables full multi-core parallelism. (This has bee... | At a high level, you can think of the combination of threads and interpreters as threads with opt-in sharing.
As a significant bonus, interpreters are sufficiently isolated that
they do not share the :term:`GIL`, which means combining threads with
multiple interpreters enables full multi-core parallelism. (This has bee... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
4320e555-e604-4109-bed7-294b1e79d207 | CPython Docs | file://datasets/cpython/Doc/library/concurrent.interpreters.rst | unknown | 346dc6a4-67bd-4c03-81c6-10d33b9ff43f | 27,077 | supabase-export-v2 | 0c0e264fa9fcfbb1 | with :mod:`pickle` when they are passed to another interpreter. Nearly all of the immutable builtin objects are either directly shared or copied efficiently. For example:
* :const:`None`
* :class:`bool` (:const:`True` and :const:`False`)
* :class:`bytes`
* :class:`str`
* :class:`int`
* :class:`float`
* :class:`tuple` (... | trusted_official_docs | CPython Docs | with :mod:`pickle` when they are passed to another interpreter. Nearly all of the immutable builtin objects are either directly shared or copied efficiently. For example:
* :const:`None`
* :class:`bool` (:const:`True` and :const:`False`)
* :class:`bytes`
* :class:`str`
* :class:`int`
* :class:`float`
* :class:`tuple` (... | with :mod:`pickle` when they are passed to another interpreter. Nearly all of the immutable builtin objects are either directly shared or copied efficiently. For example:
* :const:`None`
* :class:`bool` (:const:`True` and :const:`False`)
* :class:`bytes`
* :class:`str`
* :class:`int`
* :class:`float`
* :class:`tuple` (... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
48d5a374-b082-4e67-ac97-279fa0900657 | CPython Docs | file://datasets/cpython/Doc/library/concurrent.interpreters.rst | unknown | 346dc6a4-67bd-4c03-81c6-10d33b9ff43f | 27,072 | supabase-export-v2 | 589350f1ea2cbf19 | a way to communicate between them. This usually involves some form of message passing, but can even mean sharing data in some carefully managed way.
With this in mind, the :mod:`!concurrent.interpreters` module provides
a :class:`queue.Queue` implementation, available through
:func:`create_queue`. | trusted_official_docs | CPython Docs | a way to communicate between them. This usually involves some form of message passing, but can even mean sharing data in some carefully managed way.
With this in mind, the :mod:`!concurrent.interpreters` module provides
a :class:`queue.Queue` implementation, available through
:func:`create_queue`. | a way to communicate between them. This usually involves some form of message passing, but can even mean sharing data in some carefully managed way.
With this in mind, the :mod:`!concurrent.interpreters` module provides
a :class:`queue.Queue` implementation, available through
:func:`create_queue`. | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
497f8a60-954a-46ec-90ba-013deaa32b3a | CPython Docs | file://datasets/cpython/Doc/library/concurrent.interpreters.rst | unknown | 346dc6a4-67bd-4c03-81c6-10d33b9ff43f | 27,057 | supabase-export-v2 | 551a5efe3219e3cd | .. note::
Interpreters in the same process can technically never be strictly
isolated from one another since there are few restrictions on memory
access within the same process. The Python runtime makes a best
effort at isolation but extension modules may easily violate that. Therefore, do not use multiple interpret... | trusted_official_docs | CPython Docs | .. note::
Interpreters in the same process can technically never be strictly
isolated from one another since there are few restrictions on memory
access within the same process. The Python runtime makes a best
effort at isolation but extension modules may easily violate that. Therefore, do not use multiple interpret... | .. note::
Interpreters in the same process can technically never be strictly
isolated from one another since there are few restrictions on memory
access within the same process. The Python runtime makes a best
effort at isolation but extension modules may easily violate that. Therefore, do not use multiple interpret... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
4c51d444-a71e-42be-bc65-d211fcc82e13 | CPython Docs | file://datasets/cpython/Doc/library/concurrent.interpreters.rst | unknown | 346dc6a4-67bd-4c03-81c6-10d33b9ff43f | 27,064 | supabase-export-v2 | 8e8f075445b681aa | Concurrency and Parallelism ^^^^^^^^^^^^^^^^^^^^^^^^^^^
As noted earlier, interpreters do not provide any concurrency
on their own. They strictly represent the isolated execution
context the runtime will use *in the current thread*. That isolation
makes them similar to processes, but they still enjoy in-process
efficie... | trusted_official_docs | CPython Docs | Concurrency and Parallelism ^^^^^^^^^^^^^^^^^^^^^^^^^^^
As noted earlier, interpreters do not provide any concurrency
on their own. They strictly represent the isolated execution
context the runtime will use *in the current thread*. That isolation
makes them similar to processes, but they still enjoy in-process
efficie... | Concurrency and Parallelism ^^^^^^^^^^^^^^^^^^^^^^^^^^^
As noted earlier, interpreters do not provide any concurrency
on their own. They strictly represent the isolated execution
context the runtime will use *in the current thread*. That isolation
makes them similar to processes, but they still enjoy in-process
efficie... | python, official-docs, cpython, P0 | Local_Trusted_Corpus | |
5707ea3c-5060-4fc3-9ba7-4a98fd3c186f | CPython Docs | file://datasets/cpython/Doc/library/concurrent.interpreters.rst | unknown | 346dc6a4-67bd-4c03-81c6-10d33b9ff43f | 27,054 | supabase-export-v2 | 6601bbd2a9976ef2 | Multiple Interpreters and Isolation ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
A Python implementation may support using multiple interpreters in the
same process. CPython has this support. Each interpreter is
effectively isolated from the others (with a limited number of
carefully managed process-global exceptions to the rul... | trusted_official_docs | CPython Docs | Multiple Interpreters and Isolation ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
A Python implementation may support using multiple interpreters in the
same process. CPython has this support. Each interpreter is
effectively isolated from the others (with a limited number of
carefully managed process-global exceptions to the rul... | Multiple Interpreters and Isolation ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
A Python implementation may support using multiple interpreters in the
same process. CPython has this support. Each interpreter is
effectively isolated from the others (with a limited number of
carefully managed process-global exceptions to the rul... | python, official-docs, cpython, P0 | Local_Trusted_Corpus |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.