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

unittest.mock.call does not chain __getitem__ to another _Call object #82153

Closed
blhsing mannequin opened this issue Aug 28, 2019 · 8 comments
Closed

unittest.mock.call does not chain __getitem__ to another _Call object #82153

blhsing mannequin opened this issue Aug 28, 2019 · 8 comments
Labels
3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@blhsing
Copy link
Mannequin

blhsing mannequin commented Aug 28, 2019

BPO 37972
Nosy @cjw296, @voidspace, @mariocj89, @tirkarthi, @blhsing
PRs
  • bpo-37972: unittest.mock._Call now passes on __getitem__ to the __getattr__ chaining so that call() can be subscriptable #15565
  • [3.8] bpo-37972: unittest.mock._Call now passes on __getitem__ to the __getattr__ chaining so that call() can be subscriptable (GH-15565) #15965
  • 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-10-10.14:40:24.310>
    created_at = <Date 2019-08-28.20:33:01.517>
    labels = ['3.8', 'type-bug', 'library', '3.9']
    title = 'unittest.mock.call does not chain __getitem__ to another _Call object'
    updated_at = <Date 2020-01-29.16:25:00.824>
    user = 'https://github.com/blhsing'

    bugs.python.org fields:

    activity = <Date 2020-01-29.16:25:00.824>
    actor = 'cjw296'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-10-10.14:40:24.310>
    closer = 'xtreak'
    components = ['Library (Lib)']
    creation = <Date 2019-08-28.20:33:01.517>
    creator = 'blhsing'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 37972
    keywords = ['patch']
    message_count = 8.0
    messages = ['350688', '351830', '351846', '351877', '351903', '352102', '354383', '360973']
    nosy_count = 6.0
    nosy_names = ['cjw296', 'michael.foord', 'python-dev', 'mariocj89', 'xtreak', 'blhsing']
    pr_nums = ['15565', '15965']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue37972'
    versions = ['Python 3.8', 'Python 3.9']

    @blhsing
    Copy link
    Mannequin Author

    blhsing mannequin commented Aug 28, 2019

    As reported on StackOverflow:
    https://stackoverflow.com/questions/57636747/how-to-perform-assert-has-calls-for-a-getitem-call

    The following code would output: [call(), call().foo(), call().foo().__getitem__('bar')]

    from unittest.mock import MagicMock, call
    mm = MagicMock()
    mm().foo()['bar']
    print(mm.mock_calls)

    but trying to use that list with mm.assert_has_calls([call(), call().foo(), call().foo().__getitem__('bar')]) would result in:

    TypeError: tuple indices must be integers or slices, not str

    @blhsing blhsing mannequin added 3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Aug 28, 2019
    @tirkarthi
    Copy link
    Member

    If we go ahead with this then we should also support other dunder methods of tuple like __setitem__ and so on.

    @tirkarthi tirkarthi removed the 3.7 (EOL) end of life label Sep 11, 2019
    @blhsing
    Copy link
    Mannequin Author

    blhsing mannequin commented Sep 11, 2019

    Agreed. I've submitted a new commit with the call chaining behavior now generalized for all dunder methods by delegating the resolution of all attributes not directly in the _Call object's __dict__ keys to the getattr method.

    @blhsing
    Copy link
    Mannequin Author

    blhsing mannequin commented Sep 11, 2019

    Correction. I've now done this by delegating the resolution of attributes belonging to the tuple class to _Call.__getattr__ instead. Passed all build tests.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Sep 11, 2019

    New changeset 72c3599 by Michael Foord (blhsing) in branch 'master':
    bpo-37972: unittest.mock._Call now passes on __getitem__ to the __getattr__ chaining so that call() can be subscriptable (GH-15565)
    72c3599

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Sep 12, 2019

    New changeset db0d8a5 by Michael Foord (Miss Islington (bot)) in branch '3.8':
    bpo-37972: unittest.mock._Call now passes on __getitem__ to the __getattr__ chaining so that call() can be subscriptable (GH-15565) (GH-15965)
    db0d8a5

    @tirkarthi
    Copy link
    Member

    Closing as fixed since PRs were merged. Thanks Ben.

    @cjw296
    Copy link
    Contributor

    cjw296 commented Jan 29, 2020

    New changeset db5e86a by Chris Withers in branch 'master':
    Get mock coverage back to 100% (GH-18228)
    db5e86a

    @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 stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants