summaryrefslogtreecommitdiffstats
path: root/Lib/unittest/mock.py
Commit message (Collapse)AuthorAgeFilesLines
* gh-104745: Limit starting a patcher more than once without stopping it (#126649)Red4Ru2024-11-131-0/+9
| | | | | | | | Previously, this would cause an `AttributeError` if the patch stopped more than once after this, and would also disrupt the original patched object. --------- Co-authored-by: Peter Bierma <zintensitydev@gmail.com> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
* gh-124176: Add special support for dataclasses to `create_autospec` (#124429)sobolevn2024-09-271-6/+16
|
* gh-124234: Improve docs for `Mock.reset_mock` (#124237)sobolevn2024-09-261-2/+4
|
* gh-123934: Fix `MagicMock` not to reset magic method return values (#124038)sobolevn2024-09-191-1/+12
|
* Remove unused variable in `MagicMixin._mock_set_magics` (#124092)sobolevn2024-09-141-2/+0
|
* gh-122858: Deprecate `asyncio.iscoroutinefunction` (#122875)Wulian2024-08-111-2/+2
| | | | | Deprecate `asyncio.iscoroutinefunction` in favor of `inspect.iscoroutinefunction`. Co-authored-by: Kumar Aditya <kumaraditya@python.org>
* gh-117765: Improve documentation for `mocker.patch.dict` (#121755)Dominic H2024-07-151-1/+2
|
* gh-120732: Fix `name` passing to `Mock`, when using kwargs to ↵Nikita Sobolev2024-06-191-7/+6
| | | | `create_autospec` (#120737)
* gh-65454: avoid triggering call to a PropertyMock in ↵blhsing2024-06-111-0/+3
| | | | NonCallableMock.__setattr__ (#120019)
* gh-119600: mock: do not access attributes of original when new_callable is ↵Robert Collins2024-06-111-5/+9
| | | | | | | | | | set (#119601) In order to patch flask.g e.g. as in #84982, that proxies getattr must not be invoked. For that, mock must not try to read from the original object. In some cases that is unavoidable, e.g. when doing autospec. However, patch("flask.g", new_callable=MagicMock) should be entirely safe.
* Remove almost all unpaired backticks in docstrings (#119231)Geoffrey Thomas2024-05-221-1/+1
| | | | | | | | | | | | | | | | | | As reported in #117847 and #115366, an unpaired backtick in a docstring tends to confuse e.g. Sphinx running on subclasses of standard library objects, and the typographic style of using a backtick as an opening quote is no longer in favor. Convert almost all uses of the form The variable `foo' should do xyz to The variable 'foo' should do xyz and also fix up miscellaneous other unpaired backticks (extraneous / missing characters). No functional change is intended here other than in human-readable docstrings.
* gh-90848: Fixed create_autospec ignoring configure_mock style kwargs (#118163)infohash2024-05-021-8/+12
|
* gh-75988: Fix issues with autospec ignoring wrapped object (#115223)infohash2024-03-081-2/+11
| | | | | | | | | | | * set default return value of functional types as _mock_return_value * added test of wrapping child attributes * added backward compatibility with explicit return * added docs on the order of precedence * added test to check default return_value
* gh-113569: Display calls in Mock.assert_has_calls failure when empty (GH-113573)wookie1842024-01-041-4/+4
|
* gh-113407: Fix import of unittest.mock when CPython is built without ↵Serhiy Storchaka2023-12-241-5/+11
| | | | docstrings (GH-113408)
* gh-111019: Align expected and actual titles in test output (#111020)James2023-10-181-3/+3
| | | | Align expected and actual titles in output from assert_has_calls/assert_called_with for greater readability
* Remove unused branches from mock module (#106617)Chris Withers2023-07-111-6/+1
| | | | | | | * lambda has a name of __none__, but no async lambda so this branch is not needed * _get_signature_object only returns None for bound builtins. There are no async builtins so this branch isn't needed * Exclude a couple of methods from coverage checking in the downstream rolling backport of mock
* GH-61215: threadingmock: Remove unused branch for `timeout` (#106591)Mario Corchero2023-07-101-3/+1
| | | | | | threadingmock: Remove unused branch for `timeout` This is no longer needed as the mock does not hold a "timeout" parameter, the timeout is stored in `_mock_wait_timeout`.
* gh-61215: Rename `wait_until_any_call` to `wait_until_any_call_with` (#106414)Mario Corchero2023-07-041-1/+1
| | | | | | mock: Rename `wait_until_any_call` to `wait_until_any_call_with` Rename the method to be more explicit that it expects the args and kwargs to wait for.
* gh-61215: New mock to wait for multi-threaded events to happen (#16094)Mario Corchero2023-07-031-0/+94
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | mock: Add `ThreadingMock` class Add a new class that allows to wait for a call to happen by using `Event` objects. This mock class can be used to test and validate expectations of multithreading code. It uses two attributes for events to distinguish calls with any argument and calls with specific arguments. The calls with specific arguments need a lock to prevent two calls in parallel from creating the same event twice. The timeout is configured at class and constructor level to allow users to set a timeout, we considered passing it as an argument to the function but it could collide with a function parameter. Alternatively we also considered passing it as positional only but from an API caller perspective it was unclear what the first number meant on the function call, think `mock.wait_until_called(1, "arg1", "arg2")`, where 1 is the timeout. Lastly we also considered adding the new attributes to magic mock directly rather than having a custom mock class for multi threading scenarios, but we preferred to have specialised class that can be composed if necessary. Additionally, having added it to `MagicMock` directly would have resulted in `AsyncMock` having this logic, which would not work as expected, since when if user "waits" on a coroutine does not have the same meaning as waiting on a standard call. Co-authored-by: Karthikeyan Singaravelan <tir.karthi@gmail.com>
* bpo-44185: Added close() to mock_open __exit__ (#26902)Samet YASLAN2023-06-111-0/+4
|
* gh-94924: support `inspect.iscoroutinefunction` in ↵Thomas Grainger2023-06-091-2/+30
| | | | | | | | | | `create_autospec(async_def)` (#94962) * support inspect.iscoroutinefunction in create_autospec(async_def) * test create_autospec with inspect.iscoroutine and inspect.iscoroutinefunction * test when create_autospec functions check their signature
* gh-85934: Use getattr_static when adding mock spec (#22209)melanie witt2023-05-231-1/+7
| | | | Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu> Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net>
* gh-102978: Fix mock.patch function signatures for class and staticmethod ↵Tomas R2023-04-131-0/+6
| | | | | | | decorators (#103228) Fixes unittest.mock.patch not enforcing function signatures for methods decorated with @classmethod or @staticmethod when patch is called with autospec=True.
* gh-96127: Fix `inspect.signature` call on mocks (#96335)Nikita Sobolev2023-01-071-1/+9
|
* gh-100690: [mock] hide `ATTRIB_DENY_LIST` and make it immutable (#100819)Nikita Sobolev2023-01-071-2/+6
|
* gh-100690: Raise an AttributeError when the assert_ prefix is forgotten when ↵Christian Klein2023-01-061-4/+10
| | | | | | | | using Mock (#100691) Mock objects which are not unsafe will now raise an AttributeError when accessing an attribute that matches the name of an assertion but without the prefix `assert_`, e.g. accessing `called_once` instead of `assert_called_once`. This is in addition to this already happening for accessing attributes with prefixes assert, assret, asert, aseert, and assrt.
* gh-100739: Respect mock spec when checking for unsafe prefixes (#100740)Christian Klein2023-01-041-1/+1
| | | Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
* gh-100287: Fix unittest.mock.seal with AsyncMock (#100496)Shantanu2022-12-241-4/+4
|
* gh-83076: 3.8x speed improvement in (Async)Mock instantiation (#100252)Carl Meyer2022-12-231-16/+22
|
* gh-98086: Now ``patch.dict`` can decorate async functions (#98095)Nikita Sobolev2022-11-111-0/+18
|
* gh-91803: Mock - fix error when using autospec methods with seal (#92213)andrei kulakov2022-11-071-0/+1
| | | | | | Fixes https://github.com/python/cpython/issues/91803. Co-authored-by: Karthikeyan Singaravelan <tir.karthi@gmail.com> Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
* gh-98624 Add mutex to unittest.mock.NonCallableMock (#98688)noah-weingarden2022-10-281-28/+38
| | | | | | | | | | | * Added lock to NonCallableMock in unittest.mock * Add blurb * Nitpick blurb * Edit comment based on @Jason-Y-Z's review * Add link to GH issue
* gh-84753: Make inspect.iscoroutinefunction() work with AsyncMock (#94050)Mehdi ABAAKOUK2022-06-301-0/+4
| | | | | | | | | The inspect version was not working with unittest.mock.AsyncMock. The fix introduces special-casing of AsyncMock in `inspect.iscoroutinefunction` equivalent to the one performed in `asyncio.iscoroutinefunction`. Co-authored-by: Łukasz Langa <lukasz@langa.pl>
* bpo-46852: Remove the float.__set_format__() method (GH-31585)Victor Stinner2022-02-251-1/+1
| | | | | | Remove the undocumented private float.__set_format__() method, previously known as float.__set_format__() in Python 3.7. Its docstring said: "You probably don't want to use this function. It exists mainly to be used in Python's test suite."
* Restrict use of Mock objects as specs (GH-31090)Matthew Suozzo2022-02-031-1/+9
| | | | | Follow-on to https://github.com/python/cpython/pull/25326 This covers cases where mock objects are passed directly to spec.
* bpo-41403: Improve error message for invalid mock target (GH-30833)Irit Katriel2022-01-231-3/+3
|
* bpo-45156: Fixes inifite loop on unittest.mock.seal() (GH-28300)Nikita Sobolev2021-09-141-6/+7
| | | | | | Fixes infinite loop on unittest.mock.seal() of mocks created by unittest.create_autospec(). Co-authored-by: Dong-hee Na <donghee.na92@gmail.com>
* bpo-45010: Remove support of special method __div__ in unittest.mock (GH-27965)Serhiy Storchaka2021-08-261-1/+1
|
* bpo-44686 replace unittest.mock._importer with pkgutil.resolve_name (GH-18544)Thomas Grainger2021-07-211-23/+4
| | | Automerge-Triggered-By: GH:cjw296
* bpo-44534: fix wording and docstring sync in unittest.Mock GH27000Jack DeVries2021-07-051-0/+5
|
* Fix typos in multiple files (GH-26689)Binbin2021-06-131-1/+1
| | | Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
* bpo-43478: Restrict use of Mock objects as specs (GH-25326)Matthew Suozzo2021-04-101-4/+38
| | | | * Restrict using Mock objects as specs as this is always a test bug where the resulting mock is misleadingly useless. * Skip a broken test that exposes a bug elsewhere in mock (noted in the original issue).
* bpo-41877: Check for misspelled speccing arguments (GH-23737)vabr-g2020-12-141-8/+31
| | | | | | | | | | | | patch, patch.object and create_autospec silently ignore misspelled arguments such as autospect, auto_spec and set_spec. This can lead to tests failing to check what they are supposed to check. This change adds a check causing a RuntimeError if the above functions get any of the above misspellings as arguments. It also adds a new argument, "unsafe", which can be set to True to disable this check. Also add "!r" to format specifiers in added error messages.
* bpo-41877: Improve docs for assert misspellings check in mock (GH-23729)vabr-g2020-12-101-2/+3
| | | | | | | | | This is a follow-up to https://github.com/python/cpython/commit/4662fa9bfe4a849fe87bfb321d8ef0956c89a772. That original commit expanded guards against misspelling assertions on mocks. This follow-up updates the documentation and improves the error message by pointing out the potential cause and solution. Automerge-Triggered-By: GH:gpshead
* bpo-42532: Check if NonCallableMock's spec_arg is not None instead of call ↵idanw2062020-12-061-1/+1
| | | | | | its __bool__ function (GH23613) Check if NonCallableMock's spec_arg is not None instead of call its __bool__ function
* bpo-41877 Check for asert, aseert, assrt in mocks (GH-23165)vabr-g2020-11-051-2/+2
| | | | | | | | | | | Currently, a Mock object which is not unsafe will raise an AttributeError if an attribute with the prefix assert or assret is accessed on it. This protects against misspellings of real assert method calls, which lead to tests passing silently even if the tested code does not satisfy the intended assertion. Recently a check was done in a large code base (Google) and three more frequent ways of misspelling assert were found causing harm: asert, aseert, assrt. These are now added to the existing check.
* bpo-39966: Revert "bpo-25597: Ensure wraps' return value is used for magic ↵Karthikeyan Singaravelan2020-04-281-6/+0
| | | | | | | methods in MagicMock" (GH-19734) * Revert "bpo-25597: Ensure wraps' return value is used for magic methods in MagicMock (#16029)" This reverts commit 72b1004657e60c900e4cd031b2635b587f4b280e.
* bpo-40126: Fix reverting multiple patches in unittest.mock. (GH-19351)Serhiy Storchaka2020-04-111-48/+26
| | | | | Patcher's __exit__() is now never called if its __enter__() is failed. Returning true from __exit__() silences now the exception.
* bpo-39915: Ensure await_args_list is updated according to the order in which ↵Karthikeyan Singaravelan2020-03-111-1/+1
| | | | | coroutines were awaited (GH-18924) Create call objects with awaited arguments instead of using call_args which has only last call value.