Skip to main content

Better duck-typing with mypy-compatible extensions to Protocol

Project description

https://img.shields.io/pypi/v/quacks.svg https://img.shields.io/pypi/l/quacks.svg https://img.shields.io/pypi/pyversions/quacks.svg https://img.shields.io/readthedocs/quacks.svg https://img.shields.io/badge/code%20style-black-000000.svg

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

Better duck-typing with mypy-compatible extensions to Protocol

🎯 Why?

PEP544 gave Python 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.mypy

⭐️ 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: ...

Partial protocols (🏗)

(🚧 work in progress 🚧)

What if you only need part of a protocol? Imagine we have several functions who use various properties of User. With partial protocols you can reuse a data ‘shape’ without requiring all attributes.

(exact syntax TBD)

from quacks import q

def determine_discount(u: User[q.id.is_premium]) -> int:
    ...  # access `id` and `is_premium` attributes

def greet(u: User[q.id.name]) -> None:
    ...  # access `id` and `name` attributes

u: User = ...

determine_discount(u)
greet(u)

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-0.1.2.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

quacks-0.1.2-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file quacks-0.1.2.tar.gz.

File metadata

  • Download URL: quacks-0.1.2.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.9.9 Darwin/21.1.0

File hashes

Hashes for quacks-0.1.2.tar.gz
Algorithm Hash digest
SHA256 1c14d9e6185230cf3110f5a5920e53b8dc614b07b9d449231871e0f14d0a816a
MD5 ad3bdc11def1deec9f3eaa4eb2d7410d
BLAKE2b-256 58805a0696e4073ad8f343f9ba7653aea31897fcaecdba4650acd9d6382f2c3b

See more details on using hashes here.

Provenance

File details

Details for the file quacks-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: quacks-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.9.9 Darwin/21.1.0

File hashes

Hashes for quacks-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ffaf24c30db969202969b69f08e97f4be0bf7d3a79adf4978207cdd5bf64fb5e
MD5 9799d01a62e08ed05eca3ec24034a691
BLAKE2b-256 e58c3b65f6c9029096b8aaaabed04087c375a95df837e04757bcd595b58f989f

See more details on using hashes here.

Provenance

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