Skip to content

Commit 3a1355c

Browse files
boris-42swilly22
andauthored
Remove assert statements from code base (#110)
Asserts shall not be a part of production code, they are stript if python is run with -o (optimization parameter) Instead of assert use if and raise exceptions Co-authored-by: Roi Lipman <[email protected]>
1 parent 6e6959d commit 3a1355c

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

Diff for: redisgraph/edge.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ def __init__(self, src_node, relation, dest_node, edge_id=None, properties=None)
1010
"""
1111
Create a new edge.
1212
"""
13-
assert src_node is not None and dest_node is not None
13+
if not (src_node and dest_node):
14+
# NOTE(bors-42): It makes sense to change AssertionError to
15+
# ValueError here
16+
raise AssertionError("Both src_node & dest_node must be provided")
1417

1518
self.id = edge_id
16-
self.relation = '' or relation
17-
self.properties = {} or properties
19+
self.relation = relation or ''
20+
self.properties = properties or {}
1821
self.src_node = src_node
1922
self.dest_node = dest_node
2023

Diff for: redisgraph/graph.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,12 @@ def add_node(self, node):
9393

9494
def add_edge(self, edge):
9595
"""
96-
Addes an edge to the graph.
96+
Adds an edge to the graph.
9797
"""
98+
if not (self.nodes[edge.src_node.alias]
99+
and self.nodes[edge.dest_node.alias]):
100+
raise AssertionError("Both edge's end must be in the graph")
98101

99-
# Make sure edge both ends are in the graph
100-
assert self.nodes[edge.src_node.alias] is not None and self.nodes[edge.dest_node.alias] is not None
101102
self.edges.append(edge)
102103

103104
def commit(self):
@@ -128,12 +129,13 @@ def flush(self):
128129
self.edges = []
129130

130131
def build_params_header(self, params):
131-
assert type(params) == dict
132+
if not isinstance(params, dict):
133+
raise TypeError("'params' must be a dict")
132134
# Header starts with "CYPHER"
133135
params_header = "CYPHER "
134136
for key, value in params.items():
135137
# If value is string add quotation marks.
136-
if type(value) == str:
138+
if isinstance(value, str):
137139
value = quote_string(value)
138140
# Value is None, replace with "null" string.
139141
elif value is None:
@@ -189,7 +191,6 @@ def execution_plan(self, query, params=None):
189191
Get the execution plan for given query,
190192
GRAPH.EXPLAIN returns an array of operations.
191193
"""
192-
193194
if params is not None:
194195
query = self.build_params_header(params) + query
195196

0 commit comments

Comments
 (0)