Beispiel #1
0
    def __init__(self, payload_type):
        """Initializes `SymbolTree` with its payload type.

    Args:
      payload_type: Class which subclasses BoundVariableTracker; the type of
        payloads to be constructed and held in this SymbolTree.
    """
        initial_node = SequentialBindingNode(_BeginScopePointer())
        py_typecheck.check_subclass(payload_type, BoundVariableTracker)
        self.active_node = initial_node
        self.payload_type = payload_type
        self._node_ids = {id(initial_node): 1}
Beispiel #2
0
def type_tree_contains(type_tree, types_to_check_for):
    """Indicates whether the `type_ree` contains any `types_to_check_for`.

  Args:
    type_tree: Instance of `computation_types.Type`, the type whose tree we
      wish to check for the presence of `types_to_check_for`.
    types_to_check_for: The types we are looking for. Similar semantics as the
      second argument to `isinstance`; can be a single subclass of
      `computation_types.Type` or a tuple thereof.

  Returns:
    Boolean indicating whether or not there are any instances of
    `types_to_check_for` in the type tree under `type_tree`.

  Raises:
    TypeError: If `types_to_check_for` is not a subclass or tuple of subclasses
    of `computation_types.Type`, or `type_tree` is not an instance of
    `computation_types.Type`.
  """

    py_typecheck.check_type(type_tree, computation_types.Type)
    if isinstance(types_to_check_for, tuple):
        for t in types_to_check_for:
            py_typecheck.check_subclass(t, computation_types.Type)
    else:
        py_typecheck.check_subclass(types_to_check_for, computation_types.Type)

    # TODO(b/129791812): Clean up Python 2 and 3 compatibility issues.
    contains = [False]

    def _track_types(type_tree, types_to_check_for):
        """Checks subtree of `type_tree` for `types_to_check_for`."""
        if isinstance(type_tree, types_to_check_for):
            contains[0] = True
        return types_to_check_for

    preorder_call(type_tree, _track_types, types_to_check_for)

    return contains[0]
Beispiel #3
0
 def test_check_subclass(self):
   py_typecheck.check_subclass(PyTypeCheckTest, absltest.TestCase)
   py_typecheck.check_subclass(PyTypeCheckTest, (absltest.TestCase, int))
   py_typecheck.check_subclass(int, (int, float))
   py_typecheck.check_subclass(float, float)
   with self.assertRaisesRegex(TypeError, 'Expected .* to subclass '):
     py_typecheck.check_subclass(int, float)
     py_typecheck.check_subclass(int, (float, float))
   with self.assertRaisesRegex(TypeError, 'Expected a class,'):
     py_typecheck.check_subclass(0, int)
     py_typecheck.check_subclass(int, 0)
     py_typecheck.check_subclass(int, (int, 0))
Beispiel #4
0
 def test_check_subclass(self):
     py_typecheck.check_subclass(PyTypeCheckTest, parameterized.TestCase)
     py_typecheck.check_subclass(PyTypeCheckTest,
                                 (parameterized.TestCase, int))
     py_typecheck.check_subclass(int, (int, float))
     py_typecheck.check_subclass(float, float)
     with self.assertRaisesRegex(TypeError, 'Expected .* to subclass '):
         py_typecheck.check_subclass(int, float)
         py_typecheck.check_subclass(int, (float, float))