gh-74690: typing: Call `_get_protocol_attrs` and `_callable_members_only` at protocol class creation time, not during `isinstance()` checks by AlexWaygood · Pull Request #103160 · python/cpython · GitHub

github.com

Tin mới

AlexWaygood:protocol-attrs-cache-1

GitHub CopilotWrite better code with AI

GitHub Copilot appDirect agents from issue to merge

ActionsAutomate any workflow

CodespacesInstant dev environments

IssuesPlan and track work

Code ReviewManage code changes

Code QualityEnforce quality at merge

GitHub Advanced SecurityFind and fix vulnerabilities

Code securitySecure your code as you build

Secret protectionStop leaks before they start

View all use cases

Financial services

View all industries

Software Development

GitHub SponsorsFund open source developers

Copilot for BusinessEnterprise-grade AI features

Premium SupportEnterprise-grade 24/7 support

Pull requests 2.6k

Security and quality 0

Performance of typing._ProtocolMeta._get_protocol_attrs and isinstance #74690

: typing: Cache results of

awaiting core review

awaiting core review

Simplify and inline

Use a metaclass instance method, not a classmethod on the class

Move to class-creation time

Use __init__, not __new__

View reviewed changes

Learn more about hiding disruptive comments

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/antonagestam/phantom-types/blob/68ee857af4452a90a48669a8dbc7d1d7110b31d0/src/phantom/sized.py#L71-L82

Confirmed that there's at least a few uses of _ProtocolMeta out in the wild:

https://github.com/protolambda/remerkleable/blob/91ed092d08ef0ba5ab076f0a34b0b371623db728/remerkleable/core.py#L18-L22

Confirmed that there's at least a few uses of _ProtocolMeta out in the wild:

https://github.com/InterStella0/stella_bot/blob/bf5f5632bcd88670df90be67b888c282c6e83d99/utils/useful.py#L337-L343

Confirmed that there's at least a few uses of _ProtocolMeta out in the wild:

Add missing super().__init__() call, to be safe

gh-102433: Use inspect.getattr_static in typing._ProtocolMeta.__instancecheck__ #103034

Merge branch 'main' into protocol-attrs-cache-1

Typing: undocumented behaviour change for protocols decorated with @final and @runtime_checkable in 3.11 #103171

View reviewed changes

Merge branch 'main' into protocol-attrs-cache-1

View reviewed changes

Update Lib/typing.py

gh-74690: Document changes made to runtime-checkable protocols in 3.12 #103348

gh-74690: typing: Call `_get_protocol_attrs` and `_callable_members_only` at protocol class creation time, not during `isinstance()` checks (#103160)

gh-74690: typing: Call `_get_protocol_attrs` and `_callable_members_only` at protocol class creation time, not during `isinstance()` checks (#103160)

Backport performance improvements to runtime-checkable protocols python/typing_extensions#137

gh-74690: Don't set special protocol attributes on non-protocol subclasses of protocols #104622

Add typing.get_protocol_members and typing.is_protocol #104873

[Bug] typing_extensions.Protocol unsupported under typing_extensions ≥ 4.6.0 beartype/beartype#241

datetime.__sub__ overload order python/typeshed#10924

Is the protocol caching still needed? beartype/numerary#20