Skip to content

Commit

Permalink
Fix type comparisons -> isinstance in some modules
Browse files Browse the repository at this point in the history
  • Loading branch information
NickleDave committed Feb 2, 2024
1 parent 6533b59 commit 134a67f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/crowsetta/formats/seq/birdsongrec.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def __init__(self, wav_file: PathLike, position: int, length: int, syl_list: lis
raise TypeError(f"length must be an int, not type {type(length)}")
if not isinstance(syl_list, list):
raise TypeError(f"syl_list must be a list, not type {type(syl_list)}")
if not all([type(syl) == BirdsongRecSyllable for syl in syl_list]):
if not all([isinstance(syl, BirdsongRecSyllable) for syl in syl_list]):
raise TypeError("not all elements in syl list are of type BirdsongRecSyllable: " f"{syl_list}")
self.wav_file = wav_file
self.position = position
Expand Down
4 changes: 2 additions & 2 deletions src/crowsetta/formats/seq/yarden.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

def _cast_to_arr(val):
"""helper function that casts single elements to 1-d numpy arrays"""
if type(val) == int or type(val) == float:
if isinstance(val, int) or isinstance(val, float):
# this happens when there's only one syllable in the file
# with only one corresponding label
return np.asarray([val]) # so make it a one-element list
elif type(val) == np.ndarray:
elif isinstance(val, np.ndarray):
# this should happen whenever there's more than one label
return val
else:
Expand Down
12 changes: 6 additions & 6 deletions src/crowsetta/sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ def __init__(self, segments, labels, onsets_s=None, offsets_s=None, onset_sample
Numpy array of type char, label for each annotated segment.
"""
if segments is not None:
if type(segments) == Segment:
if isinstance(segments, Segment):
segments = (segments,)
elif type(segments) in (list, tuple):
elif isinstance(segments, (list, tuple)):
segments = tuple(segments)
else:
raise TypeError(
Expand Down Expand Up @@ -140,7 +140,7 @@ def __hash__(self):
self._offset_samples,
self._labels,
]
list_for_hash = [tuple(item.tolist()) if type(item) == np.ndarray else item for item in list_for_hash]
list_for_hash = [tuple(item.tolist()) if isinstance(item, np.ndarray) else item for item in list_for_hash]
tup_for_hash = tuple(list_for_hash)
return hash(tup_for_hash)

Expand Down Expand Up @@ -181,9 +181,9 @@ def __ge__(self, other):

@staticmethod
def _convert_labels(labels):
if type(labels) == str:
if isinstance(labels, str):
labels = np.asarray(list(labels))
elif type(labels) == list or type(labels) == tuple:
elif isinstance(labels, list) or isinstance(labels, tuple):
try:
labels = [str(label) for label in labels]
except ValueError:
Expand All @@ -194,7 +194,7 @@ def _convert_labels(labels):
@staticmethod
def _validate_segments_type(segments):
"""Validate that all items in list of segments are Segment"""
if not all([type(seg) == Segment for seg in segments]):
if not all([isinstance(seg, Segment) for seg in segments]):
raise TypeError(
"A Sequence must be made from a list of Segments but not all " "items in the list passed were Segments."
)
Expand Down

0 comments on commit 134a67f

Please sign in to comment.