Skip to content

mock uses incorrect signature for partial and partialmethod with autospec #79644

Open

Description

@tirkarthi
BPO 35463
Nosy @cjw296, @voidspace, @mariocj89, @pablogsal, @tirkarthi

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 = None
created_at = <Date 2018-12-11.14:47:32.441>
labels = ['3.7', '3.8', 'type-bug', 'library']
title = 'mock uses incorrect signature for partial and partialmethod with autospec'
updated_at = <Date 2018-12-11.14:47:32.441>
user = 'https://github.com/tirkarthi'

bugs.python.org fields:

activity = <Date 2018-12-11.14:47:32.441>
actor = 'xtreak'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2018-12-11.14:47:32.441>
creator = 'xtreak'
dependencies = []
files = []
hgrepos = []
issue_num = 35463
keywords = []
message_count = 1.0
messages = ['331631']
nosy_count = 5.0
nosy_names = ['cjw296', 'michael.foord', 'mariocj89', 'pablogsal', 'xtreak']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue35463'
versions = ['Python 3.7', 'Python 3.8']

Linked PRs

Activity

  1. tirkarthi commented on Dec 11, 2018

    @tirkarthi
    MemberAuthor

    This is a bug report for https://bugs.python.org/issue17185#msg331149 that I was asked to raise as a separate issue.

    1. When we call create_autospec it calls _get_signature_object that gets the signature for the given parameter. With functools.partial it returns a partial object and hence while getting the signature it returns the signature for the constructor of partial instead of the underlying function passed to functools.partial. I think a check needs to be added to make sure not to use func.__init__ when it's a partial object.

    2. When we call create_autospect on a class that has a partialmethod the self parameter is not skipped in the signature and thus it creates a signature with self causing error. The fix would be to handle partialmethod also in _must_skip that determines whether to skip self or not.

    Sample reproducer :

    from functools import partial, partialmethod
    from unittest.mock import create_autospec
    import inspect
    
    def foo(a, b):
        pass
    
    p = partial(foo, 1)
    m = create_autospec(p)
    m(1, 2, 3) # passes since signature is set as (*args, **kwargs) the signature of functools.partial constructor. This should throw TypeError under autospec
    
    
    class A:
    
        def f(self, a, b):
            print(a, b)
    
        g = partialmethod(f, 1)
    
    m = create_autospec(A)
    m().g(1, 2) # passes since signature is set as (self, b) and self is not skipped in _must_skip thus self=1, b=2. This should throw TypeError under autospec since the valid call is m().g(2)
  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    type-bugAn unexpected behavior, bug, or error
    on Dec 11, 2018
  3. transferred this issue fromon Apr 10, 2022
  4. added a commit that references this issue on Sep 21, 2026
  5. added a commit that references this issue on Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.7 (EOL)end of life3.8 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions