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.3.tar.gz (29.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.3-py3-none-any.whl (24.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: kapro-0.0.3.tar.gz
  • Upload date:
  • Size: 29.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.3.tar.gz
Algorithm Hash digest
SHA256 c25baa672c6bcaa6fcc1aa0f5cc58f818a05e730d61a064baeef8594c2d697b2
MD5 313371f6def3fbcddf482a10360cd36b
BLAKE2b-256 5077ce983ab05b4315552da804124fdae4c0f91897844deb403443a7dcc5f0fc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: kapro-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 24.6 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 3d22c9fe401e3f1298b8d89a8df38d2850df00336d033c47f04786ed7da356bf
MD5 bce6b93d518bf51ef7a9600f8c0f92dc
BLAKE2b-256 493f17db41afb962290c5840fad9589da0c6a6d01d4f61fc80ca1c422a285f5a

See more details on using hashes here.

Release history Release notifications | RSS feed

0.1.0

2 files

0.0.4

2 files

This release

0.0.3 This release

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