Fix/replace asserts with runtime validation#1260
Open
khushthecoder wants to merge 1 commit intomalariagen:masterfrom
Open
Fix/replace asserts with runtime validation#1260khushthecoder wants to merge 1 commit intomalariagen:masterfrom
khushthecoder wants to merge 1 commit intomalariagen:masterfrom
Conversation
…uction code Resolves malariagen#1259. Replace 40+ assert statements across 13 production modules with explicit if/raise checks using ValueError, TypeError, or RuntimeError with descriptive error messages. This ensures validation is enforced even when Python runs with -O (optimize) flag, which silently strips assert statements. Exception mapping: - Invalid parameter values (metric, contig) → ValueError - Wrong array shape/ndim/dtype → ValueError - Internal state unexpectedly None → RuntimeError - Internal consistency violations → RuntimeError Three assert statements inside @numba.njit functions in util.py are intentionally preserved, as numba JIT compilation does not support Python exception raising. Files modified: - malariagen_data/util.py - malariagen_data/mjn.py - malariagen_data/anopheles.py - malariagen_data/anoph/snp_data.py - malariagen_data/anoph/hap_data.py - malariagen_data/anoph/hap_frq.py - malariagen_data/anoph/snp_frq.py - malariagen_data/anoph/cnv_frq.py - malariagen_data/anoph/genome_features.py - malariagen_data/anoph/genome_sequence.py - malariagen_data/anoph/h1x.py - malariagen_data/anoph/sample_metadata.py - malariagen_data/anoph/aim_data.py
ef0d129 to
eaf3122
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replace
assertStatements with Proper Runtime ValidationSummary
This PR replaces 42 production
assertstatements with explicitif/raisevalidation checks across 13 production modules. This ensures that all input validation and internal state checks are enforced even when Python runs with the-O(optimize) flag, which silently stripsassertstatements — potentially causing confusing downstream errors or silent data corruption.Context
Python's
assertstatement is designed for debugging, not production validation. When Python is invoked with-Oor-OO, allassertstatements are compiled away to no-ops. This means:assert contig in self.contigs)assert ha.ndim == hb.ndim == 2)assert self._default_site_mask is not None)In a data-science library like
malariagen-data-python, where downstream code relies on array shapes and contig membership being correct, this can lead to silent data corruption — the worst kind of bug.Changes
Exception Mapping Strategy
Each
assertwas replaced with a semantically appropriate exception:ValueErrorcontig not in self.contigs,metric not in ("hamming", "jaccard")ValueErrorha.ndim != 2,ha.shape[0] != hb.shape[0]ValueErrorn_samples < min_cohort_sizeNoneRuntimeErrorself._default_site_mask is NoneRuntimeErrornobs_mode != "fixed", jackknife resampling mismatchAll replacement exceptions include descriptive error messages with actual vs. expected values (e.g.,
f"Contig {contig!r} not found. Available contigs: {self.contigs}"), significantly improving debuggability.Files Modified (14 files, +211 / -54)
malariagen_data/util.pymalariagen_data/anoph/snp_data.pymalariagen_data/anoph/hap_data.pymalariagen_data/anoph/hap_frq.pymalariagen_data/anoph/snp_frq.pymalariagen_data/anoph/genome_features.pymalariagen_data/anoph/h1x.pymalariagen_data/anoph/aim_data.pymalariagen_data/anoph/sample_metadata.pymalariagen_data/anoph/cnv_frq.pymalariagen_data/anoph/genome_sequence.pymalariagen_data/anopheles.pymalariagen_data/mjn.pyIntentionally Preserved
assert(3 instances)Three
assertstatements inside@numba.njit-decorated functions inutil.pyare intentionally kept as-is:Reason: Numba's JIT compiler does not support raising Python exceptions (
ValueError,RuntimeError, etc.) inside@numba.njitfunctions. Usingassertis the only available validation mechanism in JIT-compiled code, and Numba does compile these assertions into runtime checks (they are NOT stripped by-O).Example: Before vs. After
Before (silently stripped with
-O):After (always enforced, with descriptive message):
Verification
Behavioral Impact
ValueError/RuntimeErrormessages instead of bareAssertionError-Osafe — validation is enforced regardless of Python optimization level