GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - fluentpython/protocol_examples: A PEP 544 protocol to support type hints for functions that sort

BranchesTags
Open more actions menu

Latest commit

 

History

60 Commits
60 Commits

Folders and files

NameName
Last commit message
Last commit date
ast_hacks
ast_hacks
 
 
double
double
 
 
less_than
less_than
 
 
not_sets
not_sets
 
 
random_picker
random_picker
 
 
samples
samples
 
 
slides
slides
 
 
vector
vector
 
 
.gitignore
.gitignore
 
 
README.md
README.md
 
 
tt
tt
 
 
tt2
tt2
 
 

Repository files navigation

protocol_examples

Examples using typing.Protocol from PEP 544

Notes

Classic Example: a "file-like object"

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

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

The words "file-like" (or "file like") appear with similar implied meaning in 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 in Lib/wsgiref/types.py:

class _Readable(Protocol):
    def read(self, size: int = ..., /) -> bytes: ...

Examples as of 2024-05-23

typing.Protocol definitions found with ripgrep:

rg "Protocol\)" -g '*.pyi' | sort
  • 120 definitions on typeshed/stdlib (Python standard library)
  • 134 definitions on typeshed/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 from typing.Protocol

Not typing.Protocol

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

About

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

Resources

Readme
Activity
Custom properties

Stars

2 stars

Watchers

1 watching

Forks

1 fork
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.