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

ordered keyword arguments in unittest.mock.call repr and error messages #81393

Closed
tirkarthi opened this issue Jun 10, 2019 · 5 comments
Closed
Labels
3.8 (EOL) end of life 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@tirkarthi
Copy link
Member

BPO 37212
Nosy @cjw296, @voidspace, @zware, @mariocj89, @miss-islington, @tirkarthi
PRs
  • bpo-37212: Preserve keyword argument order in unittest.mock.call and error messages #14310
  • [3.8] bpo-37212: Preserve keyword argument order in unittest.mock.call and error messages (GH-14310) #15755
  • 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-09.10:59:38.841>
    created_at = <Date 2019-06-10.07:53:37.319>
    labels = ['3.8', 'type-bug', 'library', '3.9']
    title = 'ordered keyword arguments in unittest.mock.call repr and error messages'
    updated_at = <Date 2019-09-09.11:42:46.084>
    user = 'https://github.com/tirkarthi'

    bugs.python.org fields:

    activity = <Date 2019-09-09.11:42:46.084>
    actor = 'miss-islington'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-09-09.10:59:38.841>
    closer = 'zach.ware'
    components = ['Library (Lib)']
    creation = <Date 2019-06-10.07:53:37.319>
    creator = 'xtreak'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 37212
    keywords = ['patch']
    message_count = 5.0
    messages = ['345106', '346270', '346281', '351426', '351435']
    nosy_count = 7.0
    nosy_names = ['cjw296', 'michael.foord', 'zach.ware', 'mariocj89', 'miss-islington', 'xtreak', 'dhilst']
    pr_nums = ['14310', '15755']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue37212'
    versions = ['Python 3.8', 'Python 3.9']

    @tirkarthi
    Copy link
    Member Author

    With PEP-468 implemented in Python 3.6 the order of keyword arguments are preserved. In mock.call the arguments are sorted [0]. This makes the output different from the expectation that order should be same as the one passed. This was implemented in bpo-21256 where ordered kwargs was suggested but I guess it was not implemented and hence sort was used for deterministic output. The PEP also links to the discussion where ordered kwargs was discussed [1] in 2009 before ordered dict implementation.

    Given that keyword argument dictionary is now deterministic is it okay to drop sorting in 3.9? This is backwards incompatible with 3.8 where code might have depended upon the sorted output so if this requires a python-dev discussion I would be happy to start one.

    # (b=1, a=2) is the actual call but "actual" in error message is noted as (a=2, b=1)

    >>> from unittest.mock import call, Mock
    >>> call(b=1, a=2)
    call(a=2, b=1)
    >>> m = Mock()
    >>> m(b=1, a=2)
    <Mock name='mock()' id='4477750800'>
    >>> m.assert_called_with(c=3)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/unittest/mock.py", line 870, in assert_called_with
        raise AssertionError(_error_message()) from cause
    AssertionError: expected call not found.
    Expected: mock(c=3)
    Actual: mock(a=2, b=1)

    # With proposed change removing sorting

    >>> from unittest.mock import call, Mock
    >>> call(b=1, a=2)
    call(b=1, a=2)
    >>> m = Mock()
    >>> m(b=1, a=2)
    <Mock name='mock()' id='4419010880'>
    >>> m.assert_called_with(c=3)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/unittest/mock.py", line 870, in assert_called_with
        raise AssertionError(_error_message()) from cause
    AssertionError: expected call not found.
    Expected: mock(c=3)
    Actual: mock(b=1, a=2)

    [0]

    '%s=%r' % (key, value) for key, value in sorted(kwargs.items())

    [1] https://mail.python.org/pipermail/python-ideas/2009-April/004163.html

    @tirkarthi tirkarthi added 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jun 10, 2019
    @tirkarthi
    Copy link
    Member Author

    @tirkarthi
    Copy link
    Member Author

    I will create a PR for this as per mailing list discussion : https://mail.python.org/archives/list/python-dev@python.org/message/JWPDLKNM5X27FRWJG7UOAFXHNRUGH44S/

    @zware
    Copy link
    Member

    zware commented Sep 9, 2019

    New changeset 9d60706 by Zachary Ware (Xtreak) in branch 'master':
    bpo-37212: Preserve keyword argument order in unittest.mock.call and error messages (GH-14310)
    9d60706

    @zware zware added the 3.8 (EOL) end of life label Sep 9, 2019
    @zware zware closed this as completed Sep 9, 2019
    @miss-islington
    Copy link
    Contributor

    New changeset bee8bfe by Miss Islington (bot) in branch '3.8':
    bpo-37212: Preserve keyword argument order in unittest.mock.call and error messages (GH-14310)
    bee8bfe

    @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 (EOL) end of life 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

    3 participants