这是indexloc提供的服务,不要输入任何密码
Skip to content

inspect.getfullargspec isn't ready for deferred evaluation of annotations #141560

@jacobtylerwalls

Description

@jacobtylerwalls

Bug report

Bug description:

inspect.getfullargspec isn't compatible with 3.14 signatures that depend on deferred evaluation of annotations, and doesn't expose an argument to pass through annotationlib.Format.FORWARDREF to prevent the failure.

import inspect
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from collections.abc import Iterable

def func(a: Iterable):
    return

inspect.getfullargspec(func)
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.14/lib/python3.14/inspect.py", line 1261, in getfullargspec
    sig = _signature_from_callable(func,
                                   follow_wrapper_chains=False,
                                   skip_bound_arg=False,
                                   sigcls=Signature,
                                   eval_str=False)
  File "/Library/Frameworks/Python.framework/Versions/3.14/lib/python3.14/inspect.py", line 2502, in _signature_from_callable
    return _signature_from_function(sigcls, obj,
                                    skip_bound_arg=skip_bound_arg,
                                    globals=globals, locals=locals, eval_str=eval_str,
                                    annotation_format=annotation_format)
  File "/Library/Frameworks/Python.framework/Versions/3.14/lib/python3.14/inspect.py", line 2325, in _signature_from_function
    annotations = get_annotations(func, globals=globals, locals=locals, eval_str=eval_str,
                                  format=annotation_format)
  File "/Library/Frameworks/Python.framework/Versions/3.14/lib/python3.14/annotationlib.py", line 890, in get_annotations
    ann = _get_dunder_annotations(obj)
  File "/Library/Frameworks/Python.framework/Versions/3.14/lib/python3.14/annotationlib.py", line 1059, in _get_dunder_annotations
    ann = getattr(obj, "__annotations__", None)
  File "<stdin>", line 1, in __annotate__
    def func(a: Iterable):
                ^^^^^^^^
NameError: name 'Iterable' is not defined

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    inspect.getfullargspec(func)
    ~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.14/lib/python3.14/inspect.py", line 1271, in getfullargspec
    raise TypeError('unsupported callable') from ex
TypeError: unsupported callable

I know getfullargspec() is threatened with deprecation (#108901, #76371), but over at Django we're going to need to build a "safe" version of this function (or migrate away from it entirely) unless an upstream patch makes more sense.

I presume that's either:

  • always passing through annotationlib.Format.FORWARDREF to inspect.Signature
  • exposing an argument

Defensively always using FORWARDREF or similar seems simplest. Happy to provide a PR.

CPython versions tested on:

3.14

Operating systems tested on:

macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.14bugs and security fixes3.15new features, bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytopic-typingtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions