GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Calling inspect.signature with an AsyncMock instance raises an exception · Issue #96127 · python/cpython

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

New issue
Copy link
New issue
Copy link
Closed
Closed
Calling inspect.signature with an AsyncMock instance raises an exception#96127
Copy link
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@yvan-the-barbarian
yvan-the-barbarian
opened on Aug 19, 2022
Issue body actions

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

  • gh-96127: Fix inspect.signature call on mocks #96335
  • [3.11] gh-96127: Fix inspect.signature call on mocks (#96335) #101646
  • [3.10] gh-96127: Fix inspect.signature call on mocks (#96335) #101647
  • [3.10] Fix inspect.signature call on mocks (#96335) (gh-96127) #108652

Activity

Next
  1. yvan-the-barbarian
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Aug 19, 2022
  2. graingert commented on Aug 23, 2022

    @graingert
    graingert
    on Aug 23, 2022
    Contributor
    More actions

    Related? #94924

  3. tirkarthi commented on Aug 23, 2022

    @tirkarthi
    tirkarthi
    on Aug 23, 2022
    Member
    More actions

    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)>
    
  4. sobolevn
    added a commit that references this issue on Aug 27, 2022

    pythongh-96127: Fix `inspect.signature` call on mocks

    b1c6e20
  5. sobolevn commented on Aug 27, 2022

    @sobolevn
    sobolevn
    on Aug 27, 2022
    Member
    More actions

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

    Reacted by Thomas Grainger and Iván || HacKan
  6. cjw296 commented on Jan 3, 2023

    @cjw296
    cjw296
    on Jan 3, 2023
    Contributor
    More actions

    @sobolevn - where is your proposed solution?

  7. AlexWaygood commented on Jan 3, 2023

    @AlexWaygood
    AlexWaygood
    on Jan 3, 2023
    Member
    More actions

    @sobolevn - where is your proposed solution?

    • gh-96127: Fix inspect.signature call on mocks #96335
  8. sobolevn
    added a commit that references this issue on Jan 7, 2023

    pythongh-96127: Fix `inspect.signature` call on mocks

    d9c74a3
  9. cjw296
    added a commit that references this issue on Jan 7, 2023

    gh-96127: Fix `inspect.signature` call on mocks (#96335)

    9e7d726
  10. hauntsaninja commented on Jan 20, 2023

    @hauntsaninja
    hauntsaninja
    on Jan 20, 2023
    Contributor
    More actions

    Looks like this has been fixed, thanks all!

  11. hauntsaninja
    closed this as completedon Jan 20, 2023
  12. mdczaplicki commented on Feb 7, 2023

    @mdczaplicki
    mdczaplicki
    on Feb 7, 2023
    More actions

    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
  13. tirkarthi commented on Feb 7, 2023

    @tirkarthi
    tirkarthi
    on Feb 7, 2023
    Member
    More actions

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

    Reacted by Marek Czaplicki
  14. mdczaplicki commented on Feb 7, 2023

    @mdczaplicki
    mdczaplicki
    on Feb 7, 2023
    More actions

    Workaround:

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

    @arhadthedev
    arhadthedev
    on Feb 7, 2023
    Member
    More actions

    @sobolevn should the PR be backported to 3.11?

  16. 4 remaining items

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

    pythongh-96127: Fix `inspect.signature` call on mocks (python#96335)

    ca9f3e9

    pythongh-96127: Fix `inspect.signature` call on mocks (python#96335)

    01d94ca
  21. arhadthedev commented on Feb 8, 2023

    @arhadthedev
    arhadthedev
    on Feb 8, 2023
    Member
    More actions

    @sobolevn ping?

  22. ambv
    added a commit that references this issue on Feb 8, 2023

    [3.11] gh-96127: Fix `inspect.signature` call on mocks (#96335) (#101646

    efcab38
  23. arhadthedev commented on Feb 8, 2023

    @arhadthedev
    arhadthedev
    on Feb 8, 2023
    Member
    More actions

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

    Reacted by Marek Czaplicki
    Reacted by Marek Czaplicki
  24. arhadthedev commented on Feb 8, 2023

    @arhadthedev
    arhadthedev
    on Feb 8, 2023
    Member
    More actions

    Fixed by gh-96335.

  25. arhadthedev
    closed this as completedon Feb 8, 2023
  26. arhadthedev
    moved this from In Progress to Done in Contribution Queueon Feb 8, 2023
  27. AlexWaygood commented on Feb 8, 2023

    @AlexWaygood
    AlexWaygood
    on Feb 8, 2023
    Member
    More actions

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

    @arhadthedev where is the 3.10 backport?

  28. arhadthedev commented on Feb 8, 2023

    @arhadthedev
    arhadthedev
    on Feb 8, 2023
    Member
    More actions

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

    Reacted by Alex Waygood
  29. thechrisu
    added a commit that references this issue on Aug 29, 2023

    pythongh-96127: Fix `inspect.signature` call on mocks (python#96335)

    ac3aad4
  30. bedevere-bot commented on Aug 29, 2023

    @bedevere-bot
    bedevere-bot
    on Aug 29, 2023
    More actions

    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/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.