Пример #1
0
def parse_type(string, expect=None):
    """Convert a string into a type expression

    Parameters
    ----------
    string : str
        The string type expression to convert into a TypeExpression
    expect : {'semantic', 'primitive', 'visualization'}, optional
        Will raise a TypeError if the resulting TypeExpression is not a member
        of `expect`.

    Returns
    -------
    type expression

    """
    if expect is not None and expect not in {'semantic', 'primitive',
                                             'visualization'}:
        raise ValueError("`expect` got %r, must be 'semantic', 'primitive',"
                         " 'visualization', or None." % (expect,))

    type_expr = _parse.ast_to_type(_parse.string_to_ast(string))

    if expect is None:
        pass
    elif expect == 'semantic' and qtype.is_semantic_type(type_expr):
        pass
    elif expect == 'primitive' and qtype.is_primitive_type(type_expr):
        pass
    elif expect == 'visualization' and type_expr == qtype.Visualization:
        pass
    else:
        raise TypeError("Type expression %r is not a %s type."
                        % (type_expr, expect))
    return type_expr
Пример #2
0
    def assert_roundtrip(self, type):
        ast = string_to_ast(repr(type))
        type1 = ast_to_type(ast)
        type2 = ast_to_type(type1.to_ast())

        self.assertEqual(type, type1)
        self.assertEqual(ast, type1.to_ast())
        self.assertEqual(type1, type2)
Пример #3
0
 def test_weird(self):
     with self.assertRaisesRegex(ValueError, "Unknown literal"):
         string_to_ast('FeatureTable(Foo + Bar)')
Пример #4
0
 def test_more_bad(self):
     with self.assertRaisesRegex(ValueError, "Unknown expression"):
         string_to_ast('lambda x: x')
Пример #5
0
 def test_bad_juju(self):
     with self.assertRaisesRegex(ValueError, "one type expression"):
         string_to_ast('import os; os.rmdir("something-important")')
Пример #6
0
 def test_syntax_error(self):
     with self.assertRaisesRegex(ValueError, "could not be parsed"):
         string_to_ast('$')