From 2a0d05384d3794ccb9b472dd28eb74aba97738e6 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Wed, 24 May 2023 09:18:18 -0700 Subject: [PATCH] Use __protocol_attrs__ --- docs/changelog.md | 2 ++ pyanalyze/checker.py | 2 ++ 2 files changed, 4 insertions(+) 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__"):