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

Update MagicMock __aenter__ and __aexit__ to return AsyncMock's #82274

Closed
lisroach opened this issue Sep 10, 2019 · 3 comments
Closed

Update MagicMock __aenter__ and __aexit__ to return AsyncMock's #82274

lisroach opened this issue Sep 10, 2019 · 3 comments
Assignees
Labels
3.8 only security fixes 3.9 only security fixes type-bug An unexpected behavior, bug, or error

Comments

@lisroach
Copy link
Contributor

BPO 38093
Nosy @ezio-melotti, @voidspace, @matrixise, @lisroach, @tirkarthi
PRs
  • bpo-38093: Correctly returns AsyncMock for async subclasses. #15947
  • [3.8] bpo-38093: Correctly returns AsyncMock for async subclasses. (GH-15947) #16299
  • 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 = <Date 2019-09-30.04:22:17.343>
    created_at = <Date 2019-09-10.14:42:41.155>
    labels = ['type-bug', '3.8', '3.9']
    title = "Update MagicMock __aenter__ and __aexit__ to return AsyncMock's"
    updated_at = <Date 2019-09-30.04:22:17.343>
    user = 'https://github.com/lisroach'

    bugs.python.org fields:

    activity = <Date 2019-09-30.04:22:17.343>
    actor = 'lisroach'
    assignee = 'lisroach'
    closed = True
    closed_date = <Date 2019-09-30.04:22:17.343>
    closer = 'lisroach'
    components = []
    creation = <Date 2019-09-10.14:42:41.155>
    creator = 'lisroach'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 38093
    keywords = ['patch']
    message_count = 3.0
    messages = ['351682', '352829', '352928']
    nosy_count = 5.0
    nosy_names = ['ezio.melotti', 'michael.foord', 'matrixise', 'lisroach', 'xtreak']
    pr_nums = ['15947', '16299']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue38093'
    versions = ['Python 3.8', 'Python 3.9']

    @lisroach
    Copy link
    Contributor Author

    After a discussion with Michael, xtreak, and Ezio we've decided to try to make the process of mocking an async context manager slightly easier.

    Currently if you want to mock a context manager that is used like this:

    async with cm():  # note that cm is called here
      blah

    You need to mock cm as a MagicMock and set __aenter__ and __aexit__ return values directly because they do not exist on MagicMocks.

    Our first step to making this easier is setting the calculated return value of MagicMock __aenter__ and __aexit__ to be AsyncMock, which will make it so you do not need to set them specifically.

    A future improvement may be to create a ContextManagerMock directly that can take an async kwarg that would return a context manager with AsyncMocks for __aenter__ and __aexit__ automatically.

    @lisroach lisroach added 3.8 only security fixes 3.9 only security fixes labels Sep 10, 2019
    @lisroach lisroach self-assigned this Sep 10, 2019
    @lisroach lisroach added the type-bug An unexpected behavior, bug, or error label Sep 10, 2019
    @lisroach
    Copy link
    Contributor Author

    New changeset 8b03f94 by Lisa Roach in branch 'master':
    bpo-38093: Correctly returns AsyncMock for async subclasses. (GH-15947)
    8b03f94

    @matrixise
    Copy link
    Member

    New changeset 865bb68 by Stéphane Wirtel (Lisa Roach) in branch '3.8':
    [3.8] bpo-38093: Correctly returns AsyncMock for async subclasses. (GH-15947) (GH-16299)
    865bb68

    @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
    3.8 only security fixes 3.9 only security fixes type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants