Skip to content

Commit 59fea40

Browse files
committed
Fixed state class.
Signed-off-by: Pavel Kirilin <win10@list.ru>
1 parent 6f7765a commit 59fea40

File tree

2 files changed

+73
-4
lines changed

2 files changed

+73
-4
lines changed

taskiq/state.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
1+
from collections import UserDict
12
from typing import Any
23

34

4-
class TaskiqState(dict[str, Any]):
5+
class TaskiqState(UserDict[str, Any]):
56
"""
67
State class.
78
89
This class is used to store useful variables
910
for later use.
1011
"""
1112

13+
def __init__(self) -> None:
14+
self.__dict__["data"] = {}
15+
1216
def __getattr__(self, name: str) -> Any:
1317
try:
14-
return super().__getitem__(name) # noqa: WPS613
18+
return self.__dict__["data"][name]
1519
except KeyError:
1620
cls_name = self.__class__.__name__
1721
raise AttributeError(f"'{cls_name}' object has no attribute '{name}'")
1822

1923
def __setattr__(self, name: str, value: Any) -> None:
20-
super().__setitem__(name, value) # noqa: WPS613
24+
self[name] = value
2125

2226
def __delattr__(self, name: str) -> None: # noqa: WPS603
23-
return super().__delitem__(name) # noqa: WPS613
27+
try:
28+
del self[name] # noqa: WPS420
29+
except KeyError:
30+
cls_name = self.__class__.__name__
31+
raise AttributeError(f"'{cls_name}' object has no attribute '{name}'")
32+
33+
def __str__(self) -> str:
34+
return "TaskiqState(%s)" % super().__str__()

taskiq/tests/test_state.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from taskiq.state import TaskiqState
2+
3+
4+
def test_state_set() -> None:
5+
"""Tests that you can sel values as dict items."""
6+
state = TaskiqState()
7+
state["a"] = 1
8+
9+
assert state["a"] == 1
10+
11+
12+
def test_state_get() -> None:
13+
"""Tests that you can get values as dict items."""
14+
state = TaskiqState()
15+
16+
state["a"] = 1
17+
18+
assert state["a"] == 1
19+
20+
21+
def test_state_del() -> None:
22+
"""Tests that you can del values as dict items."""
23+
state = TaskiqState()
24+
25+
state["a"] = 1
26+
27+
del state["a"] # noqa: WPS420
28+
29+
assert state.get("a") is None
30+
31+
32+
def test_state_set_attr() -> None:
33+
"""Tests that you can set values by attribute."""
34+
state = TaskiqState()
35+
36+
state.a = 1
37+
38+
assert state["a"] == 1
39+
40+
41+
def test_state_get_attr() -> None:
42+
"""Tests that you can get values by attribute."""
43+
state = TaskiqState()
44+
45+
state["a"] = 1
46+
47+
assert state.a == 1
48+
49+
50+
def test_state_del_attr() -> None:
51+
"""Tests that you can delete values by attribute."""
52+
state = TaskiqState()
53+
54+
state["a"] = 1
55+
56+
del state.a # noqa: WPS420
57+
58+
assert state.get("a") is None

0 commit comments

Comments
 (0)