Skip to content

Conversation

@inikolaev
Copy link
Contributor

@inikolaev inikolaev commented Oct 15, 2023

This PR adds support for resetall to create_autospec to fix #389

  1. I have added a new create_autospec function to the MockerFixture, that wraps original create_autospec and adds a reference to the _patches_and_mocks list, so it could be reset by resetall.

  2. stopall had to be modified in order to ignore None patcher, maybe there's a better way to do this instead.

  3. Added new test cases to test_mocker_resetall test

  4. test_mocker_aliases is failing - maybe we don't need to test it for create_autospec anymore, since we have it tested in test_mocker_resetall?

Comment on lines +221 to +224
if sys.version_info >= (3, 9):
# The reset on child mocks have been implemented in 3.9
# https://bugs.python.org/issue38932
assert mocked_object.run.return_value != "mocked"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't really like this check here, maybe test needs to be split, and then it's easier to use pytest.mark.skipif to skip for 3.8 and below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe have a completely separate test for resetting child mocks

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check is fine, when we drop support for Python versions it is common to search for sys.version_info checks like these. 👍

@nicoddemus
Copy link
Member

Thanks @inikolaev for the patch, appreciate it!

stopall had to be modified in order to ignore None patcher, maybe there's a better way to do this instead.

Seems good enough. 👍

test_mocker_aliases is failing - maybe we don't need to test it for create_autospec anymore, since we have it tested in test_mocker_resetall?

Yes. 👍

Copy link
Member

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @inikolaev, looks great so far!

Please take a look at my comments.

"""
for p, m in reversed(self._patches_and_mocks):
p.stop()
if p:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's be more explicit here:

Suggested change
if p:
if p is not None:

Given p is Any, is theoretically possible for it to implement __bool__ and return False for some reason.

Comment on lines +221 to +224
if sys.version_info >= (3, 9):
# The reset on child mocks have been implemented in 3.9
# https://bugs.python.org/issue38932
assert mocked_object.run.return_value != "mocked"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check is fine, when we drop support for Python versions it is common to search for sys.version_info checks like these. 👍

@nicoddemus nicoddemus merged commit 2a71a0d into pytest-dev:main Oct 19, 2023
github-actions bot referenced this pull request in mikelane/reddit-get Oct 20, 2023
Bumps [pytest-mock](https://github.com/pytest-dev/pytest-mock) from
3.11.1 to 3.12.0.
<details>
<summary>Release notes</summary>
<p><em>Sourced from <a
href="https://github.com/pytest-dev/pytest-mock/releases">pytest-mock's
releases</a>.</em></p>
<blockquote>
<h2>v3.12.0</h2>
<ul>
<li>Added support for Python 3.12.</li>
<li>Dropped support for EOL Python 3.7.</li>
<li><code>mocker.resetall()</code> now also resets mocks created by
<code>mocker.create_autospec</code> (<a
href="https://redirect.github.com/pytest-dev/pytest-mock/pull/390">#390</a>).</li>
</ul>
</blockquote>
</details>
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a
href="https://github.com/pytest-dev/pytest-mock/blob/main/CHANGELOG.rst">pytest-mock's
changelog</a>.</em></p>
<blockquote>
<h2>3.12.0 (2023-10-19)</h2>
<ul>
<li>Added support for Python 3.12.</li>
<li>Dropped support for EOL Python 3.7.</li>
<li><code>mocker.resetall()</code> now also resets mocks created by
<code>mocker.create_autospec</code>
(<code>[#390](https://github.com/pytest-dev/pytest-mock/issues/390)</code>_).</li>
</ul>
<p>.. _<a
href="https://redirect.github.com/pytest-dev/pytest-mock/issues/390">#390</a>:
<a
href="https://redirect.github.com/pytest-dev/pytest-mock/pull/390">pytest-dev/pytest-mock#390</a></p>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a
href="https://github.com/pytest-dev/pytest-mock/commit/69adc6f76c1a7baf4e7a728da9eec38741d5783e"><code>69adc6f</code></a>
Re-add norewrite tests</li>
<li><a
href="https://github.com/pytest-dev/pytest-mock/commit/b905823ba6894f89b1ecd9aafcabf1e20612ac22"><code>b905823</code></a>
Drop Python 3.7 and add Python 3.12</li>
<li><a
href="https://github.com/pytest-dev/pytest-mock/commit/2a71a0d6ab8719defc9309da20bd97b7b35262b6"><code>2a71a0d</code></a>
mocker.resetall now also resets mockers created by
<code>create_autospec</code> (<a
href="https://redirect.github.com/pytest-dev/pytest-mock/issues/390">#390</a>)</li>
<li><a
href="https://github.com/pytest-dev/pytest-mock/commit/e84f88501188504f1fa6e56e0d0e6604985298a1"><code>e84f885</code></a>
Merge pull request <a
href="https://redirect.github.com/pytest-dev/pytest-mock/issues/391">#391</a>
from pytest-dev/pre-commit-ci-update-config</li>
<li><a
href="https://github.com/pytest-dev/pytest-mock/commit/e0372217cdd057eb67ffe416177880c141cd3adf"><code>e037221</code></a>
[pre-commit.ci] pre-commit autoupdate</li>
<li><a
href="https://github.com/pytest-dev/pytest-mock/commit/f019696e44adb0d6159bea78172ed8d0aeed10e5"><code>f019696</code></a>
Merge pull request <a
href="https://redirect.github.com/pytest-dev/pytest-mock/issues/387">#387</a>
from pytest-dev/where-to-patch-link</li>
<li><a
href="https://github.com/pytest-dev/pytest-mock/commit/09f18148a5b1a2109914f47198c8d8fb1f836307"><code>09f1814</code></a>
Merge pull request <a
href="https://redirect.github.com/pytest-dev/pytest-mock/issues/388">#388</a>
from pytest-dev/pre-commit-ci-update-config</li>
<li><a
href="https://github.com/pytest-dev/pytest-mock/commit/f55f17e73b469034dbb87e5fadbb3e103913826a"><code>f55f17e</code></a>
[pre-commit.ci] pre-commit autoupdate</li>
<li><a
href="https://github.com/pytest-dev/pytest-mock/commit/cec59a0b444efc3f1fb03308460cad6bf5a81709"><code>cec59a0</code></a>
Update usage.rst</li>
<li><a
href="https://github.com/pytest-dev/pytest-mock/commit/53cc0a550fb6dbb9c1eafc2c96cc5c2bf02c4185"><code>53cc0a5</code></a>
Update usage.rst</li>
<li>Additional commits viewable in <a
href="https://github.com/pytest-dev/pytest-mock/compare/v3.11.1...v3.12.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.11.1&new-version=3.12.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)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>
github-actions bot referenced this pull request in aio-libs/aiohttp Oct 20, 2023
Bumps [pytest-mock](https://github.com/pytest-dev/pytest-mock) from
3.11.1 to 3.12.0.
<details>
<summary>Release notes</summary>
<p><em>Sourced from <a
href="https://github.com/pytest-dev/pytest-mock/releases">pytest-mock's
releases</a>.</em></p>
<blockquote>
<h2>v3.12.0</h2>
<ul>
<li>Added support for Python 3.12.</li>
<li>Dropped support for EOL Python 3.7.</li>
<li><code>mocker.resetall()</code> now also resets mocks created by
<code>mocker.create_autospec</code> (<a
href="https://redirect.github.com/pytest-dev/pytest-mock/pull/390">#390</a>).</li>
</ul>
</blockquote>
</details>
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a
href="https://github.com/pytest-dev/pytest-mock/blob/main/CHANGELOG.rst">pytest-mock's
changelog</a>.</em></p>
<blockquote>
<h2>3.12.0 (2023-10-19)</h2>
<ul>
<li>Added support for Python 3.12.</li>
<li>Dropped support for EOL Python 3.7.</li>
<li><code>mocker.resetall()</code> now also resets mocks created by
<code>mocker.create_autospec</code>
(<code>[#390](https://github.com/pytest-dev/pytest-mock/issues/390)</code>_).</li>
</ul>
<p>.. _<a
href="https://redirect.github.com/pytest-dev/pytest-mock/issues/390">#390</a>:
<a
href="https://redirect.github.com/pytest-dev/pytest-mock/pull/390">pytest-dev/pytest-mock#390</a></p>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a
href="https://github.com/pytest-dev/pytest-mock/commit/69adc6f76c1a7baf4e7a728da9eec38741d5783e"><code>69adc6f</code></a>
Re-add norewrite tests</li>
<li><a
href="https://github.com/pytest-dev/pytest-mock/commit/b905823ba6894f89b1ecd9aafcabf1e20612ac22"><code>b905823</code></a>
Drop Python 3.7 and add Python 3.12</li>
<li><a
href="https://github.com/pytest-dev/pytest-mock/commit/2a71a0d6ab8719defc9309da20bd97b7b35262b6"><code>2a71a0d</code></a>
mocker.resetall now also resets mockers created by
<code>create_autospec</code> (<a
href="https://redirect.github.com/pytest-dev/pytest-mock/issues/390">#390</a>)</li>
<li><a
href="https://github.com/pytest-dev/pytest-mock/commit/e84f88501188504f1fa6e56e0d0e6604985298a1"><code>e84f885</code></a>
Merge pull request <a
href="https://redirect.github.com/pytest-dev/pytest-mock/issues/391">#391</a>
from pytest-dev/pre-commit-ci-update-config</li>
<li><a
href="https://github.com/pytest-dev/pytest-mock/commit/e0372217cdd057eb67ffe416177880c141cd3adf"><code>e037221</code></a>
[pre-commit.ci] pre-commit autoupdate</li>
<li><a
href="https://github.com/pytest-dev/pytest-mock/commit/f019696e44adb0d6159bea78172ed8d0aeed10e5"><code>f019696</code></a>
Merge pull request <a
href="https://redirect.github.com/pytest-dev/pytest-mock/issues/387">#387</a>
from pytest-dev/where-to-patch-link</li>
<li><a
href="https://github.com/pytest-dev/pytest-mock/commit/09f18148a5b1a2109914f47198c8d8fb1f836307"><code>09f1814</code></a>
Merge pull request <a
href="https://redirect.github.com/pytest-dev/pytest-mock/issues/388">#388</a>
from pytest-dev/pre-commit-ci-update-config</li>
<li><a
href="https://github.com/pytest-dev/pytest-mock/commit/f55f17e73b469034dbb87e5fadbb3e103913826a"><code>f55f17e</code></a>
[pre-commit.ci] pre-commit autoupdate</li>
<li><a
href="https://github.com/pytest-dev/pytest-mock/commit/cec59a0b444efc3f1fb03308460cad6bf5a81709"><code>cec59a0</code></a>
Update usage.rst</li>
<li><a
href="https://github.com/pytest-dev/pytest-mock/commit/53cc0a550fb6dbb9c1eafc2c96cc5c2bf02c4185"><code>53cc0a5</code></a>
Update usage.rst</li>
<li>Additional commits viewable in <a
href="https://github.com/pytest-dev/pytest-mock/compare/v3.11.1...v3.12.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.11.1&new-version=3.12.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)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
renovate bot referenced this pull request in allenporter/pyrainbird Oct 21, 2023
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [pytest-mock](https://togithub.com/pytest-dev/pytest-mock)
([changelog](https://pytest-mock.readthedocs.io/en/latest/changelog.html))
| `==3.11.1` -> `==3.12.0` |
[![age](https://developer.mend.io/api/mc/badges/age/pypi/pytest-mock/3.12.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/pypi/pytest-mock/3.12.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/pypi/pytest-mock/3.11.1/3.12.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/pypi/pytest-mock/3.11.1/3.12.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>pytest-dev/pytest-mock (pytest-mock)</summary>

###
[`v3.12.0`](https://togithub.com/pytest-dev/pytest-mock/blob/HEAD/CHANGELOG.rst#3120-2023-10-19)

[Compare
Source](https://togithub.com/pytest-dev/pytest-mock/compare/v3.11.1...v3.12.0)

-   Added support for Python 3.12.
-   Dropped support for EOL Python 3.7.
- `mocker.resetall()` now also resets mocks created by
`mocker.create_autospec` (`#390`\_).

..
\_#390:[https://github.com/pytest-dev/pytest-mock/pull/390](https://togithub.com/pytest-dev/pytest-mock/pull/390)0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/allenporter/pyrainbird).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4xOS4yIiwidXBkYXRlZEluVmVyIjoiMzcuMTkuMiIsInRhcmdldEJyYW5jaCI6Im1haW4ifQ==-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
@renovate renovate bot mentioned this pull request Oct 20, 2024
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

resetall does not work when using create_autospec

2 participants