Skip to content

Commit

Permalink
Add type-checking code to Node constructor (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
frreiss authored Aug 5, 2021
1 parent 399765e commit c9e5a26
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions graph_def_editor/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import tensorflow.compat.v1 as tf
import sys
if sys.version >= '3':
from typing import Tuple, List, Iterable, Any, AbstractSet
from typing import Tuple, List, Iterable, Any, AbstractSet, Type

from graph_def_editor import graph, tensor, util

Expand All @@ -42,6 +42,20 @@
"Node",
]

def _type_check(obj: Any, expected_type: Type, arg_name: str):
"""
Subroutine to validate the type of a function argument.
Args:
obj: Argument to validate
expected_type: Expected type of `obj`
arg_name: Name of the function argument where the caller passed `obj`
"""
if not isinstance(obj, expected_type):
raise TypeError("'{}' argument should be of type {}, but got object of "
"type {} instead. Value received was {}."
"".format(arg_name, expected_type, type(obj), obj))


class Node(object):
"""
Expand All @@ -50,11 +64,11 @@ class Node(object):
tf.NodeDef protobuf on demand.
"""
def __init__(self,
g, # type: graph.Graph
node_id, # type: int
name, # type: int
op_name, # type: str
device = "" # type: str
g, # type: graph.Graph
node_id, # type: int
name, # type: str
op_name, # type: str
device="" # type: str
):
"""
This constructor should only be called from methods of the Graph
Expand All @@ -69,6 +83,11 @@ def __init__(self,
device: TensorFlow device specification string indicating where this node
should be located. Default value of "" means "use the default device"
"""
_type_check(g, graph.Graph, "g")
_type_check(node_id, int, "node_id")
_type_check(name, str, "name")
_type_check(op_name, str, "op_name")
_type_check(device, str, "device")
self._graph = g
self._id = node_id
self._name = name
Expand Down

0 comments on commit c9e5a26

Please sign in to comment.