def when_target_has_explicit_type_that_type_is_used_as_type_hint(): node = nodes.assign( [nodes.ref("x")], nodes.list_literal([nodes.str_literal("Hello")]), type=nodes.type_apply(nodes.ref("list"), [nodes.ref("object")]) ) context = update_context(node, type_bindings={ "list": types.meta_type(types.list_type), "object": types.meta_type(types.object_type), }) assert_equal(types.list_type(types.object_type), context.lookup_name("x"))
def test_assignment_can_have_explicit_type(): expected = nodes.assign( ["x"], nodes.list_literal([]), type=nodes.type_apply(nodes.ref("list"), [nodes.ref("str")]) ) _assert_statement_parse(expected, "#:: list[str]\nx = []")
def can_parse_signature_comment_with_type_application_with_one_generic_parameter(): expected_signature = nodes.signature( returns=nodes.type_apply(nodes.ref("list"), [nodes.ref("str")]) ) assert_equal(expected_signature, parse_explicit_type("-> list[str]"))
def can_parse_signature_comment_with_type_application_with_many_generic_parameters(): expected_signature = nodes.signature( returns=nodes.type_apply(nodes.ref("dict"), [nodes.ref("str"), nodes.ref("int")]), ) assert_equal(expected_signature, parse_explicit_type("-> dict[str, int]"))
def can_parse_explicit_type_with_type_application_with_one_generic_parameter(): assert_equal( nodes.type_apply(nodes.ref("list"), [nodes.ref("str")]), parse_explicit_type("list[str]") )