def f(t: Type): return match(t)(lambda X: { Type.template_instantiation('std::tuple', [Type.template_instantiation('std::tuple', [*X]), # note: A previous match as a List\[Type\] was here Type.template_instantiation('std::tuple', [X])]): # error: Can't match X as a Type because it was already used to match a List\[Type\] Type('int'), })
def test_if_returns_success(): from tmppy import Type def f(x: bool): if x: return Type('int') return Type('float') assert f(True) == Type('int')
def f(t: Type): return match(t)(lambda X: { Type.template_instantiation('std::tuple', [Type.template_instantiation('std::tuple', [X]), # note: A previous match as a Type was here Type.template_instantiation('std::tuple', [*X])]): # error: List extraction can't be used on X because it was already used to match a Type Type('int'), })
def test_attribute_access_on_function_error(): from tmppy import Type def f(x: Type): return x assert f.type == Type( 'int' ) # error: Attribute access is not supported for values of type \(Type\) -> Type.
def test_match_variable_named_error_in_match_returning_type_ok(): from tmppy import Type, match def f(b: bool): return match(Type('int'))(lambda error: { error: Type.pointer(error), }) assert f(True) == Type.pointer(Type('int'))
def test_match_variable_named_type_ok(): from tmppy import Type, match def f(b: bool): return match(Type('int'))(lambda type: { type: Type.pointer(type), }) assert f(True) == Type.pointer(Type('int'))
def test_list_comprehension_type_to_const_type_ok(): from tmppy import Type assert [ Type('float') for x in [Type('int'), Type('float'), Type('double')] ] == [Type('float'), Type('float'), Type('float')]
def test_optimization_of_mutually_recursive_functions_with_type_param(): from tmppy import Type def f(t: Type) -> int: if t == Type('int'): return 3 else: return g(Type('int')) def g(t: Type) -> int: return f(t) assert g(Type('float')) == 3
def test_function_returning_function_returning_int_ok(): from tmppy import Type def f(x: int): return x def g(b: Type): return f assert g(Type('int'))(15) == 15
def test_function_returning_function_returning_bool_ok(): from tmppy import Type def f(x: bool): return x def g(b: Type): return f assert g(Type('int'))(True) == True
def test_match_variable_named_error_in_match_returning_type_ok(): from tmppy import Type, match, TypePattern def f(b: bool): return match(Type('int'))({ TypePattern('error'): lambda error: Type('T*', T=error), }) assert f(True) == Type('int*')
def test_match_with_function_expr_call(): from tmppy import Type, match def g(x: Type): return x def h(x: Type): return g def f(x: Type): return match(x)( lambda T, U: { Type.pointer(Type.function(T, [U])): h(T)(Type('double')), Type.pointer(Type.function(Type('int'), [T])): T, Type.pointer(Type.function(Type('float'), [T])): T, }) assert f(Type.pointer(Type.function(Type('int'), [Type('int')]))) == Type('int')
def test_match_multiple_success(): from tmppy import Type, match def f(y: Type): return match(Type.pointer(Type('int')), y)(lambda T, U: { (T, U): False, (Type.pointer(T), Type.pointer(U)): True, }) assert f(Type.pointer(Type.pointer(Type('double'))))
def test_nested_match_with_capture(): from tmppy import Type, TypePattern, match def f(x: Type, y: Type): return match(x)({ TypePattern('T'): lambda T: y, TypePattern('T(*)(U)'): lambda T, U: match(T, U)({ TypePattern('int', 'V'): lambda V: y, TypePattern('float', 'V'): lambda V: Type('bool'), }), }) assert f(Type('int(*)(int)'), Type('bool')) == Type('bool')
def f(x: Type): return match(x)({ # error: Malformed match\(...\)\({...}\) TypePattern('T(*)(U)'): lambda T, U: Type('double'), TypePattern('int(*)(T)'): lambda T: T, }, {})
def test_nested_match_success(): from tmppy import Type, TypePattern, match def f(x: Type): return match(x)({ TypePattern('T'): lambda T: Type('double'), TypePattern('T(*)(U)'): lambda T, U: match(T, U)({ TypePattern('int', 'V'): lambda V: V, TypePattern('float', 'V'): lambda V: Type('bool'), }), }) assert f(Type('int(*)(int)')) == Type('int')
def test_match_variable_named_type_ok(): from tmppy import Type, match, TypePattern def f(b: bool): return match(Type('int'))({ TypePattern('type'): lambda type: Type('T*', T=type), }) assert f(True) == Type('int*')
def f(x: Type): return match(x)({ TypePattern('T(*)(U)'): lambda T, U: Type('double'), TypePattern('int(*)(T)'): lambda T: T, }, wrong_arg=True) # error: Keyword arguments are not allowed in match
def test_match_with_function_expr_call(): from tmppy import Type, TypePattern, match def g(x: Type): return x def h(x: Type): return g def f(x: Type): return match(x)({ TypePattern('T(*)(U)'): lambda T, U: h(T)(Type('double')), TypePattern('int(*)(T)'): lambda T: T, TypePattern('float(*)(T)'): lambda T: T, }) assert f(Type('int(*)(int)')) == Type('int')
def test_if_else_with_comparisons_success(): from tmppy import Type def f(x: Type): if x == Type('int'): b = x == Type('int') else: return x == Type('float') return b == True assert f(Type('int')) == True
def test_if_else_neither_returns_success(): from tmppy import Type def f(x: bool): if x: y = Type('int') else: y = Type('float') return y assert f(True) == Type('int')
def test_if_else_defining_local_var_success(): from tmppy import Type def f(x: bool): if x: y = x return Type('int') else: return Type('float') assert f(True) == Type('int')
def test_if_else_assert_in_continuation_never_executed_ok(): from tmppy import Type def f(x: bool): if True: return Type('int') b = False assert b return Type('void') assert f(True) == Type('int')
def test_list_unpacking_as_list_success_type(): from tmppy import Type def f(b: bool): return [Type('int'), Type('float'), Type('double')] def g(b1: bool): [a, b, c] = f(b1) return b assert g(True) == Type('float')
def test_if_else_else_branch_defining_additional_var_success(): from tmppy import Type def f(x: bool): if x: y = Type('int') else: y = Type('float') b = True return y assert f(True) == Type('int')
def test_function_variable_success(): from tmppy import Type from typing import Callable def f(x: Callable[[Type], Type]): y = x return y(Type('int')) def g(x: Type): return x assert f(g) == Type('int')
def f(x: Type, y: Type): return match(x)({ TypePattern('T(*)(U)'): lambda T, U: Type('double'), TypePattern('int(*)(T)'): lambda T: y, TypePattern('float(*)(T)'): lambda T: T, })
def test_match_multiple_success(): from tmppy import Type, TypePattern, match def f(y: Type): return match(Type('int*'), y)({ TypePattern('T', 'U'): lambda T, U: False, TypePattern('T*', 'U*'): lambda T, U: True, }) assert f(Type('double**'))
def test_assert_false_in_function_call_with_constant_args_called_type_type_error( ): from tmppy import Type def f(x: Type): assert False return True def g(x: Type): return f(Type('double')) assert g(Type('float'))
def f(x: Type): return match(x)({ TypePattern('T(*)(U)'): lambda T, U: [Type('double')], TypePattern('int(*)(T)'): lambda T: [T], TypePattern('float(*)(T)'): lambda T: [T], })