Skip to content

Commit

Permalink
NN-527 Replace assert statement with explicit TypeError check
Browse files Browse the repository at this point in the history
- Replaced assert statement to ensure synonyms_list[index] is a string with an explicit check.
- Raises a TypeError if the condition is not met, addressing Bandit issue B101.
- Ensures the type check is enforced regardless of optimization levels.
  • Loading branch information
dyusuf committed Jul 26, 2024
1 parent 5555e85 commit 32ef03c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
3 changes: 2 additions & 1 deletion backend/src/util/data_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ def parse_compound_line(line):
elif line.startswith("NAME"):
name = line.lstrip("NAME").lstrip()
name = name[: last_index(name, " - ")]
assert name.strip() != ""
if name.strip() == "":
raise ValueError("Name cannot be an empty string")
elif line.startswith("DESCRIPTION"):
description = line.lstrip("DESCRIPTION").lstrip()
elif line.startswith("CLASS"):
Expand Down
15 changes: 10 additions & 5 deletions scraping/KEGG/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@

def organisms():
organisms_file = get(_organisms_endpoint)
assert organisms_file is not None
if organisms_file is None:
raise ValueError("organisms_file cannot be None")
return organisms_file


def pathways(organism_id):
endpoint = _pathway_template.substitute(organism_id=organism_id)
pathways_file = get(endpoint)
assert pathways_file is not None
if pathways_file is None:
raise ValueError("pathways_file cannot be None")
return pathways_file


Expand All @@ -34,7 +36,8 @@ def pathway(pathway_id, kgml=False):
entries=pathway_id, kgml="/kgml" if kgml else ""
)
pathway_file = get(endpoint)
assert pathway_file is not None
if pathway_file is None:
raise ValueError("pathway_file cannot be None")
return pathway_file


Expand All @@ -51,7 +54,8 @@ def map_identifiers_to_STRING(identifiers, species=None, split=10):
format="tsv", identifiers="%0d".join(identifiers_chunk), species=species
)
identifiers_file = get(endpoint)
assert identifiers_file is not None
if identifiers_file is None:
raise ValueError("identifiers_file cannot be None")
yield identifiers_file, i
if i + split >= len(identifiers):
break
Expand All @@ -62,5 +66,6 @@ def map_identifiers_to_STRING(identifiers, species=None, split=10):
format="tsv", identifiers="%0d".join(identifiers), species=species
)
identifiers_file = get(endpoint)
assert identifiers_file is not None
if identifiers_file is None:
raise ValueError("identifiers_file cannot be None")
yield identifiers_file, 0
3 changes: 2 additions & 1 deletion scraping/KEGG/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ def parse_compound_line(line):
elif line.startswith("NAME"):
name = line.lstrip("NAME").lstrip()
name = name[: last_index(name, " - ")]
assert name.strip() != ""
if name.strip() == "":
raise ValueError("Name cannot be an empty string")
elif line.startswith("DESCRIPTION"):
description = line.lstrip("DESCRIPTION").lstrip()
elif line.startswith("CLASS"):
Expand Down

0 comments on commit 32ef03c

Please sign in to comment.