Skip to main content

Typed decorators for classproperty and cached_classproperty.

Project description

typed_classproperties

Typed decorators for classproperty and cached_classproperty.

Python 3 compatible only. No dependencies.

Installation

This package is hosted on PYPI and can be installed using uv or pip. E.g.

uv add typed_classproperties
pip install typed_classproperties

Example usage

from typing import override

from typed_classproperties import classproperty, cached_classproperty


class Foo:
    @override
    def __init__(self, bar: str) -> None:
        self.bar: str = bar

    @classproperty
    def BAR(cls) -> int:
        return 1


assert Foo.BAR == 1
assert Foo(bar="one").BAR == 1


class CachedFoo:
    @override
    def __init__(self, bar: str) -> None:
        self.bar: str = bar

    @cached_classproperty
    def BAR(cls) -> int:
        print("This will be executed only once")
        return 1


assert CachedFoo.BAR == 1
assert CachedFoo(bar="bar").FOO == 1

Tests

See tests.py for usage examples and expected behaviour.

To run tests:

uv run --group test pytest

Credits

Credits to Denis Ryzhkov on Stackoverflow for the implementation of classproperty: https://stackoverflow.com/a/13624858/1280629

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

typed_classproperties-1.0.1.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

typed_classproperties-1.0.1-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file typed_classproperties-1.0.1.tar.gz.

File metadata

File hashes

Hashes for typed_classproperties-1.0.1.tar.gz
Algorithm Hash digest
SHA256 12280ba281a6185f1aa69d16079a5c3b7cd7b9b7b759778acb65dd467842cff5
MD5 006f33551cbe549accf5d036ee0f959c
BLAKE2b-256 303253f1f0c494c1fc8bfa1e8d37140045c2186524a0ea124014f3e3450fe472

See more details on using hashes here.

File details

Details for the file typed_classproperties-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for typed_classproperties-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b64d81dd2c93177eee3eab305d031e69547cdc8be3b234370dec0ff17289a88b
MD5 301839dfdf2c4e0ace937a695a933fbf
BLAKE2b-256 a71e3aace7ef733d5394eea7c2cfecedf8e75cb2fb757fd892f33b7ba1169ab0

See more details on using hashes here.

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