Skip to content

Commit 82d3eb9

Browse files
authored
Config to allow manual toggle (#417)
* feat: optional checks toggle after import
1 parent f249e95 commit 82d3eb9

File tree

13 files changed

+62
-18
lines changed

13 files changed

+62
-18
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [1.0.2] - 2025-10-23
10+
### Added:
11+
- Optional checks: Able to toggle for optional checks after importing bigtree. Previously, you can only set the
12+
environment variables before import.
13+
914
## [1.0.2] - 2025-10-23
1015
### Added:
1116
- Test: Fix code coverage.

bigtree/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from bigtree.dag.dag import DAG
77
from bigtree.dag.export import dag_to_dataframe, dag_to_dict, dag_to_dot, dag_to_list
88
from bigtree.dag.parsing import get_path_dag
9+
from bigtree.globals import ASSERTIONS
910
from bigtree.node.basenode import BaseNode
1011
from bigtree.node.binarynode import BinaryNode
1112
from bigtree.node.dagnode import DAGNode

bigtree/globals.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
import os
22

3-
ASSERTIONS: bool = bool(os.environ.get("BIGTREE_CONF_ASSERTIONS", True))
3+
4+
class ASSERTIONS:
5+
FLAG: bool = bool(os.environ.get("BIGTREE_CONF_ASSERTIONS", True))

bigtree/node/basenode.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def parent(self: T, new_parent: T) -> None:
192192
Args:
193193
new_parent: parent node
194194
"""
195-
if ASSERTIONS:
195+
if ASSERTIONS.FLAG:
196196
self.__check_parent_type(new_parent)
197197
self.__check_parent_loop(new_parent)
198198

@@ -335,7 +335,7 @@ def children(self: T, new_children: list[T] | tuple[T] | set[T]) -> None:
335335
Args:
336336
new_children: child node
337337
"""
338-
if ASSERTIONS:
338+
if ASSERTIONS.FLAG:
339339
self.__check_children_type(new_children)
340340
self.__check_children_loop(new_children)
341341
new_children = list(new_children)

bigtree/node/binarynode.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def parent(self: T, new_parent: T | None) -> None:
166166
Args:
167167
new_parent: parent node
168168
"""
169-
if ASSERTIONS:
169+
if ASSERTIONS.FLAG:
170170
self.__check_parent_type(new_parent)
171171
self._BaseNode__check_parent_loop(new_parent) # type: ignore
172172

@@ -296,7 +296,7 @@ def children(self: T, _new_children: list[T | None]) -> None:
296296
"""
297297
self._BaseNode__check_children_type(_new_children) # type: ignore
298298
new_children = self.__check_children_type(_new_children)
299-
if ASSERTIONS:
299+
if ASSERTIONS.FLAG:
300300
self.__check_children_loop(new_children)
301301

302302
current_new_children = {

bigtree/node/dagnode.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def parents(self: T, new_parents: list[T]) -> None:
212212
Args:
213213
new_parents: parent nodes
214214
"""
215-
if ASSERTIONS:
215+
if ASSERTIONS.FLAG:
216216
self.__check_parent_type(new_parents)
217217
self.__check_parent_loop(new_parents)
218218

@@ -311,7 +311,7 @@ def children(self: T, new_children: Iterable[T]) -> None:
311311
Args:
312312
new_children: child node
313313
"""
314-
if ASSERTIONS:
314+
if ASSERTIONS.FLAG:
315315
self.__check_children_type(new_children)
316316
self.__check_children_loop(new_children)
317317

bigtree/node/node.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def __init__(self, name: str, sep: str = "/", **kwargs: Any):
8484
self.name = name
8585
self._sep = sep
8686
super().__init__(**kwargs)
87-
if ASSERTIONS and not self.node_name:
87+
if ASSERTIONS.FLAG and not self.node_name:
8888
raise exceptions.TreeError("Node must have a `name` attribute")
8989

9090
@property
@@ -133,7 +133,7 @@ def _BaseNode__pre_assign_parent(self: T, new_parent: T) -> None:
133133
Args:
134134
new_parent: new parent to be added
135135
"""
136-
if ASSERTIONS and new_parent is not None:
136+
if ASSERTIONS.FLAG and new_parent is not None:
137137
if any(
138138
child.node_name == self.node_name and child is not self
139139
for child in new_parent.children
@@ -149,7 +149,7 @@ def _BaseNode__pre_assign_children(self: T, new_children: list[T]) -> None:
149149
Args:
150150
new_children: new children to be added
151151
"""
152-
if ASSERTIONS:
152+
if ASSERTIONS.FLAG:
153153
children_names = [node.node_name for node in new_children]
154154
duplicate_names = [
155155
item[0] for item in Counter(children_names).items() if item[1] > 1

docs/others/remove_checks.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,10 @@ os.environ["BIGTREE_CONF_ASSERTIONS"] = ""
2727

2828
import bigtree
2929
```
30+
31+
Alternatively, if you have already imported bigtree, you can set the configuration manually.
32+
33+
```python
34+
import bigtree
35+
bigtree.ASSERTIONS.FLAG = False
36+
```

tests/node/test_basenode_no_assertions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from bigtree.node import basenode
77

88

9-
@patch("bigtree.node.basenode.ASSERTIONS", "")
9+
@patch("bigtree.node.basenode.ASSERTIONS.FLAG", "")
1010
class TestBaseNodeNoAssertions(unittest.TestCase):
1111
def setUp(self):
1212
"""

tests/node/test_binarynode_no_assertions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from bigtree.utils.exceptions import TreeError
88

99

10-
@patch("bigtree.node.binarynode.ASSERTIONS", "")
10+
@patch("bigtree.node.binarynode.ASSERTIONS.FLAG", "")
1111
class TestBinaryNodeNoAssertions(unittest.TestCase):
1212
def setUp(self):
1313
self.a = binarynode.BinaryNode(1)

0 commit comments

Comments
 (0)