Title: gh-102433: Use `inspect.getattr_static` in `typing._ProtocolMeta.__instancecheck__` by AlexWaygood · Pull Request #103034 · python/cpython · GitHub
Open Graph Title: gh-102433: Use `inspect.getattr_static` in `typing._ProtocolMeta.__instancecheck__` by AlexWaygood · Pull Request #103034 · python/cpython
X Title: gh-102433: Use `inspect.getattr_static` in `typing._ProtocolMeta.__instancecheck__` by AlexWaygood · Pull Request #103034 · python/cpython
Description: Use getattr_static rather than hasattr in _ProtocolMeta.__instancecheck__, in order to avoid unexpectedly evaluating descriptors and __getattr__ methods in the process of calling isinstance() against runtime-checkable protocols. This slows down isinstance() checks against runtime-checkable protocols by around 46% (on my machine), but the consensus in python/typing#1363 is that the performance penalty is worth it because: Any isinstance() check causing an expensive (or otherwise badly behaved) property or __getattr__ method to be evaluated is pretty surprising behaviour. Bringing the runtime's understanding of runtime-checkable protocols closer to the way static type checkers understand these classes is highly desirable. Runtime-checkable protocols should not be used for performance-critical code anyway (this is now clearly documented following #102936.) In various subtle edge cases, this change will mean that some classes that used to be considered structural "subclasses" of a certain runtime-checkable protocol will no longer be considered subclasses of that protocol (and vice versa). As such, this can't be backported. (Perhaps we should consider documenting the existing behaviour for 3.11 and 3.10.) Issue: gh-102433 Note that most discussion has actually taken place in a python/typing issue rather than a CPython issue: python/typing#1363
Open Graph Description: Use getattr_static rather than hasattr in _ProtocolMeta.__instancecheck__, in order to avoid unexpectedly evaluating descriptors and __getattr__ methods in the process of calling isinstance() again...
X Description: Use getattr_static rather than hasattr in _ProtocolMeta.__instancecheck__, in order to avoid unexpectedly evaluating descriptors and __getattr__ methods in the process of calling isinstance() again...
Opengraph URL: https://github.com/python/cpython/pull/103034
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/checks(.:format) |
| route-controller | pull_requests |
| route-action | checks |
| fetch-nonce | v2:2ed1bca3-abd8-a1a9-4dd9-851c1c2f6136 |
| current-catalog-service-hash | 87dc3bc62d9b466312751bfd5f889726f4f1337bdff4e8be7da7c93d6c00a25a |
| request-id | ABBA:26D7FD:9502:C559:6969C20C |
| html-safe-nonce | 5ef5c00ce595400b3eab6d0d0cce85a5b853c961a20582b63b68c399967e1440 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQkJBOjI2RDdGRDo5NTAyOkM1NTk6Njk2OUMyMEMiLCJ2aXNpdG9yX2lkIjoiMTY2MTMwNDMyNTUwMjc3OTkxNyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | cd4fd7d72c84e81461d0d20097a713a3755d3a58398f70fe2e98abd2d7e97b6b |
| hovercard-subject-tag | pull_request:1290088688 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,checks,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/python/cpython/pull/103034/checks |
| twitter:image | https://avatars.githubusercontent.com/u/66076021?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/66076021?s=400&v=4 |
| og:image:alt | Use getattr_static rather than hasattr in _ProtocolMeta.__instancecheck__, in order to avoid unexpectedly evaluating descriptors and __getattr__ methods in the process of calling isinstance() again... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | acedec8b5f975d9e3d494ddd8f949b0b8a0de59d393901e26f73df9dcba80056 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive full-width full-width-p-0 |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 83c08c21cdda978090dc44364b71aa5bc6dcea79 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width