def type_name_is_definitely_bound_after_type_definition(): int_ref = nodes.ref("int") str_ref = nodes.ref("str") node = nodes.type_definition("Identifier", nodes.type_union([int_ref, str_ref])) bindings = _updated_bindings(node, is_definitely_bound=["int", "str"]) assert_equal(True, bindings.is_definitely_bound(node))
def type_of_type_union_is_metatype_of_unioned_types(): type_bindings = { "str": types.meta_type(types.str_type), "int": types.meta_type(types.int_type), } node = nodes.type_union([nodes.ref("str"), nodes.ref("int")]) inferred_type = infer(node, type_bindings=type_bindings) assert types.is_meta_type(inferred_type) assert_equal(types.union(types.str_type, types.int_type), inferred_type.type)
def test_type_definitions_can_appear_between_statements(): source = """ #:type Identifier = int | str Identifier = None """ module_node = parser.parse(source) expected_node = nodes.TypeDefinition( "Identifier", nodes.type_union([nodes.ref("int"), nodes.ref("str")]) ) assert_equal(expected_node, module_node.body[0]) assert_equal(1, len(module_node.body))
def test_types_can_be_defined_with_type_statement(): source = """ #:type Identifier = int | str x = 1 """ expected_node = nodes.TypeDefinition( "Identifier", nodes.type_union([nodes.ref("int"), nodes.ref("str")]) ) note = _parse_type_definition_note(source) assert_equal(expected_node, note)
def error_if_type_signature_argument_is_optional_but_def_argument_is_not_optional(): signature = nodes.signature( args=[nodes.signature_arg(nodes.ref("int"), optional=True)], returns=nodes.type_union([nodes.ref("int"), nodes.ref("none")]) ) args = nodes.arguments([nodes.argument("x")]) body = [nodes.ret(nodes.ref("x"))] node = nodes.func("f", args, body, type=signature) try: _infer_func_type(node) assert False, "Expected error" except errors.ArgumentsError as error: assert_equal("optional argument 'x' must have default value", str(error))
def type_definitions_can_span_multiple_lines(): source = """ #:type Identifier = #: int #: | str x = 1 """ expected_node = nodes.TypeDefinition( "Identifier", nodes.type_union([nodes.ref("int"), nodes.ref("str")]) ) note = _parse_type_definition_note(source) assert_equal(expected_node, note)
def type_definition_is_resolved_to_type_declaration(): node = nodes.type_definition("Identifier", nodes.type_union([nodes.ref("int"), nodes.ref("str")])) declarations = _create_declarations(["Identifier", "int", "str"]) references = resolve(node, declarations) assert_is(declarations.declaration("Identifier"), references.referenced_declaration(node))
def type_definition_is_declared(): node = nodes.type_definition("Identifier", nodes.type_union([nodes.ref("int"), nodes.ref("str")])) declarations = find_declarations(node) assert_equal("Identifier", declarations.declaration("Identifier").name) assert isinstance(declarations.declaration("Identifier"), name_declaration.TypeDeclarationNode)
def can_parse_type_union(): assert_equal( nodes.type_union([nodes.ref("none"), nodes.ref("str"), nodes.ref("int")]), parse_explicit_type("none | str | int") )