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.2.tar.gz (29.5 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.2-py3-none-any.whl (24.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: kapro-0.0.2.tar.gz
  • Upload date:
  • Size: 29.5 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.2.tar.gz
Algorithm Hash digest
SHA256 8279601eebc69ef91bead303708eca6921946d778d09a6806d1ce3d8ca73c9b6
MD5 a14617e7d64d39bdaf41d36e5e742b84
BLAKE2b-256 b70fe49a4e5de46f3c6adf0918f6a9cf61e54ac826879433aa795b320e3163d0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: kapro-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 24.1 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 099b17dc43f9444b812f3aa1027becd7521f204f170bdb2f5c18f87d8c668305
MD5 6c2664a802c28fd875f300164d4a3366
BLAKE2b-256 50b1157af9ec31dbfc4007149b1677e27641a7d13b5c84de4a69537821271a94

See more details on using hashes here.

Release history Release notifications | RSS feed

0.1.0

2 files

0.0.4

2 files

0.0.3

2 files

This release

0.0.2 This release

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