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)
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
def test_typevars(self): T, U, V, W, X = TypeMap({ (Foo, Bar, Str % Choices('A', 'B')): (C1[Foo], C1[Bar]), (Foo | Bar, Foo, Str): (C1[Bar], C1[Foo]) }) scope = {} T1 = ast_to_type(T.to_ast(), scope=scope) U1 = ast_to_type(U.to_ast(), scope=scope) V1 = ast_to_type(V.to_ast(), scope=scope) W1 = ast_to_type(W.to_ast(), scope=scope) X1 = ast_to_type(X.to_ast(), scope=scope) self.assertEqual(len(scope), 1) self.assertEqual(scope[id(T.mapping)], [T1, U1, V1, W1, X1]) self.assertEqual(T1.mapping.lifted, T.mapping.lifted) self.assertIs(T1.mapping, U1.mapping) self.assertIs(U1.mapping, V1.mapping) self.assertIs(V1.mapping, W1.mapping) self.assertIs(W1.mapping, X1.mapping)
def type_from_ast(ast, scope=None): """Convert a type ast (from `.to_ast()`) to a type expression. Parameters ---------- ast : json compatible object The abstract syntax tree produced by `to_ast` on a type. scope : dict A dictionary to use between multiple calls to share scope between different types. This allows type variables from the same type map to be constructed from an equivalent type map. Scope should be shared within a given call signature, but not between call signatures. Returns ------- type expression """ return _parse.ast_to_type(ast, scope=scope)
def _norm_input(t): if type(t) is dict: return ast_to_type(t) elif not isinstance(t, _ExpBase): raise TypeError("%r is not a QIIME 2 type" % (t, )) return t