diff --git a/docs/changelog.md b/docs/changelog.md index 646afc00..4de319ee 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -2,6 +2,8 @@ ## Unreleased +- Use `__protocol_attrs__` for more robust detection of + the members of a `Protocol` (#631) - Remove more old special cases and improve robustness of annotation parsing (#630) - Remove dependency on `typing_inspect` (#629) diff --git a/pyanalyze/checker.py b/pyanalyze/checker.py index 8c4c3109..f484916a 100644 --- a/pyanalyze/checker.py +++ b/pyanalyze/checker.py @@ -432,6 +432,8 @@ def _extract_protocol_members(typ: type) -> Set[str]: or is_typing_name(typ, "Protocol") ): return set() + if hasattr(typ, "__protocol_attrs__"): + return typ.__protocol_attrs__ members = set(typ.__dict__) - EXCLUDED_PROTOCOL_MEMBERS # Starting in 3.10 __annotations__ always exists on types if sys.version_info >= (3, 10) or hasattr(typ, "__annotations__"):