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-72889: remove redundant `mock.Mock()._is_coroutine = False` workarounds by graingert · Pull Request #94926 · python/cpython

Conversation

@graingert

@graingert graingert commented Jul 17, 2022 •
edited by bedevere-bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

these work-arounds were made redundant by python/asyncio#459

  • Issue: Fix asyncio.iscoroutinefunction to handle Mock objects #72889

Sorry, something went wrong.

Uh oh!

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

All reactions
@graingert
remove redundant mock.Mock()._is_coroutine = False workarounds
bfafdb5
@graingert
graingert requested review from 1st1 and asvetlov as code owners July 17, 2022 13:56
@bedevere-bot bedevere-bot added awaiting review tests Tests in the Lib/test dir labels Jul 17, 2022
graingert
graingert commented Jul 17, 2022
View reviewed changes
Comment thread Lib/test/test_asyncio/test_base_events.py
Comment on lines 1287 to -1289
self.loop._add_reader = mock.Mock()
self.loop._add_reader._is_coroutine = False

@graingert graingert Jul 17, 2022 •
edited
Loading

Uh oh!

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

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.

when asyncio.iscoroutinefunction was implemented as:

cpython/Lib/asyncio/coroutines.py

Lines 245 to 248 in 2f84144

def iscoroutinefunction(func):
"""Return True if func is a decorated coroutine function."""
return (getattr(func, '_is_coroutine', False) or
_inspect_iscoroutinefunction(func))

this meant

self.loop._add_reader = mock.Mock()
assert asyncio.iscoroutinefunction(self.loop._add_reader) is True  # mock objects have a truthy _is_coroutine attribute!
self.loop._add_reader._is_coroutine = False
assert asyncio.iscoroutinefunction(self.loop._add_reader) is False  # patching it works around the issue

however the implementation was changed to use a marker object:

cpython/Lib/asyncio/coroutines.py

Lines 16 to 23 in ec4745b

# A marker for iscoroutinefunction.
_is_coroutine = object()
def iscoroutinefunction(func):
"""Return True if func is a decorated coroutine function."""
return (inspect.iscoroutinefunction(func) or
getattr(func, '_is_coroutine', None) is _is_coroutine)

and so now the _is_coroutine = False work-around is redundant:

self.loop._add_reader = mock.Mock()
assert asyncio.iscoroutinefunction(self.loop._add_reader) is False  # mock objects have an _is_coroutine but it's not the asyncio.coroutines._is_coroutine sentinel
self.loop._add_reader._is_coroutine = False
assert asyncio.iscoroutinefunction(self.loop._add_reader) is False  # it's still False so the workaround is redundant

Sorry, something went wrong.

Uh oh!

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

All reactions
@graingert

graingert commented Jul 17, 2022

Copy link
Copy Markdown
Contributor Author

I don't think this needs a news entry

All reactions

Sorry, something went wrong.

Uh oh!

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

@graingert
Merge branch 'main' into remove-redudant-mock-is-coroutine-workarounds
d794dfd
@gvanrossum gvanrossum added topic-asyncio skip news needs backport to 3.11 only security fixes labels Jul 17, 2022
gvanrossum
gvanrossum approved these changes Jul 17, 2022
View reviewed changes

@gvanrossum gvanrossum left a comment

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.

Since this just changes tests and the tests pass, I think this is okay.

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting review labels Jul 17, 2022
@gvanrossum
gvanrossum merged commit 07aeb74 into python:main Jul 17, 2022
@miss-islington

miss-islington commented Jul 17, 2022

Copy link
Copy Markdown
Contributor

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

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-bot bedevere-bot removed the awaiting merge label Jul 17, 2022
@bedevere-bot

bedevere-bot commented Jul 17, 2022

Copy link
Copy Markdown

GH-94934 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 Jul 17, 2022
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jul 17, 2022
@graingert @miss-islington
pythongh-72889: Remove redundant mock.Mock()._is_coroutine = False wo…
ed2722b
…rkarounds (pythonGH-94926)

(cherry picked from commit 07aeb74)

Co-authored-by: Thomas Grainger <[email protected]>
@graingert
graingert deleted the remove-redudant-mock-is-coroutine-workarounds branch July 17, 2022 17:23
miss-islington added a commit that referenced this pull request Jul 17, 2022
@miss-islington @graingert
gh-72889: Remove redundant mock.Mock()._is_coroutine = False workarou…
8d0249e
…nds (GH-94926)

(cherry picked from commit 07aeb74)

Co-authored-by: Thomas Grainger <[email protected]>
@mdboom mdboom mentioned this pull request Aug 12, 2022
Metabug: Improving C-level coverage #94808
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@gvanrossum gvanrossum gvanrossum 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

Uh oh!

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

Assignees

No one assigned

Labels

skip news tests Tests in the Lib/test dir topic-asyncio

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.

4 participants

@graingert @miss-islington @bedevere-bot @gvanrossum
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.