Release/v0.4.1 #1
1 changed files with 17 additions and 12 deletions
|
|
@ -1,27 +1,32 @@
|
||||||
from typing import Any, Callable
|
from typing import Any, Callable
|
||||||
|
|
||||||
|
from snakia.utils import throw
|
||||||
|
|
||||||
class Readonly[T]:
|
from .property import Property
|
||||||
|
|
||||||
|
|
||||||
|
class Readonly[T](Property[T]):
|
||||||
"""
|
"""
|
||||||
|
|
|||||||
Readonly property.
|
Readonly property.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__slots__ = ("__fget",)
|
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
fget: Callable[[Any], T],
|
fget: Callable[[Any], T],
|
||||||
|
*,
|
||||||
|
strict: bool = False,
|
||||||
) -> None:
|
) -> None:
|
||||||
self.__fget = fget
|
super().__init__(
|
||||||
|
fget=fget,
|
||||||
def __get__(self, instance: Any, owner: type | None = None, /) -> T:
|
fset=(
|
||||||
return self.__fget(instance)
|
(lambda *_: throw(TypeError("Cannot set readonly property")))
|
||||||
|
if strict
|
||||||
def __set__(self, instance: Any, value: T, /) -> None:
|
else lambda *_: None
|
||||||
pass
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def readonly[T](value: T) -> Readonly[T]:
|
def readonly[T](value: T, *, strict: bool = False) -> Readonly[T]:
|
||||||
"""Create a readonly property with the given value.
|
"""Create a readonly property with the given value.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
|
@ -30,4 +35,4 @@ def readonly[T](value: T) -> Readonly[T]:
|
||||||
Returns:
|
Returns:
|
||||||
Readonly[T]: The readonly property.
|
Readonly[T]: The readonly property.
|
||||||
"""
|
"""
|
||||||
return Readonly(lambda _: value)
|
return Readonly(lambda _: value, strict=strict)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue
🚫 Codacy found a high ErrorProne issue: invalid syntax (F999)
The issue arises from the use of generics in the class definition. The syntax
Readonly[T](Property[T])is incorrect in Python, as the use of generics requires theGenericbase class from thetypingmodule. To fix this, you need to inherit fromGeneric[T]in addition toProperty[T].Here's the code suggestion to fix the issue:
This comment was generated by an experimental AI tool.