Skip to content

Commit

Permalink
Add compatibility function for NodeOrIndex type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
talmo committed Nov 1, 2024
1 parent 57ee01c commit 3f493af
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions sleap_io/model/skeleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ def __getitem__(self, idx) -> Node:
# type NodeOrIndex = Node | str | int # py >= 3.12


def is_node_or_index(obj: typing.Any) -> bool:
"""Check if an object is a `Node`, string name or integer index.
Args:
obj: The object to check.
Notes:
This is mainly for backwards compatibility with Python versions < 3.10 where
generics can't be used with `isinstance`. In newer Python, this is equivalent
to `isinstance(obj, NodeOrIndex)`.
"""
return isinstance(obj, (Node, str, int))


@define(eq=False)
class Skeleton:
"""A description of a set of landmark types and connections between them.
Expand Down Expand Up @@ -364,8 +378,8 @@ def add_edge(
if type(src) == tuple:
src, dst = src

if isinstance(src, NodeOrIndex):
if not isinstance(dst, NodeOrIndex):
if is_node_or_index(src):
if not is_node_or_index(dst):
raise ValueError("Destination node must be specified.")

Check warning on line 383 in sleap_io/model/skeleton.py

View check run for this annotation

Codecov / codecov/patch

sleap_io/model/skeleton.py#L383

Added line #L383 was not covered by tests

src = self.require_node(src)
Expand Down

0 comments on commit 3f493af

Please sign in to comment.