Skip to content

gh-155925: Narrow except Exception in typing Protocol helpers - #155930

Closed
lovit wants to merge 1 commit into
python:mainfrom
lovit:fix-typing-protocol-except
Closed

gh-155925: Narrow except Exception in typing Protocol helpers#155930
lovit wants to merge 1 commit into
python:mainfrom
lovit:fix-typing-protocol-except

Conversation

@lovit

@lovit lovit commented Aug 17, 2026

Copy link
Copy Markdown

Fixes gh-155925.

Narrows except Exception to except (AttributeError, NameError) in _get_protocol_attrs and _proto_hook. AttributeError alone regresses the deferred-evaluation tests, so NameError is needed too.

Added 4 regression tests; full test_typing suite passes locally.

…r) in typing Protocol helpers

_get_protocol_attrs and _proto_hook caught bare Exception around
__annotations__ access, silently swallowing unrelated errors along
with the legitimate AttributeError/NameError cases. Narrow the catch
to (AttributeError, NameError) so genuine bugs propagate instead of
being hidden.
@python-cla-bot

python-cla-bot Bot commented Aug 17, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@JelleZijlstra

Copy link
Copy Markdown
Member

I wrote it this way intentionally. Evaluating annotations can execute arbitrary code, and it is correct to fall back to FORWARDREF format in such cases.

@lovit

lovit commented Aug 17, 2026

Copy link
Copy Markdown
Author

Thanks for the explanation, that makes sense.

I confirmed locally that a function call inside an annotation can raise more than just AttributeError/NameError, and that FORWARDREF handles this correctly by not evaluating the expression at all. My narrowed catch breaks that case.

I'll close this PR. Thanks for taking the time to explain.

@lovit lovit closed this Aug 17, 2026
@lovit
lovit deleted the fix-typing-protocol-except branch August 17, 2026 05:39
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.

typing._get_protocol_attrs/_proto_hook swallow unrelated errors via an overly broad except Exception

2 participants