aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMuriithi Frederick Muriuki2021-07-29 14:09:49 +0300
committerBonfaceKilz2021-07-29 15:24:51 +0300
commit2ccbf1844afe352e23af7ff958ec2b0694cd87ea (patch)
tree855f947a7f5c09b9096fe22e791f0cfef4c29854
parent13680aa9206e2302760180bab3254182f11dde68 (diff)
downloadgenenetwork3-2ccbf1844afe352e23af7ff958ec2b0694cd87ea.tar.gz
Add partial type annotations for slink module
Issue: https://github.com/genenetwork/gn-gemtext-threads/blob/main/topics/gn1-migration-to-gn2/clustering.gmi * Add some type annotations for the `nearest` function. * Leave some comments regarding the issues experienced when trying to add some typing annotations to the function to help with future endeavours of the same.
-rw-r--r--gn3/computations/slink.py13
1 files changed, 12 insertions, 1 deletions
diff --git a/gn3/computations/slink.py b/gn3/computations/slink.py
index 4aac6b3..23d3d88 100644
--- a/gn3/computations/slink.py
+++ b/gn3/computations/slink.py
@@ -7,6 +7,10 @@ slink:
TODO: Describe what the function does...
"""
import logging
+from typing import List, Tuple, Union, Sequence
+
+NumType = Union[int, float]
+SeqOfNums = Sequence[NumType]
class LengthError(BaseException):
"""Raised whenever child lists/tuples are not the same length as the parent
@@ -73,7 +77,10 @@ raise an exception."""
def __flatten_list_of_lists(parent):
return [item for child in parent for item in child]
-def nearest(lists, i, j):
+# i and j are Union[SeqOfNums, NumType], but that leads to errors where the
+# values of i or j are indexed, since the NumType type is not indexable.
+# I don't know how to type this so that it does not fail on running `mypy .`
+def nearest(lists: Sequence[SeqOfNums], i, j) -> NumType:
"""
Computes shortest distance between member(s) in `i` and member(s) in `j`.
@@ -126,6 +133,10 @@ def nearest(lists, i, j):
raise ValueError("member values (i or j) should be lists/tuples of integers or integers")
+# `lists` here could be Sequence[SeqOfNums], but that leads to errors I do not
+# understand down the line
+# Might have to re-implement the function especially since the errors are thrown
+# where `listindexcopy` is involved
def slink(lists):
"""
DESCRIPTION: