Skip to content

Calling inspect.signature with an AsyncMock instance raises an exception #96127

Description

@yvan-the-barbarian

Bug report

Starting in Python 3.10.6, the following code raises an exception:

from inspect import signature
from unittest.mock import AsyncMock

mock = AsyncMock()
signature(mock)
  • No exception is raised when Mock is used.
  • The exception does not occur with Python 3.10.5.

Your environment

  • CPython versions tested on: 3.10.4, 3.10.5, 3.10.6 (Only occurs in 3.10.6)
  • Operating system and architecture: Windows 10, Linux (Official Docker Image)

Linked PRs

Activity

  1. graingert commented on Aug 23, 2022

    @graingert
    Contributor

    Related? #94924

  2. tirkarthi commented on Aug 23, 2022

    @tirkarthi
    Member

    This could be due to commit 4261b6b

    (myenv) ➜  cpython git:(main) ./python
    Python 3.12.0a0 (heads/main:575f8880bf, Aug 23 2022, 19:59:51) [GCC 9.4.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import inspect
    >>> from unittest.mock import AsyncMock
    >>> inspect.signature(AsyncMock())
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/home/karthikeyan/stuff/python/cpython/Lib/inspect.py", line 3272, in signature
        return Signature.from_callable(obj, follow_wrapped=follow_wrapped,
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/home/karthikeyan/stuff/python/cpython/Lib/inspect.py", line 3020, in from_callable
        return _signature_from_callable(obj, sigcls=cls,
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/home/karthikeyan/stuff/python/cpython/Lib/inspect.py", line 2507, in _signature_from_callable
        return _signature_from_function(sigcls, obj,
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/home/karthikeyan/stuff/python/cpython/Lib/inspect.py", line 2351, in _signature_from_function
        positional = arg_names[:pos_count]
                     ~~~~~~~~~^^^^^^^^^^^^
    TypeError: 'Mock' object is not subscriptable
    >>> 
    (myenv) ➜  cpython git:(main) git log Lib/unittest/mock.py 
    (myenv) ➜  cpython git:(main) git checkout 4261b6bffc0b8bb5c6d4d80578a81b7520f4aefc~1 Lib/unittest/mock.py
    Updated 1 path from ec0ed6ea8a
    (myenv) ➜  cpython git:(main) ✗ ./python                                                                    
    Python 3.12.0a0 (heads/main:575f8880bf, Aug 23 2022, 19:59:51) [GCC 9.4.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import inspect
    >>> from unittest.mock import AsyncMock
    >>> inspect.signature(AsyncMock())
    <Signature (*args, **kwargs)>
    
  3. added a commit that references this issue on Aug 27, 2022
  4. sobolevn commented on Aug 27, 2022

    @sobolevn
    Member

    @graingert yes, your link was very useful. Please, take a look at my proposed soultion :)

  5. cjw296 commented on Jan 3, 2023

    @cjw296
    Contributor

    @sobolevn - where is your proposed solution?

  6. AlexWaygood commented on Jan 3, 2023

    @AlexWaygood
    Member
  7. added a commit that references this issue on Jan 7, 2023
  8. added a commit that references this issue on Jan 7, 2023
  9. hauntsaninja commented on Jan 20, 2023

    @hauntsaninja
    Contributor

    Looks like this has been fixed, thanks all!

  10. mdczaplicki commented on Feb 7, 2023

    @mdczaplicki

    I'm on 3.11.1 and this is happening.

        def _signature_from_function(cls, func, skip_bound_arg=True,
                                     globals=None, locals=None, eval_str=False):
            """Private helper: constructs Signature for the given python function."""
        
            is_duck_function = False
            if not isfunction(func):
                if _signature_is_functionlike(func):
                    is_duck_function = True
                else:
                    # If it's not a pure Python function, and not a duck type
                    # of pure function:
                    raise TypeError('{!r} is not a Python function'.format(func))
        
            s = getattr(func, "__text_signature__", None)
            if s:
                return _signature_fromstr(cls, func, s, skip_bound_arg)
        
            Parameter = cls._parameter_cls
        
            # Parameter information.
            func_code = func.__code__
            pos_count = func_code.co_argcount
            arg_names = func_code.co_varnames
            posonly_count = func_code.co_posonlyargcount
    >       positional = arg_names[:pos_count]
    E       TypeError: 'Mock' object is not subscriptable
  11. tirkarthi commented on Feb 7, 2023

    @tirkarthi
    Member

    This commit is present on main branch and 3.12 latest alpha. I guess this is not backported.

  12. mdczaplicki commented on Feb 7, 2023

    @mdczaplicki

    Workaround:

    from inspect import signature
    from unittest.mock import AsyncMock, MagicMock
    
    temp_mock = AsyncMock()
    mock = MagicMock(return_value=temp_mock())
    signature(mock)
  13. arhadthedev commented on Feb 7, 2023

    @arhadthedev
    Member

    @sobolevn should the PR be backported to 3.11?

  14. 4 remaining items

  15. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Feb 7, 2023
  16. moved this from Todo to In Progress in Contribution Queueon Feb 7, 2023
  17. added 2 commits that reference this issue on Feb 7, 2023
  18. arhadthedev commented on Feb 8, 2023

    @arhadthedev
    Member

    @sobolevn ping?

  19. added a commit that references this issue on Feb 8, 2023
  20. arhadthedev commented on Feb 8, 2023

    @arhadthedev
    Member

    @mdczaplicki @tirkarthi The backporting is done. The fix will be available in 3.11.3 and 3.10.11 this April.

  21. arhadthedev commented on Feb 8, 2023

    @arhadthedev
    Member

    Fixed by gh-96335.

  22. moved this from In Progress to Done in Contribution Queueon Feb 8, 2023
  23. AlexWaygood commented on Feb 8, 2023

    @AlexWaygood
    Member

    The fix will be available in 3.11.3 and 3.10.11 this April.

    @arhadthedev where is the 3.10 backport?

  24. arhadthedev commented on Feb 8, 2023

    @arhadthedev
    Member

    I confused it with my other issue that was backported to both versions. Thank you for pointing out, edited.

  25. added a commit that references this issue on Aug 29, 2023
  26. bedevere-bot commented on Aug 29, 2023

    @bedevere-bot

    GH-108652 is a backport of this pull request to the 3.10 branch.

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

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions