GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-84753: Make inspect.iscoroutinefunction() work with AsyncMock by sileht · Pull Request #94050 · python/cpython

Conversation

@sileht

@sileht sileht commented Jun 21, 2022 •
edited by bedevere-bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

The inspect version was not working with unittest.mock.AsyncMock.

This change moves the asyncio fix for AsyncMock in inspect module and
make asyncio.iscoroutinefunction an alias of inspect.iscoroutinefunction.

  • Issue: inspect.iscorutinefunction() returns False for unittest.mock.AsyncMock instances #84753

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@sileht
sileht requested review from 1st1, asvetlov and cjw296 as code owners June 21, 2022 09:36
@ghost

ghost commented Jun 21, 2022 •
edited by ghost
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.
CLA signed

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot added the awaiting review label Jun 21, 2022
@bedevere-bot

bedevere-bot commented Jun 21, 2022

Copy link
Copy Markdown

Most changes to Python require a NEWS entry.

Please add it using the blurb_it web app or the blurb command-line tool.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sileht
sileht force-pushed the iscoroutinefix branch from a90d15d to 6950183 Compare June 21, 2022 09:40
@sileht

sileht commented Jun 21, 2022

Copy link
Copy Markdown
Contributor Author

@vstinner Can you take a look?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

jd
jd approved these changes Jun 21, 2022
View reviewed changes
@bedevere-bot bedevere-bot added awaiting core review and removed awaiting review labels Jun 21, 2022
@graingert

graingert commented Jun 21, 2022

Copy link
Copy Markdown
Contributor

this shouldn't be needed AsyncMock already sets

cpython/Lib/unittest/mock.py

Lines 2175 to 2178 in 5fcfdd8

code_mock = NonCallableMock(spec_set=CodeType)
code_mock.co_flags = inspect.CO_COROUTINE
self.__dict__['__code__'] = code_mock

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

cjw296
cjw296 reviewed Jun 21, 2022
View reviewed changes
Comment thread Lib/inspect.py Outdated

@cjw296 cjw296 Jun 21, 2022

Copy link
Copy Markdown
Contributor

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.

Why is this here instead of re-using asyncio.coroutines._is_coroutine?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@sileht sileht Jun 21, 2022

Copy link
Copy Markdown
Contributor Author

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.

I moved it from asyncio to inspect as the code using it is now located in inspect module.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@cjw296 cjw296 Jun 21, 2022

Copy link
Copy Markdown
Contributor

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.

_is_coroutine feels like a property of asyncio, not inspect. I think it should stay where it is.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@sileht
sileht force-pushed the iscoroutinefix branch from 6950183 to 2dfabda Compare June 21, 2022 11:04
@sileht

sileht commented Jun 21, 2022

Copy link
Copy Markdown
Contributor Author

this shouldn't be needed AsyncMock already sets

cpython/Lib/unittest/mock.py

Lines 2175 to 2178 in 5fcfdd8

code_mock = NonCallableMock(spec_set=CodeType)
code_mock.co_flags = inspect.CO_COROUTINE
self.__dict__['__code__'] = code_mock

The test cases I added passed was passing for asyncio version, but was not for the inspect version. So something doesn't work as expected.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@graingert

graingert commented Jun 21, 2022

Copy link
Copy Markdown
Contributor

looks like this issue is because mock.AsyncMock() doesn't pass inspect.isfunction:

from unittest import mock
import inspect

m = mock.AsyncMock()
with mock.patch("inspect.isfunction", new=lambda x: True):
    assert inspect.iscoroutinefunction(m)

Originally posted by @graingert in #84753 (comment)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@graingert

graingert commented Jun 21, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Could CallableMixin mess about with __instancecheck__/__subclasscheck__/__class__ to pass isinstance(AsyncMock(), types.FunctionType)?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sileht

sileht commented Jun 24, 2022

Copy link
Copy Markdown
Contributor Author

Could CallableMixin mess about with __instancecheck__/__subclasscheck__/__class__ to pass isinstance(AsyncMock(), types.FunctionType)?

I tried but this doesn't seem to work, I suspect this fall in isinstance C optimisation and __instancecheck__ and friends are never called.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@graingert

graingert commented Jun 24, 2022

Copy link
Copy Markdown
Contributor

I'm relying on inspect.iscoroutinefunction to reveal things that really really are Coroutine Functions, partials or methods and asyncio.iscoroutinefunction to reveal things that are Duck typed as a Coroutine Function

AlexWaygood reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sileht

sileht commented Jun 24, 2022

Copy link
Copy Markdown
Contributor Author

I'm relying on inspect.iscoroutinefunction to reveal things that really really are Coroutine Functions, partials or methods and asyncio.iscoroutinefunction to reveal things that are Duck typed as a Coroutine Function

According to the doc, the only difference is asyncio.iscoroutinefunction also works for deprecated generator based coroutine
This legacy coroutine will(have ?) be removed for 3.11, so now both functions should be the same.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sileht

sileht commented Jun 24, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

But, I agree that my solution doesn't work. We should keep the _is_coroutine hack as-is until generated coroutine are removed and found another solution to make isfunction pass for AsyncMock().

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sileht
sileht force-pushed the iscoroutinefix branch from 2dfabda to 9c7050b Compare June 28, 2022 17:39
@sileht

sileht commented Jun 28, 2022

Copy link
Copy Markdown
Contributor Author

I found another solution, I think it's cleaner.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sileht
sileht force-pushed the iscoroutinefix branch from 9c7050b to bbae9f1 Compare June 28, 2022 17:40
graingert
graingert reviewed Jun 28, 2022
View reviewed changes
Comment thread Lib/test/test_inspect.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@sileht
sileht force-pushed the iscoroutinefix branch 3 times, most recently from fe4fc4e to 970f727 Compare June 28, 2022 17:56
vstinner
vstinner reviewed Jun 28, 2022
View reviewed changes
Comment thread Lib/inspect.py Outdated

@vstinner vstinner Jun 28, 2022

Copy link
Copy Markdown
Member

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.

Can you try to preserve current coding style? Return early with False if a condition is false? Pseudo-code:

if not isfunction(f): return False
if not _signature_is_functionlike(f): return False
return bool(f.__code__.co_flags & flag)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@sileht sileht Jun 29, 2022

Copy link
Copy Markdown
Contributor Author

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.

done

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@sileht
sileht force-pushed the iscoroutinefix branch from 970f727 to ceb50a1 Compare June 29, 2022 06:58
@ambv ambv changed the title gh-84753: make inspect.iscoroutinefunction() works with AsyncMock gh-84753: Make inspect.iscoroutinefunction() work with AsyncMock Jun 30, 2022
@ambv
Make Blurb clearer
812d7f3
@ambv ambv added needs backport to 3.10 needs backport to 3.11 only security fixes labels Jun 30, 2022
@ambv
ambv merged commit 4261b6b into python:main Jun 30, 2022
@bedevere-bot bedevere-bot removed the awaiting merge label Jun 30, 2022
@miss-islington

miss-islington commented Jun 30, 2022

Copy link
Copy Markdown
Contributor

Thanks @sileht for the PR, and @ambv for merging it 🌮🎉.. I'm working now to backport this PR to: 3.10, 3.11.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 30, 2022
@sileht @miss-islington
pythongh-84753: Make inspect.iscoroutinefunction() work with AsyncMock (
b382d0a
pythonGH-94050)

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 <[email protected]>
(cherry picked from commit 4261b6b)

Co-authored-by: Mehdi ABAAKOUK <[email protected]>
@bedevere-bot

bedevere-bot commented Jun 30, 2022

Copy link
Copy Markdown

GH-94460 is a backport of this pull request to the 3.11 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the needs backport to 3.11 only security fixes label Jun 30, 2022
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 30, 2022
@sileht @miss-islington
pythongh-84753: Make inspect.iscoroutinefunction() work with AsyncMock (
c2879e4
pythonGH-94050)

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 <[email protected]>
(cherry picked from commit 4261b6b)

Co-authored-by: Mehdi ABAAKOUK <[email protected]>
@bedevere-bot bedevere-bot removed the needs backport to 3.10 label Jun 30, 2022
@bedevere-bot

bedevere-bot commented Jun 30, 2022

Copy link
Copy Markdown

GH-94461 is a backport of this pull request to the 3.10 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sileht

sileht commented Jun 30, 2022

Copy link
Copy Markdown
Contributor Author

Thanks all! That was fast 🚀

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ambv pushed a commit that referenced this pull request Jun 30, 2022
@miss-islington @sileht
gh-84753: Make inspect.iscoroutinefunction() work with AsyncMock (GH-…
47f23b2
…94050) (GH-94461)

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 <[email protected]>
(cherry picked from commit 4261b6b)

Co-authored-by: Mehdi ABAAKOUK <[email protected]>
@ambv ambv mentioned this pull request Jun 30, 2022
inspect.iscorutinefunction() returns False for unittest.mock.AsyncMock instances #84753
Closed
ambv pushed a commit that referenced this pull request Jun 30, 2022
@miss-islington @sileht
gh-84753: Make inspect.iscoroutinefunction() work with AsyncMock (GH-…
9ebec7d
…94050) (GH-94460)

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 <[email protected]>
(cherry picked from commit 4261b6b)

Co-authored-by: Mehdi ABAAKOUK <[email protected]>
ambv added a commit to ambv/cpython that referenced this pull request Jul 5, 2022
@ambv
pythongh-84753: Clarify change made to inspect functions
18695d7
The wording used in pythonGH-94050 suggests narrower scope than what was actually
implemented. This commit clarifies the full impact of pythonGH-94050 in the change
log.
@ambv ambv mentioned this pull request Jul 5, 2022
gh-84753: Clarify change made to inspect functions #94554
Merged
@graingert graingert mentioned this pull request Jul 17, 2022
make asyncio.iscoroutinefunction a deprecated alias of inspect.iscoroutinefunction and remove asyncio.coroutines._is_coroutine #94912
Closed
This was referenced Jul 19, 2022
iscoroutine returns False for native coroutines in python 3.5+ cython/cython#2092
Closed
asyncio.iscoroutinefunction returns false for Cython async function objects cython/cython#2273
Closed
Make Cython async functions set CO_COROUTINE and CO_ASYNC_GENERATOR code flags cython/cython#4902
Merged
@tirkarthi

tirkarthi commented Aug 23, 2022

Copy link
Copy Markdown
Member

This change seems to have caused a regression reported in #96127

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sobolevn sobolevn mentioned this pull request Aug 27, 2022
gh-96127: Fix inspect.signature call on mocks #96335
Merged
@maximlt maximlt mentioned this pull request Dec 22, 2023
Unpack partial in iscoroutinefunction holoviz/param#894
Merged
1 task done
@da-woods da-woods mentioned this pull request Mar 25, 2024
[BUG] Generators after being cythonized cannot be inspect as generators cython/cython#6096
Closed
@illia-v illia-v mentioned this pull request Nov 9, 2024
Deprecate asyncio.iscoroutinefunction #122858
Closed
@phillipuniverse phillipuniverse mentioned this pull request Nov 5, 2025
Replace asyncio.iscoroutinefunction with inspect.iscoroutinefunction open-telemetry/opentelemetry-python-contrib#3880
Merged
5 of 11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@vstinner vstinner vstinner left review comments
@graingert graingert graingert left review comments
@cjw296 cjw296 cjw296 approved these changes
@1st1 1st1 Awaiting requested review from 1st1 1st1 is a code owner
@asvetlov asvetlov Awaiting requested review from asvetlov asvetlov is a code owner
+1 more reviewer
@jd jd jd approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

9 participants

@sileht @bedevere-bot @graingert @miss-islington @tirkarthi @jd @vstinner @cjw296 @ambv
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.