-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add typing.get_protocol_members
and typing.is_protocol
#104873
Comments
The worry about documenting it is that people might start monkey-patching it, which I don't want to encourage :) We could mitigate that by making it a read-only property (and by making it a frozenset instead of a mutable set). But making it a property might introduce some performance overhead, and the whole reason we added this attribute was to address a performance issue. Having a dunder attribute also wouldn't work very well with static type checkers -- because of how special So, I guess I vote for a |
Yes, I think a getter function makes sense here. I was just thinking about this while writing a patch for #104874, where I do think it makes sense to simply document the dunder attribute. The difference there is that the |
Agreed on all counts. |
|
__protocol_attrs__
documented and publictyping.get_protocol_members
and typing.is_protocol
Based on PR review, going to also add |
I know that this is most relevant for
With —— Edit: removed word at the end that was left in by accident. |
…04878) Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Oh, I think this is done now! 🎉 |
Just out of curiosity, but does |
Yes — |
@NeilGirdhar the relevant commit is here if you'd like to read the source code :-) fc8037d |
The more important reason is that concrete classes can be subclasses of Protocol.
|
@JelleZijlstra Makes perfect sense, thanks! |
#103160 added an attribute
__protocol_attrs__
that holds the names of all protocol attributes:This is useful for code that needs to extract the names included in a Protocol at runtime. Previously, this was quite difficult, as you had to look at the class's
__dict__
and__annotations__
directly and exclude a long list of internal attributes (e.g. https://github.com/quora/pyanalyze/blob/bd7f520adc2d8b098be657dfa514d1433bea3b0c/pyanalyze/checker.py#L428).However, currently
__protocol_attrs__
is an undocumented private attribute. I think we should either document it or add an introspection helper liketyping.get_protocol_attrs()
that exposes it.Linked PRs
The text was updated successfully, but these errors were encountered: