Skip to content

Commit 068a395

Browse files
authored
Merge pull request #1192 from kunal-10-cloud/fix/replace-assert-isinstance-with-explicit-validation
fix: replace assert isinstance with explicit type validation
2 parents 159b91e + 105debd commit 068a395

4 files changed

Lines changed: 90 additions & 20 deletions

File tree

malariagen_data/anoph/cnv_frq.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ def _gene_cnv(
9090
inline_array,
9191
):
9292
# Sanity check.
93-
assert isinstance(region, Region)
93+
if not isinstance(region, Region):
94+
raise TypeError(
95+
f"Expected region to be a Region object, "
96+
f"got {type(region).__name__}: {region!r}"
97+
)
9498

9599
# Access genes within the region of interest.
96100
df_genome_features = self.genome_features(region=region)
@@ -260,7 +264,11 @@ def _gene_cnv_frequencies(
260264
debug = self._log.debug
261265

262266
debug("sanity check - this function is one region at a time")
263-
assert isinstance(region, Region)
267+
if not isinstance(region, Region):
268+
raise TypeError(
269+
f"Expected region to be a Region object, "
270+
f"got {type(region).__name__}: {region!r}"
271+
)
264272

265273
debug("get gene copy number data")
266274
ds_cnv = self.gene_cnv(
@@ -504,7 +512,11 @@ def _gene_cnv_frequencies_advanced(
504512
debug = self._log.debug
505513

506514
debug("sanity check - here we deal with one region only")
507-
assert isinstance(region, Region)
515+
if not isinstance(region, Region):
516+
raise TypeError(
517+
f"Expected region to be a Region object, "
518+
f"got {type(region).__name__}: {region!r}"
519+
)
508520

509521
debug("access gene CNV calls")
510522
ds_cnv = self.gene_cnv(

malariagen_data/anoph/frq_base.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,11 @@ def plot_frequencies_heatmap(
341341
for j in range(1, idx_vals.shape[1]):
342342
index_col = index_col + ", " + idx_vals[:, j]
343343
else:
344-
assert isinstance(index, str)
344+
if not isinstance(index, str):
345+
raise TypeError(
346+
f"Expected index to be str or list, "
347+
f"got {type(index).__name__}: {index!r}"
348+
)
345349
index_col = df[index].astype(str)
346350

347351
# Check that index is unique.
@@ -588,7 +592,11 @@ def plot_frequencies_map_markers(
588592
ds_variant = ds.isel(variants=variant)
589593
variant_label = ds["variant_label"].values[variant]
590594
else:
591-
assert isinstance(variant, str)
595+
if not isinstance(variant, str):
596+
raise TypeError(
597+
f"Expected variant to be int or str, "
598+
f"got {type(variant).__name__}: {variant!r}"
599+
)
592600
ds_variant = ds.set_index(variants="variant_label").sel(variants=variant)
593601
variant_label = variant
594602

malariagen_data/anoph/sample_metadata.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,7 +1235,11 @@ def lookup_sample(
12351235
if isinstance(sample, str):
12361236
sample_rec = df_samples.loc[sample]
12371237
else:
1238-
assert isinstance(sample, int)
1238+
if not isinstance(sample, int):
1239+
raise TypeError(
1240+
f"Expected sample to be str or int, "
1241+
f"got {type(sample).__name__}: {sample!r}"
1242+
)
12391243
sample_rec = df_samples.iloc[sample]
12401244
return sample_rec
12411245

@@ -1348,7 +1352,11 @@ def _setup_sample_symbol(
13481352

13491353
else:
13501354
# Custom grouping using queries.
1351-
assert isinstance(symbol, Mapping)
1355+
if not isinstance(symbol, Mapping):
1356+
raise TypeError(
1357+
f"Expected symbol to be str or Mapping, "
1358+
f"got {type(symbol).__name__}: {symbol!r}"
1359+
)
13521360
data["symbol"] = ""
13531361
for key, value in symbol.items():
13541362
data.loc[data.query(value).index, "symbol"] = key
@@ -1397,7 +1405,11 @@ def _setup_sample_colors_plotly(
13971405

13981406
else:
13991407
# Custom grouping using queries.
1400-
assert isinstance(color, Mapping)
1408+
if not isinstance(color, Mapping):
1409+
raise TypeError(
1410+
f"Expected color to be str or Mapping, "
1411+
f"got {type(color).__name__}: {color!r}"
1412+
)
14011413
data["color"] = ""
14021414
for key, value in color.items():
14031415
data.loc[data.query(value).index, "color"] = key
@@ -1493,13 +1505,17 @@ def _setup_cohort_queries(
14931505
"""Convenience function to normalise the `cohorts` parameter to a
14941506
dictionary mapping cohort labels to sample metadata queries."""
14951507

1496-
if isinstance(cohorts, dict):
1508+
if isinstance(cohorts, Mapping):
14971509
# User has supplied a custom dictionary mapping cohort identifiers
14981510
# to pandas queries.
14991511
cohort_queries = cohorts
15001512

15011513
else:
1502-
assert isinstance(cohorts, str)
1514+
if not isinstance(cohorts, str):
1515+
raise TypeError(
1516+
f"Expected cohorts to be Mapping or str, "
1517+
f"got {type(cohorts).__name__}: {cohorts!r}"
1518+
)
15031519
# User has supplied a column in the sample metadata.
15041520
df_samples = self.sample_metadata(
15051521
sample_sets=sample_sets,
@@ -1855,7 +1871,11 @@ def _locate_cohorts(*, cohorts, data, min_cohort_size):
18551871
coh_dict[coh] = loc_coh
18561872

18571873
else:
1858-
assert isinstance(cohorts, str)
1874+
if not isinstance(cohorts, str):
1875+
raise TypeError(
1876+
f"Expected cohorts to be Mapping or str, "
1877+
f"got {type(cohorts).__name__}: {cohorts!r}"
1878+
)
18591879
# User has supplied the name of a sample metadata column.
18601880

18611881
# Convenience to allow things like "admin1_year" instead of "cohort_admin1_year".

malariagen_data/util.py

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,11 @@ def _dask_compress_dataset(ds, indexer, dim):
311311
# the underlying data
312312
indexer_computed = indexer.compute()
313313
else:
314-
assert isinstance(indexer, np.ndarray)
314+
if not isinstance(indexer, np.ndarray):
315+
raise TypeError(
316+
f"Expected indexer to be a dask.array.Array or numpy.ndarray, "
317+
f"got {type(indexer).__name__}"
318+
)
315319
indexer_computed = indexer
316320

317321
coords = dict()
@@ -375,7 +379,11 @@ def _da_compress(
375379

376380
# Load the indexer temporarily for chunk size computations.
377381
if indexer_computed is None:
378-
assert isinstance(indexer, da.Array)
382+
if not isinstance(indexer, da.Array):
383+
raise TypeError(
384+
f"Expected indexer to be a dask.array.Array, "
385+
f"got {type(indexer).__name__}"
386+
)
379387
indexer_computed = indexer.compute()
380388

381389
# Ensure indexer and data are chunked in the same way.
@@ -665,7 +673,11 @@ def _parse_single_region(resource, region: single_region_param_type) -> Region:
665673
end=region.get("end"),
666674
)
667675

668-
assert isinstance(region, str)
676+
if not isinstance(region, str):
677+
raise TypeError(
678+
f"Expected region to be a mapping or str, "
679+
f"got {type(region).__name__}: {region!r}"
680+
)
669681

670682
# check if region is a whole contig
671683
if region in _valid_contigs(resource):
@@ -1495,8 +1507,14 @@ def _apply_allele_mapping(x, mapping, max_allele):
14951507

14961508

14971509
def _dask_apply_allele_mapping(v, mapping, max_allele):
1498-
assert isinstance(v, da.Array)
1499-
assert isinstance(mapping, np.ndarray)
1510+
if not isinstance(v, da.Array):
1511+
raise TypeError(
1512+
f"Expected v to be a dask.array.Array, " f"got {type(v).__name__}"
1513+
)
1514+
if not isinstance(mapping, np.ndarray):
1515+
raise TypeError(
1516+
f"Expected mapping to be a numpy.ndarray, " f"got {type(mapping).__name__}"
1517+
)
15001518
assert v.ndim == 2
15011519
assert mapping.ndim == 2
15021520
assert v.shape[0] == mapping.shape[0]
@@ -1516,8 +1534,14 @@ def _genotype_array_map_alleles(gt, mapping):
15161534
# Transform genotype calls via an allele mapping.
15171535
# N.B., scikit-allel does not handle empty blocks well, so we
15181536
# include some extra logic to handle that better.
1519-
assert isinstance(gt, np.ndarray)
1520-
assert isinstance(mapping, np.ndarray)
1537+
if not isinstance(gt, np.ndarray):
1538+
raise TypeError(
1539+
f"Expected gt to be a numpy.ndarray, " f"got {type(gt).__name__}"
1540+
)
1541+
if not isinstance(mapping, np.ndarray):
1542+
raise TypeError(
1543+
f"Expected mapping to be a numpy.ndarray, " f"got {type(mapping).__name__}"
1544+
)
15211545
assert gt.ndim == 3
15221546
assert mapping.ndim == 3
15231547
assert gt.shape[0] == mapping.shape[0]
@@ -1536,8 +1560,14 @@ def _genotype_array_map_alleles(gt, mapping):
15361560

15371561

15381562
def _dask_genotype_array_map_alleles(gt, mapping):
1539-
assert isinstance(gt, da.Array)
1540-
assert isinstance(mapping, np.ndarray)
1563+
if not isinstance(gt, da.Array):
1564+
raise TypeError(
1565+
f"Expected gt to be a dask.array.Array, " f"got {type(gt).__name__}"
1566+
)
1567+
if not isinstance(mapping, np.ndarray):
1568+
raise TypeError(
1569+
f"Expected mapping to be a numpy.ndarray, " f"got {type(mapping).__name__}"
1570+
)
15411571
assert gt.ndim == 3
15421572
assert mapping.ndim == 2
15431573
assert gt.shape[0] == mapping.shape[0]

0 commit comments

Comments
 (0)