API referenceΒΆ

quacks.readonly(cls: type) β†’ type[source]ΒΆ

Decorate a Protocol to make it read-only.

Unlike default protocol attributes, read-only protocols will match frozen dataclasses and other immutable types.

Read-only attributes are already supported in protocols with @property, but this is cumbersome to do for many attributes. The @readonly decorator effectively transforms all mutable attributes into read-only properties.

Example

from quacks import readonly

@readonly
class User(Protocol):
    id: int
    name: str
    is_premium: bool

# equivalent to:
class User(Protocol):
    @property
    def id(self) -> int: ...
    @property
    def name(self) -> str: ...
    @property
    def is_premium(self) -> bool: ...

Warning

Subprotocols and inherited attributes are not supported yet.