inspect.signature crashes with NameError for TYPE_CHECKING-only annotations
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
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
Linked PRs
- gh-153055
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the inspect.signature entry point and the Python 3.14 lazy-annotation behavior described in the report. Review linked PR gh-153055 before determining whether the accepted scope is documentation or a behavior change; the issue does not define a single completion condition.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100