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

Uploaded Source

Built Distribution

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

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

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for typed_classproperties-1.1.0.tar.gz
Algorithm Hash digest
SHA256 8d47dac5d3a66befa2f098f50c66db5c5e1f4522f4db9504def5e4bc7d50a105
MD5 c6c5ba1c921b5a2c6afd975beec3f16e
BLAKE2b-256 50e950069e8006cde5fdce43bd5ed66e92015f9c02d50b539a638e34990d2cf0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typed_classproperties-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ed4524d53d1f0091640c3c8061ab61ed6245e5c92b613a8b7798b433df1c99a7
MD5 068b499a6d97848f0c0073630051dd83
BLAKE2b-256 4c6baea042f7448fde844f69aea0a3ea3b3222d6c469f2a6bfec9547ca31e257

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page