text
stringlengths 12
786k
|
---|
let package_signatures units = let units_with_ids = List . map ( fun ( name , sg ) -> let oldid = Ident . create_persistent name in let newid = Ident . create_local name in ( oldid , newid , sg ) ) units in let subst = List . fold_left ( fun acc ( oldid , newid , _ ) -> Subst . add_module oldid ( Pident newid ) acc ) Subst . identity units_with_ids in List . map ( fun ( _ , newid , sg ) -> let sg = Subst . signature Make_local subst sg in let md = { md_type = Mty_signature sg ; md_attributes [ ] ; = md_loc = Location . none ; } in Sig_module ( newid , Mp_present , md , Trec_not , Exported ) ) units_with_ids
|
let package_units initial_env objfiles cmifile modulename = let units = List . map ( fun f -> let pref = chop_extensions f in let modname = String . capitalize_ascii ( Filename . basename pref ) in let sg = Env . read_signature modname ( pref ^ " . cmi " ) in if Filename . check_suffix f " . cmi " && not ( Mtype . no_code_needed_sig Env . initial_safe_string sg ) then raise ( Error ( Location . none , Env . empty , Implementation_is_required f ) ) ; ( modname , Env . read_signature modname ( pref ^ " . cmi " ) ) ) objfiles in Ident . reinit ( ) ; let sg = package_signatures units in let prefix = Filename . remove_extension cmifile in let mlifile = prefix ^ ! Config . interface_suffix in if Sys . file_exists mlifile then begin if not ( Sys . file_exists cmifile ) then begin raise ( Error ( Location . in_file mlifile , Env . empty , Interface_not_compiled mlifile ) ) end ; let dclsig = Env . read_signature modulename cmifile in Cmt_format . save_cmt ( prefix ^ " . cmt " ) modulename ( Cmt_format . Packed ( sg , objfiles ) ) None initial_env None ; Includemod . compunit initial_env " ( obtained by packing ) " sg mlifile dclsig end else begin let unit_names = List . map fst units in let imports = List . filter ( fun ( name , _crc ) -> not ( List . mem name unit_names ) ) ( Env . imports ( ) ) in if not ! Clflags . dont_write_files then begin let cmi = Env . save_signature_with_imports ~ alerts : Misc . Stdlib . String . Map . empty sg modulename ( prefix ^ " . cmi " ) imports in Cmt_format . save_cmt ( prefix ^ " . cmt " ) modulename ( Cmt_format . Packed ( cmi . Cmi_format . cmi_sign , objfiles ) ) None initial_env ( Some cmi ) end ; Tcoerce_none end
|
let report_error ppf = function Cannot_apply mty -> fprintf ppf " [ @ This module is not a functor ; it has type @ % a ] " @ modtype mty | Not_included errs -> fprintf ppf " [ @< v > Signature mismatch :@ % a ] " @ Includemod . report_error errs | Cannot_eliminate_dependency mty -> fprintf ppf " [ @ This functor has type @ % a @ \ The parameter cannot be eliminated in the result type . @ \ Please bind the argument to a module identifier . ] " @ modtype mty | Signature_expected -> fprintf ppf " This module type is not a signature " | Structure_expected mty -> fprintf ppf " [ @ This module is not a structure ; it has type @ % a " modtype mty | With_no_component lid -> fprintf ppf " [ @ The signature constrained by ` with ' has no component named % a ] " @ longident lid | With_mismatch ( lid , explanation ) -> fprintf ppf " [ @< v >\ [ @ In this ` with ' constraint , the new definition of % a @ \ does not match its original definition @ \ in the constrained signature ] :@@ \ % a ] " @ longident lid Includemod . report_error explanation | With_makes_applicative_functor_ill_typed ( lid , path , explanation ) -> fprintf ppf " [ @< v >\ [ @ This ` with ' constraint on % a makes the applicative functor @ \ type % s ill - typed in the constrained signature ] :@@ \ % a ] " @ longident lid ( Path . name path ) Includemod . report_error explanation | With_changes_module_alias ( lid , id , path ) -> fprintf ppf " [ @< v >\ [ @ This ` with ' constraint on % a changes % s , which is aliased @ \ in the constrained signature ( as % s ) ] . ] " @@ longident lid ( Path . name path ) ( Ident . name id ) | With_cannot_remove_constrained_type -> fprintf ppf " [ @< v > Destructive substitutions are not supported for constrained @ \ types ( other than when replacing a type constructor with @ \ a type constructor with the same arguments ) . ] " @ | Repeated_name ( kind , name ) -> fprintf ppf " [ @ Multiple definition of the % s name % s . @ \ Names must be unique in a given structure or signature . ] " @ ( Sig_component_kind . to_string kind ) name | Non_generalizable typ -> fprintf ppf " [ @ The type of this expression , @ % a , @ \ contains type variables that cannot be generalized ] " @ type_scheme typ | Non_generalizable_class ( id , desc ) -> fprintf ppf " [ @ The type of this class , @ % a , @ \ contains type variables that cannot be generalized ] " @ ( class_declaration id ) desc | Non_generalizable_module mty -> fprintf ppf " [ @ The type of this module , @ % a , @ \ contains type variables that cannot be generalized ] " @ modtype mty | Implementation_is_required intf_name -> fprintf ppf " [ @ The interface % a @ declares values , not just types . @ \ An implementation must be provided . ] " @ Location . print_filename intf_name | Interface_not_compiled intf_name -> fprintf ppf " [ @ Could not find the . cmi file for interface @ % a . ] " @ Location . print_filename intf_name | Not_allowed_in_functor_body -> fprintf ppf " [ @ This expression creates fresh types . @ % s ] " @ " It is not allowed inside applicative functors . " | Not_a_packed_module ty -> fprintf ppf " This expression is not a packed module . It has type @ % a " type_expr ty | Incomplete_packed_module ty -> fprintf ppf " The type of this packed module contains variables :@ % a " type_expr ty | Scoping_pack ( lid , ty ) -> fprintf ppf " The type % a in this module cannot be exported . @ " longident lid ; fprintf ppf " Its type contains local dependencies :@ % a " type_expr ty | Recursive_module_require_explicit_type -> fprintf ppf " Recursive modules require an explicit module type . " | Apply_generative -> fprintf ppf " This is a generative functor . It can only be applied to ( ) " | Cannot_scrape_alias p -> fprintf ppf " This is an alias for module % a , which is missing " path p | Badly_formed_signature ( context , err ) -> fprintf ppf " [ @ In % s :@ % a ] " @ context Typedecl . report_error err | Cannot_hide_id Illegal_shadowing { shadowed_item_kind ; shadowed_item_id ; shadowed_item_loc ; shadower_id ; user_id ; user_kind ; user_loc } -> let shadowed_item_kind = Sig_component_kind . to_string shadowed_item_kind in fprintf ppf " [ @< v > Illegal shadowing of included % s % a by % a @ \ % a ; :@< 1 2 >% s % a came from this include @ \ % a ; :@< 1 2 > The % s % s has no valid type if % a is shadowed ] " @ shadowed_item_kind Ident . print shadowed_item_id Ident . print shadower_id Location . print_loc shadowed_item_loc ( String . capitalize_ascii shadowed_item_kind ) Ident . print shadowed_item_id Location . print_loc user_loc ( Sig_component_kind . to_string user_kind ) ( Ident . name user_id ) Ident . print shadowed_item_id | Cannot_hide_id Appears_in_signature { opened_item_kind ; opened_item_id ; user_id ; user_kind ; user_loc } -> let opened_item_kind = Sig_component_kind . to_string opened_item_kind in fprintf ppf " [ @< v > The % s % a introduced by this open appears in the signature @ \ % a ; :@< 1 2 > The % s % s has no valid type if % a is hidden ] " @ opened_item_kind Ident . print opened_item_id Location . print_loc user_loc ( Sig_component_kind . to_string user_kind ) ( Ident . name user_id ) Ident . print opened_item_id | Invalid_type_subst_rhs -> fprintf ppf " Only type synonyms are allowed on the right of " :=
|
let report_error env ppf err = Printtyp . wrap_printing_env ~ error : true env ( fun ( ) -> report_error ppf err )
|
let ( ) = Location . register_error_of_exn ( function | Error ( loc , env , err ) -> Some ( Location . error_of_printer ~ loc ( report_error env ) err ) | Error_forward err -> Some err | _ -> None )
|
let should_hide ( x : Typedtree . module_binding ) = match x . mb_attributes with | [ ] -> false | { attr_name = { txt = " internal . local " ; _ } ; _ } :: _ -> true | _ :: rest -> Ext_list . exists rest ( fun { attr_name = x ; _ } -> x . txt = " internal . local " )
|
let attrs : Parsetree . attributes = [ { attr_name = { txt = " internal . local " ; loc = Location . none } ; attr_payload = PStr [ ] ; attr_loc = Location . none ; } ; ]
|
let no_type_defined ( x : Parsetree . structure_item ) = match x . pstr_desc with | Pstr_eval _ | Pstr_value _ | Pstr_primitive _ | Pstr_typext _ | Pstr_exception _ -> true | Pstr_include { pincl_mod = { pmod_desc = Pmod_constraint ( { pmod_desc = Pmod_structure [ { pstr_desc = Pstr_primitive _ } ] ; } , _ ) ; } ; } -> true | _ -> false
|
let check ( x : Parsetree . structure ) = Ext_list . iter x ( fun x -> if not ( no_type_defined x ) then Location . raise_errorf ~ loc : x . pstr_loc " the structure is not supported in local extension " )
|
module List = struct include List let compare cmp a b = let rec loop a b = match a , b with | [ ] , [ ] -> 0 | [ ] , _ -> - 1 | _ , [ ] -> 1 | x :: xs , y :: ys -> let n = cmp x y in if n = 0 then loop xs ys else n in loop a b ; ; end
|
module Uid : sig type t val compare : t -> t -> int val equal : t -> t -> bool val next : string -> t val hash : t -> int val name : t -> string val static : t type t = { code : int ; name : string } let compare a b = compare ( a . code : int ) b . code let equal a b = ( a . code : int ) = b . code let uid = ref 0 let next name = let code = ! uid in incr uid ; { code ; name } ; ; let hash a = Hashtbl . hash a . code let name a = a . name let static = next " static " end
|
module Key = struct type t = { uid : Uid . t ; params : t list } let rec compare k1 k2 = if k1 == k2 then 0 else ( let cmp = Uid . compare k1 . uid k2 . uid in if cmp <> 0 then cmp else List . compare compare k1 . params k2 . params ) ; ; let equal a b = compare a b = 0 let hash = ( Hashtbl . hash : t -> int ) let static = { uid = Uid . static ; params = [ ] } end
|
type ' a t = Key . t
|
type ' a typename = ' a t
|
let key t = t
|
let uid t = t . Key . uid
|
let name t = Uid . name t . Key . uid
|
let create ( ? name = " Typename . create " ) ( ) = { Key . uid = Uid . next name ; params = [ ] } let same ( type a b ) ( nm1 : a t ) ( nm2 : b t ) = Key . compare nm1 nm2 = 0 let same_witness ( type a b ) ( nm1 : a t ) ( nm2 : b t ) = if Key . compare nm1 nm2 = 0 then Some ( Obj . magic Type_equal . refl : ( a , b ) Type_equal . t ) else None ; ; let same_witness_exn ( type a b ) ( nm1 : a t ) ( nm2 : b t ) = if Key . compare nm1 nm2 = 0 then ( Obj . magic Type_equal . refl : ( a , b ) Type_equal . t ) else failwith " Typename . same_witness_exn " ; ; end
|
module type S0 = sig type t val typename_of_t : t typename end
|
module type S1 = sig type ' a t val typename_of_t : ' a typename -> ' a t typename end
|
module type S2 = sig type ( ' a , ' b ) t val typename_of_t : ' a typename -> ' b typename -> ( ' a , ' b ) t typename end
|
module type S3 = sig type ( ' a , ' b , ' c ) t val typename_of_t : ' a typename -> ' b typename -> ' c typename -> ( ' a , ' b , ' c ) t typename end
|
module type S4 = sig type ( ' a , ' b , ' c , ' d ) t val typename_of_t : ' a typename -> ' b typename -> ' c typename -> ' d typename -> ( ' a , ' b , ' c , ' d ) t typename end
|
module type S5 = sig type ( ' a , ' b , ' c , ' d , ' e ) t val typename_of_t : ' a typename -> ' b typename -> ' c typename -> ' d typename -> ' e typename -> ( ' a , ' b , ' c , ' d , ' e ) t typename end
|
module Make0 ( X : Named_intf . S0 ) = struct let uid = Uid . next X . name let typename_of_t = { Key . uid ; params = [ ] } end
|
module Make1 ( X : Named_intf . S1 ) = struct let uid = Uid . next X . name let typename_of_t a = { Key . uid ; params = [ a ] } end
|
module Make2 ( X : Named_intf . S2 ) = struct let uid = Uid . next X . name let typename_of_t a b = { Key . uid ; params = [ a ; b ] } end
|
module Make3 ( X : Named_intf . S3 ) = struct let uid = Uid . next X . name let typename_of_t a b c = { Key . uid ; params = [ a ; b ; c ] } end
|
module Make4 ( X : Named_intf . S4 ) = struct let uid = Uid . next X . name let typename_of_t a b c d = { Key . uid ; params = [ a ; b ; c ; d ] } end
|
module Make5 ( X : Named_intf . S5 ) = struct let uid = Uid . next X . name let typename_of_t a b c d e = { Key . uid ; params = [ a ; b ; c ; d ; e ] } end
|
module Key_table = Hashtbl . Make ( Key )
|
module Table ( X : sig type ' a t end ) = struct type data = Data : ' a t * ' a X . t -> data type t = data Key_table . t let create int = Key_table . create int let mem table name = Key_table . mem table ( key name ) let set table name data = Key_table . replace table ( key name ) ( Data ( name , data ) ) let find ( type a ) table ( name : a typename ) = let data = try Some ( Key_table . find table ( key name ) ) with | Base . Not_found_s _ | Caml . Not_found -> None in match data with | None -> None | Some ( Data ( name ' , data ) ) -> ( fun ( type b ) ( name ' : b typename ) ( data : b X . t ) -> let Type_equal . T = ( same_witness_exn name ' name : ( b , a ) Type_equal . t ) in Some ( data : a X . t ) ) name ' data ; ; end
|
let fail uid_a uid_b = let msg = Printf . sprintf " Typename . Same_witness_exn % S % S " ( Uid . name uid_a ) ( Uid . name uid_b ) in failwith msg ; ;
|
module Same_witness_exn_1 ( A : S1 ) ( B : S1 ) = struct type t = { eq : ' a . ( ' a A . t , ' a B . t ) Type_equal . t } let witness = let uid_a = uid ( A . typename_of_t static ) in let uid_b = uid ( B . typename_of_t static ) in if Uid . equal uid_a uid_b then { eq = Obj . magic Type_equal . refl } else fail uid_a uid_b ; ; end
|
module Same_witness_exn_2 ( A : S2 ) ( B : S2 ) = struct type t = { eq : ' a ' b . ( ( ' a , ' b ) A . t , ( ' a , ' b ) B . t ) Type_equal . t } let witness = let uid_a = uid ( A . typename_of_t static static ) in let uid_b = uid ( B . typename_of_t static static ) in if Uid . equal uid_a uid_b then { eq = Obj . magic Type_equal . refl } else fail uid_a uid_b ; ; end
|
module Same_witness_exn_3 ( A : S3 ) ( B : S3 ) = struct type t = { eq : ' a ' b ' c . ( ( ' a , ' b , ' c ) A . t , ( ' a , ' b , ' c ) B . t ) Type_equal . t } let witness = let uid_a = uid ( A . typename_of_t static static static ) in let uid_b = uid ( B . typename_of_t static static static ) in if Uid . equal uid_a uid_b then { eq = Obj . magic Type_equal . refl } else fail uid_a uid_b ; ; end
|
module Same_witness_exn_4 ( A : S4 ) ( B : S4 ) = struct type t = { eq : ' a ' b ' c ' d . ( ( ' a , ' b , ' c , ' d ) A . t , ( ' a , ' b , ' c , ' d ) B . t ) Type_equal . t } let witness = let uid_a = uid ( A . typename_of_t static static static static ) in let uid_b = uid ( B . typename_of_t static static static static ) in if Uid . equal uid_a uid_b then { eq = Obj . magic Type_equal . refl } else fail uid_a uid_b ; ; end
|
module Same_witness_exn_5 ( A : S5 ) ( B : S5 ) = struct type t = { eq : ' a ' b ' c ' d ' e . ( ( ' a , ' b , ' c , ' d , ' e ) A . t , ( ' a , ' b , ' c , ' d , ' e ) B . t ) Type_equal . t } let witness = let uid_a = uid ( A . typename_of_t static static static static static ) in let uid_b = uid ( B . typename_of_t static static static static static ) in if Uid . equal uid_a uid_b then { eq = Obj . magic Type_equal . refl } else fail uid_a uid_b ; ; end
|
type t = { env : Env . t ; get_search_dirs : string -> string list } let from_summary ~ get_search_dirs summary subst = Persistent_env . get_search_dirs := get_search_dirs ; Envaux . reset_cache ( ) ; let env = Envaux . env_from_summary ( Obj . magic summary ) summary subst in { env ; get_search_dirs } let find_value path { env ; get_search_dirs } = Persistent_env . get_search_dirs := get_search_dirs ; Env . find_value path env let find_value_by_name name { env ; get_search_dirs } = Persistent_env . get_search_dirs := get_search_dirs ; Env . find_value_by_name name env let find_type path { env ; get_search_dirs } = Persistent_env . get_search_dirs := get_search_dirs ; Env . find_type path env let find_constructor_by_name lid { env ; get_search_dirs } = Persistent_env . get_search_dirs := get_search_dirs ; Env . find_constructor_by_name lid env let find_value_address path { env ; get_search_dirs } = Persistent_env . get_search_dirs := get_search_dirs ; Env . find_value_address path env |> Obj . magic let find_module_address path { env ; get_search_dirs } = Persistent_env . get_search_dirs := get_search_dirs ; Env . find_module_address path env |> Obj . magic let find_modtype_expansion path { env ; get_search_dirs } = Persistent_env . get_search_dirs := get_search_dirs ; Env . find_modtype_expansion path env let is_structure_module path { env ; get_search_dirs } = Persistent_env . get_search_dirs := get_search_dirs ; Env . is_structure_module path env let add_module ? arg id presence mty { env ; get_search_dirs } = Persistent_env . get_search_dirs := get_search_dirs ; let env ' = Env . add_module ? arg id presence mty env in { env = env ' ; get_search_dirs } let extract_modules path { env ; get_search_dirs } = Persistent_env . get_search_dirs := get_search_dirs ; Env . extract_modules path env let extract_values path { env ; get_search_dirs } = Persistent_env . get_search_dirs := get_search_dirs ; Env . extract_values path env let type_apply { env ; get_search_dirs } = Persistent_env . get_search_dirs := get_search_dirs ; Ctype . apply ( Obj . magic env ) env let type_matches { env ; get_search_dirs } = Persistent_env . get_search_dirs := get_search_dirs ; Ctype . matches ( Obj . magic env ) env let full_expand { env ; get_search_dirs } = Persistent_env . get_search_dirs := get_search_dirs ; Ctype . full_expand ( Obj . magic env ) env let ( ) = Env . add_delayed_check_forward := Obj . magic ! Ocaml_common . Env . add_delayed_check_forward ; Env . same_constr := Obj . magic ! Ocaml_common . Env . same_constr ; Env . check_well_formed_module := Obj . magic ! Ocaml_common . Env . check_well_formed_module ; Env . check_functor_application := Obj . magic ! Ocaml_common . Env . check_functor_application ; Env . strengthen := Obj . magic ! Ocaml_common . Env . strengthen ; Env . print_longident := Obj . magic ! Ocaml_common . Env . print_longident ; Env . print_path := Obj . magic ! Ocaml_common . Env . print_path ; Ctype . package_subtype := Obj . magic ! Ocaml_common . Ctype . package_subtype ; Persistent_env . add_delayed_check_forward := Obj . magic ! Ocaml_common . Persistent_env . add_delayed_check_forward ;
|
type callable_and_self_argument = { callable : Type . Callable . t ; self_argument : Type . t option ; }
|
module TypeOperation = struct module Compose = struct let apply_callable ~ signature_select input_annotation ~ callable_and_self { : callable ; self_argument } = match signature_select ~ arguments : [ { AttributeResolution . Argument . expression = None ; kind = Ast . Expression . Call . Argument . Positional ; resolved = input_annotation ; } ; ] ~ callable ~ self_argument with | SignatureSelectionTypes . Found { selected_return_annotation } -> Some selected_return_annotation | _ -> None let compose ~ signature_select { callable = original_left_callable ; self_argument = left_self_argument } right_callable_and_self = let replace_return_annotation new_annotation ~ input = { input with Type . Callable . implementation = { input . Type . Callable . implementation with annotation = new_annotation } ; } in let namespace = Type . Variable . Namespace . create_fresh ( ) in let compose left_callable_namespaced = let left_free_variables = Type . Variable . all_free_variables ( Type . Callable left_callable_namespaced ) in Type . Callable . map ~ f ( : Type . Variable . mark_all_variables_as_bound ~ specific : left_free_variables ) left_callable_namespaced >>| ( fun { Type . Callable . implementation = { annotation ; _ } ; _ } -> annotation ) >>= apply_callable ~ signature_select ~ callable_and_self : right_callable_and_self >>| Type . Variable . mark_all_variables_as_free ~ specific ( : List . map ~ f : Type . Variable . mark_as_bound left_free_variables ) >>| replace_return_annotation ~ input : left_callable_namespaced >>| fun result -> { callable = result ; self_argument = left_self_argument } in Type . Callable . map ~ f ( : Type . Variable . namespace_all_free_variables ~ namespace ) original_left_callable >>= compose let compose_list ~ signature_select = function | [ ] -> None | first :: rest -> List . fold ~ init ( : Some first ) ~ f ( : fun left right -> left >>= fun inner_left -> compose ~ signature_select inner_left right ) rest end end
|
let to_defined parameters = Type . Callable . Defined ( List . mapi ~ f ( : fun i element -> Type . Callable . Parameter . PositionalOnly { index = i ; annotation = element ; default = false } ) parameters )
|
let make_overload ~ parameters ~ return = Some { Type . Callable . annotation = return ; parameters = to_defined parameters }
|
let test_compose_list _ = let assert_compose_list ( ? aliases = fun _ -> None ) ~ signature_select given expected = let parse source = Type . create ~ aliases ( : fun ? replace_unbound_parameters_with_any : _ -> aliases ) ( parse_single_expression ~ preprocess : true source ) in let merge_option_list = function | [ ] -> Some [ ] | first :: rest -> List . fold_left ~ init ( : first >>| List . return ) ~ f ( : Option . map2 ~ f ( : Fn . flip List . cons ) ) rest >>| List . rev in let get_callable = function | Type . Callable callable -> Some { callable ; self_argument = None } | _ -> None in let annotation_list = List . map ~ f : parse given |> List . map ~ f : get_callable |> merge_option_list in let actual = annotation_list >>= TypeOperation . Compose . compose_list ~ signature_select in assert_equal ~ cmp : ( Option . equal ( fun { callable = left_callable ; self_argument = left_self_argument } { callable = right_callable ; self_argument = right_self_argument } -> [ % eq : Type . t option ] left_self_argument right_self_argument && Type . namespace_insensitive_compare ( Type . Callable left_callable ) ( Type . Callable right_callable ) = 0 ) ) ~ printer [ :% show : callable_and_self_argument option ] actual ( expected >>| fun expected -> { callable = { Type . Callable . kind = Type . Callable . Anonymous ; implementation = expected ; overloads = [ ] ; } ; self_argument = None ; } ) in let constant_select annotation ~ arguments : _ ~ callable : _ ~ self_argument : _ = SignatureSelectionTypes . Found { selected_return_annotation = annotation } in let fail_select ~ arguments : _ ~ callable : _ ~ self_argument : _ = SignatureSelectionTypes . NotFound { closest_return_annotation = Type . Bottom ; reason = None } in assert_compose_list ~ signature_select ( : constant_select Type . bool ) [ " typing . Callable [ [ int ] , str ] " ; " typing . Callable [ [ str ] , bool ] " ] ( make_overload ~ parameters [ : Type . integer ] ~ return : Type . bool ) ; assert_compose_list ~ signature_select : fail_select [ " typing . Callable [ [ int ] , bool ] " ; " typing . Callable [ [ str ] , bool ] " ] None ; let variable = Type . Variable . Unary . create " T " in let variable2 = Type . Variable . Unary . create " R " in let identity_select ~ arguments ~ callable ~ self_argument = match arguments with | [ { AttributeResolution . Argument . resolved ; _ } ] -> SignatureSelectionTypes . Found { selected_return_annotation = resolved } | _ -> fail_select ~ arguments ~ callable ~ self_argument in let aliases = function | " T " -> Some ( Type . Variable variable ) | _ -> None in let aliases = create_type_alias_table aliases in assert_compose_list ~ aliases ~ signature_select : identity_select [ " typing . Callable [ [ int ] , int ] " ; " typing . Callable [ [ T ] , T ] " ] ( make_overload ~ parameters [ : Type . integer ] ~ return : Type . integer ) ; let aliases = function | " T " -> Some ( Type . Variable variable ) | " R " -> Some ( Type . Variable variable2 ) | _ -> None in let aliases = create_type_alias_table aliases in assert_compose_list ~ aliases ~ signature_select : identity_select [ " typing . Callable [ [ R ] , R ] " ; " typing . Callable [ [ T ] , T ] " ] ( make_overload ~ parameters [ : Type . Variable variable2 ] ~ return ( : Type . Variable variable2 ) ) ; assert_compose_list ~ signature_select ( : constant_select Type . integer ) [ " typing . Callable [ [ int ] , int ] " ] ( make_overload ~ parameters [ : Type . integer ] ~ return : Type . integer ) ; assert_compose_list ~ signature_select : identity_select [ ] None ; assert_compose_list ~ aliases ~ signature_select : identity_select [ " typing . Callable [ [ int ] , int ] " ; " typing . Callable [ [ T ] , T ] " ; " typing . Callable [ [ R ] , R ] " ; " typing . Callable [ [ T ] , T ] " ; ] ( make_overload ~ parameters [ : Type . integer ] ~ return : Type . integer ) ; ( )
|
let ( ) = " typeOperation " >::: [ " compose_list " >:: test_compose_list ] |> Test . run
|
let scrape_ty env ty = let ty = Ctype . expand_head_opt env ( Ctype . correct_levels ty ) in match ty . desc with | Tconstr ( p , _ , _ ) -> begin match Env . find_type p env with | { type_unboxed = { unboxed = true ; _ } ; _ } -> begin match Typedecl . get_unboxed_type_representation env ty with | None -> ty | Some ty2 -> ty2 end | _ -> ty | exception Not_found -> ty end | _ -> ty
|
let scrape env ty = ( scrape_ty env ty ) . desc
|
let is_function_type env ty = match scrape env ty with | Tarrow ( _ , lhs , rhs , _ ) -> Some ( lhs , rhs ) | _ -> None
|
let is_base_type env ty base_ty_path = match scrape env ty with | Tconstr ( p , _ , _ ) -> Path . same p base_ty_path | _ -> false
|
let maybe_pointer_type env ty = let ty = scrape_ty env ty in if Ctype . maybe_pointer_type env ty then Pointer else Immediate
|
let maybe_pointer exp = maybe_pointer_type exp . exp_env exp . exp_type
|
type classification = | Int | Float | Lazy | Addr | Any
|
let classify env ty = let ty = scrape_ty env ty in if maybe_pointer_type env ty = Immediate then Int else match ty . desc with | Tvar _ | Tunivar _ -> Any | Tconstr ( p , _args , _abbrev ) -> if Path . same p Predef . path_float then Float else if Path . same p Predef . path_lazy_t then Lazy else if Path . same p Predef . path_string || Path . same p Predef . path_bytes || Path . same p Predef . path_array || Path . same p Predef . path_nativeint || Path . same p Predef . path_int32 || Path . same p Predef . path_int64 then Addr else begin try match ( Env . find_type p env ) . type_kind with | Type_abstract -> Any | Type_record _ | Type_variant _ | Type_open -> Addr with Not_found -> Any end | Tarrow _ | Ttuple _ | Tpackage _ | Tobject _ | Tnil | Tvariant _ -> Addr | Tlink _ | Tsubst _ | Tpoly _ | Tfield _ -> assert false
|
let array_type_kind env ty = match scrape env ty with | Tconstr ( p , [ elt_ty ] , _ ) | Tpoly ( { desc = Tconstr ( p , [ elt_ty ] , _ ) } , _ ) when Path . same p Predef . path_array -> begin match classify env elt_ty with | Any -> if Config . flat_float_array then Pgenarray else Paddrarray | Float -> if Config . flat_float_array then Pfloatarray else Paddrarray | Addr | Lazy -> Paddrarray | Int -> Pintarray end | Tconstr ( p , [ ] , _ ) | Tpoly ( { desc = Tconstr ( p , [ ] , _ ) } , _ ) when Path . same p Predef . path_floatarray -> Pfloatarray | _ -> Pgenarray
|
let array_kind exp = array_type_kind exp . exp_env exp . exp_type
|
let array_pattern_kind pat = array_type_kind pat . pat_env pat . pat_type
|
let bigarray_decode_type env ty tbl dfl = match scrape env ty with | Tconstr ( Pdot ( Pident mod_id , type_name ) , [ ] , _ ) when Ident . name mod_id = " Stdlib__bigarray " -> begin try List . assoc type_name tbl with Not_found -> dfl end | _ -> dfl
|
let kind_table = [ " float32_elt " , Pbigarray_float32 ; " float64_elt " , Pbigarray_float64 ; " int8_signed_elt " , Pbigarray_sint8 ; " int8_unsigned_elt " , Pbigarray_uint8 ; " int16_signed_elt " , Pbigarray_sint16 ; " int16_unsigned_elt " , Pbigarray_uint16 ; " int32_elt " , Pbigarray_int32 ; " int64_elt " , Pbigarray_int64 ; " int_elt " , Pbigarray_caml_int ; " nativeint_elt " , Pbigarray_native_int ; " complex32_elt " , Pbigarray_complex32 ; " complex64_elt " , Pbigarray_complex64 ]
|
let layout_table = [ " c_layout " , Pbigarray_c_layout ; " fortran_layout " , Pbigarray_fortran_layout ]
|
let bigarray_type_kind_and_layout env typ = match scrape env typ with | Tconstr ( _p , [ _caml_type ; elt_type ; layout_type ] , _abbrev ) -> ( bigarray_decode_type env elt_type kind_table Pbigarray_unknown , bigarray_decode_type env layout_type layout_table Pbigarray_unknown_layout ) | _ -> ( Pbigarray_unknown , Pbigarray_unknown_layout )
|
let value_kind env ty = match scrape env ty with | Tconstr ( p , _ , _ ) when Path . same p Predef . path_int -> Pintval | Tconstr ( p , _ , _ ) when Path . same p Predef . path_char -> Pintval | Tconstr ( p , _ , _ ) when Path . same p Predef . path_float -> Pfloatval | Tconstr ( p , _ , _ ) when Path . same p Predef . path_int32 -> Pboxedintval Pint32 | Tconstr ( p , _ , _ ) when Path . same p Predef . path_int64 -> Pboxedintval Pint64 | Tconstr ( p , _ , _ ) when Path . same p Predef . path_nativeint -> Pboxedintval Pnativeint | _ -> Pgenval
|
let function_return_value_kind env ty = match is_function_type env ty with | Some ( _lhs , rhs ) -> value_kind env rhs | None -> Pgenval
|
let lazy_val_requires_forward env ty = match classify env ty with | Any | Lazy -> true | Float -> Config . flat_float_array | Addr | Int -> false
|
let classify_lazy_argument : Typedtree . expression -> [ ` Constant_or_function ` | Float_that_cannot_be_shortcut ` | Identifier of [ ` Forward_value ` | Other ] ` | Other ] = fun e -> match e . exp_desc with | Texp_constant ( Const_int _ | Const_char _ | Const_string _ | Const_int32 _ | Const_int64 _ | Const_nativeint _ ) | Texp_function _ | Texp_construct ( _ , { cstr_arity = 0 } , _ ) -> ` Constant_or_function | Texp_constant ( Const_float _ ) -> if Config . flat_float_array then ` Float_that_cannot_be_shortcut else ` Constant_or_function | Texp_ident _ when lazy_val_requires_forward e . exp_env e . exp_type -> ` Identifier ` Forward_value | Texp_ident _ -> ` Identifier ` Other | _ -> ` Other
|
let value_kind_union k1 k2 = if k1 = k2 then k1 else Pgenval
|
module type FullOrderTypeWithoutT = sig val always_less_or_equal : order -> left : Type . t -> right : Type . t -> bool val meet : order -> Type . t -> Type . t -> Type . t val join : order -> Type . t -> Type . t -> Type . t end
|
module type FullOrderType = sig type t = order include FullOrderTypeWithoutT end
|
module type OrderedConstraintsType = TypeConstraints . OrderedConstraintsType with type order = order
|
module OrderImplementation = struct module Make ( OrderedConstraintsSet : ConstraintsSet . OrderedConstraintsSetType ) = struct type t = order let rec always_less_or_equal order ~ left ~ right = OrderedConstraintsSet . add ConstraintsSet . empty ~ new_constraint : ( LessOrEqual { left = Type . Variable . mark_all_variables_as_bound left ; right = Type . Variable . mark_all_variables_as_bound right ; } ) ~ order |> ConstraintsSet . potentially_satisfiable and join_implementations ~ parameter_join ~ return_join order left right = let open Callable in let parameters = match left . parameters , right . parameters with | Undefined , Undefined -> Some Undefined | Defined left , Defined right -> ( try let join_parameter sofar left right = match sofar with | Some sofar -> let joined = if Type . Callable . Parameter . names_compatible left right then match left , right with | Parameter . PositionalOnly left , Parameter . PositionalOnly right when Bool . equal left . default right . default -> Some ( Parameter . PositionalOnly { left with annotation = parameter_join order left . annotation right . annotation ; } ) | Parameter . PositionalOnly anonymous , Parameter . Named named | Parameter . Named named , Parameter . PositionalOnly anonymous when Bool . equal named . default anonymous . default -> Some ( Parameter . PositionalOnly { anonymous with annotation = parameter_join order named . annotation anonymous . annotation ; } ) | Parameter . Named left , Parameter . Named right when Bool . equal left . default right . default -> Some ( Parameter . Named { left with annotation = parameter_join order left . annotation right . annotation ; } ) | Parameter . Variable ( Concrete left ) , Parameter . Variable ( Concrete right ) -> Some ( Parameter . Variable ( Concrete ( parameter_join order left right ) ) ) | Parameter . Keywords left , Parameter . Keywords right -> Some ( Parameter . Keywords ( parameter_join order left right ) ) | _ -> None else None in joined >>| fun joined -> joined :: sofar | None -> None in List . fold2_exn ~ init ( : Some [ ] ) ~ f : join_parameter left right >>| List . rev >>| fun parameters -> Defined parameters with | _ -> None ) | Undefined , Defined right -> Some ( Defined right ) | Defined left , Undefined -> Some ( Defined left ) | _ -> None in parameters >>| fun parameters -> { annotation = return_join order left . annotation right . annotation ; parameters } and join ( { ConstraintsSet . class_hierarchy = { least_upper_bound ; instantiate_successors_parameters ; variables ; _ } ; is_protocol ; assumptions = { protocol_assumptions ; _ } ; _ ; } as order ) left right = let union = Type . union [ left ; right ] in if Type . equal left right then left else if Type . Variable . contains_escaped_free_variable left || Type . Variable . contains_escaped_free_variable right then union else match left , right with | Type . Bottom , other | other , Type . Bottom -> other | Type . Top , _ | _ , Type . Top -> Type . Top | Type . Any , _ | _ , Type . Any -> Type . Any | Type . ParameterVariadicComponent _ , _ | _ , Type . ParameterVariadicComponent _ -> union | Type . NoneType , _ | _ , Type . NoneType -> union | Type . Annotated left , _ -> Type . annotated ( join order left right ) | _ , Type . Annotated right -> Type . annotated ( join order left right ) | Type . RecursiveType left_recursive_type , Type . RecursiveType right_recursive_type -> let new_name = Type . RecursiveType . Namespace . create_fresh_name ( ) in Type . RecursiveType . create ~ name : new_name ~ body : ( join order ( Type . RecursiveType . body_with_replaced_name ~ new_name left_recursive_type ) ( Type . RecursiveType . body_with_replaced_name ~ new_name right_recursive_type ) ) | Type . RecursiveType _ , _ | _ , Type . RecursiveType _ -> if always_less_or_equal order ~ left ~ right then right else if always_less_or_equal order ~ left : right ~ right : left then left else Type . union [ left ; right ] | Type . Union left , Type . Union right -> Type . union ( left @ right ) | ( Type . Union elements as union ) , other | other , ( Type . Union elements as union ) -> if always_less_or_equal order ~ left : other ~ right : union && not ( Type . contains_any other ) then union else let rec flat_join elements new_element = match elements with | [ ] -> [ new_element ] | [ head ] -> ( match join order head new_element with | Type . Union _ -> [ head ; new_element ] | joined -> [ joined ] ) | head :: tail -> ( match join order head new_element with | Type . Union _ -> head :: flat_join tail new_element | joined -> joined :: tail ) in Type . union ( List . fold ~ f : flat_join ~ init [ ] : ( other :: elements ) ) | Type . IntExpression ( Data polynomial ) , other when Type . Polynomial . is_base_case polynomial -> join order other ( Type . polynomial_to_type polynomial ) | other , Type . IntExpression ( Data polynomial ) when Type . Polynomial . is_base_case polynomial -> join order other ( Type . polynomial_to_type polynomial ) | Type . IntExpression _ , other | other , Type . IntExpression _ -> join order other ( Type . Primitive " int " ) | _ , Type . Variable _ | Type . Variable _ , _ -> union | ( Type . Parametric { name = left_primitive ; _ } , Type . Parametric { name = right_primitive ; _ } ) | Type . Parametric { name = left_primitive ; _ } , Type . Primitive right_primitive | Type . Primitive left_primitive , Type . Parametric { name = right_primitive ; _ } -> if always_less_or_equal order ~ left ~ right && not ( Type . contains_any left ) then right else if always_less_or_equal order ~ left : right ~ right : left && not ( Type . contains_any right ) then left else let target = try if always_less_or_equal ~ left ( : Primitive left_primitive ) ~ right ( : Primitive right_primitive ) order then Some right_primitive else if always_less_or_equal ~ left ( : Primitive right_primitive ) ~ right ( : Primitive left_primitive ) order then Some left_primitive else match join order ( Primitive left_primitive ) ( Primitive right_primitive ) with | Primitive target -> Some target | _ -> None with | ClassHierarchy . Untracked _ -> None in let handle_target target = let left_parameters = instantiate_successors_parameters ~ source : left ~ target in let right_parameters = instantiate_successors_parameters ~ source : right ~ target in let variables = variables target in let join_parameters_respecting_variance = function | Type . Variable . UnaryPair ( unary , left ) , Type . Variable . UnaryPair ( _ , right ) -> ( match left , right , unary with | Type . Bottom , other , _ | other , Type . Bottom , _ -> Some other | Type . Top , _ , _ | _ , Type . Top , _ -> Some Type . Top | left , right , { variance = Covariant ; _ } -> Some ( join order left right ) | left , right , { variance = Contravariant ; _ } -> ( match meet order left right with | Type . Bottom -> None | not_bottom -> Some not_bottom ) | left , right , { variance = Invariant ; _ } -> if always_less_or_equal order ~ left ~ right && always_less_or_equal order ~ left : right ~ right : left then Some left else None ) | Type . Variable . TupleVariadicPair _ , Type . Variable . TupleVariadicPair _ | Type . Variable . ParameterVariadicPair _ , Type . Variable . ParameterVariadicPair _ -> None | _ -> None in match left_parameters , right_parameters , variables with | Some left_parameters , Some right_parameters , Some variables -> let replace_free_unary_variables_with_top = let replace_if_free variable = Option . some_if ( Type . Variable . Unary . is_free variable ) Type . Top in Type . Variable . GlobalTransforms . Unary . replace_all replace_if_free in Type . Variable . zip_variables_with_two_parameter_lists ~ left_parameters ~ right_parameters variables >>| List . map ~ f : join_parameters_respecting_variance >>= Option . all >>| List . map ~ f : replace_free_unary_variables_with_top >>| List . map ~ f ( : fun single -> Type . Parameter . Single single ) >>| fun parameters -> Type . parametric target parameters | _ -> None in target >>= handle_target |> Option . value ~ default : union | Type . Tuple ( Concrete left ) , Type . Tuple ( Concrete right ) when List . length left = List . length right -> List . map2_exn left right ~ f ( : join order ) |> Type . tuple | Type . Tuple ( Concatenation left ) , Type . Tuple ( Concatenation right ) -> let left_unbounded_element = Type . OrderedTypes . Concatenation . extract_sole_unbounded_annotation left |> Option . value ~ default : Type . object_primitive in let right_unbounded_element = Type . OrderedTypes . Concatenation . extract_sole_unbounded_annotation right |> Option . value ~ default : Type . object_primitive in Type . Tuple ( Type . OrderedTypes . create_unbounded_concatenation ( join order left_unbounded_element right_unbounded_element ) ) | Type . Tuple ( Concrete concrete ) , Type . Tuple ( Concatenation concatenation ) | Type . Tuple ( Concatenation concatenation ) , Type . Tuple ( Concrete concrete ) -> ( let unbounded_element = Type . OrderedTypes . Concatenation . extract_sole_unbounded_annotation concatenation |> Option . value ~ default : Type . object_primitive in match concrete with | concrete_head :: tail when List . for_all ~ f ( : fun element -> Type . equal element concrete_head ) tail && always_less_or_equal order ~ left : concrete_head ~ right : unbounded_element -> Type . Tuple ( Type . OrderedTypes . create_unbounded_concatenation unbounded_element ) | _ -> Type . union [ Tuple ( Concrete concrete ) ; Type . Tuple ( Type . OrderedTypes . create_unbounded_concatenation unbounded_element ) ; ] ) | other , Type . Tuple ( Concatenation concatenation ) | Type . Tuple ( Concatenation concatenation ) , other -> ( let unbounded_element = Type . OrderedTypes . Concatenation . extract_sole_unbounded_annotation concatenation |> Option . value ~ default : Type . object_primitive in match other with | ( Type . Parametric _ as annotation ) | ( Type . Primitive _ as annotation ) -> join order ( Type . parametric " tuple " [ Single unbounded_element ] ) annotation | _ -> Type . union [ left ; right ] ) | Type . Tuple ( Concrete parameters ) , ( Type . Parametric _ as annotation ) | ( Type . Parametric _ as annotation ) , Type . Tuple ( Concrete parameters ) -> let parameter = List . fold ~ init : Type . Bottom ~ f ( : join order ) parameters in join order ( Type . parametric " tuple " [ Single parameter ] ) annotation | Type . Tuple _ , _ | _ , Type . Tuple _ -> Type . union [ left ; right ] | ( ( Type . Callable { Callable . kind = Callable . Named left_name ; _ } as callable ) , Type . Callable { Callable . kind = Callable . Named right_name ; _ } ) -> if Reference . equal left_name right_name then callable else Type . union [ left ; right ] | Type . Callable left , Type . Callable right -> if List . is_empty left . Callable . overloads && List . is_empty right . Callable . overloads then let kind = if Type . Callable . equal_kind left . kind right . kind then left . kind else Type . Callable . Anonymous in join_implementations ~ parameter_join : meet ~ return_join : join order left . Callable . implementation right . Callable . implementation >>| ( fun implementation -> Type . Callable { left with Callable . kind ; implementation } ) |> Option . value ~ default : union else union | Type . Callable callable , other | other , Type . Callable callable -> ( match ConstraintsSet . resolve_callable_protocol ~ order ~ assumption ( : Callable callable ) other with | Some other_callable -> join order other_callable ( Type . Callable callable ) | None -> Type . union [ left ; right ] ) | Type . Literal ( String AnyLiteral ) , Type . Literal ( String _ ) | Type . Literal ( String _ ) , Type . Literal ( String AnyLiteral ) -> Type . Literal ( String AnyLiteral ) | ( Type . Literal _ as literal ) , other | other , ( Type . Literal _ as literal ) -> join order other ( Type . weaken_to_arbitrary_literal_if_possible literal ) | _ when is_protocol right ~ protocol_assumptions && always_less_or_equal order ~ left ~ right -> right | _ when is_protocol left ~ protocol_assumptions && always_less_or_equal order ~ left : right ~ right : left -> left | Primitive left , Primitive right -> ( match List . hd ( least_upper_bound left right ) with | Some joined -> if Type . Primitive . equal joined left then Type . Primitive left else if Type . Primitive . equal joined right then Type . Primitive right else union | None -> union ) | TypeOperation _ , _ | _ , TypeOperation _ -> union and meet_callable_implementations order { Type . Callable . parameters = left_parameters ; annotation = left_annotation ; _ } { Type . Callable . parameters = right_parameters ; annotation = right_annotation ; _ } = let open Callable in let parameters = match left_parameters , right_parameters with | Undefined , Undefined -> Some Undefined | Defined left , Defined right -> ( let meet_of_parameters left right = if Type . Callable . Parameter . names_compatible left right then match left , right with | Parameter . PositionalOnly left , Parameter . PositionalOnly right when Bool . equal left . default right . default -> Some ( Parameter . PositionalOnly { left with annotation = join order left . annotation right . annotation } ) | Parameter . PositionalOnly anonymous , Parameter . Named named | Parameter . Named named , Parameter . PositionalOnly anonymous when Bool . equal named . default anonymous . default -> Some ( Parameter . Named { named with annotation = join order named . annotation anonymous . annotation ; } ) | Parameter . Named left , Parameter . Named right when Bool . equal left . default right . default -> Some ( Parameter . Named { left with annotation = join order left . annotation right . annotation } ) | Parameter . Variable ( Concrete left ) , Parameter . Variable ( Concrete right ) -> Some ( Parameter . Variable ( Concrete ( join order left right ) ) ) | Parameter . Keywords left , Parameter . Keywords right -> Some ( Parameter . Keywords ( join order left right ) ) | _ -> None else None in let add_meet_of_parameters sofar left right = sofar >>= fun sofar -> meet_of_parameters left right >>| fun meet_of_parameters -> meet_of_parameters :: sofar in match List . fold2 ~ init ( : Some [ ] ) ~ f : add_meet_of_parameters left right with | Ok parameters -> parameters >>| fun parameters -> Defined ( List . rev parameters ) | Unequal_lengths -> None ) | Undefined , Defined _ | Defined _ , Undefined -> Some Undefined | _ -> None in parameters >>| fun parameters -> { annotation = meet order left_annotation right_annotation ; parameters } and meet ( { is_protocol ; assumptions = { protocol_assumptions ; _ } ; _ } as order ) left right = if Type . equal left right then left else match left , right with | Type . Top , other | other , Type . Top -> other | Type . Any , other when not ( Type . contains_unknown other ) -> other | other , Type . Any when not ( Type . contains_unknown other ) -> other | Type . Bottom , _ | _ , Type . Bottom -> Type . Bottom | Type . ParameterVariadicComponent _ , _ | _ , Type . ParameterVariadicComponent _ -> Type . Bottom | Type . NoneType , _ | _ , Type . NoneType -> Type . Bottom | Type . Annotated left , _ -> Type . annotated ( meet order left right ) | _ , Type . Annotated right -> Type . annotated ( meet order left right ) | ( Type . Variable _ as variable ) , other | other , ( Type . Variable _ as variable ) -> if always_less_or_equal order ~ left : variable ~ right : other then variable else Type . Bottom | ( Type . Union elements as union ) , other | other , ( Type . Union elements as union ) -> if always_less_or_equal order ~ left : other ~ right : union then other else List . map elements ~ f ( : meet order other ) |> List . fold ~ f ( : join order ) ~ init : Type . Bottom | Type . Parametric _ , Type . Parametric _ | Type . Primitive _ , Type . Primitive _ -> if always_less_or_equal order ~ left ~ right then left else if always_less_or_equal order ~ left : right ~ right : left then right else Type . Bottom | Type . Tuple ( Concrete left ) , Type . Tuple ( Concrete right ) when List . length left = List . length right -> List . map2_exn left right ~ f ( : meet order ) |> Type . tuple | Type . Tuple ( Concatenation left ) , Type . Tuple ( Concatenation right ) -> ( match ( Type . OrderedTypes . Concatenation . extract_sole_unbounded_annotation left , Type . OrderedTypes . Concatenation . extract_sole_unbounded_annotation right ) with | Some left_unbounded_element , Some right_unbounded_element -> Type . Tuple ( Type . OrderedTypes . create_unbounded_concatenation ( meet order left_unbounded_element right_unbounded_element ) ) | _ -> Type . Bottom ) | Type . Tuple ( Concrete concrete ) , Type . Tuple ( Concatenation concatenation ) | Type . Tuple ( Concatenation concatenation ) , Type . Tuple ( Concrete concrete ) -> ( match ( Type . OrderedTypes . Concatenation . extract_sole_unbounded_annotation concatenation , concrete ) with | Some unbounded_element , concrete_head :: tail when List . for_all ~ f ( : fun element -> Type . equal element concrete_head ) tail && always_less_or_equal order ~ left : concrete_head ~ right : unbounded_element -> Type . Tuple ( Type . OrderedTypes . create_unbounded_concatenation concrete_head ) | _ -> Type . Bottom ) | ( Type . Tuple _ as tuple ) , ( Type . Parametric _ as parametric ) | ( Type . Parametric _ as parametric ) , ( Type . Tuple _ as tuple ) -> if always_less_or_equal order ~ left : tuple ~ right : parametric then tuple else Type . Bottom | Type . Tuple _ , _ | _ , Type . Tuple _ -> Type . Bottom | Type . Parametric _ , Type . Primitive _ | Type . Primitive _ , Type . Parametric _ -> if always_less_or_equal order ~ left ~ right then left else if always_less_or_equal order ~ left : right ~ right : left then right else Type . Bottom | Type . RecursiveType _ , _ | _ , Type . RecursiveType _ -> if always_less_or_equal order ~ left ~ right then left else if always_less_or_equal order ~ left : right ~ right : left then right else Type . Bottom | ( ( Type . Callable { Callable . kind = Callable . Named left ; _ } as callable ) , Type . Callable { Callable . kind = Callable . Named right ; _ } ) when Reference . equal left right -> callable | ( Type . Callable { overloads = [ ] ; kind = left_kind ; implementation = { annotation = left_annotation ; _ } as left_implementation ; } , Type . Callable { overloads = [ ] ; kind = right_kind ; implementation = { annotation = right_annotation ; _ } as right_implementation ; } ) -> let kind = if Type . Callable . equal_kind left_kind right_kind then left_kind else Type . Callable . Anonymous in meet_callable_implementations order left_implementation right_implementation >>| ( fun implementation -> Type . Callable { Callable . kind ; implementation ; overloads = [ ] } ) |> Option . value ~ default : ( Type . Callable . create ~ annotation ( : meet order left_annotation right_annotation ) ( ) ) | Type . Callable _ , _ | _ , Type . Callable _ -> Bottom | ( Type . IntExpression _ as int_expression ) , other | other , ( Type . IntExpression _ as int_expression ) | ( Type . Literal _ as int_expression ) , other | other , ( Type . Literal _ as int_expression ) -> if always_less_or_equal order ~ left : int_expression ~ right : other then int_expression else Type . Bottom | Type . Primitive _ , _ when always_less_or_equal order ~ left ~ right -> left | _ , Type . Primitive _ when always_less_or_equal order ~ left : right ~ right : left -> right | _ when is_protocol right ~ protocol_assumptions && always_less_or_equal order ~ left ~ right -> left | _ when is_protocol left ~ protocol_assumptions && always_less_or_equal order ~ left : right ~ right : left -> right | _ -> Log . debug " No lower bound found for % a and % a " Type . pp left Type . pp right ; Type . Bottom end end
|
module rec Constraints : OrderedConstraintsType = TypeConstraints . OrderedConstraints ( Implementation ) ConstraintsSet . Make ( Constraints )
|
module IncludableImplementation : FullOrderTypeWithoutT = Implementation
|
let rec is_compatible_with order ~ left ~ right = let fallback ( ) = always_less_or_equal order ~ left ~ right in match left , right with | _ , Type . Any | Type . Any , _ -> true | _ , Type . Top -> true | Type . Top , _ -> false | Type . NoneType , Type . NoneType -> true | Type . Tuple ( Concrete left ) , Type . Tuple ( Concrete right ) when List . length left = List . length right -> List . for_all2_exn left right ~ f ( : fun left right -> is_compatible_with order ~ left ~ right ) | Type . Tuple ( Concrete bounded ) , Type . Tuple ( Concatenation concatenation ) -> ( match Type . OrderedTypes . Concatenation . extract_sole_unbounded_annotation concatenation with | Some unbounded_element -> List . for_all bounded ~ f ( : fun bounded_type -> is_compatible_with order ~ left : bounded_type ~ right : unbounded_element ) | None -> fallback ( ) ) | Type . Union left , right -> List . fold ~ init : true ~ f ( : fun current left -> current && is_compatible_with order ~ left ~ right ) left | left , Type . Union right -> List . exists ~ f ( : fun right -> is_compatible_with order ~ left ~ right ) right | ( Parametric { name = left_name ; parameters = left_parameters } , Parametric { name = right_name ; parameters = right_parameters } ) when Type . Primitive . equal left_name right_name && Int . equal ( List . length left_parameters ) ( List . length right_parameters ) -> ( match Type . Parameter . all_singles left_parameters , Type . Parameter . all_singles right_parameters with | Some left_parameters , Some right_parameters -> List . for_all2_exn left_parameters right_parameters ~ f ( : fun left right -> is_compatible_with order ~ left ~ right ) | _ -> fallback ( ) ) | _ , _ -> fallback ( )
|
let widen order ~ widening_threshold ~ previous ~ next ~ iteration = if iteration > widening_threshold then Type . Top else join order previous next
|
let ( ! ) concretes = List . map concretes ~ f ( : fun single -> Type . Parameter . Single single )
|
let environment ? source context = let { ScratchProject . BuiltGlobalEnvironment . global_environment ; _ } = let sources = Option . value_map source ~ f ( : fun source -> [ " test . py " , source ] ) ~ default [ ] : in ScratchProject . setup ~ context sources |> ScratchProject . build_global_environment in global_environment
|
let resolution ? source context = let environment = environment ? source context in AnnotatedGlobalEnvironment . read_only environment |> GlobalResolution . create
|
let concrete_connect ? parameters = let parameters = parameters >>| List . map ~ f ( : fun single -> Type . Parameter . Single single ) in MockClassHierarchyHandler . connect ? parameters
|
let make_attributes ~ class_name = let parse_attribute ( name , annotation ) = Attribute . create ~ annotation ~ original_annotation : annotation ~ uninstantiated_annotation ( : Some annotation ) ~ visibility : ReadWrite ~ abstract : false ~ async_property : false ~ class_variable : false ~ defined : true ~ initialized : OnClass ~ parent : class_name ~ property : false ~ name ~ undecorated_signature : None ~ problem : None in List . map ~ f : parse_attribute
|
let parse_attributes ~ class_name ~ parse_annotation attributes = List . map attributes ~ f ( : fun ( name , annotation ) -> name , parse_annotation annotation ) |> make_attributes ~ class_name
|
let get_typed_dictionary _ = None
|
let hierarchy class_hierarchy_handler = { ConstraintsSet . instantiate_successors_parameters = ClassHierarchy . instantiate_successors_parameters class_hierarchy_handler ; is_transitive_successor = ClassHierarchy . is_transitive_successor class_hierarchy_handler ; variables = ClassHierarchy . variables class_hierarchy_handler ; least_upper_bound = ClassHierarchy . least_upper_bound class_hierarchy_handler ; }
|
let attribute_from_attributes attributes = let attribute annotation ~ assumptions ~ name = let find attribute = String . equal ( Annotated . Attribute . name attribute ) name in attributes annotation ~ assumptions >>= List . find ~ f : find in attribute
|
let less_or_equal ( ? attributes = fun _ ~ assumptions : _ -> None ) ( ? is_protocol = fun _ ~ protocol_assumptions : _ -> false ) handler = always_less_or_equal { ConstraintsSet . class_hierarchy = hierarchy handler ; all_attributes = attributes ; attribute = attribute_from_attributes attributes ; is_protocol ; assumptions = { protocol_assumptions = ProtocolAssumptions . empty ; callable_assumptions = CallableAssumptions . empty ; decorator_assumptions = DecoratorAssumptions . empty ; } ; get_typed_dictionary ; metaclass = ( fun _ ~ assumptions : _ -> Some ( Type . Primitive " type " ) ) ; }
|
let is_compatible_with handler = is_compatible_with { ConstraintsSet . class_hierarchy = hierarchy handler ; all_attributes = ( fun _ ~ assumptions : _ -> None ) ; attribute = ( fun _ ~ assumptions : _ ~ name : _ -> None ) ; is_protocol = ( fun _ ~ protocol_assumptions : _ -> false ) ; assumptions = { protocol_assumptions = ProtocolAssumptions . empty ; callable_assumptions = CallableAssumptions . empty ; decorator_assumptions = DecoratorAssumptions . empty ; } ; get_typed_dictionary ; metaclass = ( fun _ ~ assumptions : _ -> Some ( Type . Primitive " type " ) ) ; }
|
let join ( ? attributes = fun _ ~ assumptions : _ -> None ) handler = join { ConstraintsSet . class_hierarchy = hierarchy handler ; all_attributes = attributes ; attribute = attribute_from_attributes attributes ; is_protocol = ( fun _ ~ protocol_assumptions : _ -> false ) ; assumptions = { protocol_assumptions = ProtocolAssumptions . empty ; callable_assumptions = CallableAssumptions . empty ; decorator_assumptions = DecoratorAssumptions . empty ; } ; get_typed_dictionary ; metaclass = ( fun _ ~ assumptions : _ -> Some ( Type . Primitive " type " ) ) ; }
|
let meet handler = meet { ConstraintsSet . class_hierarchy = hierarchy handler ; all_attributes = ( fun _ ~ assumptions : _ -> None ) ; attribute = ( fun _ ~ assumptions : _ ~ name : _ -> None ) ; is_protocol = ( fun _ ~ protocol_assumptions : _ -> false ) ; assumptions = { protocol_assumptions = ProtocolAssumptions . empty ; callable_assumptions = CallableAssumptions . empty ; decorator_assumptions = DecoratorAssumptions . empty ; } ; get_typed_dictionary ; metaclass = ( fun _ ~ assumptions : _ -> Some ( Type . Primitive " type " ) ) ; }
|
let order = let bottom = " bottom " in let order = MockClassHierarchyHandler . create ( ) in let open MockClassHierarchyHandler in insert order bottom ; insert order " 0 " ; insert order " 1 " ; insert order " 2 " ; insert order " 3 " ; insert order " 4 " ; insert order " 5 " ; connect order ~ predecessor " : 0 " ~ successor " : 3 " ; connect order ~ predecessor " : 1 " ~ successor " : 3 " ; connect order ~ predecessor " : 4 " ~ successor " : 2 " ; connect order ~ predecessor : bottom ~ successor " : 0 " ; connect order ~ predecessor : bottom ~ successor " : 1 " ; connect order ~ predecessor : bottom ~ successor " : 2 " ; connect order ~ predecessor : bottom ~ successor " : 4 " ; handler order
|
let disconnected_order = let order = MockClassHierarchyHandler . create ( ) in let open MockClassHierarchyHandler in insert order " A " ; insert order " B " ; handler order
|
let variance_order = let order = MockClassHierarchyHandler . create ( ) in let open MockClassHierarchyHandler in insert order " object " ; insert order " bool " ; insert order " str " ; insert order " int " ; insert order " float " ; connect order ~ predecessor " : int " ~ successor " : float " ; insert order " typing . Generic " ; let variable_t = Type . variable " _T " in let variable_t_2 = Type . variable " _T_2 " in let variable_t_co = Type . variable " _T_co " ~ variance : Covariant in let variable_t_contra = Type . variable " _T_contra " ~ variance : Contravariant in insert order " LinkedList " ; insert order " Map " ; insert order " Box " ; insert order " Sink " ; concrete_connect order ~ predecessor " : LinkedList " ~ successor " : typing . Generic " ~ parameters [ : variable_t ] ; concrete_connect order ~ predecessor " : Map " ~ successor " : typing . Generic " ~ parameters [ : variable_t ; variable_t_2 ] ; concrete_connect order ~ predecessor " : Box " ~ successor " : typing . Generic " ~ parameters [ : variable_t_co ] ; concrete_connect order ~ predecessor " : Sink " ~ successor " : typing . Generic " ~ parameters [ : variable_t_contra ] ; insert order " Base " ; insert order " Derived " ; concrete_connect order ~ predecessor " : Base " ~ successor " : typing . Generic " ~ parameters [ : variable_t_contra ] ; concrete_connect order ~ predecessor " : Derived " ~ successor " : Base " ~ parameters [ : variable_t_co ] ; concrete_connect order ~ predecessor " : Derived " ~ successor " : typing . Generic " ~ parameters [ : variable_t_co ] ; handler order
|
let multiplane_variance_order = let order = MockClassHierarchyHandler . create ( ) in let open MockClassHierarchyHandler in insert order " str " ; insert order " int " ; insert order " float " ; insert order " object " ; insert order " bool " ; connect order ~ predecessor " : int " ~ successor " : float " ; insert order " typing . Generic " ; let variable_t_co = Type . variable " _T_co " ~ variance : Covariant in let variable_t_contra = Type . variable " _T_contra " ~ variance : Contravariant in insert order " A " ; insert order " B " ; insert order " C " ; insert order " D " ; concrete_connect order ~ predecessor " : A " ~ successor " : typing . Generic " ~ parameters [ : variable_t_co ; variable_t_contra ] ; concrete_connect order ~ predecessor " : B " ~ successor " : A " ~ parameters [ : variable_t_contra ; variable_t_co ] ; concrete_connect order ~ predecessor " : B " ~ successor " : typing . Generic " ~ parameters [ : variable_t_contra ; variable_t_co ] ; concrete_connect order ~ predecessor " : C " ~ successor " : B " ~ parameters [ : Type . integer ; Type . integer ] ; concrete_connect order ~ predecessor " : D " ~ successor " : B " ~ parameters [ : Type . float ; Type . float ] ; handler order
|
let parallel_planes_variance_order = let order = MockClassHierarchyHandler . create ( ) in let open MockClassHierarchyHandler in insert order " str " ; insert order " int " ; insert order " float " ; insert order " object " ; connect order ~ predecessor " : int " ~ successor " : float " ; insert order " typing . Generic " ; let variable_t_co = Type . variable " _T_co " ~ variance : Covariant in let variable_t_contra = Type . variable " _T_contra " ~ variance : Contravariant in insert order " A " ; insert order " B " ; insert order " C " ; insert order " D " ; concrete_connect order ~ predecessor " : A " ~ successor " : typing . Generic " ~ parameters [ : variable_t_co ; variable_t_contra ] ; concrete_connect order ~ predecessor " : B " ~ successor " : A " ~ parameters [ : variable_t_co ; variable_t_contra ] ; concrete_connect order ~ predecessor " : B " ~ successor " : typing . Generic " ~ parameters [ : variable_t_co ; variable_t_contra ] ; concrete_connect order ~ predecessor " : C " ~ successor " : B " ~ parameters [ : Type . integer ; Type . integer ] ; concrete_connect order ~ predecessor " : D " ~ successor " : B " ~ parameters [ : Type . float ; Type . float ] ; handler order
|
let default = let order = MockClassHierarchyHandler . create ( ) in let open MockClassHierarchyHandler in insert order " typing . Generic " ; insert order " int " ; insert order " str " ; insert order " bytes " ; insert order " bool " ; insert order " float " ; insert order " object " ; connect order ~ predecessor " : str " ~ successor " : object " ; connect order ~ predecessor " : bytes " ~ successor " : object " ; connect order ~ predecessor " : int " ~ successor " : float " ; connect order ~ predecessor " : float " ~ successor " : object " ; let type_builtin = " type " in let type_variable = Type . Variable ( Type . Variable . Unary . create " _T " ) in insert order type_builtin ; connect order ~ predecessor : type_builtin ~ parameters [ : Single type_variable ] ~ successor " : typing . Generic " ; let typed_dictionary = " TypedDictionary " in let non_total_typed_dictionary = " NonTotalTypedDictionary " in let typing_mapping = " typing . Mapping " in insert order non_total_typed_dictionary ; insert order typed_dictionary ; insert order typing_mapping ; connect order ~ predecessor : non_total_typed_dictionary ~ successor : typed_dictionary ; concrete_connect order ~ predecessor : typed_dictionary ~ parameters [ : Type . string ; Type . object_primitive ] ~ successor : typing_mapping ; let variable = Type . variable " _T " in let other_variable = Type . variable " _T2 " in let variable_covariant = Type . variable " _T_co " ~ variance : Covariant in insert order " typing . Sequence " ; concrete_connect order ~ predecessor " : typing . Sequence " ~ successor " : typing . Generic " ~ parameters [ : variable ] ; insert order " list " ; insert order " typing . Sized " ; connect order ~ predecessor " : list " ~ successor " : typing . Sized " ; concrete_connect order ~ predecessor " : list " ~ successor " : typing . Generic " ~ parameters [ : variable ] ; concrete_connect order ~ predecessor " : list " ~ successor " : typing . Sequence " ~ parameters [ : variable ] ; insert order " typing . AbstractSet " ; insert order " set " ; connect order ~ predecessor " : set " ~ successor " : typing . Sized " ; concrete_connect order ~ predecessor " : set " ~ successor " : typing . Generic " ~ parameters [ : variable ] ; concrete_connect order ~ predecessor " : typing . AbstractSet " ~ successor " : typing . Generic " ~ parameters [ : variable ] ; concrete_connect order ~ predecessor " : set " ~ successor " : typing . AbstractSet " ~ parameters [ : variable ] ; insert order " typing . Iterator " ; concrete_connect order ~ predecessor " : list " ~ successor " : typing . Iterator " ~ parameters [ : variable ] ; concrete_connect order ~ predecessor " : typing . Iterator " ~ successor " : typing . Generic " ~ parameters [ : variable_covariant ] ; insert order " typing . Iterable " ; concrete_connect order ~ predecessor " : typing . Iterator " ~ successor " : typing . Iterable " ~ parameters [ : variable_covariant ] ; concrete_connect order ~ predecessor " : typing . Iterable " ~ successor " : typing . Generic " ~ parameters [ : variable_covariant ] ; concrete_connect order ~ predecessor " : list " ~ successor " : typing . Iterable " ~ parameters [ : variable ] ; insert order " tuple " ; concrete_connect order ~ predecessor " : tuple " ~ successor " : typing . Iterator " ~ parameters [ : variable ] ; concrete_connect order ~ predecessor " : tuple " ~ successor " : typing . Generic " ~ parameters [ : variable ] ; insert order " typing . Generator " ; concrete_connect order ~ predecessor " : typing . Generator " ~ successor " : typing . Iterator " ~ parameters [ : variable ] ; concrete_connect order ~ predecessor " : typing . Generator " ~ successor " : typing . Generic " ~ parameters [ : variable ] ; concrete_connect order ~ predecessor " : str " ~ successor " : typing . Iterable " ~ parameters [ : Type . Primitive " str " ] ; connect order ~ predecessor " : typing . Iterable " ~ successor " : object " ; insert order " AnyIterable " ; connect order ~ predecessor " : AnyIterable " ~ successor " : typing . Iterable " ; insert order " typing . Mapping " ; concrete_connect order ~ predecessor " : typing . Mapping " ~ successor " : typing . Generic " ~ parameters [ : variable ; variable_covariant ] ; insert order " dict " ; concrete_connect order ~ predecessor " : dict " ~ successor " : typing . Generic " ~ parameters [ : variable ; other_variable ] ; concrete_connect order ~ predecessor " : dict " ~ successor " : typing . Mapping " ~ parameters [ : variable ; other_variable ] ; concrete_connect order ~ predecessor " : dict " ~ successor " : typing . Iterator " ~ parameters [ : variable ] ; insert order " collections . OrderedDict " ; concrete_connect order ~ predecessor " : collections . OrderedDict " ~ successor " : typing . Generic " ~ parameters [ : variable ; other_variable ] ; concrete_connect order ~ predecessor " : collections . OrderedDict " ~ successor " : dict " ~ parameters [ : variable ; other_variable ] ; insert order " PartiallySpecifiedDict " ; concrete_connect order ~ predecessor " : PartiallySpecifiedDict " ~ successor " : dict " ~ parameters [ : Primitive " int " ] ; insert order " OverSpecifiedDict " ; concrete_connect order ~ predecessor " : OverSpecifiedDict " ~ successor " : dict " ~ parameters [ : Primitive " int " ; Primitive " int " ; Primitive " str " ] ; insert order " GenericContainer " ; concrete_connect order ~ predecessor " : GenericContainer " ~ successor " : typing . Generic " ~ parameters [ : variable ; other_variable ] ; insert order " NonGenericContainerChild " ; concrete_connect order ~ predecessor " : NonGenericContainerChild " ~ successor " : GenericContainer " ~ parameters [ : Primitive " int " ; Primitive " str " ] ; insert order " DifferentGenericContainer " ; concrete_connect order ~ predecessor " : DifferentGenericContainer " ~ successor " : typing . Generic " ~ parameters [ : variable ; other_variable ] ; insert order " CommonNonGenericChild " ; concrete_connect order ~ predecessor " : CommonNonGenericChild " ~ successor " : GenericContainer " ~ parameters [ : Primitive " int " ; Primitive " str " ] ; concrete_connect order ~ predecessor " : CommonNonGenericChild " ~ successor " : DifferentGenericContainer " ~ parameters [ : Primitive " int " ; Primitive " str " ] ; handler order
|
let ( !! ) name = Type . Primitive name
|
let test_less_or_equal context = assert_true ( less_or_equal order ~ left : Type . Bottom ~ right : Type . Top ) ; assert_false ( less_or_equal order ~ left : Type . Top ~ right : Type . Bottom ) ; assert_true ( less_or_equal order ~ left " :!! 0 " ~ right " :!! 0 " ) ; assert_true ( less_or_equal order ~ left : Type . Bottom ~ right " :!! 0 " ) ; assert_true ( less_or_equal order ~ left : Type . Bottom ~ right " :!! 1 " ) ; assert_true ( less_or_equal order ~ left : Type . Bottom ~ right " :!! 2 " ) ; assert_true ( less_or_equal order ~ left : Type . Bottom ~ right " :!! 3 " ) ; assert_false ( less_or_equal order ~ left " :!! 3 " ~ right : Type . Bottom ) ; assert_false ( less_or_equal order ~ left " :!! 2 " ~ right : Type . Bottom ) ; assert_false ( less_or_equal order ~ left " :!! 1 " ~ right : Type . Bottom ) ; assert_false ( less_or_equal order ~ left " :!! 0 " ~ right : Type . Bottom ) ; assert_true ( less_or_equal order ~ left " :!! 0 " ~ right " :!! 3 " ) ; assert_true ( less_or_equal order ~ left " :!! 1 " ~ right " :!! 3 " ) ; assert_false ( less_or_equal order ~ left " :!! 2 " ~ right " :!! 3 " ) ; assert_true ( less_or_equal default ~ left " :!! list " ~ right " :!! typing . Sized " ) ; assert_true ( less_or_equal default ~ left ( : Type . list Type . integer ) ~ right " :!! typing . Sized " ) ; assert_true ( less_or_equal default ~ left ( : Type . annotated Type . integer ) ~ right : Type . float ) ; assert_true ( less_or_equal default ~ left : Type . integer ~ right ( : Type . annotated Type . float ) ) ; assert_true ( less_or_equal default ~ left ( : Type . annotated Type . integer ) ~ right ( : Type . annotated Type . float ) ) ; assert_true ( less_or_equal default ~ left ( : Type . list Type . integer ) ~ right ( : Type . iterator Type . integer ) ) ; assert_false ( less_or_equal default ~ left ( : Type . list Type . float ) ~ right ( : Type . iterator Type . integer ) ) ; assert_true ( less_or_equal default ~ left ( : Type . iterator Type . integer ) ~ right ( : Type . iterable Type . integer ) ) ; assert_true ( less_or_equal default ~ left ( : Type . iterator Type . integer ) ~ right ( : Type . iterable Type . float ) ) ; assert_true ( less_or_equal default ~ left : Type . string ~ right ( : Type . iterable Type . string ) ) ; assert_true ( less_or_equal default ~ left ( : Type . tuple [ Type . integer ; Type . integer ] ) ~ right ( : Type . iterator Type . integer ) ) ; assert_false ( less_or_equal default ~ left ( : Type . tuple [ Type . integer ; Type . float ] ) ~ right ( : Type . iterator Type . integer ) ) ; assert_true ( less_or_equal default ~ left ( : Type . tuple [ Type . integer ; Type . float ] ) ~ right ( : Type . iterator Type . float ) ) ; assert_true ( less_or_equal default ~ left ( : Type . Tuple ( Type . OrderedTypes . create_unbounded_concatenation Type . integer ) ) ~ right ( : Type . iterator Type . integer ) ) ; assert_false ( less_or_equal default ~ left ( : Type . Tuple ( Type . OrderedTypes . create_unbounded_concatenation Type . float ) ) ~ right ( : Type . iterator Type . integer ) ) ; assert_true ( less_or_equal default ~ left ( : Type . Primitive " tuple " ) ~ right ( : Type . Tuple ( Type . OrderedTypes . create_unbounded_concatenation Type . float ) ) ) ; assert_true ( less_or_equal default ~ left ( : Type . Tuple ( Concrete [ Type . integer ; Type . integer ] ) ) ~ right ( : Type . parametric " tuple " [ ! Type . integer ] ) ) ; assert_true ( less_or_equal default ~ left : Type . NoneType ~ right ( : Type . optional Type . integer ) ) ; assert_true ( less_or_equal default ~ left ( : Type . optional Type . string ) ~ right ( : Type . Union [ Type . integer ; Type . optional Type . string ] ) ) ; assert_true ( less_or_equal default ~ left ( : Type . tuple [ Type . integer ; Type . integer ] ) ~ right ( : Type . Tuple ( Type . OrderedTypes . create_unbounded_concatenation Type . integer ) ) ) ; assert_true ( less_or_equal default ~ left ( : Type . tuple [ Type . integer ; Type . integer ] ) ~ right ( : Type . Tuple ( Type . OrderedTypes . create_unbounded_concatenation Type . float ) ) ) ; assert_true ( less_or_equal default ~ left ( : Type . tuple [ Type . integer ; Type . float ] ) ~ right ( : Type . Tuple ( Type . OrderedTypes . create_unbounded_concatenation Type . float ) ) ) ; let order = let order = MockClassHierarchyHandler . create ( ) in let open MockClassHierarchyHandler in insert order " object " ; insert order " str " ; insert order " int " ; insert order " float " ; concrete_connect order ~ predecessor " : int " ~ successor " : float " ; insert order " tuple " ; insert order " A " ; insert order " B " ; insert order " C " ; insert order " typing . Generic " ; insert order " FloatToStrCallable " ; insert order " ParametricCallableToStr " ; insert order " typing . Callable " ; concrete_connect order ~ predecessor " : A " ~ successor " : typing . Generic " ~ parameters [ : Type . variable " _1 " ; Type . variable " _2 " ] ; concrete_connect order ~ predecessor " : B " ~ successor " : typing . Generic " ~ parameters [ : Type . variable " _T " ] ; concrete_connect order ~ predecessor " : C " ~ successor " : typing . Generic " ~ parameters [ : Type . variable " _T " ] ; concrete_connect order ~ predecessor " : A " ~ successor " : B " ~ parameters [ : Type . tuple [ Type . variable " _1 " ; Type . variable " _2 " ] ] ; concrete_connect order ~ predecessor " : B " ~ successor " : C " ~ parameters [ : Type . union [ Type . variable " _T " ; Type . float ] ] ; concrete_connect order ~ parameters [ : Type . variable " _T " ] ~ predecessor " : ParametricCallableToStr " ~ successor " : typing . Generic " ; let typed_dictionary = " TypedDictionary " in let non_total_typed_dictionary = " NonTotalTypedDictionary " in let typing_mapping = " typing . Mapping " in insert order typed_dictionary ; insert order non_total_typed_dictionary ; insert order typing_mapping ; concrete_connect order ~ predecessor : non_total_typed_dictionary ~ successor : typed_dictionary ; concrete_connect order ~ predecessor : typed_dictionary ~ parameters [ : Type . string ; Type . object_primitive ] ~ successor : typing_mapping ; concrete_connect order ~ parameters [ : Type . variable " _T " ; Type . variable ~ variance : Covariant " _TCov " ] ~ predecessor : typing_mapping ~ successor " : typing . Generic " ; insert order " dict " ; insert order " MatchesProtocol " ; insert order " DoesNotMatchProtocol " ; handler order in assert_true ( less_or_equal order ~ left ( : Type . parametric " A " [ ! Type . integer ; Type . string ] ) ~ right ( : Type . parametric " B " [ ! Type . tuple [ Type . integer ; Type . string ] ] ) ) ; assert_false ( less_or_equal order ~ left ( : Type . parametric " A " [ ! Type . integer ; Type . string ] ) ~ right ( : Type . tuple [ Type . integer ; Type . string ] ) ) ; assert_true ( less_or_equal order ~ left ( : Type . parametric " A " [ ! Type . integer ; Type . string ] ) ~ right : ( Type . parametric " C " [ ! Type . union [ Type . tuple [ Type . integer ; Type . string ] ; Type . float ] ] ) ) ; assert_false ( less_or_equal order ~ left ( : Type . parametric " A " [ ! Type . string ; Type . integer ] ) ~ right : ( Type . parametric " C " [ ! Type . union [ Type . tuple [ Type . integer ; Type . string ] ; Type . float ] ] ) ) ; assert_true ( less_or_equal order ~ left ( : Type . variable " T " ) ~ right : Type . Any ) ; assert_false ( less_or_equal order ~ left ( : Type . variable " T " ) ~ right : Type . integer ) ; assert_true ( less_or_equal order ~ left : Type . Any ~ right ( : Type . variable " T " ) ) ; assert_false ( less_or_equal order ~ left : Type . integer ~ right ( : Type . variable " T " ) ) ; assert_true ( less_or_equal order ~ left ( : Type . variable " T " ) ~ right ( : Type . union [ Type . string ; Type . variable " T " ] ) ) ; assert_false ( less_or_equal order ~ left : Type . integer ~ right ( : Type . variable ~ constraints ( : Type . Variable . Explicit [ Type . float ; Type . integer ] ) " T " ) ) ; assert_true ( less_or_equal order ~ left ( : Type . variable ~ constraints ( : Type . Variable . Explicit [ Type . float ; Type . string ] ) " T " ) ~ right ( : Type . union [ Type . float ; Type . string ] ) ) ; assert_true ( less_or_equal order ~ left ( : Type . variable ~ constraints ( : Type . Variable . Explicit [ Type . float ; Type . string ] ) " T " ) ~ right ( : Type . union [ Type . float ; Type . string ; " !! A " ] ) ) ; assert_false ( less_or_equal order ~ left ( : Type . variable ~ constraints ( : Type . Variable . Explicit [ Type . float ; Type . string ] ) " T " ) ~ right ( : Type . union [ Type . float ] ) ) ; assert_true ( less_or_equal order ~ left : ( Type . variable ~ constraints ( : Type . Variable . Bound ( Type . union [ Type . float ; Type . string ] ) ) " T " ) ~ right ( : Type . union [ Type . float ; Type . string ; " !! A " ] ) ) ; assert_false ( less_or_equal order ~ left : Type . string ~ right ( : Type . variable ~ constraints ( : Type . Variable . Bound Type . string ) " T " ) ) ; let float_string_variable = Type . variable ~ constraints ( : Type . Variable . Explicit [ Type . float ; Type . string ] ) " T " in assert_true ( less_or_equal order ~ left : float_string_variable ~ right ( : Type . union [ float_string_variable ; " !! A " ] ) ) ; assert_true ( less_or_equal order ~ left ( : Type . variable ~ constraints ( : Type . Variable . Bound " !! A " ) " T " ) ~ right ( : Type . union [ Type . variable ~ constraints ( : Type . Variable . Bound " !! A " ) " T " ; Type . string ] ) ) ; assert_true ( less_or_equal order ~ left ( : Type . variable ~ constraints ( : Type . Variable . Bound " !! A " ) " T " ) ~ right ( : Type . optional ( Type . variable ~ constraints ( : Type . Variable . Bound " !! A " ) " T " ) ) ) ; assert_true ( less_or_equal order ~ left ( : Type . variable ~ constraints ( : Type . Variable . Bound ( Type . optional " !! A " ) ) " T " ) ~ right ( : Type . optional " !! A " ) ) ; assert_true ( less_or_equal order ~ left ( : Type . variable ~ constraints ( : Type . Variable . Bound Type . integer ) " T " ) ~ right ( : Type . union [ Type . float ; Type . string ] ) ) ; assert_true ( less_or_equal order ~ left ( : Type . variable ~ constraints ( : Type . Variable . Bound Type . integer ) " T " ) ~ right : ( Type . union [ Type . variable ~ constraints ( : Type . Variable . Bound Type . integer ) " T " ; Type . string ] ) ) ; assert_true ( less_or_equal order ~ left ( : Type . variable ~ constraints : Type . Variable . Unconstrained " T " ) ~ right : Type . Top ) ; assert_true ( less_or_equal order ~ left ( : Type . variable ~ constraints : Type . Variable . Unconstrained " T " ) ~ right ( : Type . union [ Type . variable ~ constraints : Type . Variable . Unconstrained " T " ; Type . string ] ) ) ; assert_true ( less_or_equal order ~ left : ( Type . variable ~ constraints ( : Type . Variable . Bound ( Type . union [ Type . float ; Type . string ] ) ) " T " ) ~ right ( : Type . union [ Type . float ; Type . string ] ) ) ; assert_false ( less_or_equal order ~ left : Type . integer ~ right ( : Type . variable ~ constraints ( : Type . Variable . Bound Type . float ) " T " ) ) ; assert_false ( less_or_equal order ~ left : Type . float ~ right ( : Type . variable ~ constraints ( : Type . Variable . Bound Type . integer ) " T " ) ) ; assert_false ( less_or_equal order ~ left ( : Type . union [ Type . string ; Type . integer ] ) ~ right ( : Type . variable ~ constraints ( : Type . Variable . Explicit [ Type . string ; Type . integer ] ) " T " ) ) ; assert_false ( less_or_equal order ~ left : Type . integer ~ right ( : Type . variable ~ constraints ( : Type . Variable . Explicit [ Type . string ] ) " T " ) ) ; assert_false ( less_or_equal order ~ left : Type . integer ~ right ( : Type . variable ~ constraints : Type . Variable . LiteralIntegers " T " ) ) ; assert_true ( less_or_equal order ~ left ( : Type . variable ~ constraints : Type . Variable . LiteralIntegers " T " ) ~ right : Type . integer ) ; let less_or_equal ? attributes ? is_protocol order ~ left ~ right = let aliases = function | " T_Unconstrained " -> Some ( Type . variable " T_Unconstrained " ) | " T_int_bool " -> Some ( Type . variable " T_int_bool " ~ constraints ( : Type . Variable . Explicit [ Type . integer ; Type . bool ] ) ) | _ -> None in let attributes annotation ~ assumptions : _ = let parse_annotation = let aliases = function | " _T " -> Some ( Type . variable " _T " ) | _ -> None in let aliases = create_type_alias_table aliases in parse_callable ~ aliases in match annotation with | Type . Primitive " FloatToStrCallable " -> Some ( parse_attributes ~ parse_annotation ~ class_name " : MatchesProtocol " [ " __call__ " , " typing . Callable [ [ float ] , str ] " ] ) | Type . Parametric { name = " ParametricCallableToStr " ; parameters = [ Single ( Primitive parameter ) ] } -> let callable = Format . sprintf " typing . Callable [ [ % s ] , str ] " parameter in Some ( parse_attributes ~ parse_annotation ~ class_name " : MatchesProtocol " [ " __call__ " , callable ] ) | annotation -> ( match attributes with | Some attributes -> attributes annotation | None -> failwith ( " getting attributes for wrong class " ^ Type . show annotation ) ) in let aliases = create_type_alias_table aliases in less_or_equal ~ attributes ? is_protocol order ~ left ( : parse_callable ~ aliases left ) ~ right ( : parse_callable ~ aliases right ) in assert_true ( less_or_equal order ~ left " : typing . Callable [ [ int ] , int ] " ~ right " : typing . Callable [ [ int ] , int ] " ) ; assert_false ( less_or_equal order ~ left " : typing . Callable [ [ str ] , int ] " ~ right " : typing . Callable [ [ int ] , int ] " ) ; assert_false ( less_or_equal order ~ left " : typing . Callable [ [ int ] , int ] " ~ right " : typing . Callable [ [ str ] , int ] " ) ; assert_false ( less_or_equal order ~ left " : typing . Callable [ [ int ] , str ] " ~ right " : typing . Callable [ [ int ] , int ] " ) ; assert_false ( less_or_equal order ~ left " : typing . Callable [ [ int ] , float ] " ~ right " : typing . Callable [ [ int ] , int ] " ) ; assert_true ( less_or_equal order ~ left " : typing . Callable [ [ int ] , int ] " ~ right " : typing . Callable [ [ int ] , float ] " ) ; assert_true ( less_or_equal order ~ left " : typing . Callable [ [ float ] , int ] " ~ right " : typing . Callable [ [ int ] , int ] " ) ; assert_false ( less_or_equal order ~ left " : typing . Callable [ [ int ] , int ] " ~ right " : typing . Callable [ [ float ] , int ] " ) ; assert_false ( less_or_equal order ~ left " : typing . Callable [ [ int ] , int ] " ~ right " : typing . Callable ( ' foo ' ) [ [ int ] , int ] " ) ; assert_false ( less_or_equal order ~ left " : typing . Callable [ [ str ] , int ] " ~ right " : typing . Callable ( ' foo ' ) [ [ int ] , int ] " ) ; assert_true ( less_or_equal order ~ left " : typing . Callable ( ' foo ' ) [ [ int ] , int ] " ~ right " : typing . Callable [ [ int ] , int ] " ) ; assert_false ( less_or_equal order ~ left " : typing . Callable ( ' foo ' ) [ [ str ] , int ] " ~ right " : typing . Callable [ [ int ] , int ] " ) ; assert_true ( less_or_equal order ~ left " : typing . Callable ( ' foo ' ) [ [ int ] , int ] " ~ right " : typing . Callable ( ' foo ' ) [ [ int ] , int ] " ) ; assert_false ( less_or_equal order ~ left " : typing . Callable ( ' bar ' ) [ [ str ] , int ] " ~ right " : typing . Callable ( ' foo ' ) [ [ int ] , int ] " ) ; assert_false ( less_or_equal order ~ left " : typing . Callable ( ' foo ' ) [ [ int ] , int ] " ~ right " : typing . Callable ( ' bar ' ) [ [ int ] , int ] " ) ; assert_true ( less_or_equal order ~ left " : typing . Callable ( ' foo ' ) [ [ str ] , int ] " ~ right " : typing . Callable ( ' foo ' ) [ [ int ] , int ] " ) ; assert_true ( less_or_equal order ~ left " : typing . Callable [ [ Named ( foo , bool , default ) ] , int ] " ~ right " : typing . Callable [ [ KeywordOnly ( foo , bool , default ) ] , int ] " ) ; assert_true ( less_or_equal order ~ left " : typing . Callable [ [ Named ( foo , bool , default ) ] , int ] " ~ right " : typing . Callable [ [ KeywordOnly ( foo , bool ) ] , int ] " ) ; assert_true ( less_or_equal order ~ left " : typing . Callable [ [ Named ( foo , bool ) ] , int ] " ~ right " : typing . Callable [ [ KeywordOnly ( foo , bool ) ] , int ] " ) ; assert_false ( less_or_equal order ~ left " : typing . Callable [ [ Named ( foo , bool ) ] , int ] " ~ right " : typing . Callable [ [ KeywordOnly ( foo , bool , default ) ] , int ] " ) ; assert_true ( less_or_equal order ~ left " : typing . Callable [ . . . , int ] " ~ right " : typing . Callable [ . . . , float ] " ) ; assert_true ( less_or_equal order ~ left " : typing . Callable [ [ int ] , int ] " ~ right " : typing . Callable [ . . . , int ] " ) ; assert_true ( less_or_equal order ~ left " : typing . Callable [ . . . , int ] " ~ right " : typing . Callable [ [ int ] , float ] " ) ; assert_true ( less_or_equal order ~ left " : typing . Callable [ [ Variable ( object ) , Keywords ( object ) ] , int ] " ~ right " : typing . Callable [ . . . , int ] " ) ; assert_true ( less_or_equal order ~ left " : typing . Callable [ [ Variable ( int ) , Keywords ( object ) ] , int ] " ~ right " : typing . Callable [ . . . , int ] " ) ; assert_true ( less_or_equal order ~ left " : FloatToStrCallable " ~ right " : typing . Callable [ [ float ] , str ] " ) ; assert_true ( less_or_equal order ~ left " : FloatToStrCallable " ~ right " : typing . Callable [ [ int ] , str ] " ) ; assert_false ( less_or_equal order ~ left " : FloatToStrCallable " ~ right " : typing . Callable [ [ float ] , int ] " ) ; assert_true ( less_or_equal order ~ left " : ParametricCallableToStr [ int ] " ~ right " : typing . Callable [ [ int ] , str ] " ) ; assert_true ( less_or_equal order ~ left " : ParametricCallableToStr [ float ] " ~ right " : typing . Callable [ [ int ] , str ] " ) ; assert_false ( less_or_equal order ~ left " : ParametricCallableToStr [ int ] " ~ right " : typing . Callable [ [ float ] , str ] " ) ; assert_false ( less_or_equal order ~ left " : ParametricCallableToStr [ int ] " ~ right " : typing . Callable [ [ int ] , int ] " ) ; assert_true ( less_or_equal order ~ left " : typing . Callable [ [ Variable ( int ) ] , str ] " ~ right " : typing . Callable [ [ int ] , str ] " ) ; assert_true ( less_or_equal order ~ left " : typing . Callable [ [ Variable ( int ) ] , str ] " ~ right " : typing . Callable [ [ int , int ] , str ] " ) ; assert_false ( less_or_equal order ~ left " : typing . Callable [ [ Variable ( str ) ] , str ] " ~ right " : typing . Callable [ [ int ] , str ] " ) ; assert_false ( less_or_equal order ~ left " : typing . Callable [ [ Variable ( int ) ] , str ] " ~ right " : typing . Callable [ [ Named ( arg , int ) ] , str ] " ) ; assert_true ( less_or_equal order ~ left " : typing . Callable [ [ Variable ( int ) , Keywords ( int ) ] , int ] " ~ right " : typing . Callable [ [ Keywords ( int ) ] , int ] " ) ; assert_false ( less_or_equal order ~ left " : typing . Callable [ [ Keywords ( int ) ] , str ] " ~ right " : typing . Callable [ [ Named ( arg , int ) ] , str ] " ) ; assert_true ( less_or_equal order ~ left " : typing . Callable [ [ Variable ( int ) , Keywords ( int ) ] , str ] " ~ right " : typing . Callable [ [ Named ( arg , int ) ] , str ] " ) ; assert_false ( less_or_equal order ~ left " : typing . Callable [ [ Variable ( str ) , Keywords ( int ) ] , str ] " ~ right " : typing . Callable [ [ Named ( arg , int ) ] , str ] " ) ; assert_false ( less_or_equal order ~ left " : typing . Callable [ [ Named ( arg , T_Unconstrained ) ] , T_Unconstrained ] " ~ right " : typing . Callable [ [ Named ( arg , int ) ] , str ] " ) ; assert_false ( less_or_equal order ~ left " : typing . Callable [ [ Named ( arg , T_Unconstrained ) ] , T_Unconstrained ] " ~ right " : typing . Callable [ [ Named ( arg , int ) ] , int ] " ) ; assert_false ( less_or_equal order ~ right " : typing . Callable [ [ Named ( arg , int ) ] , str ] " ~ left " : typing . Callable [ [ T_Unconstrained ] , T_Unconstrained ] " ) ; assert_false ( less_or_equal order ~ left " : typing . Callable [ [ Named ( arg , T_int_bool ) ] , T_int_bool ] " ~ right " : typing . Callable [ [ Named ( arg , int ) ] , int ] " ) ; assert_false ( less_or_equal order ~ left " : typing . Callable [ [ Named ( arg , T_int_bool ) ] , T_int_bool ] " ~ right " : typing . Callable [ [ Named ( arg , str ) ] , str ] " ) ; assert_true ( less_or_equal order ~ left " : typing . Callable [ [ object ] , object ] [ [ [ str ] , int ] [ [ int ] , str ] ] " ~ right " : typing . Callable [ [ object ] , object ] [ [ [ int ] , str ] ] " ) ; assert_false ( less_or_equal order ~ left " : typing . Callable [ [ object ] , object ] [ [ [ str ] , int ] [ [ int ] , str ] ] " ~ right " : typing . Callable [ [ object ] , object ] [ [ [ str ] , str ] ] " ) ; assert_true ( less_or_equal order ~ left " : typing_extensions . Literal [ ' a ' ] " ~ right " : typing_extensions . Literal [ ' a ' ] " ) ; assert_true ( less_or_equal order ~ left " : typing_extensions . Literal [ ' a ' ] " ~ right " : str " ) ; assert_false ( less_or_equal order ~ left " : str " ~ right " : typing_extensions . Literal [ ' a ' ] " ) ; assert_true ( less_or_equal order ~ left " : typing_extensions . Literal [ ' a ' ] " ~ right " : typing_extensions . Literal [ ' a ' , ' b ' ] " ) ; let parse_annotation = let aliases = function | " _T " -> Some ( Type . variable " _T " ) | _ -> None in let aliases = create_type_alias_table aliases in parse_callable ~ aliases in let is_protocol annotation ~ protocol_assumptions : _ = match annotation with | Type . Primitive " MatchesProtocol " | Type . Primitive " DoesNotMatchProtocol " | Type . Parametric { name = " B " ; _ } -> true | _ -> false in let attributes annotation = match annotation with | Type . Primitive " MatchesProtocol " -> Some ( parse_attributes ~ parse_annotation ~ class_name " : MatchesProtocol " [ " __call__ " , " typing . Callable [ [ int ] , str ] " ] ) | Type . Primitive " DoesNotMatchProtocol " -> Some ( parse_attributes ~ parse_annotation ~ class_name " : DoesNotMatchProtocol " [ " __call__ " , " typing . Callable [ [ str ] , int ] " ] ) | Type . Parametric { name = " B " ; _ } -> Some ( parse_attributes ~ parse_annotation ~ class_name " : B " [ " __call__ " , " typing . Callable [ [ _T ] , str ] " ] ) | Type . Callable _ -> Some ( make_attributes ~ class_name " : typing . Callable " [ " __call__ " , annotation ] ) | _ -> failwith " getting attributes for wrong class " in assert_true ( less_or_equal order ~ is_protocol ~ attributes ~ left " : typing . Callable [ [ int ] , str ] " ~ right " : MatchesProtocol " ) ; assert_false ( less_or_equal order ~ is_protocol ~ attributes ~ left " : typing . Callable [ [ int ] , str ] " ~ right " : DoesNotMatchProtocol " ) ; assert_true ( less_or_equal order ~ is_protocol ~ attributes ~ left " : typing . Callable [ [ int ] , str ] " ~ right " : B [ int ] " ) ; assert_false ( less_or_equal order ~ is_protocol ~ attributes ~ left " : typing . Callable [ [ int ] , str ] " ~ right " : B [ str ] " ) ; let assert_less_or_equal ( ? source = " " ) ~ left ~ right expected_result = let resolution = resolution ~ source context in let parse_annotation annotation = annotation |> parse_single_expression ~ preprocess : true |> GlobalResolution . parse_annotation resolution in let left , right = parse_annotation left , parse_annotation right in assert_equal ~ printer ( : Printf . sprintf " % B " ) expected_result ( GlobalResolution . less_or_equal resolution ~ left ~ right ) in assert_less_or_equal ~ source : { | from typing import Generic , TypeVar T1 = TypeVar ( " T1 " ) T2 = TypeVar ( " T2 " ) class GenericBase ( Generic [ T1 , T2 ] ) : pass class NonGenericChild ( GenericBase ) : pass } | ~ left " : test . NonGenericChild " ~ right " : test . GenericBase [ typing . Any , typing . Any ] " true ; assert_less_or_equal ~ source : { | from typing import Generic , TypeVar T1 = TypeVar ( " T1 " ) T2 = TypeVar ( " T2 " ) class GenericBase ( Generic [ T1 , T2 ] ) : pass class NonGenericChild ( GenericBase ) : pass } | ~ left " : test . NonGenericChild " ~ right " : test . GenericBase [ int , str ] " true ; assert_less_or_equal ~ source : { | from typing import Generic , TypeVar T1 = TypeVar ( " T1 " , contravariant = True ) T2 = TypeVar ( " T2 " , contravariant = True ) class GenericBase ( Generic [ T1 , T2 ] ) : pass class NonGenericChild ( GenericBase ) : pass } | ~ left " : test . GenericBase [ typing . Any , typing . Any ] " ~ right " : test . GenericBase [ int , str ] " true ; assert_less_or_equal ~ source : { | from typing import Generic , TypeVar T1 = TypeVar ( " T1 " , contravariant = True ) T2 = TypeVar ( " T2 " , contravariant = True ) class GenericBase ( Generic [ T1 , T2 ] ) : pass class NonGenericChild ( GenericBase ) : pass } | ~ left " : test . NonGenericChild " ~ right " : test . GenericBase [ int , str ] " true ; assert_less_or_equal ~ source : { | from typing import Generic , TypeVar T1 = TypeVar ( " T1 " ) T2 = TypeVar ( " T2 " ) class GenericBase ( Generic [ T1 , T2 ] ) : pass class NonGenericChild ( GenericBase ) : pass class Grandchild ( NonGenericChild ) : pass } | ~ left " : test . Grandchild " ~ right " : test . GenericBase [ typing . Any , typing . Any ] " true ; assert_less_or_equal ~ source : { | from mypy_extensions import TypedDict class Alpha ( TypedDict ) : foo : str bar : int baz : int class Beta ( TypedDict ) : foo : str bar : int } | ~ left " : test . Alpha " ~ right " : test . Beta " true ; assert_less_or_equal ~ source : { | from mypy_extensions import TypedDict class Alpha ( TypedDict , total = False ) : foo : str bar : int baz : int class Beta ( TypedDict , total = False ) : foo : str bar : int } | ~ left " : test . Alpha " ~ right " : test . Beta " true ; assert_less_or_equal ~ source : { | from mypy_extensions import TypedDict class Alpha ( TypedDict , total = False ) : foo : str bar : int baz : int class Beta ( TypedDict ) : foo : str bar : int } | ~ left " : test . Alpha " ~ right " : test . Beta " false ; assert_less_or_equal ~ source : { | from mypy_extensions import TypedDict class Alpha ( TypedDict ) : foo : str bar : int baz : int class Beta ( TypedDict , total = False ) : foo : str bar : int } | ~ left " : test . Alpha " ~ right " : test . Beta " false ; assert_less_or_equal ~ source : { | from mypy_extensions import TypedDict class Alpha ( TypedDict ) : foo : str bar : float class Beta ( TypedDict ) : foo : str bar : int } | ~ left " : test . Alpha " ~ right " : test . Beta " false ; assert_less_or_equal ~ source : { | from mypy_extensions import TypedDict class Alpha ( TypedDict ) : bar : int foo : str class Beta ( TypedDict ) : foo : str bar : int } | ~ left " : test . Alpha " ~ right " : test . Beta " true ; assert_less_or_equal ~ source : { | from mypy_extensions import TypedDict class Alpha ( TypedDict ) : bar : int foo : int } | ~ left " : test . Alpha " ~ right " : typing . Mapping [ str , typing . Any ] " true ; assert_less_or_equal ~ source : { | from mypy_extensions import TypedDict class Alpha ( TypedDict , total = False ) : bar : int foo : int } | ~ left " : test . Alpha " ~ right " : typing . Mapping [ str , typing . Any ] " true ; assert_less_or_equal ~ source : { | from mypy_extensions import TypedDict class Alpha ( TypedDict ) : bar : int foo : int } | ~ left " : test . Alpha " ~ right " : typing . Mapping [ str , int ] " false ; assert_less_or_equal ~ source : { | from mypy_extensions import TypedDict class Alpha ( TypedDict ) : bar : int foo : int } | ~ left " : typing . Mapping [ str , typing . Any ] " ~ right " : test . Alpha " false ; assert_less_or_equal ~ source : { | from mypy_extensions import TypedDict class Alpha ( TypedDict ) : bar : int foo : int } | ~ left " : test . Alpha " ~ right " : dict [ str , typing . Any ] " false ; assert_less_or_equal ~ source : { | from mypy_extensions import TypedDict class Base ( TypedDict ) : foo : str class BarNotRequired ( Base , total = False ) : bar : int class BarRequired ( Base ) : bar : int } | ~ left " : test . BarNotRequired " ~ right " : test . BarRequired " false ; assert_less_or_equal ~ source : { | from mypy_extensions import TypedDict class Base ( TypedDict ) : foo : str class BarNotRequired ( Base , total = False ) : bar : int class BarRequired ( Base ) : bar : int } | ~ left " : test . BarRequired " ~ right " : test . BarNotRequired " false ; assert_less_or_equal ~ source : { | from mypy_extensions import TypedDict class Base ( TypedDict ) : foo : str class BarNotRequired ( Base , total = False ) : bar : int class BarNotPresent ( TypedDict ) : foo : str } | ~ left " : test . BarNotRequired " ~ right " : test . BarNotPresent " true ; assert_less_or_equal ~ source : { | from mypy_extensions import TypedDict class Base ( TypedDict ) : foo : str class BarRequired ( Base ) : bar : int class BarNotPresent ( TypedDict ) : foo : str } | ~ left " : test . BarRequired " ~ right " : test . BarNotPresent " true ; ( )
|
let test_is_compatible_with _ = let assert_is_compatible ( ? order = default ) left right = assert_true ( is_compatible_with order ~ left ~ right ) in let assert_not_compatible ( ? order = default ) left right = assert_false ( is_compatible_with order ~ left ~ right ) in let list_of_integer = Type . list Type . integer in let list_of_float = Type . list Type . float in let list_of_string = Type . list Type . string in let list_of_top = Type . list Type . Top in let list_of_any = Type . list Type . Any in let iterable_of_integer = Type . iterable Type . integer in let iterable_of_any = Type . iterable Type . Any in assert_is_compatible list_of_integer Type . Any ; assert_is_compatible Type . Any list_of_integer ; assert_is_compatible Type . none Type . Any ; assert_is_compatible Type . Any Type . none ; assert_is_compatible ( Type . Primitive " A " ) Type . Any ; assert_is_compatible Type . Any ( Type . Primitive " A " ) ; assert_is_compatible Type . Top Type . Any ; assert_is_compatible Type . Any Type . Top ; assert_is_compatible list_of_integer list_of_any ; assert_is_compatible list_of_any list_of_integer ; assert_is_compatible Type . Any Type . Any ; assert_is_compatible list_of_integer Type . Top ; assert_not_compatible Type . Top list_of_integer ; assert_is_compatible Type . none Type . Top ; assert_not_compatible Type . Top Type . none ; assert_is_compatible ( Type . Primitive " A " ) Type . Top ; assert_not_compatible Type . Top ( Type . Primitive " A " ) ; assert_is_compatible list_of_integer list_of_top ; assert_not_compatible list_of_top list_of_integer ; assert_is_compatible Type . Top Type . Top ; assert_is_compatible list_of_integer list_of_integer ; assert_is_compatible list_of_integer list_of_float ; assert_not_compatible list_of_float list_of_integer ; assert_not_compatible list_of_integer list_of_string ; assert_is_compatible Type . none ( Type . optional Type . Any ) ; assert_is_compatible ( Type . optional Type . Any ) Type . none ; assert_is_compatible Type . none ( Type . optional Type . Top ) ; assert_not_compatible ( Type . optional Type . Top ) Type . none ; assert_is_compatible list_of_integer ( Type . optional list_of_integer ) ; assert_is_compatible ( Type . optional list_of_integer ) ( Type . optional list_of_integer ) ; assert_is_compatible list_of_integer ( Type . optional list_of_float ) ; assert_is_compatible ( Type . optional list_of_integer ) ( Type . optional list_of_float ) ; assert_not_compatible list_of_float ( Type . optional list_of_integer ) ; assert_not_compatible list_of_integer ( Type . optional list_of_string ) ; assert_is_compatible ( Type . tuple [ list_of_integer ; list_of_string ] ) ( Type . tuple [ list_of_integer ; list_of_string ] ) ; assert_is_compatible ( Type . tuple [ list_of_integer ; list_of_string ] ) ( Type . tuple [ list_of_float ; list_of_string ] ) ; assert_is_compatible ( Type . tuple [ list_of_string ; list_of_integer ] ) ( Type . tuple [ list_of_string ; list_of_float ] ) ; assert_is_compatible ( Type . tuple [ list_of_integer ; list_of_integer ] ) ( Type . tuple [ list_of_float ; list_of_float ] ) ; assert_is_compatible ( Type . tuple [ list_of_integer ; list_of_integer ] ) ( Type . Tuple ( Type . OrderedTypes . create_unbounded_concatenation list_of_integer ) ) ; assert_is_compatible ( Type . tuple [ list_of_integer ; list_of_integer ] ) ( Type . Tuple ( Type . OrderedTypes . create_unbounded_concatenation list_of_float ) ) ; assert_not_compatible ( Type . tuple [ list_of_integer ; list_of_string ] ) ( Type . tuple [ list_of_string ; list_of_string ] ) ; assert_not_compatible ( Type . tuple [ list_of_float ; list_of_integer ] ) ( Type . tuple [ list_of_integer ; list_of_float ] ) ; assert_not_compatible ( Type . tuple [ list_of_string ; list_of_integer ] ) ( Type . Tuple ( Type . OrderedTypes . create_unbounded_concatenation list_of_float ) ) ; assert_is_compatible list_of_integer ( Type . union [ list_of_integer ] ) ; assert_is_compatible list_of_integer ( Type . union [ list_of_float ] ) ; assert_is_compatible list_of_float ( Type . union [ list_of_float ; list_of_integer ] ) ; assert_is_compatible list_of_string ( Type . union [ list_of_float ; list_of_string ] ) ; assert_is_compatible list_of_string ( Type . union [ list_of_float ; Type . optional list_of_string ] ) ; assert_not_compatible list_of_string ( Type . union [ list_of_float ; list_of_integer ] ) ; assert_is_compatible ( Type . union [ Type . integer ; Type . string ] ) ( Type . union [ Type . integer ; Type . string ] ) ; assert_is_compatible ( Type . union [ Type . integer ; Type . string ] ) ( Type . union [ Type . string ; Type . integer ] ) ; assert_is_compatible ( Type . set ( Type . union [ Type . integer ; Type . string ] ) ) ( Type . set ( Type . union [ Type . string ; Type . integer ] ) ) ; assert_is_compatible ( Type . union [ Type . integer ; list_of_integer ] ) ( Type . union [ Type . integer ; Type . integer ; list_of_integer ] ) ; assert_not_compatible ( Type . union [ Type . integer ; Type . string ] ) Type . integer ; assert_not_compatible ( Type . union [ Type . integer ; Type . string ; list_of_float ] ) ( Type . union [ Type . integer ; list_of_float ] ) ; assert_not_compatible ( Type . set ( Type . union [ Type . integer ; Type . string ] ) ) ( Type . set ( Type . union [ list_of_string ; list_of_integer ] ) ) ; assert_is_compatible ( Type . dictionary ~ key : list_of_integer ~ value : list_of_string ) ( Type . dictionary ~ key : list_of_integer ~ value : list_of_string ) ; assert_is_compatible ( Type . dictionary ~ key : list_of_integer ~ value : list_of_string ) ( Type . dictionary ~ key : list_of_float ~ value : list_of_string ) ; assert_is_compatible ( Type . dictionary ~ key : list_of_string ~ value : list_of_integer ) ( Type . dictionary ~ key : list_of_string ~ value : list_of_float ) ; assert_is_compatible ( Type . dictionary ~ key : list_of_integer ~ value : list_of_integer ) ( Type . dictionary ~ key : list_of_float ~ value : list_of_float ) ; assert_not_compatible ( Type . dictionary ~ key : list_of_integer ~ value : list_of_integer ) ( Type . dictionary ~ key : list_of_string ~ value : list_of_integer ) ; assert_not_compatible ( Type . dictionary ~ key : list_of_string ~ value : list_of_string ) ( Type . dictionary ~ key : list_of_string ~ value : list_of_integer ) ; assert_not_compatible ( Type . dictionary ~ key : list_of_string ~ value : list_of_integer ) list_of_string ; assert_not_compatible list_of_string ( Type . dictionary ~ key : list_of_string ~ value : list_of_integer ) ; assert_is_compatible ( Type . dictionary ~ key : list_of_integer ~ value : list_of_string ) ( Type . dictionary ~ key : list_of_any ~ value : list_of_any ) ; assert_is_compatible ( Type . dictionary ~ key : list_of_any ~ value : list_of_any ) ( Type . dictionary ~ key : list_of_integer ~ value : list_of_string ) ; assert_is_compatible ( Type . dictionary ~ key : list_of_integer ~ value : list_of_string ) ( Type . dictionary ~ key : list_of_top ~ value : list_of_top ) ; assert_not_compatible ( Type . dictionary ~ key : list_of_top ~ value : list_of_top ) ( Type . dictionary ~ key : list_of_integer ~ value : list_of_string ) ; assert_is_compatible list_of_integer iterable_of_integer ; assert_not_compatible list_of_string iterable_of_integer ; assert_not_compatible iterable_of_integer list_of_integer ; assert_is_compatible list_of_any iterable_of_any ; assert_not_compatible iterable_of_any list_of_any ; assert_is_compatible list_of_any iterable_of_integer ; assert_is_compatible list_of_integer iterable_of_any ; ( )
|
let test_less_or_equal_variance _ = let assert_strict_less ~ order ~ right ~ left = assert_equal ~ printer ( : fun pair -> Format . sprintf " % B , % B " ( fst pair ) ( snd pair ) ) ( true , false ) ( less_or_equal order ~ left ~ right , less_or_equal order ~ left : right ~ right : left ) in assert_false ( less_or_equal variance_order ~ left ( : Type . parametric " LinkedList " [ ! Type . integer ] ) ~ right ( : Type . parametric " LinkedList " [ ! Type . float ] ) ) ; assert_false ( less_or_equal variance_order ~ left ( : Type . parametric " LinkedList " [ ! Type . float ] ) ~ right ( : Type . parametric " LinkedList " [ ! Type . integer ] ) ) ; assert_true ( less_or_equal variance_order ~ left ( : Type . parametric " LinkedList " [ ! Type . integer ] ) ~ right ( : Type . parametric " LinkedList " [ ! Type . Any ] ) ) ; assert_true ( less_or_equal variance_order ~ left ( : Type . parametric " LinkedList " [ ! Type . Any ] ) ~ right ( : Type . parametric " LinkedList " [ ! Type . integer ] ) ) ; assert_strict_less ~ order : variance_order ~ left ( : Type . parametric " LinkedList " [ ! Type . integer ] ) ~ right ( : Type . parametric " LinkedList " [ ! Type . Top ] ) ; assert_strict_less ~ order : variance_order ~ left ( : Type . parametric " Box " [ ! Type . integer ] ) ~ right ( : Type . parametric " Box " [ ! Type . float ] ) ; assert_true ( less_or_equal variance_order ~ left ( : Type . parametric " Box " [ ! Type . integer ] ) ~ right ( : Type . parametric " Box " [ ! Type . Any ] ) ) ; assert_strict_less ~ order : variance_order ~ left ( : Type . parametric " Sink " [ ! Type . float ] ) ~ right ( : Type . parametric " Sink " [ ! Type . integer ] ) ; assert_true ( less_or_equal variance_order ~ left ( : Type . parametric " Sink " [ ! Type . Any ] ) ~ right ( : Type . parametric " Sink " [ ! Type . integer ] ) ) ; let _obsolete_tests ( ) = assert_strict_less ~ order : variance_order ~ left ( : Type . parametric " Derived " [ ! Type . integer ] ) ~ right ( : Type . parametric " Derived " [ ! Type . float ] ) ; assert_strict_less ~ order : variance_order ~ left ( : Type . parametric " Derived " [ ! Type . integer ] ) ~ right ( : Type . parametric " Base " [ ! Type . integer ] ) ; assert_strict_less ~ order : variance_order ~ left ( : Type . parametric " Derived " [ ! Type . float ] ) ~ right ( : Type . parametric " Base " [ ! Type . float ] ) ; assert_strict_less ~ order : variance_order ~ left ( : Type . parametric " Base " [ ! Type . float ] ) ~ right ( : Type . parametric " Base " [ ! Type . integer ] ) ; assert_strict_less ~ order : variance_order ~ left ( : Type . parametric " Derived " [ ! Type . integer ] ) ~ right ( : Type . parametric " Base " [ ! Type . float ] ) ; assert_strict_less ~ order : variance_order ~ left ( : Type . parametric " Derived " [ ! Type . float ] ) ~ right ( : Type . parametric " Base " [ ! Type . integer ] ) ; assert_strict_less ~ order : multiplane_variance_order ~ left ( : Type . parametric " A " [ ! Type . integer ; Type . float ] ) ~ right ( : Type . parametric " A " [ ! Type . float ; Type . integer ] ) ; assert_strict_less ~ order : multiplane_variance_order ~ left ( : Type . parametric " B " [ ! Type . float ; Type . integer ] ) ~ right ( : Type . parametric " B " [ ! Type . integer ; Type . float ] ) ; assert_strict_less ~ order : multiplane_variance_order ~ left ( : Type . parametric " B " [ ! Type . integer ; Type . integer ] ) ~ right ( : Type . parametric " A " [ ! Type . integer ; Type . integer ] ) ; assert_strict_less ~ order : multiplane_variance_order ~ left ( : Type . parametric " B " [ ! Type . integer ; Type . integer ] ) ~ right ( : Type . parametric " A " [ ! Type . integer ; Type . float ] ) ; assert_strict_less ~ order : multiplane_variance_order ~ left ( : Type . parametric " B " [ ! Type . integer ; Type . integer ] ) ~ right ( : Type . parametric " A " [ ! Type . float ; Type . float ] ) ; assert_strict_less ~ order : multiplane_variance_order ~ left ( : Type . parametric " B " [ ! Type . float ; Type . float ] ) ~ right ( : Type . parametric " A " [ ! Type . integer ; Type . integer ] ) ; assert_strict_less ~ order : multiplane_variance_order ~ left ( : Type . parametric " B " [ ! Type . float ; Type . float ] ) ~ right ( : Type . parametric " A " [ ! Type . integer ; Type . float ] ) ; assert_strict_less ~ order : multiplane_variance_order ~ left ( : Type . parametric " B " [ ! Type . float ; Type . float ] ) ~ right ( : Type . parametric " A " [ ! Type . float ; Type . float ] ) ; assert_strict_less ~ order : multiplane_variance_order ~ left ( : Type . parametric " B " [ ! Type . float ; Type . integer ] ) ~ right ( : Type . parametric " A " [ ! Type . integer ; Type . integer ] ) ; assert_strict_less ~ order : multiplane_variance_order ~ left ( : Type . parametric " B " [ ! Type . float ; Type . integer ] ) ~ right ( : Type . parametric " A " [ ! Type . integer ; Type . float ] ) ; assert_strict_less ~ order : multiplane_variance_order ~ left ( : Type . parametric " B " [ ! Type . float ; Type . integer ] ) ~ right ( : Type . parametric " A " [ ! Type . float ; Type . float ] ) ; assert_strict_less ~ order : multiplane_variance_order ~ left ( : Type . parametric " B " [ ! Type . float ; Type . integer ] ) ~ right ( : Type . parametric " A " [ ! Type . float ; Type . integer ] ) ; assert_strict_less ~ order : multiplane_variance_order ~ left ( : Type . parametric " C " [ ] ) ! ~ right ( : Type . parametric " A " [ ! Type . float ; Type . float ] ) ; assert_strict_less ~ order : multiplane_variance_order ~ left " :!! C " ~ right ( : Type . parametric " A " [ ! Type . float ; Type . float ] ) ; assert_strict_less ~ order : multiplane_variance_order ~ left ( : Type . parametric " D " [ ] ) ! ~ right ( : Type . parametric " A " [ ! Type . integer ; Type . integer ] ) ; assert_strict_less ~ order : multiplane_variance_order ~ left " :!! D " ~ right ( : Type . parametric " A " [ ! Type . integer ; Type . integer ] ) ; assert_false ( less_or_equal parallel_planes_variance_order ~ left ( : Type . parametric " C " [ ] ) ! ~ right ( : Type . parametric " A " [ ! Type . float ; Type . float ] ) ) ; assert_false ( less_or_equal parallel_planes_variance_order ~ left " :!! C " ~ right ( : Type . parametric " A " [ ! Type . float ; Type . float ] ) ) ; assert_false ( less_or_equal parallel_planes_variance_order ~ left ( : Type . parametric " D " [ ] ) ! ~ right ( : Type . parametric " A " [ ! Type . integer ; Type . integer ] ) ) ; assert_false ( less_or_equal parallel_planes_variance_order ~ left " :!! D " ~ right ( : Type . parametric " A " [ ! Type . integer ; Type . integer ] ) ) in ( )
|
let test_join context = let assert_join ( ? order = default ) ( ? aliases = Type . empty_aliases ) left right expected = let parse_annotation = function | " $ bottom " -> Type . Bottom | _ as source -> parse_single_expression source |> Type . create ~ aliases in let attributes annotation ~ assumptions : _ = let parse_annotation = let aliases = function | " _T " -> Some ( Type . variable " _T " ) | _ -> None in let aliases = create_type_alias_table aliases in parse_callable ~ aliases in match annotation with | Type . Primitive " CallableClass " -> Some ( parse_attributes ~ parse_annotation ~ class_name " : MatchesProtocol " [ " __call__ " , " typing . Callable [ [ int ] , str ] " ] ) | Type . Parametric { name = " ParametricCallableToStr " ; parameters = [ Single ( Primitive parameter ) ] } -> let callable = Format . sprintf " typing . Callable [ [ % s ] , str ] " parameter in Some ( parse_attributes ~ parse_annotation ~ class_name " : MatchesProtocol " [ " __call__ " , callable ] ) | annotation -> failwith ( " getting attributes for wrong class " ^ Type . show annotation ) in assert_type_equal ( parse_annotation expected ) ( join ~ attributes order ( parse_annotation left ) ( parse_annotation right ) ) ; assert_type_equal ( parse_annotation expected ) ( join ~ attributes order ( parse_annotation right ) ( parse_annotation left ) ) in assert_join " list " " typing . Sized " " typing . Sized " ; assert_join " typing . Sized " " list " " typing . Sized " ; assert_join " typing . List [ int ] " " typing . Sized " " typing . Sized " ; assert_join " int " " str " " typing . Union [ int , str ] " ; assert_join " typing . List [ float ] " " typing . List [ float ] " " typing . List [ float ] " ; assert_join " typing . List [ float ] " " typing . List [ int ] " " typing . Union [ typing . List [ float ] , typing . List [ int ] ] " ; assert_join " typing . List [ int ] " " typing . Iterator [ int ] " " typing . Iterator [ int ] " ; assert_join " typing . Iterator [ int ] " " typing . List [ int ] " " typing . Iterator [ int ] " ; assert_join " typing . List [ float ] " " typing . Iterator [ int ] " " typing . Iterator [ float ] " ; assert_join " typing . List [ float ] " " float [ int ] " " typing . Union [ typing . List [ float ] , float [ int ] ] " ; assert_join " typing . Annotated [ int ] " " float " " typing . Annotated [ float ] " ; assert_join " typing . Annotated [ int ] " " typing . Annotated [ float ] " " typing . Annotated [ float ] " ; assert_join " typing_extensions . Annotated [ int ] " " float " " typing_extensions . Annotated [ float ] " ; assert_join " typing_extensions . Annotated [ int ] " " typing_extensions . Annotated [ float ] " " typing_extensions . Annotated [ float ] " ; assert_join " typing . Tuple [ int , int ] " " typing . Iterator [ int ] " " typing . Iterator [ int ] " ; let variadic = Type . Variable . Variadic . Tuple . create " Ts " in assert_join ~ aliases ( : fun ? replace_unbound_parameters_with_any : _ name -> match name with | " Ts " -> Some ( Type . VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | _ -> None ) " typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] " " typing . Tuple [ int , . . . ] " " typing . Tuple [ object , . . . ] " ; assert_join " str " " typing . Optional [ str ] " " typing . Optional [ str ] " ; assert_join " typing . List [ int ] " " typing . List [ typing . Any ] " " typing . List [ typing . Any ] " ; assert_join " typing . List [ typing . Any ] " " typing . List [ int ] " " typing . List [ typing . Any ] " ; assert_join " typing . Optional [ bool ] " " typing . Union [ int , typing . Optional [ bool ] ] " " typing . Union [ int , typing . Optional [ bool ] ] " ; assert_join " typing . Union [ int , str ] " " typing . Union [ int , bytes ] " " typing . Union [ int , str , bytes ] " ; assert_join " typing . Union [ int , str ] " " None " " typing . Union [ int , str , None ] " ; assert_join " typing . Dict [ str , str ] " " typing . Dict [ str , typing . List [ str ] ] " " typing . Union [ typing . Dict [ str , typing . List [ str ] ] , typing . Dict [ str , str ] ] " ; assert_join " typing . Union [ typing . List [ int ] , typing . Set [ int ] ] " " typing . Sized " " typing . Sized " ; assert_join " typing . Tuple [ int , . . . ] " " typing . Iterable [ int ] " " typing . Iterable [ int ] " ; assert_join " typing . Tuple [ str , . . . ] " " typing . Iterator [ str ] " " typing . Iterator [ str ] " ; assert_join " typing . Tuple [ int , . . . ] " " typing . Iterable [ str ] " " typing . Iterable [ typing . Union [ int , str ] ] " ; assert_join " typing . Optional [ float ] " " typing . Union [ float , int ] " " typing . Optional [ typing . Union [ float , int ] ] " ; assert_join " typing . List [ typing . Any ] " " typing . Union [ typing . List [ int ] , typing . List [ str ] ] " " typing . List [ typing . Any ] " ; assert_join " typing . Tuple [ int , int ] " " typing . Tuple [ int , int , str ] " " typing . Union [ typing . Tuple [ int , int ] , typing . Tuple [ int , int , str ] ] " ; let order = let order = MockClassHierarchyHandler . create ( ) in let open MockClassHierarchyHandler in insert order " object " ; insert order " str " ; insert order " int " ; insert order " float " ; insert order " A " ; insert order " B " ; insert order " C " ; insert order " CallableClass " ; insert order " ParametricCallableToStr " ; insert order " typing . Callable " ; insert order " typing . Generic " ; concrete_connect order ~ predecessor " : int " ~ successor " : float " ; concrete_connect order ~ predecessor " : float " ~ successor " : object " ; concrete_connect order ~ predecessor " : A " ~ successor " : B " ~ parameters [ : Type . tuple [ Type . variable " _1 " ; Type . variable " _2 " ] ] ; concrete_connect order ~ predecessor " : A " ~ successor " : typing . Generic " ~ parameters [ : Type . variable " _1 " ; Type . variable " _2 " ] ; concrete_connect order ~ predecessor " : B " ~ successor " : typing . Generic " ~ parameters [ : Type . variable " _T " ] ; concrete_connect order ~ predecessor " : B " ~ successor " : C " ~ parameters [ : Type . union [ Type . variable " _T " ; Type . float ] ] ; concrete_connect order ~ predecessor " : C " ~ successor " : typing . Generic " ~ parameters [ : Type . variable " _T " ] ; concrete_connect order ~ parameters [ : Type . variable " _T " ] ~ predecessor " : ParametricCallableToStr " ~ successor " : typing . Generic " ; handler order in assert_join ~ order : disconnected_order " A " " B " " typing . Union [ A , B ] " ; assert_join " typing . Type [ int ] " " typing . Type [ str ] " " typing . Type [ typing . Union [ int , str ] ] " ; assert_join " typing . Callable [ . . . , int ] " " typing . Callable [ . . . , str ] " " typing . Callable [ . . . , typing . Union [ int , str ] ] " ; assert_join " typing . Callable ( ' derp ' ) [ . . . , int ] " " typing . Callable ( ' derp ' ) [ . . . , int ] " " typing . Callable ( ' derp ' ) [ . . . , int ] " ; assert_join " typing . Callable ( ' derp ' ) [ . . . , int ] " " typing . Callable ( ' other ' ) [ . . . , int ] " " typing . Union [ typing . Callable ( derp ) [ . . . , int ] , typing . Callable ( other ) [ . . . , int ] ] " ; assert_join " typing . Callable [ . . . , int ] [ [ . . . , str ] ] " " typing . Callable [ . . . , int ] " " typing . Union [ typing . Callable [ . . . , int ] [ [ . . . , str ] ] , typing . Callable [ . . . , int ] ] " ; assert_join " typing . Callable [ [ Named ( a , int ) , Named ( b , str ) ] , int ] " " typing . Callable [ [ Named ( a , int ) , Named ( b , str ) ] , int ] " " typing . Callable [ [ Named ( a , int ) , Named ( b , str ) ] , int ] " ; assert_join " typing . Callable [ [ Named ( a , int ) ] , int ] " " typing . Callable [ [ int ] , int ] " " typing . Callable [ [ int ] , int ] " ; assert_join " typing . Callable [ [ int ] , int ] " " typing . Callable [ [ Named ( a , int ) ] , int ] " " typing . Callable [ [ int ] , int ] " ; assert_join " typing . Callable [ [ Named ( b , int ) ] , int ] " " typing . Callable [ [ Named ( a , int ) ] , int ] " " typing . Union [ typing . Callable [ [ Named ( b , int ) ] , int ] , typing . Callable [ [ Named ( a , int ) ] , int ] ] " ; assert_join " typing . Callable [ . . . , typing . Any ] " " typing . Callable [ [ int ] , int ] " " typing . Callable [ [ int ] , typing . Any ] " ; assert_join ~ order " CallableClass " " typing . Callable [ [ int ] , str ] " " typing . Callable [ [ int ] , str ] " ; assert_join ~ order " typing . Callable [ [ int ] , str ] " " CallableClass " " typing . Callable [ [ int ] , str ] " ; assert_join ~ order " typing . Callable [ [ int ] , int ] " " CallableClass " " typing . Callable [ [ int ] , typing . Union [ int , str ] ] " ; assert_join ~ order " ParametricCallableToStr [ int ] " " typing . Callable [ [ int ] , str ] " " typing . Callable [ [ int ] , str ] " ; assert_join ~ order " typing . Callable [ [ int ] , str ] " " ParametricCallableToStr [ int ] " " typing . Callable [ [ int ] , str ] " ; assert_join ~ order " typing . Callable [ [ int ] , int ] " " ParametricCallableToStr [ int ] " " typing . Callable [ [ int ] , typing . Union [ int , str ] ] " ; assert_join ~ order " ParametricCallableToStr [ int ] " " typing . Callable [ [ int ] , str ] " " typing . Callable [ [ int ] , str ] " ; assert_join ~ order " typing . Callable [ [ float ] , str ] " " ParametricCallableToStr [ int ] " " typing . Callable [ [ int ] , str ] " ; assert_join ~ order " typing . Callable [ [ int ] , str ] " " ParametricCallableToStr [ float ] " " typing . Callable [ [ int ] , str ] " ; assert_join ~ order " typing . Callable [ [ int ] , int ] " " ParametricCallableToStr [ int ] " " typing . Callable [ [ int ] , typing . Union [ int , str ] ] " ; assert_type_equal ( join order Type . integer ( Type . variable " T " ) ) ( Type . union [ Type . integer ; Type . variable " T " ] ) ; assert_type_equal ( join order Type . integer ( Type . variable ~ constraints ( : Type . Variable . Bound Type . string ) " T " ) ) ( Type . union [ Type . integer ; Type . variable ~ constraints ( : Type . Variable . Bound Type . string ) " T " ] ) ; assert_type_equal ( join order Type . string ( Type . variable ~ constraints ( : Type . Variable . Explicit [ Type . float ; Type . integer ] ) " T " ) ) ( Type . union [ Type . string ; Type . variable ~ constraints ( : Type . Variable . Explicit [ Type . float ; Type . integer ] ) " T " ; ] ) ; assert_type_equal ( join order Type . string ( Type . variable ~ constraints : Type . Variable . LiteralIntegers " T " ) ) ( Type . union [ Type . string ; Type . variable ~ constraints : Type . Variable . LiteralIntegers " T " ] ) ; assert_type_equal ( join order ( Type . literal_integer 7 ) ( Type . variable ~ constraints : Type . Variable . LiteralIntegers " T " ) ) ( Type . union [ Type . literal_integer 7 ; Type . variable ~ constraints : Type . Variable . LiteralIntegers " T " ] ) ; assert_type_equal ( join variance_order ( Type . parametric " LinkedList " [ ! Type . integer ] ) ( Type . parametric " LinkedList " [ ! Type . Top ] ) ) ( Type . parametric " LinkedList " [ ! Type . Top ] ) ; assert_type_equal ( join variance_order ( Type . parametric " LinkedList " [ ! Type . Top ] ) ( Type . parametric " LinkedList " [ ! Type . integer ] ) ) ( Type . parametric " LinkedList " [ ! Type . Top ] ) ; assert_type_equal ( join variance_order ( Type . parametric " LinkedList " [ ! Type . Bottom ] ) ( Type . parametric " LinkedList " [ ! Type . Top ] ) ) ( Type . parametric " LinkedList " [ ! Type . Top ] ) ; assert_type_equal ( join variance_order ( Type . parametric " LinkedList " [ ! Type . Top ] ) ( Type . parametric " LinkedList " [ ! Type . Bottom ] ) ) ( Type . parametric " LinkedList " [ ! Type . Top ] ) ; assert_type_equal ( join variance_order ( Type . parametric " LinkedList " [ ! Type . Any ] ) ( Type . parametric " LinkedList " [ ! Type . Top ] ) ) ( Type . parametric " LinkedList " [ ! Type . Top ] ) ; assert_type_equal ( join variance_order ( Type . parametric " LinkedList " [ ! Type . Top ] ) ( Type . parametric " LinkedList " [ ! Type . Any ] ) ) ( Type . parametric " LinkedList " [ ! Type . Top ] ) ; assert_type_equal ( join variance_order ( Type . parametric " LinkedList " [ ! Type . Top ] ) ( Type . parametric " LinkedList " [ ! Type . Top ] ) ) ( Type . parametric " LinkedList " [ ! Type . Top ] ) ; assert_type_equal ( join variance_order ( Type . parametric " Map " [ ! Type . integer ; Type . integer ] ) ( Type . parametric " Map " [ ! Type . Top ; Type . Top ] ) ) ( Type . parametric " Map " [ ! Type . Top ; Type . Top ] ) ; assert_type_equal ( join variance_order ( Type . parametric " Map " [ ! Type . integer ; Type . integer ] ) ( Type . parametric " Map " [ ! Type . Top ; Type . integer ] ) ) ( Type . parametric " Map " [ ! Type . Top ; Type . integer ] ) ; assert_type_equal ( join variance_order ( Type . parametric " Map " [ ! Type . integer ; Type . integer ] ) ( Type . parametric " Map " [ ! Type . Top ; Type . string ] ) ) ( Type . union [ Type . parametric " Map " [ ! Type . integer ; Type . integer ] ; Type . parametric " Map " [ ! Type . Top ; Type . string ] ; ] ) ; assert_type_equal ( join variance_order ( Type . parametric " LinkedList " [ ! Type . integer ] ) ( Type . parametric " LinkedList " [ ! Type . Any ] ) ) ( Type . parametric " LinkedList " [ ! Type . Any ] ) ; assert_type_equal ( join variance_order ( Type . parametric " LinkedList " [ ! Type . Any ] ) ( Type . parametric " LinkedList " [ ! Type . integer ] ) ) ( Type . parametric " LinkedList " [ ! Type . Any ] ) ; assert_type_equal ( join variance_order ( Type . parametric " Sink " [ ! Type . integer ] ) ( Type . parametric " Sink " [ ! Type . string ] ) ) ( Type . Union [ Type . parametric " Sink " [ ! Type . integer ] ; Type . parametric " Sink " [ ! Type . string ] ] ) ; let variance_aliases = Identifier . Table . of_alist_exn [ " _T " , Type . variable " _T " ; " _T_co " , Type . variable " _T_co " ~ variance : Covariant ; " _T_contra " , Type . variable " _T_contra " ~ variance : Contravariant ; ] |> Identifier . Table . find in let _obsolete_variance_tests ( ) = let variance_aliases = create_type_alias_table variance_aliases in assert_join ~ order : variance_order ~ aliases : variance_aliases " Derived [ int ] " " Base [ int ] " " Base [ int ] " ; assert_join ~ order : variance_order ~ aliases : variance_aliases " Derived [ float ] " " Base [ float ] " " Base [ float ] " ; assert_join ~ order : variance_order ~ aliases : variance_aliases " Derived [ int ] " " Base [ float ] " " Base [ float ] " ; assert_join ~ order : variance_order ~ aliases : variance_aliases " Derived [ float ] " " Base [ int ] " " Base [ int ] " ; assert_join ~ order : multiplane_variance_order ~ aliases : variance_aliases " B [ int , float ] " " A [ int , float ] " " A [ int , float ] " ; assert_join ~ order : multiplane_variance_order ~ aliases : variance_aliases " B [ int , int ] " " A [ int , float ] " " A [ int , float ] " ; assert_join ~ order : multiplane_variance_order ~ aliases : variance_aliases " B [ float , int ] " " A [ int , float ] " " A [ int , float ] " ; assert_join ~ order : multiplane_variance_order ~ aliases : variance_aliases " B [ float , float ] " " A [ int , float ] " " A [ int , float ] " ; assert_join ~ order : multiplane_variance_order ~ aliases : variance_aliases " B [ int , float ] " " A [ float , float ] " " A [ float , float ] " ; assert_join ~ order : multiplane_variance_order ~ aliases : variance_aliases " B [ int , int ] " " A [ float , float ] " " A [ float , float ] " ; assert_join ~ order : multiplane_variance_order ~ aliases : variance_aliases " B [ float , int ] " " A [ float , float ] " " A [ float , float ] " ; assert_join ~ order : multiplane_variance_order ~ aliases : variance_aliases " B [ float , float ] " " A [ float , float ] " " A [ float , float ] " ; assert_join ~ order : parallel_planes_variance_order ~ aliases : variance_aliases " B [ float , float ] " " A [ int , float ] " " A [ float , float ] " ; assert_join ~ order : parallel_planes_variance_order ~ aliases : variance_aliases " B [ float , float ] " " A [ int , int ] " " A [ float , int ] " in assert_type_equal ( join order ( Type . literal_string " A " ) ( Type . literal_string " A " ) ) ( Type . literal_string " A " ) ; assert_type_equal ( join order ( Type . literal_string " A " ) ( Type . literal_string " B " ) ) ( Type . Literal ( String AnyLiteral ) ) ; assert_type_equal ( join order ( Type . literal_string " A " ) Type . string ) Type . string ; assert_type_equal ( join order ( Type . literal_string " A " ) Type . integer ) ( Type . union [ Type . string ; Type . integer ] ) ; assert_type_equal ( join order ( Type . Literal ( String AnyLiteral ) ) ( Type . Literal ( String AnyLiteral ) ) ) ( Type . Literal ( String AnyLiteral ) ) ; assert_type_equal ( join order ( Type . Literal ( String AnyLiteral ) ) ( Type . literal_string " hello " ) ) ( Type . Literal ( String AnyLiteral ) ) ; assert_type_equal ( join order ( Type . Literal ( String AnyLiteral ) ) Type . string ) Type . string ; let assert_join ( ? source = " " ) ~ left ~ right expected_result = let resolution = resolution ~ source context in let parse_annotation annotation = annotation |> parse_single_expression |> GlobalResolution . parse_annotation resolution in let left , right = parse_annotation left , parse_annotation right in assert_type_equal ( parse_annotation expected_result ) ( GlobalResolution . join resolution left right ) in let assert_join_direct ( ? source = " " ) ~ left ~ right expected_annotation = let resolution = resolution ~ source context in let parse_annotation annotation = annotation |> parse_single_expression |> GlobalResolution . parse_annotation resolution in let left , right = parse_annotation left , parse_annotation right in assert_type_equal expected_annotation ( GlobalResolution . join resolution left right ) in assert_join ~ source : { | from typing import Generic , TypeVar T1 = TypeVar ( " T1 " , covariant = True ) T2 = TypeVar ( " T2 " , covariant = True ) class GenericBase ( Generic [ T1 , T2 ] ) : pass class NonGenericChild ( GenericBase ) : pass } | ~ left " : test . NonGenericChild " ~ right " : test . GenericBase [ int , str ] " " test . GenericBase [ int , str ] " ; let recursive_alias_source = { | from typing import Tuple , Union Tree = Union [ int , Tuple [ " Tree " , " Tree " ] ] Tree2 = Union [ int , Tuple [ " Tree2 " , " Tree2 " ] ] TreeWithStr = Union [ str , Tuple [ " TreeWithStr " , " TreeWithStr " ] ] TreeWithStrAndInt = Union [ str , int , Tuple [ " TreeWithStrAndInt " , " TreeWithStrAndInt " ] ] } | in assert_join ~ source : recursive_alias_source ~ left " : test . Tree " ~ right " : test . Tree " " test . Tree " ; assert_join ~ source : recursive_alias_source ~ left " : test . Tree " ~ right " : int " " test . Tree " ; assert_join ~ source : recursive_alias_source ~ left " : int " ~ right " : test . Tree " " test . Tree " ; assert_join ~ source : recursive_alias_source ~ left " : test . Tree " ~ right " : str " " typing . Union [ test . Tree , str ] " ; Type . RecursiveType . Namespace . reset ( ) ; let fresh_name = Type . RecursiveType . Namespace . create_fresh_name ( ) in Type . RecursiveType . Namespace . reset ( ) ; assert_join_direct ~ source : recursive_alias_source ~ left " : test . Tree " ~ right " : test . Tree2 " ( Type . RecursiveType . create ~ name : fresh_name ~ body : ( Type . union [ Type . integer ; Type . tuple [ Type . Primitive fresh_name ; Type . Primitive fresh_name ] ] ) ) ; Type . RecursiveType . Namespace . reset ( ) ; assert_join_direct ~ source : recursive_alias_source ~ left " : test . Tree " ~ right " : test . TreeWithStrAndInt " ( Type . RecursiveType . create ~ name : fresh_name ~ body : ( Type . union [ Type . integer ; Type . string ; Type . tuple [ Type . Primitive fresh_name ; Type . Primitive fresh_name ] ; ] ) ) ; Type . RecursiveType . Namespace . reset ( ) ; assert_join_direct ~ source : recursive_alias_source ~ left " : test . Tree " ~ right " : test . TreeWithStr " ( Type . RecursiveType . create ~ name : fresh_name ~ body : ( Type . union [ Type . integer ; Type . string ; Type . tuple [ Type . Primitive fresh_name ; Type . Primitive fresh_name ] ; ] ) ) ; ( )
|
let test_meet _ = let assert_meet ( ? order = default ) ( ? aliases = Type . empty_aliases ) left right expected = let parse_annotation = function | " $ bottom " -> Type . Bottom | _ as source -> parse_single_expression source |> Type . create ~ aliases in assert_type_equal ( parse_annotation expected ) ( meet order ( parse_annotation left ) ( parse_annotation right ) ) in assert_meet " typing . List [ float ] " " typing . Any " " typing . List [ float ] " ; assert_meet " list " " typing . Sized " " list " ; assert_meet " typing . Sized " " list " " list " ; assert_meet " typing . List [ int ] " " typing . Sized " " typing . List [ int ] " ; assert_meet " typing . Annotated [ int ] " " float " " typing . Annotated [ int ] " ; assert_meet " typing . Annotated [ int ] " " typing . Annotated [ float ] " " typing . Annotated [ int ] " ; assert_meet " typing_extensions . Annotated [ int ] " " float " " typing_extensions . Annotated [ int ] " ; assert_meet " typing_extensions . Annotated [ int ] " " typing_extensions . Annotated [ float ] " " typing_extensions . Annotated [ int ] " ; assert_meet " typing . Union [ int , str ] " " typing . Union [ int , bytes ] " " int " ; assert_meet " typing . Union [ int , str ] " " typing . Union [ str , int ] " " typing . Union [ int , str ] " ; assert_meet " typing . Union [ int , str ] " " float " " int " ; assert_meet " typing . Union [ int , str ] " " typing . List [ int ] " " $ bottom " ; assert_meet " typing . Union [ int , str ] " " typing . Union [ float , bool ] " " int " ; assert_meet " typing . Union [ int , str ] " " typing . Union [ int , bool ] " " int " ; assert_meet " typing . Union [ int , str ] " " typing . Union [ int , typing . Optional [ str ] ] " " typing . Union [ int , str ] " ; assert_meet " typing . Union [ int , typing . Optional [ str ] ] " " typing . Optional [ str ] " " typing . Optional [ str ] " ; assert_meet " typing . List [ int ] " " typing . Iterator [ int ] " " typing . List [ int ] " ; assert_meet " typing . List [ float ] " " typing . Iterator [ int ] " " $ bottom " ; assert_meet " typing . List [ float ] " " float [ int ] " " $ bottom " ; assert_meet " typing . Dict [ str , str ] " " typing . Dict [ str , typing . List [ str ] ] " " $ bottom " ; assert_meet ~ order : disconnected_order " A " " B " " $ bottom " ; assert_meet " GenericContainer [ int , str ] " " DifferentGenericContainer [ int , str ] " " $ bottom " ; assert_meet " GenericContainer [ int , str ] " " DifferentGenericContainer [ str , int ] " " $ bottom " ; assert_type_equal ( meet default Type . integer ( Type . variable " T " ) ) Type . Bottom ; assert_type_equal ( meet default Type . integer ( Type . variable ~ constraints ( : Type . Variable . Bound Type . float ) " T " ) ) Type . Bottom ; assert_type_equal ( meet default Type . string ( Type . variable ~ constraints ( : Type . Variable . Explicit [ Type . float ; Type . string ] ) " T " ) ) Type . Bottom ; assert_type_equal ( meet variance_order ( Type . parametric " LinkedList " [ ! Type . integer ] ) ( Type . parametric " LinkedList " [ ! Type . Top ] ) ) ( Type . parametric " LinkedList " [ ! Type . integer ] ) ; assert_type_equal ( meet variance_order ( Type . parametric " LinkedList " [ ! Type . Top ] ) ( Type . parametric " LinkedList " [ ! Type . integer ] ) ) ( Type . parametric " LinkedList " [ ! Type . integer ] ) ; assert_type_equal ( meet variance_order ( Type . parametric " LinkedList " [ ! Type . integer ] ) ( Type . parametric " LinkedList " [ ! Type . Any ] ) ) ( Type . parametric " LinkedList " [ ! Type . integer ] ) ; assert_type_equal ( meet variance_order ( Type . parametric " LinkedList " [ ! Type . Any ] ) ( Type . parametric " LinkedList " [ ! Type . integer ] ) ) ( Type . parametric " LinkedList " [ ! Type . Any ] ) ; let _obsolete_tests ( ) = assert_meet ~ order : variance_order " Derived [ int ] " " Base [ int ] " " Derived [ int ] " ; assert_meet ~ order : variance_order " Derived [ float ] " " Base [ float ] " " Derived [ float ] " ; assert_meet ~ order : variance_order " Derived [ int ] " " Base [ float ] " " Derived [ int ] " ; assert_meet ~ order : variance_order " Derived [ float ] " " Base [ int ] " " Derived [ float ] " ; assert_meet ~ order : multiplane_variance_order " B [ int , float ] " " A [ int , float ] " " B [ int , float ] " ; assert_meet ~ order : multiplane_variance_order " B [ int , int ] " " A [ int , float ] " " B [ int , int ] " ; assert_meet ~ order : multiplane_variance_order " B [ float , int ] " " A [ int , float ] " " B [ float , int ] " ; assert_meet ~ order : multiplane_variance_order " B [ float , float ] " " A [ int , float ] " " B [ float , float ] " ; assert_meet ~ order : multiplane_variance_order " B [ int , float ] " " A [ float , float ] " " B [ int , float ] " ; assert_meet ~ order : multiplane_variance_order " B [ int , int ] " " A [ float , float ] " " B [ int , int ] " ; assert_meet ~ order : multiplane_variance_order " B [ float , int ] " " A [ float , float ] " " B [ float , int ] " ; assert_meet ~ order : multiplane_variance_order " B [ float , float ] " " A [ float , float ] " " B [ float , float ] " ; assert_meet ~ order : parallel_planes_variance_order " B [ float , float ] " " A [ int , float ] " " B [ int , float ] " ; assert_meet ~ order : parallel_planes_variance_order " B [ float , float ] " " A [ int , int ] " " B [ int , float ] " in let make_potentially_inconsistent_order ~ x_before_y = let order = MockClassHierarchyHandler . create ( ) in let open MockClassHierarchyHandler in insert order " A " ; insert order " B " ; insert order " X " ; insert order " Y " ; insert order " M " ; insert order " typing . Generic " ; insert order " str " ; insert order " int " ; let variable = Type . Variable ( Type . Variable . Unary . create " T " ) in let variable2 = Type . Variable ( Type . Variable . Unary . create " T2 " ) in concrete_connect order ~ predecessor " : M " ~ successor " : typing . Generic " ~ parameters [ : variable ] ; concrete_connect order ~ predecessor " : M " ~ successor " : A " ~ parameters [ : variable ] ; concrete_connect order ~ predecessor " : M " ~ successor " : X " ~ parameters [ : variable ] ; concrete_connect order ~ predecessor " : M " ~ successor " : Y " ~ parameters [ : variable ] ; concrete_connect order ~ predecessor " : A " ~ successor " : typing . Generic " ~ parameters [ : variable ] ; let connect_x ( ) = concrete_connect order ~ predecessor " : X " ~ successor " : typing . Generic " ~ parameters [ : variable ] ; concrete_connect order ~ predecessor " : X " ~ successor " : B " ~ parameters [ : variable ; Type . string ] in if x_before_y then connect_x ( ) ; concrete_connect order ~ predecessor " : Y " ~ successor " : typing . Generic " ~ parameters [ : variable ] ; concrete_connect order ~ predecessor " : Y " ~ successor " : B " ~ parameters [ : Type . integer ; variable ] ; if not x_before_y then connect_x ( ) ; concrete_connect order ~ predecessor " : B " ~ successor " : typing . Generic " ~ parameters [ : variable ; variable2 ] ; handler order in assert_meet ~ order ( : make_potentially_inconsistent_order ~ x_before_y : true ) " B [ int , str ] " " A [ str ] " " $ bottom " ; assert_meet ~ order ( : make_potentially_inconsistent_order ~ x_before_y : false ) " B [ int , str ] " " A [ str ] " " $ bottom " ; let tree_annotation = Type . RecursiveType . create ~ name " : Tree " ~ body ( : Type . union [ Type . integer ; Type . tuple [ Type . Primitive " Tree " ; Type . Primitive " Tree " ] ] ) in let tree_annotation2 = Type . RecursiveType . create ~ name " : Tree2 " ~ body ( : Type . union [ Type . integer ; Type . tuple [ Type . Primitive " Tree2 " ; Type . Primitive " Tree2 " ] ] ) in let tree_annotation_with_string = Type . RecursiveType . create ~ name " : Tree2 " ~ body : ( Type . union [ Type . integer ; Type . string ; Type . tuple [ Type . Primitive " Tree2 " ; Type . Primitive " Tree2 " ] ] ) in let non_tree = Type . RecursiveType . create ~ name " : NonTree " ~ body ( : Type . union [ Type . bool ; Type . tuple [ Type . Primitive " NonTree " ] ] ) in assert_type_equal tree_annotation ( meet default tree_annotation tree_annotation ) ; assert_type_equal tree_annotation ( meet default tree_annotation tree_annotation2 ) ; assert_type_equal tree_annotation ( meet default tree_annotation tree_annotation_with_string ) ; assert_type_equal Type . integer ( meet default tree_annotation Type . integer ) ; assert_type_equal Type . Bottom ( meet default tree_annotation Type . string ) ; assert_type_equal Type . Bottom ( meet default tree_annotation non_tree ) ; ( )
|
let test_meet_callable _ = let assert_meet ( ? order = default ) left right expected = assert_type_equal expected ( meet order left right ) in let named_int_to_int = Type . Callable . create ~ name ( : Reference . create " name " ) ~ parameters : ( Defined [ Type . Callable . Parameter . Named { name = " a " ; annotation = Type . integer ; default = false } ; ] ) ~ annotation : Type . integer ( ) in assert_meet named_int_to_int named_int_to_int named_int_to_int ; let anonymous_str_to_int = Type . Callable . create ~ parameters : ( Defined [ Type . Callable . Parameter . Named { name = " a " ; annotation = Type . string ; default = false } ] ) ~ annotation : Type . integer ( ) in let anonymous_positional_only_str_to_int = Type . Callable . create ~ parameters : ( Defined [ Type . Callable . Parameter . PositionalOnly { index = 0 ; annotation = Type . string ; default = false } ; ] ) ~ annotation : Type . integer ( ) in let anonymous_int_to_float = Type . Callable . create ~ parameters : ( Defined [ Type . Callable . Parameter . Named { name = " a " ; annotation = Type . integer ; default = false } ; ] ) ~ annotation : Type . float ( ) in let anonymous_union_int_str_to_int = Type . Callable . create ~ parameters : ( Defined [ Type . Callable . Parameter . Named { name = " a " ; annotation = Type . union [ Type . integer ; Type . string ] ; default = false } ; ] ) ~ annotation : Type . integer ( ) in assert_meet anonymous_int_to_float anonymous_str_to_int anonymous_union_int_str_to_int ; assert_meet anonymous_positional_only_str_to_int anonymous_str_to_int anonymous_str_to_int ; let anonymous_undefined_to_object = Type . Callable . create ~ annotation : Type . object_primitive ( ) in let anonymous_undefined_to_int = Type . Callable . create ~ annotation : Type . integer ( ) in let anonymous_str_named_b_to_int = Type . Callable . create ~ parameters : ( Defined [ Type . Callable . Parameter . Named { name = " b " ; annotation = Type . string ; default = false } ] ) ~ annotation : Type . integer ( ) in assert_meet anonymous_str_to_int anonymous_str_named_b_to_int anonymous_undefined_to_int ; assert_meet anonymous_positional_only_str_to_int anonymous_undefined_to_object anonymous_undefined_to_int ; assert_meet anonymous_str_to_int anonymous_undefined_to_object anonymous_undefined_to_int ; assert_meet named_int_to_int anonymous_undefined_to_object anonymous_undefined_to_int ; assert_meet anonymous_undefined_to_object named_int_to_int anonymous_undefined_to_int ; assert_meet named_int_to_int anonymous_str_to_int anonymous_union_int_str_to_int ; assert_meet anonymous_str_to_int named_int_to_int anonymous_union_int_str_to_int ; let overloaded_str_to_int = Type . Callable . create ~ parameters : ( Defined [ Type . Callable . Parameter . Named { name = " a " ; annotation = Type . string ; default = false } ] ) ~ annotation ( : Type . union [ Type . integer ; Type . string ] ) ~ overloads : [ { parameters = Defined [ Type . Callable . Parameter . Named { name = " a " ; annotation = Type . string ; default = false } ; ] ; annotation = Type . integer ; } ; ] ( ) in assert_meet overloaded_str_to_int overloaded_str_to_int overloaded_str_to_int ; assert_meet overloaded_str_to_int anonymous_str_to_int Type . Bottom ; ( )
|
let ( ) = " order " >::: [ " join " >:: test_join ; " less_or_equal " >:: test_less_or_equal ; " less_or_equal_variance " >:: test_less_or_equal_variance ; " is_compatible_with " >:: test_is_compatible_with ; " meet " >:: test_meet ; " meet_callable " >:: test_meet_callable ; ] |> Test . run
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.