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

mock could be smarter and inspect the spec's signature #61217

Closed
pitrou opened this issue Jan 22, 2013 · 9 comments
Closed

mock could be smarter and inspect the spec's signature #61217

pitrou opened this issue Jan 22, 2013 · 9 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@pitrou
Copy link
Member

pitrou commented Jan 22, 2013

BPO 17015
Nosy @pitrou, @ezio-melotti, @merwok, @voidspace
Dependencies
  • bpo-17071: Signature.bind() fails with a keyword argument named "self"
  • Files
  • issue17015.patch
  • issue17015-3.patch
  • 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 2013-02-02.23:29:01.035>
    created_at = <Date 2013-01-22.12:44:11.909>
    labels = ['type-feature', 'library']
    title = "mock could be smarter and inspect the spec's signature"
    updated_at = <Date 2013-02-02.23:29:01.034>
    user = 'https://github.com/pitrou'

    bugs.python.org fields:

    activity = <Date 2013-02-02.23:29:01.034>
    actor = 'pitrou'
    assignee = 'none'
    closed = True
    closed_date = <Date 2013-02-02.23:29:01.035>
    closer = 'pitrou'
    components = ['Library (Lib)']
    creation = <Date 2013-01-22.12:44:11.909>
    creator = 'pitrou'
    dependencies = ['17071']
    files = ['28886', '28903']
    hgrepos = []
    issue_num = 17015
    keywords = ['patch']
    message_count = 9.0
    messages = ['180381', '180382', '180886', '180945', '180976', '180981', '180982', '181229', '181230']
    nosy_count = 5.0
    nosy_names = ['pitrou', 'ezio.melotti', 'eric.araujo', 'michael.foord', 'python-dev']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue17015'
    versions = ['Python 3.4']

    @pitrou
    Copy link
    Member Author

    pitrou commented Jan 22, 2013

    This is a bit annoying:

    >>> def f(a, b): pass
    ... 
    >>> mock = Mock(spec=f)
    >>> mock(1, 2)
    <Mock name='mock()' id='140654219634288'>
    >>> mock.assert_called_with(1, 2)
    >>> mock.assert_called_with(a=1, b=2)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/home/antoine/cpython/default/Lib/unittest/mock.py", line 726, in assert_called_with
        raise AssertionError(msg)
    AssertionError: Expected call: mock(b=2, a=1)
    Actual call: mock(1, 2)

    This means your test assertions will depend unduly on some code style details (whether some function is called using positional or keyword arguments).
    Note: if this is fixed, it should be made to work with method calls too.

    @pitrou pitrou added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Jan 22, 2013
    @pitrou
    Copy link
    Member Author

    pitrou commented Jan 22, 2013

    (note: also fails if I use mock = Mock(wraps=f) instead of mock = Mock(spec=f))

    @pitrou
    Copy link
    Member Author

    pitrou commented Jan 28, 2013

    Proof-of-concept patch. mock is ugly!

    @pitrou
    Copy link
    Member Author

    pitrou commented Jan 29, 2013

    Here is a new patch, making all assert_*_call methods work as well as autospeccing, and adding tests. Not sure I'm forgetting something else.

    @voidspace
    Copy link
    Contributor

    Wow, impressive work Antoine - thanks. I am a little concerned that this relies on Python 3 only features of inspect, and *in fact* relies on bug fixes in Python 3.4 to work. That means it would be hard / impossible for the backport "mock" to have the new feature. That may be solveable (there is a backport of function signatures which mock could use for example).

    @pitrou
    Copy link
    Member Author

    pitrou commented Jan 30, 2013

    I am a little concerned that this relies on Python 3 only features of
    inspect, and *in fact* relies on bug fixes in Python 3.4 to work.

    The bug fix has landed in 3.3 as well ;-)
    (see 49fd1c8aeca5)
    I guess a backport of inspect.signature() would allow it to work on previous Pythons, too.

    @voidspace
    Copy link
    Contributor

    Ah, well if the bugfix exists everywhere that function signatures exist then it shouldn't be a problem. (I think there is already a backport of inspect.signature() by Nick Coghlan.)

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Feb 2, 2013

    New changeset b888c9043566 by Antoine Pitrou in branch 'default':
    Issue bpo-17015: When it has a spec, a Mock object now inspects its signature when matching calls, so that arguments can be matched positionally or by name.
    http://hg.python.org/cpython/rev/b888c9043566

    @pitrou
    Copy link
    Member Author

    pitrou commented Feb 2, 2013

    Committed!

    @pitrou pitrou closed this as completed Feb 2, 2013
    @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-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants