Content-Length: 338696 | pFad | https://github.com/python/cpython/issues/98086

15 `unittest.mock.patch.dict` returns function when applied to coroutines · Issue #98086 · python/cpython · GitHub
Skip to content
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

unittest.mock.patch.dict returns function when applied to coroutines #98086

Closed
seifertm opened this issue Oct 8, 2022 · 1 comment
Closed
Labels
type-bug An unexpected behavior, bug, or error

Comments

@seifertm
Copy link

seifertm commented Oct 8, 2022

Bug report

unittest.mock.patch.dict returns a synchronous function when applied to coroutines. Contrary to that, unittest.mock.patch returns coroutines when applied to coroutines (see #81177).

My expectation is that patch and patch.dict behave the same when applied to coroutines. I expect the following code to print True and True, but it prints True and False for CPython>=3.8:

import inspect
from unittest.mock import Mock, patch


@patch("binascii.b64encode")
async def patch_coro():
    ...


@patch.dict("sys.modules", test_module=Mock())
async def patch_dict_coro():
    ...

print(inspect.iscoroutinefunction(patch_coro))
print(inspect.iscoroutinefunction(patch_dict_coro))

Your environment

  • CPython versions tested on: 3.7, 3.8, 3.9, 3.10, 3.11-rc2
  • Operating system and architecture: Linux x86_64

CPython v3.7 is not affected by this issue, because support for coroutines was added to mock.patch in v3.8 (see #81177). The supplied example prints False and False.

@seifertm seifertm added the type-bug An unexpected behavior, bug, or error label Oct 8, 2022
sobolevn added a commit to sobolevn/cpython that referenced this issue Oct 8, 2022
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 11, 2022
…onGH-98095)

(cherry picked from commit 67b4d27)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 11, 2022
…onGH-98095)

(cherry picked from commit 67b4d27)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
ethanfurman pushed a commit to ethanfurman/cpython that referenced this issue Nov 12, 2022
cjw296 pushed a commit that referenced this issue Nov 19, 2022
…98095) (#99365)

gh-98086: Now ``patch.dict`` can decorate async functions (GH-98095)
(cherry picked from commit 67b4d27)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
tirkarthi pushed a commit that referenced this issue Nov 20, 2022
…98095) (GH-99366)

gh-98086: Now ``patch.dict`` can decorate async functions (GH-98095)
(cherry picked from commit 67b4d27)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Co-authored-by: Chris Withers <chris@withers.org>
@hauntsaninja
Copy link
Contributor

Thanks everyone, looks like this has been fixed and backported

bmwiedemann pushed a commit to bmwiedemann/openSUSE that referenced this issue Jun 3, 2023
https://build.opensuse.org/request/show/1090625
by user mcepl + dimstar_suse
- Add 99366-patch.dict-can-decorate-async.patch fixing
  gh#python/cpython#98086 (backport from Python 3.10 patch in
  gh#python/cpython!99366), fixing bsc#1211158.
- Add CVE-2007-4559-filter-tarfile_extractall.patch to fix
  CVE-2007-4559 (bsc#1203750) by adding the filter for
  tarfile.extractall (PEP 706).
mcepl added a commit to openSUSE-Python/cpython that referenced this issue Apr 3, 2024
Code is from gh#python/cpython@88b101f, it was released
upstream in 3.10.9.

Fixes: gh#python#98086
mcepl added a commit to openSUSE-Python/cpython that referenced this issue Apr 4, 2024
Code is from gh#python/cpython@88b101f, it was released
upstream in 3.10.9.

Fixes: gh#python#98086
Patch: 99366-patch.dict-can-decorate-async.patch
mcepl added a commit to openSUSE-Python/cpython that referenced this issue Apr 4, 2024
Code is from gh#python/cpython@88b101f, it was released
upstream in 3.10.9.

Fixes: gh#python#98086
Patch: 99366-patch.dict-can-decorate-async.patch
mcepl added a commit to openSUSE-Python/cpython that referenced this issue Apr 4, 2024
Code is from gh#python/cpython@88b101f, it was released
upstream in 3.10.9.

Fixes: gh#python#98086
Patch: 99366-patch.dict-can-decorate-async.patch
mcepl added a commit to openSUSE-Python/cpython that referenced this issue Apr 4, 2024
Code is from gh#python/cpython@88b101f, it was released
upstream in 3.10.9.

Fixes: gh#python#98086
Patch: 99366-patch.dict-can-decorate-async.patch
mcepl added a commit to openSUSE-Python/cpython that referenced this issue Apr 4, 2024
Code is from gh#python/cpython@88b101f, it was released
upstream in 3.10.9.

Fixes: gh#python#98086
Patch: 99366-patch.dict-can-decorate-async.patch
mcepl added a commit to openSUSE-Python/cpython that referenced this issue Apr 4, 2024
Code is from gh#python/cpython@88b101f, it was released
upstream in 3.10.9.

Fixes: gh#python#98086
Patch: 99366-patch.dict-can-decorate-async.patch
mcepl added a commit to openSUSE-Python/cpython that referenced this issue Apr 4, 2024
Code is from gh#python/cpython@88b101f, it was released
upstream in 3.10.9.

Fixes: gh#python#98086
Patch: 99366-patch.dict-can-decorate-async.patch
mcepl added a commit to openSUSE-Python/cpython that referenced this issue Apr 4, 2024
Code is from gh#python/cpython@88b101f, it was released
upstream in 3.10.9.

Fixes: gh#python#98086
Patch: 99366-patch.dict-can-decorate-async.patch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://github.com/python/cpython/issues/98086

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy