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

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

Closed
yvan-the-barbarian opened this issue Aug 19, 2022 · 18 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@yvan-the-barbarian
Copy link

yvan-the-barbarian commented Aug 19, 2022

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

@graingert
Copy link
Contributor

Related? #94924

@tirkarthi
Copy link
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)>

@sobolevn
Copy link
Member

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

@cjw296
Copy link
Contributor

cjw296 commented Jan 3, 2023

@sobolevn - where is your proposed solution?

@AlexWaygood
Copy link
Member

@sobolevn - where is your proposed solution?

sobolevn added a commit to sobolevn/cpython that referenced this issue Jan 7, 2023
@hauntsaninja
Copy link
Contributor

Looks like this has been fixed, thanks all!

@mdczaplicki
Copy link

mdczaplicki commented Feb 7, 2023

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

@tirkarthi
Copy link
Member

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

@mdczaplicki
Copy link

mdczaplicki commented Feb 7, 2023

Workaround:

from inspect import signature
from unittest.mock import AsyncMock, MagicMock

temp_mock = AsyncMock()
mock = MagicMock(return_value=temp_mock())
signature(mock)

@arhadthedev
Copy link
Member

@sobolevn should the PR be backported to 3.11?

@sobolevn
Copy link
Member

sobolevn commented Feb 7, 2023

Automatic backport has failed :(
I don't have time right now to do it manually, please help me!

@arhadthedev
Copy link
Member

Got it, will start this in a couple of hours.

@arhadthedev arhadthedev reopened this Feb 7, 2023
@arhadthedev arhadthedev added the stdlib Python modules in the Lib dir label Feb 7, 2023
@arhadthedev arhadthedev moved this from Todo to In Progress in Contribution Queue Feb 7, 2023
arhadthedev pushed a commit to arhadthedev/cpython that referenced this issue Feb 7, 2023
arhadthedev pushed a commit to arhadthedev/cpython that referenced this issue Feb 7, 2023
@arhadthedev
Copy link
Member

@sobolevn ping?

ambv pushed a commit that referenced this issue Feb 8, 2023
)

(cherry picked from commit 9e7d726)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
@arhadthedev
Copy link
Member

arhadthedev commented Feb 8, 2023

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

@arhadthedev
Copy link
Member

Fixed by gh-96335.

@arhadthedev arhadthedev moved this from In Progress to Done in Contribution Queue Feb 8, 2023
@AlexWaygood
Copy link
Member

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

@arhadthedev where is the 3.10 backport?

@arhadthedev
Copy link
Member

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

@bedevere-bot
Copy link

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
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

10 participants