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

AsyncMock child mocks should detect their type #82344

Closed
lisroach opened this issue Sep 13, 2019 · 5 comments
Closed

AsyncMock child mocks should detect their type #82344

lisroach opened this issue Sep 13, 2019 · 5 comments

Comments

@lisroach
Copy link
Contributor

BPO 38163
Nosy @lisroach, @tirkarthi
PRs
  • bpo-38163: Child mocks detect their type as sync or async #16471
  • [3.8] bpo-38163: Child mocks detect their type as sync or async (GH-16471) #16484
  • 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 = None
    closed_at = <Date 2019-09-30.05:23:09.989>
    created_at = <Date 2019-09-13.17:05:04.520>
    labels = []
    title = 'AsyncMock child mocks should detect their type'
    updated_at = <Date 2019-09-30.05:23:09.988>
    user = 'https://github.com/lisroach'

    bugs.python.org fields:

    activity = <Date 2019-09-30.05:23:09.988>
    actor = 'lisroach'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-09-30.05:23:09.989>
    closer = 'lisroach'
    components = []
    creation = <Date 2019-09-13.17:05:04.520>
    creator = 'lisroach'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 38163
    keywords = ['patch']
    message_count = 5.0
    messages = ['352369', '353263', '353264', '353534', '353537']
    nosy_count = 2.0
    nosy_names = ['lisroach', 'xtreak']
    pr_nums = ['16471', '16484']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue38163'
    versions = []

    @lisroach
    Copy link
    Contributor Author

    We want child mocks to match their "type" (async or sync).

    Ex:

    class Prod:
       async def foo():
          pass
        def bar():
           pass
    
    a_mock = AsyncMock(Prod)
    isinstance(a_mock.foo(), AsyncMock) == True
    isinstance(a_mock.bar(), MagicMock) == True

    Also this should include magic methods:

    int(a_mock) should work and return 1 like MagicMocks do.
    a_mock.__enter__ should exist and be a MagicMock, just as a_mock.__aenter__ exists and is an AsyncMock.

    @lisroach
    Copy link
    Contributor Author

    This should be simple to do, after #16060 we can add:

                elif self._mock_methods and _new_name in self._mock_methods:
                    # Methods that are not in _spec_asyncs are normal methods
                    klass = MagicMock

    to _get_child_mocks and test with:

        def test_normal_methods_on_class(self):
            am = AsyncMock(AsyncClass)
            self.assertIsInstance(am.async_method, AsyncMock)
            self.assertIsInstance(am.normal_method, MagicMock)

    This might be starting to make _get_child_mocks too complicated, and no longer follows the default "child mocks are the same as their parent". I am not sure how to redefine _get_child_mocks without needing to double up a lot of code to support MagicMocks having AsyncMock child_mocks as well.

    @lisroach
    Copy link
    Contributor Author

    Lately I have been having my doubts around having MagicMocks also create AsyncMocks as children.

    Should a MagicMock really have a __aenter__ magic method?
    Is this too far from the original MagicMock if we change child mocks to default not to their parent type, but to their parent type *or* AsyncMock if asynchronous?

    It makes the using the mocks easier, since you can just use MagicMock or AsyncMock for whatever you are doing and not think about when to use which. But it also might make the code more confusing, since it isn't as clear when to use one or the other.

    @lisroach
    Copy link
    Contributor Author

    New changeset 3667e1e by Lisa Roach in branch 'master':
    bpo-38163: Child mocks detect their type as sync or async (GH-16471)
    3667e1e

    @lisroach
    Copy link
    Contributor Author

    New changeset 21f24ea by Lisa Roach in branch '3.8':
    [3.8] bpo-38163: Child mocks detect their type as sync or async (GH-16471) (GH-16484)
    21f24ea

    @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
    None yet
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant