Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Typing docs: increase prominence of warnings regarding@runtime_checkable#127985

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
AlexWaygood wants to merge2 commits intopython:main
base:main
Choose a base branch
Loading
fromAlexWaygood:runtime-checkable-bad

Conversation

AlexWaygood
Copy link
Member

@AlexWaygoodAlexWaygood commentedDec 16, 2024
edited by github-actionsbot
Loading

We have some warnings in the docs fortyping.runtime_checkable already, but:

  • they're not very prominent
  • I think it's not absolutely clear how the problems described by these notes might cause concrete issues for users

This PR makes the warnings more prominent and expands on the examples. It follows renewed discussion on how runtime-checkable protocols are fundamentally unsafe in many ways on the Discourse threadhttps://discuss.python.org/t/pep-767-annotating-read-only-attributes/73408


📚 Documentation preview 📚:https://cpython-previews--127985.org.readthedocs.build/

@AlexWaygood
Copy link
MemberAuthor

@@ -2486,14 +2486,14 @@ types.

.. warning::

Runtime-checkable protocols are known to beunsafe in several ways.
Runtime-checkable protocols are known to beunsound in several ways.
You should only use them for simple protocols, and even then only use

Choose a reason for hiding this comment

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

What about suggesting that they should only be used for method-only protocols? Or just for protocols with dunder methods -- since dunder methods typically have a well-understood signature, so the check is more likely to do the right thing at runtime?

JelleZijlstra reacted with thumbs up emoji
Copy link
Contributor

Choose a reason for hiding this comment

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

It isn't safe on even just dunder methods. An example of this is numpy arrays and__bool__, see other discussion about type checkers making unsafe assumptions related to thishere

Copy link
Member

@JelleZijlstraJelleZijlstra left a comment

Choose a reason for hiding this comment

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

This looks good but I agree with Jukka that it's probably worth mentioning that runtime-checkable protocols make a bit more sense when only methods are involved.

@hugovkhugovk removed the needs backport to 3.12only security fixes labelApr 10, 2025
@serhiy-storchakaserhiy-storchaka added the needs backport to 3.14bugs and security fixes labelMay 8, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@JukkaLJukkaLJukkaL left review comments

@mikeshardmindmikeshardmindmikeshardmind left review comments

@JelleZijlstraJelleZijlstraJelleZijlstra approved these changes

Assignees
No one assigned
Labels
awaiting mergedocsDocumentation in the Doc dirneeds backport to 3.13bugs and security fixesneeds backport to 3.14bugs and security fixesskip issueskip newstopic-typing
Projects
Status: Todo
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

6 participants
@AlexWaygood@JelleZijlstra@JukkaL@mikeshardmind@hugovk@serhiy-storchaka

[8]ページ先頭

©2009-2025 Movatter.jp