-
-
Notifications
You must be signed in to change notification settings - Fork 147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Warn instead of raising exception in context manager #221
Conversation
Instead of raising an exception, warn when a pytest-mock mock is used as a context manager. This lets pytest-mock mock objects that are used as context managers, like threading.Lock, while still letting users know that mocks returned from pytest-mock do not need their __enter__ method called to take effect. Create a PytestMockWarning class so that the warning is easy to pinpoint and handle.
Test that warnings are issued when mocks are used as context manager. Verify that the warnings have the expected message and that they point to the correct code.
Use a stacklevel=5 for Python 3.8 and up so that the emitted warning points to the correct line of code. At stacklevel=4, it was incorrectly pointing to code in the standard library.
Hey @iforapsy, Thanks a lot for the PR, sorry for the delay! The patch looks great, it shows you took great care on it, thanks. One suggestion is to perhaps tweak the warning message a bit explaining more what's happening? Otherwise when using it as a context, the user might the impression that something is not correct. Alternatively (and this just occurred to me now), perhaps we can have a different function, which operates like import threading
class Foo:
CREATION_LOCK = threading.Lock()
def create_widgets(self):
with self.CREATION_LOCK:
print('Creating object')
def test_create_widgets_acquires_lock(mocker):
"""Test that the creation lock is acquired."""
lock_mock = mocker.patch.context_manager(Foo, "CREATION_LOCK", autospec=True)
Foo().create_widgets()
lock_mock.__enter__.assert_called() What do you think? |
Make the warning message when a mock is used as a context manager more detailed. In the message, describe the use case to avoid and when the warning can be safely ignored.
Create patch.context_manager method that allows the patched object to be used as a context manager without issuing a warning on __enter__. Otherwise, the method is functionally equivalent to patch.object.
Hi @nicoddemus, No need to apologize. I understand that it's the holidays. I have not been the fastest responder either. Thank you for your kind feedback. I like both your suggestions. I made the warning message more explanatory and more newbie-friendly. Though it won't be useful for my code base (as the |
Hi @iforapsy, just passing by to let you know I did not forget about this PR, I will probably have time to review/merge/make a new release tomorrow. Thanks again for the patience! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great PR, thanks @iforapsy!
|
Bumps [pytest-mock](https://github.com/pytest-dev/pytest-mock) from 3.4.0 to 3.5.0. <details> <summary>Changelog</summary> <p><em>Sourced from <a href="https://github.com/pytest-dev/pytest-mock/blob/master/CHANGELOG.rst">pytest-mock's changelog</a>.</em></p> <blockquote> <h2>3.5.0 (2021-01-04)</h2> <ul> <li> <p>Now all patch functions will emit a warning instead of raising a <code>ValueError</code> when used as a context-manager. Thanks <code>@iforapsy</code>_ for the PR (<code>[#221](https://github.com/pytest-dev/pytest-mock/issues/221)</code>_).</p> </li> <li> <p>Additionally, <code>mocker.patch.context_manager</code> is available when the user intends to mock a context manager (for example <code>threading.Lock</code> object), which will not emit that warning.</p> </li> </ul> <p>.. _<a href="https://github.com/iforapsy">@iforapsy</a>: <a href="https://github.com/iforapsy">https://github.com/iforapsy</a> .. _<a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/221">#221</a>: <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/pull/221">pytest-dev/pytest-mock#221</a></p> </blockquote> </details> <details> <summary>Commits</summary> <ul> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/cdd5d709f5d1e30526ca30ae93c64fbbaebcd58d"><code>cdd5d70</code></a> Update context-manager docs and prepare for 3.5.0</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/f623fa88211e07ac4965a9079afabe9f23334758"><code>f623fa8</code></a> Warn instead of raising exception in context manager (<a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/221">#221</a>)</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/5f6cab7ead376d4f2de20c32bf01e244ffd44898"><code>5f6cab7</code></a> Merge pull request <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/220">#220</a> from webknjaz/docs/216-changelog</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/9771def1069d3f1560525799ec8ba9d8e2c75ed3"><code>9771def</code></a> Fix typos in a change note for the PR <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/216">#216</a></li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/d4e3f3edd4e085e6b4bc15930a440e759984352b"><code>d4e3f3e</code></a> Merge pull request <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/208">#208</a> from pytest-dev/graingert-patch-1</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/252eba86090fc0522c23805c097eb0c570a2c754"><code>252eba8</code></a> Merge pull request <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/219">#219</a> from nicoddemus/release-3.4.0</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/e6cc1eca07b9133627da6781833429bdaa76364f"><code>e6cc1ec</code></a> remove type annotations from docstrings</li> <li>See full diff in <a href="https://github.com/pytest-dev/pytest-mock/compare/v3.4.0...v3.5.0">compare view</a></li> </ul> </details> <br /> [![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=pytest-mock&package-manager=pip&previous-version=3.4.0&new-version=3.5.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores) Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end)
Bumps [pytest-mock](https://github.com/pytest-dev/pytest-mock) from 3.3.1 to 3.5.0. <details> <summary>Changelog</summary> <p><em>Sourced from <a href="https://github.com/pytest-dev/pytest-mock/blob/master/CHANGELOG.rst">pytest-mock's changelog</a>.</em></p> <blockquote> <h2>3.5.0 (2021-01-04)</h2> <ul> <li> <p>Now all patch functions will emit a warning instead of raising a <code>ValueError</code> when used as a context-manager. Thanks <code>@iforapsy</code>_ for the PR (<code>[#221](https://github.com/pytest-dev/pytest-mock/issues/221)</code>_).</p> </li> <li> <p>Additionally, <code>mocker.patch.context_manager</code> is available when the user intends to mock a context manager (for example <code>threading.Lock</code> object), which will not emit that warning.</p> </li> </ul> <p>.. _<a href="https://github.com/iforapsy">@iforapsy</a>: <a href="https://github.com/iforapsy">https://github.com/iforapsy</a> .. _<a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/221">#221</a>: <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/pull/221">pytest-dev/pytest-mock#221</a></p> <h2>3.4.0 (2020-12-15)</h2> <ul> <li> <p>Add <code>mock.seal</code> alias to the <code>mocker</code> fixture (<code>[#211](https://github.com/pytest-dev/pytest-mock/issues/211)</code><em>). Thanks <code>@coiax</code></em> for the PR.</p> </li> <li> <p>Fixed spying on exceptions not covered by the <code>Exception</code> superclass (<code>[#215](https://github.com/pytest-dev/pytest-mock/issues/215)</code><em>), like <code>KeyboardInterrupt</code> -- PR <code>[#216](https://github.com/pytest-dev/pytest-mock/issues/216)</code></em> by <code>@webknjaz</code>_.</p> <p>Before the fix, both <code>spy_return</code> and <code>spy_exception</code> were always assigned to <code>None</code> whenever such an exception happened. And after this fix, <code>spy_exception</code> is set to a correct value of an exception that has actually happened.</p> </li> </ul> <p>.. _<a href="https://github.com/coiax">@coiax</a>: <a href="https://github.com/coiax">https://github.com/coiax</a> .. _<a href="https://github.com/webknjaz">@webknjaz</a>: <a href="https://github.com/sponsors/webknjaz">https://github.com/sponsors/webknjaz</a> .. _<a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/211">#211</a>: <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/pull/211">pytest-dev/pytest-mock#211</a> .. _<a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/215">#215</a>: <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/215">pytest-dev/pytest-mock#215</a> .. _<a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/216">#216</a>: <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/pull/216">pytest-dev/pytest-mock#216</a></p> </blockquote> </details> <details> <summary>Commits</summary> <ul> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/cdd5d709f5d1e30526ca30ae93c64fbbaebcd58d"><code>cdd5d70</code></a> Update context-manager docs and prepare for 3.5.0</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/f623fa88211e07ac4965a9079afabe9f23334758"><code>f623fa8</code></a> Warn instead of raising exception in context manager (<a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/221">#221</a>)</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/5f6cab7ead376d4f2de20c32bf01e244ffd44898"><code>5f6cab7</code></a> Merge pull request <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/220">#220</a> from webknjaz/docs/216-changelog</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/9771def1069d3f1560525799ec8ba9d8e2c75ed3"><code>9771def</code></a> Fix typos in a change note for the PR <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/216">#216</a></li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/d4e3f3edd4e085e6b4bc15930a440e759984352b"><code>d4e3f3e</code></a> Merge pull request <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/208">#208</a> from pytest-dev/graingert-patch-1</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/252eba86090fc0522c23805c097eb0c570a2c754"><code>252eba8</code></a> Merge pull request <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/219">#219</a> from nicoddemus/release-3.4.0</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/df9ec17198d9c5d8e26eb91c892c5514b5793b02"><code>df9ec17</code></a> Prepare release 3.4.0</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/91b4afb0b9ab3a454a2e8235da682e7287ab8f32"><code>91b4afb</code></a> Add testing for Python 3.9 (<a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/218">#218</a>)</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/9640a36d06f1b35796e316d3b4529022e9d67fb7"><code>9640a36</code></a> Merge pull request <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/216">#216</a> from webknjaz/bugfixes/215-spy-on-keyboard-interrupt</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/0e6b221080f5b8ef425a862ca45052131527894a"><code>0e6b221</code></a> Ensure all the exceptions cat be spied on</li> <li>Additional commits viewable in <a href="https://github.com/pytest-dev/pytest-mock/compare/v3.3.1...v3.5.0">compare view</a></li> </ul> </details> <br /> [![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=pytest-mock&package-manager=pip&previous-version=3.3.1&new-version=3.5.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores) Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end)
Bumps [pytest-mock](https://github.com/pytest-dev/pytest-mock) from 3.3.1 to 3.5.0. <details> <summary>Changelog</summary> <p><em>Sourced from <a href="https://github.com/pytest-dev/pytest-mock/blob/master/CHANGELOG.rst">pytest-mock's changelog</a>.</em></p> <blockquote> <h2>3.5.0 (2021-01-04)</h2> <ul> <li> <p>Now all patch functions will emit a warning instead of raising a <code>ValueError</code> when used as a context-manager. Thanks <code>@iforapsy</code>_ for the PR (<code>[#221](https://github.com/pytest-dev/pytest-mock/issues/221)</code>_).</p> </li> <li> <p>Additionally, <code>mocker.patch.context_manager</code> is available when the user intends to mock a context manager (for example <code>threading.Lock</code> object), which will not emit that warning.</p> </li> </ul> <p>.. _<a href="https://github.com/iforapsy">@iforapsy</a>: <a href="https://github.com/iforapsy">https://github.com/iforapsy</a> .. _<a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/221">#221</a>: <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/pull/221">pytest-dev/pytest-mock#221</a></p> <h2>3.4.0 (2020-12-15)</h2> <ul> <li> <p>Add <code>mock.seal</code> alias to the <code>mocker</code> fixture (<code>[#211](https://github.com/pytest-dev/pytest-mock/issues/211)</code><em>). Thanks <code>@coiax</code></em> for the PR.</p> </li> <li> <p>Fixed spying on exceptions not covered by the <code>Exception</code> superclass (<code>[#215](https://github.com/pytest-dev/pytest-mock/issues/215)</code><em>), like <code>KeyboardInterrupt</code> -- PR <code>[#216](https://github.com/pytest-dev/pytest-mock/issues/216)</code></em> by <code>@webknjaz</code>_.</p> <p>Before the fix, both <code>spy_return</code> and <code>spy_exception</code> were always assigned to <code>None</code> whenever such an exception happened. And after this fix, <code>spy_exception</code> is set to a correct value of an exception that has actually happened.</p> </li> </ul> <p>.. _<a href="https://github.com/coiax">@coiax</a>: <a href="https://github.com/coiax">https://github.com/coiax</a> .. _<a href="https://github.com/webknjaz">@webknjaz</a>: <a href="https://github.com/sponsors/webknjaz">https://github.com/sponsors/webknjaz</a> .. _<a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/211">#211</a>: <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/pull/211">pytest-dev/pytest-mock#211</a> .. _<a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/215">#215</a>: <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/215">pytest-dev/pytest-mock#215</a> .. _<a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/216">#216</a>: <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/pull/216">pytest-dev/pytest-mock#216</a></p> </blockquote> </details> <details> <summary>Commits</summary> <ul> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/cdd5d709f5d1e30526ca30ae93c64fbbaebcd58d"><code>cdd5d70</code></a> Update context-manager docs and prepare for 3.5.0</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/f623fa88211e07ac4965a9079afabe9f23334758"><code>f623fa8</code></a> Warn instead of raising exception in context manager (<a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/221">#221</a>)</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/5f6cab7ead376d4f2de20c32bf01e244ffd44898"><code>5f6cab7</code></a> Merge pull request <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/220">#220</a> from webknjaz/docs/216-changelog</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/9771def1069d3f1560525799ec8ba9d8e2c75ed3"><code>9771def</code></a> Fix typos in a change note for the PR <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/216">#216</a></li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/d4e3f3edd4e085e6b4bc15930a440e759984352b"><code>d4e3f3e</code></a> Merge pull request <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/208">#208</a> from pytest-dev/graingert-patch-1</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/252eba86090fc0522c23805c097eb0c570a2c754"><code>252eba8</code></a> Merge pull request <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/219">#219</a> from nicoddemus/release-3.4.0</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/df9ec17198d9c5d8e26eb91c892c5514b5793b02"><code>df9ec17</code></a> Prepare release 3.4.0</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/91b4afb0b9ab3a454a2e8235da682e7287ab8f32"><code>91b4afb</code></a> Add testing for Python 3.9 (<a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/218">#218</a>)</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/9640a36d06f1b35796e316d3b4529022e9d67fb7"><code>9640a36</code></a> Merge pull request <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/216">#216</a> from webknjaz/bugfixes/215-spy-on-keyboard-interrupt</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/0e6b221080f5b8ef425a862ca45052131527894a"><code>0e6b221</code></a> Ensure all the exceptions cat be spied on</li> <li>Additional commits viewable in <a href="https://github.com/pytest-dev/pytest-mock/compare/v3.3.1...v3.5.0">compare view</a></li> </ul> </details> <br /> [![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=pytest-mock&package-manager=pip&previous-version=3.3.1&new-version=3.5.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores) Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end)
Bumps [pytest-mock](https://github.com/pytest-dev/pytest-mock) from 3.3.1 to 3.5.0. <details> <summary>Changelog</summary> <p><em>Sourced from <a href="https://github.com/pytest-dev/pytest-mock/blob/master/CHANGELOG.rst">pytest-mock's changelog</a>.</em></p> <blockquote> <h2>3.5.0 (2021-01-04)</h2> <ul> <li> <p>Now all patch functions will emit a warning instead of raising a <code>ValueError</code> when used as a context-manager. Thanks <code>@iforapsy</code>_ for the PR (<code>[aio-libs#221](https://github.com/pytest-dev/pytest-mock/issues/221)</code>_).</p> </li> <li> <p>Additionally, <code>mocker.patch.context_manager</code> is available when the user intends to mock a context manager (for example <code>threading.Lock</code> object), which will not emit that warning.</p> </li> </ul> <p>.. _<a href="https://github.com/iforapsy">@iforapsy</a>: <a href="https://github.com/iforapsy">https://github.com/iforapsy</a> .. _<a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/221">#221</a>: <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/pull/221">pytest-dev/pytest-mock#221</a></p> <h2>3.4.0 (2020-12-15)</h2> <ul> <li> <p>Add <code>mock.seal</code> alias to the <code>mocker</code> fixture (<code>[aio-libs#211](https://github.com/pytest-dev/pytest-mock/issues/211)</code><em>). Thanks <code>@coiax</code></em> for the PR.</p> </li> <li> <p>Fixed spying on exceptions not covered by the <code>Exception</code> superclass (<code>[aio-libs#215](https://github.com/pytest-dev/pytest-mock/issues/215)</code><em>), like <code>KeyboardInterrupt</code> -- PR <code>[aio-libs#216](https://github.com/pytest-dev/pytest-mock/issues/216)</code></em> by <code>@webknjaz</code>_.</p> <p>Before the fix, both <code>spy_return</code> and <code>spy_exception</code> were always assigned to <code>None</code> whenever such an exception happened. And after this fix, <code>spy_exception</code> is set to a correct value of an exception that has actually happened.</p> </li> </ul> <p>.. _<a href="https://github.com/coiax">@coiax</a>: <a href="https://github.com/coiax">https://github.com/coiax</a> .. _<a href="https://github.com/webknjaz">@webknjaz</a>: <a href="https://github.com/sponsors/webknjaz">https://github.com/sponsors/webknjaz</a> .. _<a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/211">#211</a>: <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/pull/211">pytest-dev/pytest-mock#211</a> .. _<a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/215">#215</a>: <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/215">pytest-dev/pytest-mock#215</a> .. _<a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/216">#216</a>: <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/pull/216">pytest-dev/pytest-mock#216</a></p> </blockquote> </details> <details> <summary>Commits</summary> <ul> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/cdd5d709f5d1e30526ca30ae93c64fbbaebcd58d"><code>cdd5d70</code></a> Update context-manager docs and prepare for 3.5.0</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/f623fa88211e07ac4965a9079afabe9f23334758"><code>f623fa8</code></a> Warn instead of raising exception in context manager (<a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/221">#221</a>)</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/5f6cab7ead376d4f2de20c32bf01e244ffd44898"><code>5f6cab7</code></a> Merge pull request <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/220">#220</a> from webknjaz/docs/216-changelog</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/9771def1069d3f1560525799ec8ba9d8e2c75ed3"><code>9771def</code></a> Fix typos in a change note for the PR <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/216">#216</a></li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/d4e3f3edd4e085e6b4bc15930a440e759984352b"><code>d4e3f3e</code></a> Merge pull request <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/208">#208</a> from pytest-dev/graingert-patch-1</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/252eba86090fc0522c23805c097eb0c570a2c754"><code>252eba8</code></a> Merge pull request <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/219">#219</a> from nicoddemus/release-3.4.0</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/df9ec17198d9c5d8e26eb91c892c5514b5793b02"><code>df9ec17</code></a> Prepare release 3.4.0</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/91b4afb0b9ab3a454a2e8235da682e7287ab8f32"><code>91b4afb</code></a> Add testing for Python 3.9 (<a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/218">#218</a>)</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/9640a36d06f1b35796e316d3b4529022e9d67fb7"><code>9640a36</code></a> Merge pull request <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/216">#216</a> from webknjaz/bugfixes/215-spy-on-keyboard-interrupt</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/0e6b221080f5b8ef425a862ca45052131527894a"><code>0e6b221</code></a> Ensure all the exceptions cat be spied on</li> <li>Additional commits viewable in <a href="https://github.com/pytest-dev/pytest-mock/compare/v3.3.1...v3.5.0">compare view</a></li> </ul> </details> <br /> [![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=pytest-mock&package-manager=pip&previous-version=3.3.1&new-version=3.5.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores) Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end)
Bumps [pytest-mock](https://github.com/pytest-dev/pytest-mock) from 3.3.1 to 3.5.0. <details> <summary>Changelog</summary> <p><em>Sourced from <a href="https://github.com/pytest-dev/pytest-mock/blob/master/CHANGELOG.rst">pytest-mock's changelog</a>.</em></p> <blockquote> <h2>3.5.0 (2021-01-04)</h2> <ul> <li> <p>Now all patch functions will emit a warning instead of raising a <code>ValueError</code> when used as a context-manager. Thanks <code>@iforapsy</code>_ for the PR (<code>[aio-libs#221](https://github.com/pytest-dev/pytest-mock/issues/221)</code>_).</p> </li> <li> <p>Additionally, <code>mocker.patch.context_manager</code> is available when the user intends to mock a context manager (for example <code>threading.Lock</code> object), which will not emit that warning.</p> </li> </ul> <p>.. _<a href="https://github.com/iforapsy">@iforapsy</a>: <a href="https://github.com/iforapsy">https://github.com/iforapsy</a> .. _<a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/221">#221</a>: <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/pull/221">pytest-dev/pytest-mock#221</a></p> <h2>3.4.0 (2020-12-15)</h2> <ul> <li> <p>Add <code>mock.seal</code> alias to the <code>mocker</code> fixture (<code>[aio-libs#211](https://github.com/pytest-dev/pytest-mock/issues/211)</code><em>). Thanks <code>@coiax</code></em> for the PR.</p> </li> <li> <p>Fixed spying on exceptions not covered by the <code>Exception</code> superclass (<code>[aio-libs#215](https://github.com/pytest-dev/pytest-mock/issues/215)</code><em>), like <code>KeyboardInterrupt</code> -- PR <code>[aio-libs#216](https://github.com/pytest-dev/pytest-mock/issues/216)</code></em> by <code>@webknjaz</code>_.</p> <p>Before the fix, both <code>spy_return</code> and <code>spy_exception</code> were always assigned to <code>None</code> whenever such an exception happened. And after this fix, <code>spy_exception</code> is set to a correct value of an exception that has actually happened.</p> </li> </ul> <p>.. _<a href="https://github.com/coiax">@coiax</a>: <a href="https://github.com/coiax">https://github.com/coiax</a> .. _<a href="https://github.com/webknjaz">@webknjaz</a>: <a href="https://github.com/sponsors/webknjaz">https://github.com/sponsors/webknjaz</a> .. _<a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/211">#211</a>: <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/pull/211">pytest-dev/pytest-mock#211</a> .. _<a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/215">#215</a>: <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/215">pytest-dev/pytest-mock#215</a> .. _<a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/216">#216</a>: <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/pull/216">pytest-dev/pytest-mock#216</a></p> </blockquote> </details> <details> <summary>Commits</summary> <ul> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/cdd5d709f5d1e30526ca30ae93c64fbbaebcd58d"><code>cdd5d70</code></a> Update context-manager docs and prepare for 3.5.0</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/f623fa88211e07ac4965a9079afabe9f23334758"><code>f623fa8</code></a> Warn instead of raising exception in context manager (<a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/221">#221</a>)</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/5f6cab7ead376d4f2de20c32bf01e244ffd44898"><code>5f6cab7</code></a> Merge pull request <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/220">#220</a> from webknjaz/docs/216-changelog</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/9771def1069d3f1560525799ec8ba9d8e2c75ed3"><code>9771def</code></a> Fix typos in a change note for the PR <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/216">#216</a></li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/d4e3f3edd4e085e6b4bc15930a440e759984352b"><code>d4e3f3e</code></a> Merge pull request <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/208">#208</a> from pytest-dev/graingert-patch-1</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/252eba86090fc0522c23805c097eb0c570a2c754"><code>252eba8</code></a> Merge pull request <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/219">#219</a> from nicoddemus/release-3.4.0</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/df9ec17198d9c5d8e26eb91c892c5514b5793b02"><code>df9ec17</code></a> Prepare release 3.4.0</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/91b4afb0b9ab3a454a2e8235da682e7287ab8f32"><code>91b4afb</code></a> Add testing for Python 3.9 (<a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/218">#218</a>)</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/9640a36d06f1b35796e316d3b4529022e9d67fb7"><code>9640a36</code></a> Merge pull request <a href="https://github-redirect.dependabot.com/pytest-dev/pytest-mock/issues/216">#216</a> from webknjaz/bugfixes/215-spy-on-keyboard-interrupt</li> <li><a href="https://github.com/pytest-dev/pytest-mock/commit/0e6b221080f5b8ef425a862ca45052131527894a"><code>0e6b221</code></a> Ensure all the exceptions cat be spied on</li> <li>Additional commits viewable in <a href="https://github.com/pytest-dev/pytest-mock/compare/v3.3.1...v3.5.0">compare view</a></li> </ul> </details> <br /> [![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=pytest-mock&package-manager=pip&previous-version=3.3.1&new-version=3.5.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores) Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end)
Instead of raising an exception, warn when a pytest-mock mock is used as
a context manager. This lets pytest-mock mock objects that are used as
context managers, like threading.Lock, while still letting users know
that mocks returned from pytest-mock do not need their enter method
called to take effect.
Create a PytestMockWarning class so that the warning is easy to
pinpoint and handle.
#192