Skip to main content

Typed decorators for classproperty and cached_classproperty.

Project description

typed_classproperties

Pydowndoc PyPI Version Python Version Tests Status mypy Status pre-commit Status Ruff uv

Typed decorators for classproperty and cached_classproperty.

Python 3 compatible only*. No dependencies*footnote:[The library [typing_extensions](https://github.com/python/typing_extensions) is required when running with Python < 3.12].

Installation

This package is hosted on PyPI and can be installed using uv or pip.

Add to your uv project/script’s dependencies

uv add typed_classproperties

Install using pip after creating a virtual environment

path/to/venv/python -m 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 further usage examples and expected behaviour.

To run tests

uv run --group test -- pytest

Credits

Credits to Denis Ryzhkov, on Stack Overflow, for the original implementation of the @classproperty decorator: 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.3.0.tar.gz (8.9 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.3.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for typed_classproperties-1.3.0.tar.gz
Algorithm Hash digest
SHA256 f3be519eaaa20e6710971b9392179cc3fb46d2b81e708e00567012a716a96bfe
MD5 f5cea1b313c6b9b023996ff71a82e4c2
BLAKE2b-256 74e5ce7b56a356f543875e331d6bccbf5ad9cbc95a81aeab5f891159ac2f885a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typed_classproperties-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2edaf0de7d71520372c0f52ee5dcac51053e229bd59a309c840682ec11627c7c
MD5 c1bc097553691141da77aae85542f651
BLAKE2b-256 13505484cef35e4051b63a4ca133ae0137c0fed3640d56c5da1bf0f777aecc07

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