Skip to content

ariebovenberg/quacks

Folders and files

NameName
Last commit message
Last commit date
Oct 19, 2024
Oct 19, 2024
Jan 29, 2024
Jan 29, 2024
Dec 7, 2023
Dec 15, 2021
Dec 12, 2021
Oct 19, 2024
Dec 24, 2021
Dec 15, 2021
Dec 12, 2021
Feb 3, 2023
Feb 3, 2023
Mar 3, 2025
Mar 3, 2025
Oct 19, 2024

Repository files navigation

🦆 Quacks

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.

Installation

pip install quacks

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

[mypy]
plugins = quacks

Features

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

@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):
    @property
    def id(self) -> int: ...
    @property
    def name(self) -> str: ...
    @property
    def is_premium(self) -> bool: ...