Skip to content

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

Description

@lovit

Bug report

Bug description:

_get_protocol_attrs and _proto_hook in Lib/typing.py unconditionally swallow failures from base.__annotations__ access like this:

try:
    annotations = base.__annotations__
except Exception:
    annotations = annotationlib.get_annotations(...)
import typing

class BrokenMeta(type):
    def __getattribute__(cls, name):
        if name == '__annotations__':
            raise RuntimeError('simulated unrelated bug')
        return super().__getattribute__(name)

class Base(metaclass=BrokenMeta):
    pass

print(typing._get_protocol_attrs(Base))
set()

This looks intended to only handle AttributeError (no annotations) or NameError (undefined name during deferred evaluation), but except Exception is far too broad and also swallows genuine, unrelated bugs. _proto_hook reproduces the same way through issubclass().

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard 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