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

Uncaught ValueError in inspect.signature() #504

Open
2 tasks done
sth opened this issue Dec 4, 2024 · 0 comments
Open
2 tasks done

Uncaught ValueError in inspect.signature() #504

sth opened this issue Dec 4, 2024 · 0 comments
Labels

Comments

@sth
Copy link

sth commented Dec 4, 2024

Things to check first

  • I have searched the existing issues and didn't find my bug already reported there

  • I have checked that my bug is still present in the latest release

Typeguard version

4.4.1

Python version

3.13.0

What happened?

In check_signature_compatible(), Typeguard calls inspect.signature(), which can fail with ValueError if the signature cannot be determined for some reason. Typeguard should catch that error and probably ignore/skip the problematic signature check. Currently the exception falls through to the calling code:

Traceback (most recent call last):
[...]
  File "/data/src/converters/pb2.py", line 157, in _populate_entry_pb2
    def _populate_entry_pb2(
    ...<30 lines>...
            )
  File "/usr/local/lib/python3.13/site-packages/typeguard/_functions.py", line 137, in check_argument_types
    check_type_internal(value, annotation, memo)
    ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/typeguard/_checkers.py", line 946, in check_type_internal
    checker(value, origin_type, args, memo)
    ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/typeguard/_checkers.py", line 837, in check_protocol
    check_signature_compatible(subject, origin_type, attrname)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/typeguard/_checkers.py", line 651, in check_signature_compatible
    subject_sig = inspect.signature(getattr(subject, attrname))
  File "/usr/local/lib/python3.13/inspect.py", line 3340, in signature
    return Signature.from_callable(obj, follow_wrapped=follow_wrapped,
           ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                                   globals=globals, locals=locals, eval_str=eval_str)
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/inspect.py", line 3066, in from_callable
    return _signature_from_callable(obj, sigcls=cls,
                                    follow_wrapper_chains=follow_wrapped,
                                    globals=globals, locals=locals, eval_str=eval_str)
  File "/usr/local/lib/python3.13/inspect.py", line 2578, in _signature_from_callable
    return _signature_from_builtin(sigcls, obj,
                                   skip_bound_arg=skip_bound_arg)
  File "/usr/local/lib/python3.13/inspect.py", line 2364, in _signature_from_builtin
    raise ValueError("no signature found for builtin {!r}".format(func))
ValueError: no signature found for builtin <method 'add' of 'google._upb._message.RepeatedCompositeContainer' objects>

How can we reproduce the bug?

Since datetime.timedelta seems to have a similar problem with inspect.signature, the issue can be reproduced like this:

from typing import Protocol
from datetime import timedelta
from typeguard import typechecked

class Proto(Protocol):
    def attr(self) -> None:
        pass

class Obj(object):
    attr = timedelta

@typechecked
def f(param: Proto) -> None:
    pass

f(Obj())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant