Skip to content

Conversation

@lisroach
Copy link
Contributor

@lisroach lisroach commented Sep 29, 2019

This should be the last diff to get the child mocks detecting their type as either synchronous or asynchronous and returning AsyncMock or MagicMock (or Mock) depending on their type.

The main goal is for classes that have a mix of asynchronous and synchronous methods to auto-detect whether they should be AsyncMock or a synchronous mocks. Example:

  >>> class ExampleClass:
  ...     def sync_foo():
  ...         pass
  ...     async def async_foo():
  ...         pass
  ...
  >>> a_mock = AsyncMock(ExampleClass)
  >>> a_mock.sync_foo
  <MagicMock name='a_mock.sync_foo' id='...'>
  >>> a_mock.async_foo
  <AsyncMock name='a_mock.async_foo' id='...'>
  >>> mock = Mock(ExampleClass)
  >>> mock.sync_foo
  <Mock name='mock.sync_foo' id='...'>
  >>> mock.async_foo
  <AsyncMock name='mock.async_foo' id='...'>

This defies Mock's typical functionality where all child mocks follow the type of their parent mock, but after some discussion I believe the auto-detection is more important to the typical user than a consistency with current Mock style.

https://bugs.python.org/issue38163

@lisroach lisroach merged commit 3667e1e into python:master Sep 30, 2019
@lisroach lisroach deleted the issue38163 branch September 30, 2019 04:56
@miss-islington
Copy link
Contributor

Thanks @lisroach for the PR 🌮🎉.. I'm working now to backport this PR to: 3.8.
🐍🍒⛏🤖

@miss-islington
Copy link
Contributor

I'm having trouble backporting to 3.8. Reason: 'Error 110 while writing to socket. Connection timed out.'. Please retry by removing and re-adding the needs backport to 3.8 label.

@miss-islington
Copy link
Contributor

Thanks @lisroach for the PR 🌮🎉.. I'm working now to backport this PR to: 3.8.
🐍🍒⛏🤖

@miss-islington
Copy link
Contributor

Sorry @lisroach, I had trouble checking out the 3.8 backport branch.
Please backport using cherry_picker on command line.
cherry_picker 3667e1ee6c90e6d3b6a745cd590ece87118f81ad 3.8

lisroach added a commit to lisroach/cpython that referenced this pull request Sep 30, 2019
@bedevere-bot
Copy link

GH-16484 is a backport of this pull request to the 3.8 branch.

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.

4 participants