Skip to content

AsyncMock force always creating an AsyncMock for child mocks #82303

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

Open
lisroach opened this issue Sep 11, 2019 · 4 comments
Open

AsyncMock force always creating an AsyncMock for child mocks #82303

lisroach opened this issue Sep 11, 2019 · 4 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@lisroach
Copy link
Contributor

BPO 38122
Nosy @cjw296, @ezio-melotti, @voidspace, @phmc, @matrixise, @lisroach, @tirkarthi
PRs
  • bpo-38122: minor fixes to AsyncMock spec handling #16099
  • [3.8] bpo-38122: minor fixes to AsyncMock spec handling (GH-16099). #16137
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/lisroach'
    closed_at = None
    created_at = <Date 2019-09-11.17:24:54.683>
    labels = ['type-bug', 'library']
    title = 'AsyncMock force always creating an AsyncMock for child mocks'
    updated_at = <Date 2019-10-28.20:47:37.252>
    user = 'https://github.com/lisroach'

    bugs.python.org fields:

    activity = <Date 2019-10-28.20:47:37.252>
    actor = 'pconnell'
    assignee = 'lisroach'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2019-09-11.17:24:54.683>
    creator = 'lisroach'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 38122
    keywords = ['patch']
    message_count = 4.0
    messages = ['351991', '352000', '352101', '352360']
    nosy_count = 7.0
    nosy_names = ['cjw296', 'ezio.melotti', 'michael.foord', 'pconnell', 'matrixise', 'lisroach', 'xtreak']
    pr_nums = ['16099', '16137']
    priority = 'low'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue38122'
    versions = []

    @lisroach
    Copy link
    Contributor Author

    This idea has been brought up in person before and also discussed on AsyncTest: Martiusweb/asynctest#100

    It could be useful if someone has a lot of attributes that are also async that need to be mocked.

    It could probably be done with a flag that gets passed on to _get_child_mock and overrides the if statement to always return an AsyncMock.

    Looking mostly for if people think this is a good idea/bad idea and help see around any corners that may be there.

    @lisroach lisroach self-assigned this Sep 11, 2019
    @lisroach lisroach added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Sep 11, 2019
    @tirkarthi
    Copy link
    Member

    Sorry, is there an example of this use case. I went through the issue but have trouble understanding it since there are different suggestions. I guess child mock is always an AsyncMock irrespective of sync/async as I understand. We already had some reports over detecting async while patching to return AsyncMock and also had to document it to the users along with covering different ways of async functions.

    from unittest.mock import AsyncMock
    
    
    class Foo:
    
        def foo(self):
            pass
    
        async def bar(self):
            pass
    
    m = AsyncMock(Foo)
    f = m()
    print(m.foo)
    print(m.bar)
    $ python3.8 /tmp/bar.py
    <AsyncMock name='mock.foo' id='4313089744'>
    <AsyncMock name='mock.bar' id='4313395696'>
    sys:1: RuntimeWarning: coroutine 'AsyncMockMixin._mock_call' was never awaited

    @voidspace
    Copy link
    Contributor

    That may change though, right?

    In general I dislike the proliferation of keyword arguments if it's at all possible to avoid. I added way too many of them originally and the mock API is really big.

    @matrixise
    Copy link
    Member

    New changeset 14fd925 by Stéphane Wirtel (Michael Foord) in branch 'master':
    bpo-38122: minor fixes to AsyncMock spec handling (GH-16099)
    14fd925

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    Status: No status
    Development

    No branches or pull requests

    4 participants