https://github.com/ariebovenberg/quacks
🦆 Better duck-typing with mypy-compatible extensions to Protocol
https://github.com/ariebovenberg/quacks
duck-typing immutability mypy mypy-plugins protocols traits
Last synced: about 1 year ago
JSON representation
🦆 Better duck-typing with mypy-compatible extensions to Protocol
- Host: GitHub
- URL: https://github.com/ariebovenberg/quacks
- Owner: ariebovenberg
- License: mit
- Created: 2021-12-10T16:08:37.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-04-01T05:13:35.000Z (about 2 years ago)
- Last Synced: 2024-04-01T06:27:03.898Z (about 2 years ago)
- Topics: duck-typing, immutability, mypy, mypy-plugins, protocols, traits
- Language: Python
- Homepage: https://quacks.rtfd.io
- Size: 409 KB
- Stars: 15
- Watchers: 3
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.rst
- Changelog: CHANGELOG.rst
- Contributing: CONTRIBUTING.rst
- License: LICENSE
Awesome Lists containing this project
README
🦆 Quacks
=========
.. image:: https://img.shields.io/pypi/v/quacks.svg
:target: https://pypi.python.org/pypi/quacks
.. image:: https://img.shields.io/pypi/l/quacks.svg
:target: https://pypi.python.org/pypi/quacks
.. image:: https://img.shields.io/pypi/pyversions/quacks.svg
:target: https://pypi.python.org/pypi/quacks
.. image:: https://github.com/ariebovenberg/quacks/actions/workflows/build.yml/badge.svg
:target: https://github.com/ariebovenberg/quacks/actions/workflows/build.yml
.. image:: https://img.shields.io/readthedocs/quacks.svg
:target: http://quacks.readthedocs.io/
.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
:target: https://github.com/psf/black
.. epigraph::
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
------------
.. code-block:: bash
pip install quacks
⚠️ For type checking to work with ``mypy``, you'll need to enable the plugin in
your `mypy config file `_:
.. code-block:: ini
[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:
.. code-block:: python
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:
.. code-block:: python
class User(Protocol):
@property
def id(self) -> int: ...
@property
def name(self) -> str: ...
@property
def is_premium(self) -> bool: ...