Skip to main content

Better duck-typing with mypy-compatible extensions to Protocol

Project description

If it walks like a duck and it quacks like a duck, then it must be a duck

Thanks to PEP544, Python now has protocols: a way to define duck typing statically. This library gives you some niceties to make common idioms easier.


pip install quacks

⚠️ For type checking to work with mypy, you’ll need to enable the plugin in your mypy config file:

plugins = quacks


Easy read-only protocols

Defining read-only protocols is great for encouraging immutability and working with frozen dataclasses. Use the readonly decorator:

from quacks import readonly

class User(Protocol):
    id: int
    name: str
    is_premium: bool

Without this decorator, we’d have to write quite a lot of cruft, reducing readability:

class User(Protocol):
    def id(self) -> int: ...
    def name(self) -> str: ...
    def is_premium(self) -> bool: ...

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

quacks-1.1.0.tar.gz (4.8 kB view hashes)

Uploaded source

Built Distribution

quacks-1.1.0-py3-none-any.whl (6.4 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page