|
6 | 6 | working code examples. |
7 | 7 | """ |
8 | 8 |
|
9 | | -import pytest |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +from dataclasses import field |
10 | 12 |
|
11 | | -from dataclasses import dataclass, field |
12 | | -from typing import Optional |
| 13 | +import pytest |
13 | 14 |
|
14 | 15 | from libtmux._internal.frozen_dataclass_sealable import ( |
15 | 16 | frozen_dataclass_sealable, |
|
19 | 20 |
|
20 | 21 | def test_basic_usage(): |
21 | 22 | """Test basic usage of frozen_dataclass_sealable.""" |
| 23 | + |
22 | 24 | @frozen_dataclass_sealable |
23 | 25 | class Config: |
24 | 26 | name: str |
25 | 27 |
|
26 | 28 | values: dict[str, int] = field( |
27 | | - default_factory=dict, |
28 | | - metadata={"mutable_during_init": True} |
| 29 | + default_factory=dict, metadata={"mutable_during_init": True} |
29 | 30 | ) |
30 | 31 |
|
31 | 32 | # Create an instance |
32 | 33 | config = Config(name="test-config") |
33 | 34 | assert config.name == "test-config" |
34 | | - |
| 35 | + |
35 | 36 | # Cannot modify immutable fields |
36 | 37 | with pytest.raises(AttributeError): |
37 | 38 | config.name = "modified" |
38 | | - |
| 39 | + |
39 | 40 | # Can modify mutable fields |
40 | 41 | config.values["key1"] = 100 |
41 | 42 | assert config.values["key1"] == 100 |
42 | 43 |
|
43 | 44 | # Check sealable property |
44 | 45 | assert is_sealable(config) |
45 | | - |
| 46 | + |
46 | 47 | # Seal the object |
47 | 48 | config.seal() |
48 | 49 | assert hasattr(config, "_sealed") and config._sealed |
49 | | - |
| 50 | + |
50 | 51 | # Can still modify contents of mutable containers after sealing |
51 | 52 | config.values["key2"] = 200 |
52 | 53 | assert config.values["key2"] == 200 |
53 | 54 |
|
54 | 55 |
|
55 | 56 | def test_deferred_sealing(): |
56 | 57 | """Test deferred sealing with linked nodes.""" |
| 58 | + |
57 | 59 | @frozen_dataclass_sealable |
58 | 60 | class Node: |
59 | 61 | value: int |
60 | 62 |
|
61 | | - next_node: Optional['Node'] = field( |
62 | | - default=None, |
63 | | - metadata={"mutable_during_init": True} |
| 63 | + next_node: Node | None = field( |
| 64 | + default=None, metadata={"mutable_during_init": True} |
64 | 65 | ) |
65 | 66 |
|
66 | 67 | # Create a linked list (not circular to avoid recursion issues) |
67 | 68 | node1 = Node(value=1) |
68 | 69 | node2 = Node(value=2) |
69 | 70 | node1.next_node = node2 |
70 | | - |
| 71 | + |
71 | 72 | # Verify structure |
72 | 73 | assert node1.value == 1 |
73 | 74 | assert node2.value == 2 |
74 | 75 | assert node1.next_node is node2 |
75 | | - |
| 76 | + |
76 | 77 | # Verify sealable property |
77 | 78 | assert is_sealable(node1) |
78 | 79 | assert is_sealable(node2) |
79 | | - |
80 | | - # Seal nodes individually |
| 80 | + |
| 81 | + # Seal nodes individually |
81 | 82 | node1.seal() |
82 | 83 | node2.seal() |
83 | | - |
| 84 | + |
84 | 85 | # Verify both nodes are sealed |
85 | 86 | assert hasattr(node1, "_sealed") and node1._sealed |
86 | 87 | assert hasattr(node2, "_sealed") and node2._sealed |
87 | | - |
| 88 | + |
88 | 89 | # Verify immutability after sealing |
89 | 90 | with pytest.raises(AttributeError): |
90 | 91 | node1.value = 10 |
|
0 commit comments