Skip to content

Fix gh-152835: make inspect.signature robust to TYPE_CHECKING-only annotations#153055

Closed
raman118 wants to merge 1 commit into
python:mainfrom
raman118:fix-152835
Closed

Fix gh-152835: make inspect.signature robust to TYPE_CHECKING-only annotations#153055
raman118 wants to merge 1 commit into
python:mainfrom
raman118:fix-152835

Conversation

@raman118

@raman118 raman118 commented Jul 5, 2026

Copy link
Copy Markdown

Fixes #152835

This PR introduces a robust fallback mechanism in inspect.signature() to handle annotation evaluation failures (such as NameError, AttributeError, or ImportError).

When an exception is raised while evaluating parameter annotations under the default Format.VALUE format (common for annotations only imported in if TYPE_CHECKING: blocks), the logic now falls back to Format.FORWARDREF with eval_str=False, converting the failed references to ForwardRef objects instead of crashing. This prevents regressions for libraries and frameworks (like FastAPI, Pydantic, Click) that inspect signatures.

@bedevere-app

bedevere-app Bot commented Jul 5, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@python-cla-bot

python-cla-bot Bot commented Jul 5, 2026

Copy link
Copy Markdown

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

inspect.signature crashes with NameError for TYPE_CHECKING-only annotations

1 participant