Skip to main content

title: kapro description: Class-level, mixed, and pinned descriptors for Python

[ref: #kapro]

kapro

kapro is a small Python library of property descriptors.

It gives you three public entry points:

  • class_property — a descriptor that lives on the class and is shared by every instance.
  • mixed_property — a descriptor that can be defined on the class but resolved per instance.
  • pin — a cached instance property that stores its value in the instance dictionary.

[ref: #installation]

Installation

Install with uv:

uv add kapro

Or with any PEP 517-compatible tool:

pip install kapro

[ref: #class-property]

Class-level properties with class_property

class_property behaves like a class-level attribute, but it is computed lazily and cached on the class.

from kapro import class_property

class Config:
    @class_property
    def name(cls) -> str:
        return "default"

assert Config.name == "default"
assert Config().name == "default"

The value is stored on the class, so every instance sees the same value.

[ref: #mixed-property]

Mixed properties with mixed_property

mixed_property calls the same function with the class when accessed on the class and with the instance when accessed on an instance.

from kapro import mixed_property

class User:
    def __init__(self, id: int) -> None:
        self.id = id

    @mixed_property
    def label(node) -> str:
        if isinstance(node, type):
            return "User model"
        return f"user: {node.id}"

assert User.label == "User model"
assert User(id=7).label == "user: 7"

[ref: #pin]

Cached instance properties with pin

pin is the cached equivalent of a plain method-based property. The first access computes the value and stores it in the instance dictionary.

from kapro import pin

class Expensive:
    @pin
    def payload(self) -> dict:
        return {"loaded": True}

obj = Expensive()
assert obj.payload is obj.payload

[ref: #pin-flavors]

pin flavors

pin exposes several variants through class attributes.

Flavor Decorator Caches on
native @pin instance (default)
cls @pin.cls class
any @pin.any class or instance
pre @pin.pre instance, runs before the underlying call
post @pin.post instance, runs after the underlying call
from kapro import pin

class Service:
    @pin.cls
    def version(cls) -> str:
        return "1.0"

assert Service.version == "1.0"
assert Service().version == "1.0"

[ref: #full-example]

Full example

from kapro import class_property, mixed_property, pin

class App:
    @class_property
    def name(cls) -> str:
        return "MyApp"

    @mixed_property
    def greeting(node) -> str:
        return f"Hello from {node.name}"

    @pin
    def config(self) -> dict:
        return {"debug": True}


assert App.name == "MyApp"
assert App.greeting == "Hello from MyApp"
assert App().greeting == "Hello from MyApp"

app = App()
assert app.config == {"debug": True}
assert app.config is app.config

Download files

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

Source Distribution

kapro-0.0.4.tar.gz (32.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kapro-0.0.4-py3-none-any.whl (25.5 kB view details)

Uploaded Python 3

File details

Details for the file kapro-0.0.4.tar.gz.

File metadata

  • Download URL: kapro-0.0.4.tar.gz
  • Upload date:
  • Size: 32.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for kapro-0.0.4.tar.gz
Algorithm Hash digest
SHA256 d9c68bb0f30d729d9edd77b2eba92320bd881f1b26da0cef34389d71f8a31c25
MD5 deff2a828eec736a6311b0ab7d82806a
BLAKE2b-256 0e6619e5a1264caa9e0aea7f52740a18e2e78252ee0e026e3547b0aad7297376

See more details on using hashes here.

File details

Details for the file kapro-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: kapro-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 25.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for kapro-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 bb9b02a9f4fcf3ba82d636506c4bb4f27ca23d7bce04d3aee40d8375f6896bc6
MD5 714c4d84117255b206a36338db6a959f
BLAKE2b-256 d042e545868e57bd6da73e9015337b206bb7dec5340277b1c9c4b276baaa0063

See more details on using hashes here.

Release history Release notifications | RSS feed

0.1.0

2 files

This release

0.0.4 This release

2 files

0.0.3

2 files

0.0.2

2 files

0.0.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page