Skip to content

inspect.signature crashes with NameError for TYPE_CHECKING-only annotations #152835

Description

@rdaysky

Bug report

Bug description:

if typing.TYPE_CHECKING:
    from somelib import SomeType

def f(arg: SomeType): ...

inspect.signature(f)  # NameError: name 'SomeType' is not defined

Some linters have rules that move imports into TYPE_CHECKING blocks and after this seemingly benign change inspect.signature will unexpectedly fail, even if its caller never cared about annotations.

At the very least, the documentation of inspect.signature should warn about this (currently it only talks about string annotations and makes no mention of Python 3.14 lazy annotations). But perhaps there’s a way of making inspect.signature work without such a surprising behavior, maybe by making some Signature fields lazy.

CPython versions tested on:

3.14

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Fields

    No fields configured for issues without a type.

    Projects

    Status
    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions