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

A PEP 544 protocol to support type hints for functions that sort

NotificationsYou must be signed in to change notification settings

fluentpython/protocol_examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

60 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Examples usingtyping.Protocol from PEP 544

Notes

Classic Example: a "file-like object"

FromPEP 333 – Python Web Server Gateway Interface v1.0 (2003):

To be considered “file-like”, the object supplied by the applicationmust have aread() method that takes an optional size argument.

The words "file-like" (or "file like") appear with similar implied meaningin the Python 3.12 distribution:

  • 148 times in the documentation;
  • 92 times in code comments (.py or.c source files).

Also, 30 times across 21 PEPs (100, 214, 258, 282, 305, 310, 333, 368, 400, 441, 444, 578, 680, 691, 3116, 3119, 3143, 3145, 3154, 3156, 3333).

Definition inLib/wsgiref/types.py:

class_Readable(Protocol):defread(self,size:int= ...,/)->bytes: ...

Examples as of 2024-05-23

typing.Protocol definitions found withripgrep:

rg"Protocol\)" -g'*.pyi'| sort
  • 120 definitions ontypeshed/stdlib (Python standard library)
  • 134 definitions ontypeshed/stubs (external packages)

Cases to study

  • importlib/resources/abc.py:class Traversable(Protocol)with several abstract and concrete methods, looks like an ABC but derives fromtyping.Protocol

Nottyping.Protocol

  • asyncio/protocols.py: defines classesBaseProtocol,Protocol etc. for network protocols

About

A PEP 544 protocol to support type hints for functions that sort

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp