text
stringlengths
12
786k
let univars = ref ( [ ] : ( string * type_expr ) list )
let pre_univars = ref ( [ ] : type_expr list )
let used_variables = ref ( TyVarMap . empty : ( type_expr * Location . t ) TyVarMap . t )
let reset_type_variables ( ) = reset_global_level ( ) ; Ctype . reset_reified_var_counter ( ) ; type_variables := TyVarMap . empty
let narrow ( ) = ( increase_global_level ( ) , ! type_variables )
let widen ( gl , tv ) = restore_global_level gl ; type_variables := tv
let strict_ident c = ( c = ' _ ' || c >= ' a ' && c <= ' z ' || c >= ' A ' && c <= ' Z ' )
let validate_name = function None -> None | Some name as s -> if name <> " " && strict_ident name . [ 0 ] then s else None
let new_global_var ? name ( ) = new_global_var ? name ( : validate_name name ) ( )
let newvar ? name ( ) = newvar ? name ( : validate_name name ) ( )
let type_variable loc name = try TyVarMap . find name ! type_variables with Not_found -> raise ( Error ( loc , Env . empty , Unbound_type_variable ( " ' " ^ name ) ) )
let valid_tyvar_name name = name <> " " && name . [ 0 ] <> ' _ '
let transl_type_param env styp = let loc = styp . ptyp_loc in match styp . ptyp_desc with Ptyp_any -> let ty = new_global_var ~ name " : _ " ( ) in { ctyp_desc = Ttyp_any ; ctyp_type = ty ; ctyp_env = env ; ctyp_loc = loc ; ctyp_attributes = styp . ptyp_attributes ; } | Ptyp_var name -> let ty = try if not ( valid_tyvar_name name ) then raise ( Error ( loc , Env . empty , Invalid_variable_name ( " ' " ^ name ) ) ) ; ignore ( TyVarMap . find name ! type_variables ) ; raise Already_bound with Not_found -> let v = new_global_var ~ name ( ) in type_variables := TyVarMap . add name v ! type_variables ; v in { ctyp_desc = Ttyp_var name ; ctyp_type = ty ; ctyp_env = env ; ctyp_loc = loc ; ctyp_attributes = styp . ptyp_attributes ; } | _ -> assert false
let transl_type_param env styp = Builtin_attributes . warning_scope styp . ptyp_attributes ( fun ( ) -> transl_type_param env styp )
let new_pre_univar ? name ( ) = let v = newvar ? name ( ) in pre_univars := v :: ! pre_univars ; v
type policy = Fixed | Extensible | Univars
let rec transl_type env policy styp = Builtin_attributes . warning_scope styp . ptyp_attributes ( fun ( ) -> transl_type_aux env policy styp ) let loc = styp . ptyp_loc in let ctyp ctyp_desc ctyp_type = { ctyp_desc ; ctyp_type ; ctyp_env = env ; ctyp_loc = loc ; ctyp_attributes = styp . ptyp_attributes } in match styp . ptyp_desc with Ptyp_any -> let ty = if policy = Univars then new_pre_univar ( ) else if policy = Fixed then raise ( Error ( styp . ptyp_loc , env , Unbound_type_variable " _ " ) ) else newvar ( ) in ctyp Ttyp_any ty | Ptyp_var name -> let ty = if not ( valid_tyvar_name name ) then raise ( Error ( styp . ptyp_loc , env , Invalid_variable_name ( " ' " ^ name ) ) ) ; begin try instance ( List . assoc name ! univars ) with Not_found -> try instance ( fst ( TyVarMap . find name ! used_variables ) ) with Not_found -> let v = if policy = Univars then new_pre_univar ~ name ( ) else newvar ~ name ( ) in used_variables := TyVarMap . add name ( v , styp . ptyp_loc ) ! used_variables ; v end in ctyp ( Ttyp_var name ) ty | Ptyp_arrow ( l , st1 , st2 ) -> let cty1 = transl_type env policy st1 in let cty2 = transl_type env policy st2 in let ty1 = cty1 . ctyp_type in let ty1 = if Btype . is_optional l then newty ( Tconstr ( Predef . path_option , [ ty1 ] , ref Mnil ) ) else ty1 in let ty = newty ( Tarrow ( l , ty1 , cty2 . ctyp_type , Cok ) ) in ctyp ( Ttyp_arrow ( l , cty1 , cty2 ) ) ty | Ptyp_tuple stl -> assert ( List . length stl >= 2 ) ; let ctys = List . map ( transl_type env policy ) stl in let ty = newty ( Ttuple ( List . map ( fun ctyp -> ctyp . ctyp_type ) ctys ) ) in ctyp ( Ttyp_tuple ctys ) ty | Ptyp_constr ( lid , stl ) -> let ( path , decl ) = Env . lookup_type ~ loc : lid . loc lid . txt env in let stl = match stl with | [ { ptyp_desc = Ptyp_any } as t ] when decl . type_arity > 1 -> List . map ( fun _ -> t ) decl . type_params | _ -> stl in if List . length stl <> decl . type_arity then raise ( Error ( styp . ptyp_loc , env , Type_arity_mismatch ( lid . txt , decl . type_arity , List . length stl ) ) ) ; let args = List . map ( transl_type env policy ) stl in let params = instance_list decl . type_params in let unify_param = match decl . type_manifest with None -> unify_var | Some ty -> if ( repr ty ) . level = Btype . generic_level then unify_var else unify in List . iter2 ( fun ( sty , cty ) ty ' -> try unify_param env ty ' cty . ctyp_type with Unify trace -> let trace = Unification_trace . swap trace in raise ( Error ( sty . ptyp_loc , env , Type_mismatch trace ) ) ) ( List . combine stl args ) params ; let constr = newconstr path ( List . map ( fun ctyp -> ctyp . ctyp_type ) args ) in begin try Ctype . enforce_constraints env constr with Unify trace -> raise ( Error ( styp . ptyp_loc , env , Type_mismatch trace ) ) end ; ctyp ( Ttyp_constr ( path , lid , args ) ) constr | Ptyp_object ( fields , o ) -> let ty , fields = transl_fields env policy o fields in ctyp ( Ttyp_object ( fields , o ) ) ( newobj ty ) | Ptyp_class ( lid , stl ) -> let ( path , decl , _is_variant ) = try let path , decl = Env . find_type_by_name lid . txt env in let rec check decl = match decl . type_manifest with None -> raise Not_found | Some ty -> match ( repr ty ) . desc with Tvariant row when Btype . static_row row -> ( ) | Tconstr ( path , _ , _ ) -> check ( Env . find_type path env ) | _ -> raise Not_found in check decl ; Location . deprecated styp . ptyp_loc " old syntax for polymorphic variant type " ; ignore ( Env . lookup_type ~ loc : lid . loc lid . txt env ) ; ( path , decl , true ) with Not_found -> try let lid2 = match lid . txt with Longident . Lident s -> Longident . Lident ( " " # ^ s ) | Longident . Ldot ( r , s ) -> Longident . Ldot ( r , " " # ^ s ) | Longident . Lapply ( _ , _ ) -> fatal_error " Typetexp . transl_type " in let path , decl = Env . find_type_by_name lid2 env in ignore ( Env . lookup_cltype ~ loc : lid . loc lid . txt env ) ; ( path , decl , false ) with Not_found -> ignore ( Env . lookup_cltype ~ loc : lid . loc lid . txt env ) ; assert false in if List . length stl <> decl . type_arity then raise ( Error ( styp . ptyp_loc , env , Type_arity_mismatch ( lid . txt , decl . type_arity , List . length stl ) ) ) ; let args = List . map ( transl_type env policy ) stl in let params = instance_list decl . type_params in List . iter2 ( fun ( sty , cty ) ty ' -> try unify_var env ty ' cty . ctyp_type with Unify trace -> let trace = Unification_trace . swap trace in raise ( Error ( sty . ptyp_loc , env , Type_mismatch trace ) ) ) ( List . combine stl args ) params ; let ty_args = List . map ( fun ctyp -> ctyp . ctyp_type ) args in let ty = try Ctype . expand_head env ( newconstr path ty_args ) with Unify trace -> raise ( Error ( styp . ptyp_loc , env , Type_mismatch trace ) ) in let ty = match ty . desc with Tvariant row -> let row = Btype . row_repr row in let fields = List . map ( fun ( l , f ) -> l , match Btype . row_field_repr f with | Rpresent ( Some ty ) -> Reither ( false , [ ty ] , false , ref None ) | Rpresent None -> Reither ( true , [ ] , false , ref None ) | _ -> f ) row . row_fields in let row = { row_closed = true ; row_fields = fields ; row_bound = ( ) ; row_name = Some ( path , ty_args ) ; row_fixed = None ; row_more = newvar ( ) } in let static = Btype . static_row row in let row = if static then { row with row_more = newty Tnil } else if policy <> Univars then row else { row with row_more = new_pre_univar ( ) } in newty ( Tvariant row ) | Tobject ( fi , _ ) -> let _ , tv = flatten_fields fi in if policy = Univars then pre_univars := tv :: ! pre_univars ; ty | _ -> assert false in ctyp ( Ttyp_class ( path , lid , args ) ) ty | Ptyp_alias ( st , alias ) -> let cty = try let t = try List . assoc alias ! univars with Not_found -> instance ( fst ( TyVarMap . find alias ! used_variables ) ) in let ty = transl_type env policy st in begin try unify_var env t ty . ctyp_type with Unify trace -> let trace = Unification_trace . swap trace in raise ( Error ( styp . ptyp_loc , env , Alias_type_mismatch trace ) ) end ; ty with Not_found -> if ! Clflags . principal then begin_def ( ) ; let t = newvar ( ) in used_variables := TyVarMap . add alias ( t , styp . ptyp_loc ) ! used_variables ; let ty = transl_type env policy st in begin try unify_var env t ty . ctyp_type with Unify trace -> let trace = Unification_trace . swap trace in raise ( Error ( styp . ptyp_loc , env , Alias_type_mismatch trace ) ) end ; if ! Clflags . principal then begin end_def ( ) ; generalize_structure t ; end ; let t = instance t in let px = Btype . proxy t in begin match px . desc with | Tvar None -> Btype . set_type_desc px ( Tvar ( Some alias ) ) | Tunivar None -> Btype . set_type_desc px ( Tunivar ( Some alias ) ) | _ -> ( ) end ; { ty with ctyp_type = t } in ctyp ( Ttyp_alias ( cty , alias ) ) cty . ctyp_type | Ptyp_variant ( fields , closed , present ) -> let name = ref None in let mkfield l f = newty ( Tvariant { row_fields [ = l , f ] ; row_more = newvar ( ) ; row_bound ( ) ; = row_closed = true ; row_fixed = None ; row_name = None } ) in let hfields = Hashtbl . create 17 in let add_typed_field loc l f = let h = Btype . hash_variant l in try let ( l ' , f ' ) = Hashtbl . find hfields h in if l <> l ' then raise ( Error ( styp . ptyp_loc , env , Variant_tags ( l , l ' ) ) ) ; let ty = mkfield l f and ty ' = mkfield l f ' in if equal env false [ ty ] [ ty ' ] then ( ) else try unify env ty ty ' with Unify _trace -> raise ( Error ( loc , env , Constructor_mismatch ( ty , ty ' ) ) ) with Not_found -> Hashtbl . add hfields h ( l , f ) in let add_field field = let rf_loc = field . prf_loc in let rf_attributes = field . prf_attributes in let rf_desc = match field . prf_desc with | Rtag ( l , c , stl ) -> name := None ; let tl = Builtin_attributes . warning_scope rf_attributes ( fun ( ) -> List . map ( transl_type env policy ) stl ) in let f = match present with Some present when not ( List . mem l . txt present ) -> let ty_tl = List . map ( fun cty -> cty . ctyp_type ) tl in Reither ( c , ty_tl , false , ref None ) | _ -> if List . length stl > 1 || c && stl <> [ ] then raise ( Error ( styp . ptyp_loc , env , Present_has_conjunction l . txt ) ) ; match tl with [ ] -> Rpresent None | st :: _ -> Rpresent ( Some st . ctyp_type ) in add_typed_field styp . ptyp_loc l . txt f ; Ttag ( l , c , tl ) | Rinherit sty -> let cty = transl_type env policy sty in let ty = cty . ctyp_type in let nm = match repr cty . ctyp_type with { desc = Tconstr ( p , tl , _ ) } -> Some ( p , tl ) | _ -> None in begin try Hashtbl . iter ( fun _ _ -> raise Exit ) hfields ; name := nm with Exit -> name := None end ; let fl = match expand_head env cty . ctyp_type , nm with { desc = Tvariant row } , _ when Btype . static_row row -> let row = Btype . row_repr row in row . row_fields | { desc = Tvar _ } , Some ( p , _ ) -> raise ( Error ( sty . ptyp_loc , env , Undefined_type_constructor p ) ) | _ -> raise ( Error ( sty . ptyp_loc , env , Not_a_variant ty ) ) in List . iter ( fun ( l , f ) -> let f = match present with Some present when not ( List . mem l present ) -> begin match f with Rpresent ( Some ty ) -> Reither ( false , [ ty ] , false , ref None ) | Rpresent None -> Reither ( true , [ ] , false , ref None ) | _ -> assert false end | _ -> f in add_typed_field sty . ptyp_loc l f ) fl ; Tinherit cty in { rf_desc ; rf_loc ; rf_attributes ; } in let tfields = List . map add_field fields in let fields = Hashtbl . fold ( fun _ p l -> p :: l ) hfields [ ] in begin match present with None -> ( ) | Some present -> List . iter ( fun l -> if not ( List . mem_assoc l fields ) then raise ( Error ( styp . ptyp_loc , env , Present_has_no_type l ) ) ) present end ; let row = { row_fields = List . rev fields ; row_more = newvar ( ) ; row_bound = ( ) ; row_closed = ( closed = Closed ) ; row_fixed = None ; row_name = ! name } in let static = Btype . static_row row in let row = if static then { row with row_more = newty Tnil } else if policy <> Univars then row else { row with row_more = new_pre_univar ( ) } in let ty = newty ( Tvariant row ) in ctyp ( Ttyp_variant ( tfields , closed , present ) ) ty | Ptyp_poly ( vars , st ) -> let vars = List . map ( fun v -> v . txt ) vars in begin_def ( ) ; let new_univars = List . map ( fun name -> name , newvar ~ name ( ) ) vars in let old_univars = ! univars in univars := new_univars @ ! univars ; let cty = transl_type env policy st in let ty = cty . ctyp_type in univars := old_univars ; end_def ( ) ; generalize ty ; let ty_list = List . fold_left ( fun tyl ( name , ty1 ) -> let v = Btype . proxy ty1 in if deep_occur v ty then begin match v . desc with Tvar name when v . level = Btype . generic_level -> v . desc <- Tunivar name ; v :: tyl | _ -> raise ( Error ( styp . ptyp_loc , env , Cannot_quantify ( name , v ) ) ) end else tyl ) [ ] new_univars in let ty ' = Btype . newgenty ( Tpoly ( ty , List . rev ty_list ) ) in unify_var env ( newvar ( ) ) ty ' ; ctyp ( Ttyp_poly ( vars , cty ) ) ty ' | Ptyp_package ( p , l ) -> let l , mty = create_package_mty true styp . ptyp_loc env ( p , l ) in let z = narrow ( ) in let mty = ! transl_modtype env mty in widen z ; let ptys = List . map ( fun ( s , pty ) -> s , transl_type env policy pty ) l in let path = ! transl_modtype_longident styp . ptyp_loc env p . txt in let ty = newty ( Tpackage ( path , List . map ( fun ( s , _pty ) -> s . txt ) l , List . map ( fun ( _ , cty ) -> cty . ctyp_type ) ptys ) ) in ctyp ( Ttyp_package { pack_path = path ; pack_type = mty . mty_type ; pack_fields = ptys ; pack_txt = p ; } ) ty | Ptyp_extension ext -> raise ( Error_forward ( Builtin_attributes . error_of_extension ext ) ) transl_type env policy ( Ast_helper . Typ . force_poly t ) let hfields = Hashtbl . create 17 in let add_typed_field loc l ty = try let ty ' = Hashtbl . find hfields l in if equal env false [ ty ] [ ty ' ] then ( ) else try unify env ty ty ' with Unify _trace -> raise ( Error ( loc , env , Method_mismatch ( l , ty , ty ' ) ) ) with Not_found -> Hashtbl . add hfields l ty in let add_field { pof_desc ; pof_loc ; pof_attributes ; } = let of_loc = pof_loc in let of_attributes = pof_attributes in let of_desc = match pof_desc with | Otag ( s , ty1 ) -> begin let ty1 = Builtin_attributes . warning_scope of_attributes ( fun ( ) -> transl_poly_type env policy ty1 ) in let field = OTtag ( s , ty1 ) in add_typed_field ty1 . ctyp_loc s . txt ty1 . ctyp_type ; field end | Oinherit sty -> begin let cty = transl_type env policy sty in let nm = match repr cty . ctyp_type with { desc = Tconstr ( p , _ , _ ) } -> Some p | _ -> None in let t = expand_head env cty . ctyp_type in match t , nm with { desc = Tobject ( { desc ( = Tfield _ | Tnil ) as tf } , _ ) } , _ -> begin if opened_object t then raise ( Error ( sty . ptyp_loc , env , Opened_object nm ) ) ; let rec iter_add = function | Tfield ( s , _k , ty1 , ty2 ) -> begin add_typed_field sty . ptyp_loc s ty1 ; iter_add ty2 . desc end | Tnil -> ( ) | _ -> assert false in iter_add tf ; OTinherit cty end | { desc = Tvar _ } , Some p -> raise ( Error ( sty . ptyp_loc , env , Undefined_type_constructor p ) ) | _ -> raise ( Error ( sty . ptyp_loc , env , Not_an_object t ) ) end in { of_desc ; of_loc ; of_attributes ; } in let object_fields = List . map add_field fields in let fields = Hashtbl . fold ( fun s ty l -> ( s , ty ) :: l ) hfields [ ] in let ty_init = match o , policy with | Closed , _ -> newty Tnil | Open , Univars -> new_pre_univar ( ) | Open , _ -> newvar ( ) in let ty = List . fold_left ( fun ty ( s , ty ' ) -> newty ( Tfield ( s , Fpresent , ty ' , ty ) ) ) ty_init fields in ty , object_fields
let rec make_fixed_univars ty = let ty = repr ty in if ty . level >= Btype . lowest_level then begin Btype . mark_type_node ty ; match ty . desc with | Tvariant row -> let row = Btype . row_repr row in let more = Btype . row_more row in if Btype . is_Tunivar more then ty . desc <- Tvariant { row with row_fixed = Some ( Univar more ) ; row_fields = List . map ( fun ( s , f as p ) -> match Btype . row_field_repr f with Reither ( c , tl , _m , r ) -> s , Reither ( c , tl , true , r ) | _ -> p ) row . row_fields } ; Btype . iter_row make_fixed_univars row | _ -> Btype . iter_type_expr make_fixed_univars ty end
let make_fixed_univars ty = make_fixed_univars ty ; Btype . unmark_type ty
let create_package_mty = create_package_mty false
let globalize_used_variables env fixed = let r = ref [ ] in TyVarMap . iter ( fun name ( ty , loc ) -> let v = new_global_var ( ) in let snap = Btype . snapshot ( ) in if try unify env v ty ; true with _ -> Btype . backtrack snap ; false then try r := ( loc , v , TyVarMap . find name ! type_variables ) :: ! r with Not_found -> if fixed && Btype . is_Tvar ( repr ty ) then raise ( Error ( loc , env , Unbound_type_variable ( " ' " ^ name ) ) ) ; let v2 = new_global_var ( ) in r := ( loc , v , v2 ) :: ! r ; type_variables := TyVarMap . add name v2 ! type_variables ) ! used_variables ; used_variables := TyVarMap . empty ; fun ( ) -> List . iter ( function ( loc , t1 , t2 ) -> try unify env t1 t2 with Unify trace -> raise ( Error ( loc , env , Type_mismatch trace ) ) ) ! r
let transl_simple_type env fixed styp = univars := [ ] ; used_variables := TyVarMap . empty ; let typ = transl_type env ( if fixed then Fixed else Extensible ) styp in globalize_used_variables env fixed ( ) ; make_fixed_univars typ . ctyp_type ; typ
let transl_simple_type_univars env styp = univars := [ ] ; used_variables := TyVarMap . empty ; pre_univars := [ ] ; begin_def ( ) ; let typ = transl_type env Univars styp in let new_variables = ! used_variables in used_variables := TyVarMap . empty ; TyVarMap . iter ( fun name p -> if TyVarMap . mem name ! type_variables then used_variables := TyVarMap . add name p ! used_variables ) new_variables ; globalize_used_variables env false ( ) ; end_def ( ) ; generalize typ . ctyp_type ; let univs = List . fold_left ( fun acc v -> let v = repr v in match v . desc with Tvar name when v . level = Btype . generic_level -> v . desc <- Tunivar name ; v :: acc | _ -> acc ) [ ] ! pre_univars in make_fixed_univars typ . ctyp_type ; { typ with ctyp_type = instance ( Btype . newgenty ( Tpoly ( typ . ctyp_type , univs ) ) ) }
let transl_simple_type_delayed env styp = univars := [ ] ; used_variables := TyVarMap . empty ; let typ = transl_type env Extensible styp in make_fixed_univars typ . ctyp_type ; ( typ , globalize_used_variables env false )
let transl_type_scheme env styp = reset_type_variables ( ) ; begin_def ( ) ; let typ = transl_simple_type env false styp in end_def ( ) ; generalize typ . ctyp_type ; typ
let report_error env ppf = function | Unbound_type_variable name -> let add_name name _ l = if name = " _ " then l else ( " ' " ^ name ) :: l in let names = TyVarMap . fold add_name ! type_variables [ ] in fprintf ppf " The type variable % s is unbound in this type declaration . @ % a " name did_you_mean ( fun ( ) -> Misc . spellcheck names name ) | Undefined_type_constructor p -> fprintf ppf " The type constructor @ % a @ is not yet completely defined " path p | Type_arity_mismatch ( lid , expected , provided ) -> fprintf ppf " [ @ The type constructor % a @ expects % i argument ( s ) , @ \ but is here applied to % i argument ( s ) ] " @ longident lid expected provided | Bound_type_variable name -> fprintf ppf " Already bound type parameter % a " Pprintast . tyvar name | Recursive_type -> fprintf ppf " This type is recursive " | Unbound_row_variable lid -> fprintf ppf " Unbound row variable in #% a " longident lid | Type_mismatch trace -> Printtyp . report_unification_error ppf Env . empty trace ( function ppf -> fprintf ppf " This type " ) ( function ppf -> fprintf ppf " should be an instance of type " ) | Alias_type_mismatch trace -> Printtyp . report_unification_error ppf Env . empty trace ( function ppf -> fprintf ppf " This alias is bound to type " ) ( function ppf -> fprintf ppf " but is used as an instance of type " ) | Present_has_conjunction l -> fprintf ppf " The present constructor % s has a conjunctive type " l | Present_has_no_type l -> fprintf ppf " [ @< v [ >@ The constructor % s is missing from the upper bound @ \ ( between ' ' <@ and ' ' ) >@ of this polymorphic variant @ \ but is present in @ its lower bound ( after ' ' ) . ] , >@@\ [ @ Hint : Either add ` % s in the upper bound , @ \ or remove it @ from the lower bound . ] ] " @@ l l | Constructor_mismatch ( ty , ty ' ) -> wrap_printing_env ~ error : true env ( fun ( ) -> Printtyp . reset_and_mark_loops_list [ ty ; ty ' ] ; fprintf ppf " [ @< hov >% s % a @ % s @ % a ] " @ " This variant type contains a constructor " ! Oprint . out_type ( tree_of_typexp false ty ) " which should be " ! Oprint . out_type ( tree_of_typexp false ty ' ) ) | Not_a_variant ty -> fprintf ppf " [ @ The type % a @ does not expand to a polymorphic variant type ] " @ Printtyp . type_expr ty ; begin match ty . desc with | Tvar ( Some s ) -> Misc . did_you_mean ppf ( fun ( ) -> [ " ` " ^ s ] ) | _ -> ( ) end | Variant_tags ( lab1 , lab2 ) -> fprintf ppf " [ @ Variant tags ` % s @ and ` % s have the same hash value . @ % s ] " @ lab1 lab2 " Change one of them . " | Invalid_variable_name name -> fprintf ppf " The type variable name % s is not allowed in programs " name | Cannot_quantify ( name , v ) -> fprintf ppf " [ @< hov > The universal type variable % a cannot be generalized :@ " Pprintast . tyvar name ; if Btype . is_Tvar v then fprintf ppf " it escapes its scope " else if Btype . is_Tunivar v then fprintf ppf " it is already bound to another variable " else fprintf ppf " it is bound to @ % a " Printtyp . type_expr v ; fprintf ppf " . ] " ; @ | Multiple_constraints_on_type s -> fprintf ppf " Multiple constraints for type % a " longident s | Method_mismatch ( l , ty , ty ' ) -> wrap_printing_env ~ error : true env ( fun ( ) -> fprintf ppf " [ @< hov > Method ' % s ' has type % a , @ which should be % a ] " @ l Printtyp . type_expr ty Printtyp . type_expr ty ' ) | Opened_object nm -> fprintf ppf " Illegal open object type % a " ( fun ppf -> function Some p -> fprintf ppf " @ % a " path p | None -> fprintf ppf " " ) nm | Not_an_object ty -> fprintf ppf " [ @ The type % a @ is not an object type ] " @ Printtyp . type_expr ty
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 test_check_bounded_variables context = let assert_type_errors = assert_type_errors ~ context in assert_type_errors { | from typing import TypeVar , Callable TFun = TypeVar ( " TFun " , bound = Callable [ [ int ] , None ] ) def foo ( x : TFun ) -> None : x ( 7 ) } | [ ] ; assert_type_errors { | from typing import TypeVar , Callable TFun = TypeVar ( " TFun " , bound = Callable [ [ int ] , None ] ) def foo ( x : TFun ) -> None : x ( " 7 " ) } | [ " Incompatible parameter type [ 6 ] : In anonymous call , for 1st positional only parameter \ expected ` int ` but got ` str ` . " ; ] ; assert_type_errors { | from typing import TypeVar , Callable , Union T1 = TypeVar ( " T1 " , bound = Union [ Callable [ [ ] , str ] , Callable [ [ ] , int ] ] ) def foo ( x : T1 ) -> None : y = x ( ) reveal_type ( y ) } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` Union [ int , str ] ` . " ] ; assert_type_errors { | from typing import TypeVar , Callable , Union T1 = TypeVar ( " T1 " , bound = Union [ Callable [ [ ] , str ] , Callable [ [ ] , str ] ] ) def foo ( x : T1 ) -> None : y = x ( ) reveal_type ( y ) } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` str ` . " ] ; assert_type_errors { | from typing import TypeVar class CallableClass : def __call__ ( self , x : int ) -> str : return " A " T2 = TypeVar ( " T2 " , bound = CallableClass ) def foo ( x : T2 ) -> None : y = x ( 5 ) reveal_type ( y ) } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` str ` . " ] ; assert_type_errors { | from typing import TypeVar class CallableClass : def __call__ ( self , x : int ) -> str : return " A " T2 = TypeVar ( " T2 " , bound = CallableClass ) def foo ( x : T2 ) -> None : y = x ( 2 ) reveal_type ( y ) } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` str ` . " ] ; assert_type_errors { | from typing import Type , TypeVar class Constructable : def __init__ ( self , x : int ) -> None : return T3 = TypeVar ( " T3 " , bound = Type [ Constructable ] ) def foo ( x : T3 ) -> None : x ( 5 ) } | [ ] ; assert_type_errors { | from typing import TypeVar , Generic , List S = TypeVar ( ' S ' , bound = List [ float ] ) def bar ( x : List [ float ] ) -> None : pass def foo ( x : S ) -> S : bar ( x ) return x } | [ ] ; assert_type_errors { | from typing import TypeVar , Generic T = TypeVar ( ' T ' , covariant = True ) S = TypeVar ( ' S ' , bound " = Foo [ float ] " ) class Foo ( Generic [ T ] ) : def a ( self , x : S ) -> S : return x def b ( self , x : S ) -> None : self . a ( x ) def foo ( a : Foo [ int ] ) -> Foo [ float ] : return a } | [ ] ; assert_type_errors { | from typing import TypeVar , List , Tuple , Optional , Callable T = TypeVar ( " T " , int , str ) def f ( x : Callable [ [ T ] , None ] ) -> None : y = g ( x ) def g ( x : Callable [ [ T ] , None ] ) -> None : . . . } | [ ] ; assert_type_errors { | from typing import TypeVar , List , Tuple , Optional , Callable T = TypeVar ( " T " , int , str ) def f ( x : Optional [ Callable [ [ Optional [ T ] ] , None ] ] ) -> None : y = g ( x ) def g ( x : Optional [ Callable [ [ Optional [ T ] ] , None ] ] ) -> None : . . . } | [ ] ; ( )
let test_check_unbounded_variables context = let assert_type_errors = assert_type_errors ~ context in assert_type_errors { | import typing T = typing . TypeVar ( ' T ' ) def expects_any ( input : object ) -> None : . . . def expects_string ( inut : str ) -> None : . . . def foo ( input : T ) -> None : expects_any ( input ) expects_string ( input ) } | [ " Incompatible parameter type [ 6 ] : In call ` expects_string ` , for 1st positional only \ parameter expected ` str ` but got ` Variable [ T ] ` . " ; ] ; assert_type_errors { | import typing T = typing . TypeVar ( ' T ' ) def foo ( input : T ) -> typing . Any : return input } | [ " Missing return annotation [ 3 ] : Returning ` Variable [ T ] ` but type ` Any ` is specified . " ] ; assert_type_errors { | import typing T = typing . TypeVar ( ' T ' ) def foo ( input : T ) -> int : return input } | [ " Incompatible return type [ 7 ] : Expected ` int ` but got ` Variable [ T ] ` . " ] ; assert_type_errors { | import typing T = typing . TypeVar ( ' T ' ) def mapping_get ( k : str , default : typing . Union [ int , T ] ) -> typing . Union [ int , T ] : . . . def foo ( ) -> None : reveal_type ( mapping_get ( " A " , " A " ) ) reveal_type ( mapping_get ( " A " , 7 ) ) } | [ " Revealed type [ - 1 ] : Revealed type for ` test . mapping_get ( " \ A " , \ " \ A " ) ` \ is " ^ " ` typing . Union [ typing_extensions . Literal [ ' A ' ] , int ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` test . mapping_get ( " \ A " , \ 7 ) ` is ` int ` . " ; ] ; assert_type_errors { | import typing T = typing . TypeVar ( ' T ' ) def foo ( input : T ) -> None : input . impossible ( ) } | [ " Undefined attribute [ 16 ] : ` Variable [ T ] ` has no attribute ` impossible ` . " ] ; assert_type_errors { | import typing X = typing . TypeVar ( " X " ) class Foo ( typing . Generic [ X ] ) : pass reveal_type ( Foo [ float ] ) reveal_type ( Foo [ float ] ( ) ) reveal_type ( Foo [ str ] ( ) ) Foo [ " str " ] ( ) } | [ " Revealed type [ - 1 ] : Revealed type for ` test . Foo [ float ] ` is ` typing . Type [ Foo [ float ] ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` test . Foo [ float ] ( ) ` is ` Foo [ float ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` test . Foo [ str ] ( ) ` is ` Foo [ str ] ` . " ; " Incompatible parameter type [ 6 ] : In call ` typing . GenericMeta . __getitem__ ` , for 1st \ positional only parameter expected ` Type [ Variable [ X ] ] ` but got ` str ` . " ; ] ; assert_type_errors { | import typing X = typing . TypeVar ( " X " ) class Foo ( typing . Generic [ X ] ) : def __init__ ( self , x : X ) -> None : . . . def one ( ) -> Foo [ int ] : return Foo [ int ] ( 1 ) def two ( ) -> Foo [ int ] : return Foo [ int ] ( 1 . 2 ) } | [ " Incompatible parameter type [ 6 ] : In call ` Foo . __init__ ` , for 1st positional only parameter \ expected ` int ` but got ` float ` . " ; ] ; assert_type_errors { | from typing import overload , TypeVar , List , Callable , Tuple , Union @ overload def overloaded ( x : int ) -> str : . . . @ overload def overloaded ( x : bool ) -> float : . . . @ overload def overloaded ( x : float ) -> bool : . . . @ overload def overloaded ( x : str ) -> int : . . . def overloaded ( x : Union [ int , bool , float , str ] ) -> Union [ int , bool , float , str ] : . . . T1 = TypeVar ( " T1 " ) T2 = TypeVar ( " T2 " ) def generic ( x : Callable [ [ T1 ] , T2 ] , y : List [ T1 ] , z : List [ T2 ] ) -> Tuple [ T1 , T2 ] : . . . def foo ( ) -> None : reveal_type ( generic ( overloaded , [ 1 ] , [ " 1 " ] ) ) reveal_type ( generic ( overloaded , [ True ] , [ 1 . 0 ] ) ) reveal_type ( generic ( overloaded , [ 1 . 0 ] , [ False ] ) ) reveal_type ( generic ( overloaded , [ " 1 " ] , [ 7 ] ) ) generic ( overloaded , [ 1 ] , [ 7 ] ) } | [ " Revealed type [ - 1 ] : Revealed type for ` test . generic ( test . overloaded , [ 1 ] , [ " \ 1 " ] ) ` \ is \ ` Tuple [ int , str ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` test . generic ( test . overloaded , [ True ] , [ 1 . 000000 ] ) ` \ is ` Tuple [ bool , float ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` test . generic ( test . overloaded , [ 1 . 000000 ] , [ False ] ) ` \ is ` Tuple [ float , bool ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` test . generic ( test . overloaded , [ " \ 1 " ] , \ [ 7 ] ) ` is \ ` Tuple [ str , int ] ` . " ; " Incompatible parameter type [ 6 ] : In call ` generic ` , for 3rd positional only parameter \ expected ` List [ Variable [ T2 ] ] ` but got ` List [ int ] ` . " ; ] ; assert_type_errors { | import typing T = typing . TypeVar ( ' T ' ) def foo ( input : T , b : bool ) -> typing . Optional [ T ] : x = None if b : x = input reveal_type ( x ) return x } | [ " Revealed type [ - 1 ] : Revealed type for ` x ` is ` typing . Optional [ Variable [ T ] ] ` . " ] ; assert_type_errors { | from typing import TypeVar , Generic , Optional T1 = TypeVar ( " T1 " ) class Lol ( Generic [ T1 ] ) : def bar ( self , x : Optional [ T1 ] ) -> None : if x is not None and self . bop ( x ) : return def bop ( self , x : T1 ) -> bool : return True } | [ ] ; assert_type_errors { | from typing import TypeVar , Union , List T = TypeVar ( " T " ) def foo ( x : Union [ T , List [ T ] ] ) -> None : . . . def bar ( x : Union [ T , List [ T ] ] ) -> None : foo ( x ) } | [ ] ; assert_type_errors { | from builtins import identity from typing import Union , Tuple SeparatedUnion = Union [ Tuple [ int , bool ] , Tuple [ str , None ] , ] def foo ( x : SeparatedUnion ) -> SeparatedUnion : i = identity ( x ) reveal_type ( i ) return i } | [ " Revealed type [ - 1 ] : Revealed type for ` i ` is ` Union [ Tuple [ int , bool ] , Tuple [ str , None ] ] ` . " ] ; assert_type_errors { | from typing import Callable , TypeVar T = TypeVar ( " T " ) class CallMe : def __call__ ( self , x : int ) -> str : return " A " def foo ( f : Callable [ [ int ] , T ] ) -> T : return f ( 1 ) def bar ( ) -> None : x = foo ( CallMe ( ) ) reveal_type ( x ) } | [ " Revealed type [ - 1 ] : Revealed type for ` x ` is ` str ` . " ] ; assert_type_errors { | from typing import TypeVar , Callable T = TypeVar ( ' T ' ) def foo ( x : T ) -> Callable [ [ ] , T ] : def bar ( ) -> T : return x return bar } | [ ] ; assert_type_errors { | from typing import TypeVar , Generic , Callable T = TypeVar ( ' T ' ) class A ( Generic [ T ] ) : def foo ( self , x : T ) -> T : return x } | [ ] ; assert_type_errors { | from typing import TypeVar , Generic , Callable T = TypeVar ( ' T ' ) class A ( Generic [ T ] ) : def foo ( self , x : T ) -> Callable [ [ T ] , int ] : def bar ( x : T ) -> int : return 42 return bar } | [ ] ; assert_type_errors { | from typing import TypeVar , Dict , Any , Union def loads ( obj : object ) -> Dict [ str , Any ] : . . . T = TypeVar ( ' T ' ) def foo ( ) -> None : def bar ( obj : T , , * top_level : bool = True ) -> Union [ str , T ] : if isinstance ( obj , dict ) : return " dict " else : loaded = loads ( obj ) modified = bar ( loaded , top_level = False ) return str ( modified ) } | [ ] ; assert_type_errors { | from typing import TypeVar , List , Generic T_bound_int = TypeVar ( ' T_bound_int ' , bound = int ) class G ( Generic [ T_bound_int ] ) : pass T = TypeVar ( ' T ' ) def foo ( a : G [ List [ T ] ] ) -> T : . . . } | [ " Invalid type parameters [ 24 ] : Type parameter ` List [ Variable [ T ] ] ` violates constraints on \ ` Variable [ T_bound_int ( bound to int ) ] ` in generic type ` G ` . " ; ] ; assert_type_errors { | from typing import TypeVar , List , Generic T_Con = TypeVar ( ' T_Con ' , contravariant = True ) class G ( Generic [ T_Con ] ) : pass def foo ( a : G [ str ] , b : G [ int ] ) -> None : l : List [ G [ object ] ] = [ a , b ] } | [ " Incompatible variable type [ 9 ] : l is declared to have type ` List [ G [ object ] ] ` but is used as \ type ` List [ Union [ G [ int ] , G [ str ] ] ] ` . " ; ] ; assert_type_errors { | from typing import Generic , Optional , TypeVar _T = TypeVar ( ' _T ' ) class ContextVar ( Generic [ _T ] ) : def __init__ ( self , name : str , , * default : _T = . . . ) -> None : . . . def foo ( ) -> None : x : ContextVar [ Optional [ int ] ] = ContextVar [ Optional [ int ] ] ( " var1 " , default = None ) } | [ ] ; ( )
let test_check_variable_bindings context = let assert_type_errors = assert_type_errors ~ context in assert_type_errors { | from builtins import str_to_int import typing T = typing . TypeVar ( ' T ' , bound = int ) def foo ( t : T ) -> None : str_to_int ( t ) } | [ " Incompatible parameter type [ 6 ] : In call ` str_to_int ` , for 1st positional only parameter \ expected ` str ` but got ` Variable [ T ( bound to int ) ] ` . " ; ] ; assert_type_errors { | import typing T = typing . TypeVar ( ' T ' , bound = int ) def foo ( ) -> T : return 1 . 0 } | [ " Invalid type variable [ 34 ] : The type variable ` Variable [ T ( bound to int ) ] ` isn ' t present in \ the function ' s parameters . " ; " Incompatible return type [ 7 ] : Expected ` Variable [ T ( bound to int ) ] ` but got ` float ` . " ; ] ; assert_type_errors { | from builtins import int_to_str import typing T = typing . TypeVar ( ' T ' , bound = int ) def foo ( t : T ) -> None : int_to_str ( t ) def bar ( x : str ) -> None : foo ( x ) } | [ " Incompatible parameter type [ 6 ] : In call ` foo ` , for 1st positional only parameter expected \ ` Variable [ T ( bound to int ) ] ` but got ` str ` . " ; ] ; assert_type_errors { | import typing class C ( ) : def baz ( self ) -> int : return 7 T = typing . TypeVar ( ' T ' , bound = C ) def foo ( t : T ) -> int : return t . baz ( ) } | [ ] ; assert_type_errors { | from typing import TypeVar T = TypeVar ( " T " , bound = int ) def f ( x : T , y : int ) -> T : return x def buggy ( n : None ) -> None : return f ( 2 , n ) } | [ " Incompatible return type [ 7 ] : Expected ` None ` but got ` int ` . " ; " Incompatible parameter type [ 6 ] : In call ` f ` , for 2nd positional only parameter expected \ ` int ` but got ` None ` . " ; ] ; assert_type_errors { | import typing class C : pass T = typing . TypeVar ( ' T ' , bound = C ) def foo ( input : typing . Type [ T ] ) -> T : v = input ( ) reveal_type ( v ) return v } | [ " Revealed type [ - 1 ] : Revealed type for ` v ` is ` Variable [ T ( bound to C ) ] ` . " ] ; assert_type_errors { | import typing _T = typing . TypeVar ( " T " , bound = int ) class Foo : def foo ( self , x : int ) -> int : return x class Bar ( Foo ) : def foo ( self , x : _T ) -> _T : return x } | [ ] ; assert_type_errors { | import typing _T = typing . TypeVar ( " T " , bound = float ) class Foo : def foo ( self , x : int ) -> int : return x class Bar ( Foo ) : def foo ( self , x : _T ) -> _T : return x } | [ " Inconsistent override [ 15 ] : ` test . Bar . foo ` overrides method defined in ` Foo ` inconsistently . " ^ " Returned type ` Variable [ _T ( bound to float ) ] ` is not a subtype of the overridden return " ^ " ` int ` . " ; ] ; assert_type_errors { | import typing _T = typing . TypeVar ( " T " , bound = float ) class Foo : def foo ( self , x : _T ) -> _T : return x class Bar ( Foo ) : def foo ( self , x : int ) -> int : return x } | [ " Inconsistent override [ 14 ] : ` test . Bar . foo ` overrides method defined in ` Foo ` inconsistently . " ^ " Parameter of type ` int ` is not a supertype of the overridden parameter " ^ " ` Variable [ _T ( bound to float ) ] ` . " ; ] ; assert_type_errors { | from typing import TypeVar _SelfT = TypeVar ( " SelfT " , bound = C ) class C ( ) : def clone ( self : _SelfT ) -> _SelfT : . . . def foo ( self : _SelfT ) -> _SelfT : x = self . clone ( ) reveal_type ( x ) return x } | [ " Revealed type [ - 1 ] : Revealed type for ` x ` is ` Variable [ _SelfT ( bound to C ) ] ` . " ] ; assert_type_errors { | from typing import TypeVar , Type _SelfT = TypeVar ( " SelfT " , bound = C ) class C ( ) : @ classmethod def clone ( cls : Type [ _SelfT ] ) -> _SelfT : . . . @ classmethod def foop ( cls : Type [ _SelfT ] ) -> _SelfT : x = cls . clone ( ) reveal_type ( x ) return x } | [ " Revealed type [ - 1 ] : Revealed type for ` x ` is ` Variable [ _SelfT ( bound to C ) ] ` . " ] ; assert_type_errors { | import typing X = typing . TypeVar ( " X " , bound = C ) class Foo ( typing . Generic [ X ] ) : pass class C ( ) : pass class D ( C ) : pass reveal_type ( Foo [ C ] ) reveal_type ( Foo [ C ] ( ) ) reveal_type ( Foo [ D ] ( ) ) Foo [ int ] ( ) } | [ " Revealed type [ - 1 ] : Revealed type for ` test . Foo [ test . C ] ` is ` typing . Type [ Foo [ C ] ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` test . Foo [ test . C ] ( ) ` is ` Foo [ C ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` test . Foo [ test . D ] ( ) ` is ` Foo [ D ] ` . " ; " Incompatible parameter type [ 6 ] : In call ` typing . GenericMeta . __getitem__ ` , for 1st \ positional only parameter expected ` Type [ Variable [ X ( bound to C ) ] ] ` but got ` Type [ int ] ` . " ; ] ; assert_type_errors { | import typing X = typing . TypeVar ( " X " , Mineral , Animal ) class Foo ( typing . Generic [ X ] ) : pass class Mineral ( ) : pass class Animal ( ) : pass class Fish ( Animal ) : pass reveal_type ( Foo [ Animal ] ) reveal_type ( Foo [ Animal ] ( ) ) reveal_type ( Foo [ Mineral ] ( ) ) reveal_type ( Foo [ Fish ] ( ) ) Foo [ int ] ( ) } | [ " Revealed type [ - 1 ] : Revealed type for ` test . Foo [ test . Animal ] ` is " ^ " ` typing . Type [ Foo [ Animal ] ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` test . Foo [ test . Animal ] ( ) ` is ` Foo [ Animal ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` test . Foo [ test . Mineral ] ( ) ` is ` Foo [ Mineral ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` test . Foo [ test . Fish ] ( ) ` is ` Foo [ Animal ] ` . " ; " Incompatible parameter type [ 6 ] : In call ` typing . GenericMeta . __getitem__ ` , for 1st \ positional only parameter expected ` Type [ Variable [ X <: [ Mineral , Animal ] ] ] ` but got \ ` Type [ int ] ` . " ; ] ; assert_type_errors { | import typing T = typing . TypeVar ( ' T ' , bound = int ) class ConstrainedBase ( typing . Generic [ T ] ) : pass class BadChild ( ConstrainedBase [ str ] ) : pass } | [ " Invalid type parameters [ 24 ] : Type parameter ` str ` violates constraints on " ^ " ` Variable [ T ( bound to int ) ] ` in generic type ` ConstrainedBase ` . " ; ] ; assert_type_errors { | import typing T = typing . TypeVar ( ' T ' , bound = int ) class ConstrainedBase ( typing . Generic [ T ] ) : pass class AnyChild ( ConstrainedBase [ typing . Any ] ) : pass } | [ ] ; assert_type_errors { | from typing import TypeVar , Generic T = TypeVar ( ' T ' , bound " = G " ) class G ( Generic [ T ] ) : pass } | [ " Invalid type parameters [ 24 ] : Generic type ` G ` expects 1 type parameter . " ] ; assert_type_errors { | from typing import TypeVar , Generic TSelf = TypeVar ( " TSelf " , bound " = G " ) T = TypeVar ( " T " ) class G ( Generic [ T ] ) : # This method restricts the inputs to be less than ` G [ Any ] ` but does # not enforce that the two inputs are of the same type . def expect_self ( self : TSelf , other : TSelf ) -> TSelf : . . . x : G [ int ] y : G [ str ] x . expect_self ( y ) reveal_type ( x . expect_self ( y ) ) z : bool x . expect_self ( z ) } | [ " Invalid type parameters [ 24 ] : Generic type ` G ` expects 1 type parameter . " ; " Revealed type [ - 1 ] : Revealed type for ` x . expect_self ( y ) ` is ` typing . Union [ G [ int ] , G [ str ] ] ` . " ; " Incompatible parameter type [ 6 ] : In call ` G . expect_self ` , for 1st positional only parameter \ expected ` Variable [ TSelf ( bound to G [ typing . Any ] ) ] ` but got ` bool ` . " ; ] ; assert_type_errors { | from typing import TypeVar , Generic TSelf = TypeVar ( " TSelf " , bound " = G " ) T = TypeVar ( " T " ) class G ( Generic [ T ] ) : # This method restricts the inputs to be less than ` G [ Any ] ` but does # not enforce that the two inputs are of the same type . def expect_self ( self , other : TSelf ) -> TSelf : . . . x : G [ int ] y : G [ str ] x . expect_self ( y ) reveal_type ( x . expect_self ( y ) ) z : bool x . expect_self ( z ) } | [ " Invalid type parameters [ 24 ] : Generic type ` G ` expects 1 type parameter . " ; " Revealed type [ - 1 ] : Revealed type for ` x . expect_self ( y ) ` is ` G [ str ] ` . " ; " Incompatible parameter type [ 6 ] : In call ` G . expect_self ` , for 1st positional only parameter \ expected ` Variable [ TSelf ( bound to G [ typing . Any ] ) ] ` but got ` bool ` . " ; ] ; assert_type_errors { | from typing import TypeVar , Generic TSelf = TypeVar ( " TSelf " , bound " = G " ) T = TypeVar ( " T " ) class G ( Generic [ T ] ) : def expect_same_type ( self : G [ T ] , other : G [ T ] ) -> G [ T ] : . . . x : G [ int ] y : G [ str ] x . expect_same_type ( y ) reveal_type ( x . expect_same_type ( y ) ) z : bool x . expect_same_type ( z ) } | [ " Invalid type parameters [ 24 ] : Generic type ` G ` expects 1 type parameter . " ; " Incompatible parameter type [ 6 ] : In call ` G . expect_same_type ` , for 1st positional only \ parameter expected ` G [ int ] ` but got ` G [ str ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` x . expect_same_type ( y ) ` is ` G [ int ] ` . " ; " Incompatible parameter type [ 6 ] : In call ` G . expect_same_type ` , for 1st positional only \ parameter expected ` G [ int ] ` but got ` bool ` . " ; ] ; assert_type_errors { | from typing import Generic , Tuple , TypeVar T = TypeVar ( " T " ) class INode ( Generic [ T ] ) : . . . TBoundToINode = TypeVar ( " TNodeGetResult " , bound = INode ) TResult = TypeVar ( " TResult " ) class Query ( Generic [ TResult ] ) : def get_result ( self ) -> TResult : . . . class NodeGetQuery ( Query [ TBoundToINode ] ) : . . . y : NodeGetQuery [ int ] z : NodeGetQuery [ INode [ str ] ] z3 : NodeGetQuery [ INode [ int ] ] } | [ " Invalid type parameters [ 24 ] : Generic type ` INode ` expects 1 type parameter . " ; " Invalid type parameters [ 24 ] : Type parameter ` int ` violates constraints on \ ` Variable [ TBoundToINode ( bound to test . INode ) ] ` in generic type ` NodeGetQuery ` . " ; ] ; ( )
let test_unbound_variables context = let assert_type_errors = assert_type_errors ~ context in let assert_default_type_errors = assert_default_type_errors ~ context in assert_type_errors { | def foo ( ) -> None : x = [ ] } | [ " Incomplete type [ 37 ] : Type ` typing . List [ Variable [ _T ] ] ` inferred for ` x ` is incomplete , " ^ " add an explicit annotation . " ; ] ; assert_type_errors { | import typing def foo ( ) -> None : x : typing . List [ int ] = [ ] } | [ ] ; assert_type_errors { | import typing def foo ( ) -> None : x : typing . Sequence [ int ] = [ ] } | [ ] ; assert_type_errors { | def foo ( ) -> None : x : int = [ ] } | [ " Incompatible variable type [ 9 ] : x is declared to have type ` int ` but is used as type \ ` List [ Variable [ _T ] ] ` . " ; ] ; assert_type_errors { | import typing def foo ( ) -> None : x : typing . Optional [ typing . List [ int ] ] x = [ ] reveal_type ( x ) } | [ " Revealed type [ - 1 ] : Revealed type for ` x ` is ` typing . Optional [ typing . List [ int ] ] ` ( inferred : \ ` typing . List [ int ] ` ) . " ; ] ; assert_type_errors { | import typing def foo ( ) -> None : x : typing . Dict [ str , typing . List [ int ] ] = { " A " : [ ] } } | [ ] ; assert_type_errors { | import typing def foo ( ) -> None : x : typing . List [ int ] = { } } | [ " Incompatible variable type [ 9 ] : x is declared to have type ` List [ int ] ` but is used as type \ ` Dict [ Variable [ _KT ] , Variable [ _VT ] ] ` . " ; ] ; assert_type_errors { | import typing def foo ( ) -> None : x : typing . Dict [ int , str ] = [ ] } | [ " Incompatible variable type [ 9 ] : x is declared to have type ` Dict [ int , str ] ` but is used as \ type ` List [ Variable [ _T ] ] ` . " ; ] ; assert_type_errors { | import typing def foo ( ) -> None : x : typing . Dict [ int , typing . List [ int ] ] = { " A " : [ ] } } | [ " Incompatible variable type [ 9 ] : x is declared to have type ` Dict [ int , List [ int ] ] ` but is \ used as type ` Dict [ str , List [ int ] ] ` . " ; ] ; assert_type_errors { | import typing def foo ( ) -> typing . List [ int ] : return [ ] } | [ ] ; assert_type_errors { | import typing def bar ( x : typing . List [ int ] ) -> None : pass def foo ( ) -> None : bar ( [ ] ) } | [ ] ; assert_type_errors { | import typing T = typing . TypeVar ( " T " ) def bar ( x : typing . List [ T ] ) -> T : return x [ 0 ] def foo ( ) -> None : x = bar ( [ ] ) } | [ " Incomplete type [ 37 ] : Type inferred for ` x ` is incomplete , add an explicit annotation . " ] ; assert_type_errors { | import typing T_Explicit = typing . TypeVar ( " T_Explicit " , int , str ) class G ( typing . Generic [ T_Explicit ] ) : def __init__ ( self ) -> None : pass def bar ( ) -> G [ int ] : return G ( ) } | [ ] ; assert_type_errors { | import typing T_Explicit = typing . TypeVar ( " T_Explicit " , int , str ) class G ( typing . Generic [ T_Explicit ] ) : def __init__ ( self ) -> None : pass def bar ( ) -> G [ int ] : g = G ( ) reveal_type ( g ) return g } | [ " Incomplete type [ 37 ] : Type ` G [ Variable [ T_Explicit <: [ int , str ] ] ] ` inferred for ` g ` is " ^ " incomplete , add an explicit annotation . " ; " Revealed type [ - 1 ] : Revealed type for ` g ` is ` G [ typing . Any ] ` . " ; ] ; assert_default_type_errors { | import typing T_Explicit = typing . TypeVar ( " T_Explicit " , int , str ) class G ( typing . Generic [ T_Explicit ] ) : def __init__ ( self ) -> None : pass def bar ( ) -> G [ int ] : g = G ( ) reveal_type ( g ) return g } | [ " Revealed type [ - 1 ] : Revealed type for ` g ` is ` G [ typing . Any ] ` . " ] ; assert_type_errors { | import typing T_Explicit = typing . TypeVar ( " T_Explicit " , int , str ) class G ( typing . Generic [ T_Explicit ] ) : def __init__ ( self ) -> None : pass def bar ( ) -> G [ int ] : g : G [ int ] = G ( ) reveal_type ( g ) return g } | [ " Revealed type [ - 1 ] : Revealed type for ` g ` is ` G [ int ] ` . " ] ; assert_type_errors { | import typing T_Explicit = typing . TypeVar ( " T_Explicit " , int , str ) class G ( typing . Generic [ T_Explicit ] ) : def __init__ ( self ) -> None : pass def bar ( ) -> G [ bool ] : g : G [ bool ] = G ( ) reveal_type ( g ) return g } | [ " Invalid type parameters [ 24 ] : Type parameter ` bool ` violates constraints on " ^ " ` Variable [ T_Explicit <: [ int , str ] ] ` in generic type ` G ` . " ; " Invalid type parameters [ 24 ] : Type parameter ` bool ` violates constraints on " ^ " ` Variable [ T_Explicit <: [ int , str ] ] ` in generic type ` G ` . " ; " Revealed type [ - 1 ] : Revealed type for ` g ` is ` G [ typing . Any ] ` . " ; ] ; assert_default_type_errors { | import typing T_Explicit = typing . TypeVar ( " T_Explicit " , int , str ) class G ( typing . Generic [ T_Explicit ] ) : def __init__ ( self ) -> None : pass def bar ( ) -> G [ bool ] : g : G [ bool ] = G ( ) reveal_type ( g ) return g } | [ " Invalid type parameters [ 24 ] : Type parameter ` bool ` violates constraints on " ^ " ` Variable [ T_Explicit <: [ int , str ] ] ` in generic type ` G ` . " ; " Invalid type parameters [ 24 ] : Type parameter ` bool ` violates constraints on " ^ " ` Variable [ T_Explicit <: [ int , str ] ] ` in generic type ` G ` . " ; " Revealed type [ - 1 ] : Revealed type for ` g ` is ` G [ typing . Any ] ` . " ; ] ; assert_type_errors { | import typing T_Explicit = typing . TypeVar ( " T_Explicit " , int , str ) T = typing . TypeVar ( " T " ) class G ( typing . Generic [ T_Explicit , T ] ) : def __init__ ( self ) -> None : pass def bar ( g : G [ bool , bool ] ) -> None : reveal_type ( g ) } | [ " Invalid type parameters [ 24 ] : Type parameter ` bool ` violates constraints on " ^ " ` Variable [ T_Explicit <: [ int , str ] ] ` in generic type ` G ` . " ; " Revealed type [ - 1 ] : Revealed type for ` g ` is ` G [ typing . Any , bool ] ` . " ; ] ; assert_type_errors { | import typing T_Explicit = typing . TypeVar ( " T_Explicit " , int , str ) class G ( typing . Generic [ T_Explicit ] ) : def __init__ ( self ) -> None : pass def foo ( self ) -> int : return 7 def bar ( ) -> int : return G ( ) . foo ( ) } | [ " Incomplete type [ 37 ] : Type ` G [ Variable [ T_Explicit <: [ int , str ] ] ] ` inferred for ` test . G ( ) ` " ^ " is incomplete , so attribute ` foo ` cannot be accessed . Separate the expression into an " ^ " assignment and give it an explicit annotation . " ; ] ; assert_type_errors { | def bar ( ) -> None : for x in [ ] : pass } | [ " Incomplete type [ 37 ] : Type ` typing . List [ Variable [ _T ] ] ` inferred for ` [ ] ` is incomplete , so \ attribute ` __iter__ ` cannot be accessed . Separate the expression into an assignment and \ give it an explicit annotation . " ; ] ; assert_type_errors { | import typing import collections def foo ( ) -> None : x : typing . Dict [ int , typing . Dict [ int , str ] ] = collections . defaultdict ( dict ) } | [ ] ; assert_type_errors { | import typing import collections def foo ( ) -> None : x : typing . Dict [ int , str ] = collections . defaultdict ( dict ) } | [ " Incompatible variable type [ 9 ] : x is declared to have type ` Dict [ int , str ] ` but is used as \ type ` DefaultDict [ Variable [ collections . _KT ] , Dict [ Variable [ _KT ] , Variable [ _VT ] ] ] ` . " ; ] ; assert_type_errors { | import typing def foo ( ) -> typing . Tuple [ typing . List [ int ] , typing . List [ str ] ] : return [ ] , [ ] } | [ ] ; assert_type_errors { | def foo ( x : int ) -> None : pass def bar ( ) -> None : for x in [ 1 , 2 , 3 ] : foo ( [ ] ) } | [ " Incompatible parameter type [ 6 ] : In call ` foo ` , for 1st positional only parameter expected \ ` int ` but got ` List [ Variable [ _T ] ] ` . " ; ] ; assert_type_errors { | import typing def bar ( a : typing . Optional [ typing . List [ int ] ] , b : typing . Optional [ typing . List [ str ] ] ) -> typing . Tuple [ typing . List [ int ] , typing . List [ str ] ] : return a or [ ] , b or [ ] } | [ ] ; assert_type_errors { | from typing import Generic , TypeVar , Any T = TypeVar ( ' T ' ) class G ( Generic [ T ] ) : prop : T def __init__ ( self , prop : T ) -> None : self . prop = prop class C ( G [ int ] ) : def foo ( self ) -> None : reveal_type ( self . prop ) } | [ " Revealed type [ - 1 ] : Revealed type for ` self . prop ` is ` int ` . " ] ; ( )
let test_distinguish context = let assert_type_errors = assert_type_errors ~ context in assert_type_errors { | import typing _T1 = typing . TypeVar ( " _T1 " ) _T2 = typing . TypeVar ( " _T2 " ) class C ( typing . Generic [ _T1 ] ) : def pair ( self , a : _T1 , b : _T2 ) -> typing . Tuple [ _T1 , _T2 ] : return ( a , b ) def foo ( q : C [ _T2 ] , x : _T2 , y : _T1 ) -> typing . Tuple [ _T2 , _T1 ] : A = q . pair ( x , y ) reveal_type ( A ) return A } | [ " Revealed type [ - 1 ] : Revealed type for ` A ` is ` typing . Tuple [ Variable [ _T2 ] , Variable [ _T1 ] ] ` . " ] ; assert_type_errors { | import typing _T1 = typing . TypeVar ( " _T1 " ) _T2 = typing . TypeVar ( " _T2 " ) def foo ( f : typing . Callable [ [ _T1 ] , _T2 ] , p : _T1 ) -> _T2 : v = f ( p ) reveal_type ( v ) return v } | [ " Revealed type [ - 1 ] : Revealed type for ` v ` is ` Variable [ _T2 ] ` . " ] ; assert_type_errors { | import typing _T1 = typing . TypeVar ( " _T1 " ) _T2 = typing . TypeVar ( " _T2 " ) def foo ( f : typing . Callable [ [ _T1 ] , _T2 ] , p : _T1 ) -> _T2 : return f ( 1 ) } | [ " Incompatible parameter type [ 6 ] : In anonymous call , for 1st positional only parameter \ expected ` Variable [ _T1 ] ` but got ` int ` . " ; ] ; assert_type_errors { | import typing _T1 = typing . TypeVar ( " _T1 " ) _T2 = typing . TypeVar ( " _T2 " ) class B : pass class C ( B ) : pass def foo ( f : typing . Callable [ [ typing . List [ typing . Tuple [ _T1 , B ] ] ] , _T2 ] , p : _T1 ) -> _T2 : v = f ( [ ( p , C ( ) ) ] ) reveal_type ( v ) return v } | [ " Revealed type [ - 1 ] : Revealed type for ` v ` is ` Variable [ _T2 ] ` . " ] ; assert_type_errors { | import typing class C ( ) : def __init__ ( self , x : int ) -> None : pass def foo ( ) -> typing . Iterator [ C ] : v = map ( C , [ 1 , 2 , 3 ] ) reveal_type ( v ) return v } | [ " Revealed type [ - 1 ] : Revealed type for ` v ` is ` typing . Iterator [ C ] ` . " ] ; assert_type_errors { | import typing T = typing . TypeVar ( " T " ) class C ( typing . Generic [ T ] ) : def __init__ ( self , x : T ) -> None : pass def foo ( ) -> typing . Iterator [ C [ int ] ] : v = map ( C , [ 1 , 2 , 3 ] ) reveal_type ( v ) return v } | [ " Revealed type [ - 1 ] : Revealed type for ` v ` is ` typing . Iterator [ C [ int ] ] ` . " ] ; assert_type_errors { | import typing T = typing . TypeVar ( " T " ) class C ( typing . Generic [ T ] ) : def __init__ ( self , x : T ) -> None : pass def foo ( x : typing . List [ T ] ) -> typing . Iterator [ C [ T ] ] : v = map ( C , x ) reveal_type ( v ) return v } | [ " Revealed type [ - 1 ] : Revealed type for ` v ` is ` typing . Iterator [ C [ Variable [ T ] ] ] ` . " ] ; assert_type_errors { | import typing T = typing . TypeVar ( " T " ) def foo ( x : T ) -> typing . List [ T ] : return [ x ] T1 = typing . TypeVar ( " T1 " ) def bar ( x : typing . Callable [ [ T1 ] , T1 ] ) -> None : pass def baz ( ) -> None : bar ( foo ) } | [ " Mutually recursive type variables [ 36 ] : Solving type variables for call ` bar ` " ^ " led to infinite recursion . " ; ] ; assert_type_errors { | import typing T = typing . TypeVar ( " T " ) def foo ( x : T ) -> T : return x T1 = typing . TypeVar ( " T1 " ) T2 = typing . TypeVar ( " T2 " ) def bar ( x : typing . Callable [ [ T1 ] , T2 ] , y : typing . Callable [ [ T2 ] , T1 ] ) -> typing . Tuple [ T1 , T2 ] : . . . def baz ( ) -> None : x = bar ( foo , foo ) } | [ " Incomplete type [ 37 ] : Type ` typing . Tuple [ Variable [ T1 ] , Variable [ T1 ] ] ` inferred for ` x " ^ " ` is incomplete , add an explicit annotation . " ; ] ; assert_type_errors { | import typing T = typing . TypeVar ( " T " ) def identity ( x : T ) -> T : return x def f ( ) -> None : reveal_type ( map ( identity , [ 1 , 2 , 3 ] ) ) } | [ " Revealed type [ - 1 ] : Revealed type for ` map ( test . identity , [ 1 , 2 , 3 ] ) ` is \ ` typing . Iterator [ int ] ` . " ; ] ; ( )
let test_integer_variables context = assert_type_errors ~ context { | import typing_extensions T = typing_extensions . IntVar ( " T " ) X = typing_extensions . IntVar ( " X " ) def baz ( x : X ) -> X : return x def bop ( x : int ) -> None : pass def foo ( x : T ) -> T : y = x . __add__ ( 5 ) z = baz ( x ) bop ( x ) return z def bar ( ) -> None : x = foo ( 1 ) reveal_type ( x ) } | [ " Revealed type [ - 1 ] : Revealed type for ` x ` is ` typing_extensions . Literal [ 1 ] ` . " ] ; assert_type_errors ~ context { | import typing_extensions X = typing_extensions . IntVar ( " X " ) def baz ( x : X ) -> X : return x def bar ( y : int ) -> None : baz ( y ) } | [ " Incompatible parameter type [ 6 ] : In call ` baz ` , for 1st positional only parameter expected \ ` IntegerVariable [ X ] ` but got ` int ` . " ; ] ; ( )
let test_nested_variable_error context = assert_type_errors ~ context { | import typing T1 = typing . TypeVar ( " T1 " ) T2 = typing . TypeVar ( " T2 " , typing . List [ T1 ] , typing . Dict [ str , T1 ] ) } | [ " Invalid type [ 31 ] : Expression ` Variable [ T2 <: [ typing . List [ Variable [ test . T1 ] ] , " ^ " typing . Dict [ str , Variable [ test . T1 ] ] ] ] ` is not a valid type . Type variables cannot contain " ^ " other type variables in their constraints . " ; ] ; ( )
let test_single_explicit_error context = assert_type_errors ~ context { | import typing T1 = typing . TypeVar ( " T1 " , int ) } | [ " Invalid type [ 31 ] : TypeVar can ' t have a single explicit constraint . Did you mean ` bound = int ` " ; ? ] ; ( )
let test_callable_parameter_variadics context = let assert_type_errors = assert_type_errors ~ context in assert_type_errors { | from typing import Callable , List import pyre_extensions V = pyre_extensions . ParameterSpecification ( " V " ) def f ( x : Callable [ V , int ] ) -> Callable [ V , List [ int ] ] : . . . def foo ( x : int ) -> int : return 7 def bar ( x : int , y : str ) -> int : return 7 def g ( ) -> None : reveal_type ( f ( foo ) ) reveal_type ( f ( bar ) ) } | [ " Revealed type [ - 1 ] : Revealed type for ` test . f ( test . foo ) ` is ` typing . Callable [ [ Named ( x , \ int ) ] , " ^ " List [ int ] ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` test . f ( test . bar ) ` is ` typing . Callable [ [ Named ( x , \ int ) , " ^ " Named ( y , str ) ] , List [ int ] ] ` . " ; ] ; assert_type_errors { | import typing import pyre_extensions V = pyre_extensions . ParameterSpecification ( " V " ) class Propagating ( typing . List [ typing . Callable [ V , int ] ] ) : def foo ( self ) -> int : . . . } | [ ] ; assert_type_errors ~ handle " : qualifier . py " { | from typing import Callable , List from pyre_extensions import ParameterSpecification from pyre_extensions . type_variable_operators import PositionalArgumentsOf , KeywordArgumentsOf V = ParameterSpecification ( " V " ) def f ( x : Callable [ V , int ] ) -> Callable [ V , List [ int ] ] : def decorated ( * args : V . args , ** kwargs : V . kwargs ) -> List [ int ] : return [ x ( * args , ** kwargs ) ] return decorated } | [ ] ; assert_type_errors { | from typing import Callable from pyre_extensions import ParameterSpecification TParams = ParameterSpecification ( " TParams " ) def eek ( x : Callable [ TParams , int ] ) -> Callable [ TParams , float ] : return x } | [ ] ; assert_type_errors { | from typing import Protocol , Callable , TypeVar import pyre_extensions TParams = pyre_extensions . ParameterSpecification ( " TParams " ) TReturn = TypeVar ( " TReturn " ) def call_this_function ( __f : Callable [ TParams , TReturn ] , * args : TParams . args , ** kwargs : TParams . kwargs ) -> TReturn : return __f ( * args , ** kwargs ) def int_to_string ( i : int ) -> str : return " A " def foo ( ) -> None : x = call_this_function ( int_to_string , 1 ) reveal_type ( x ) y = call_this_function ( int_to_string , i = 1 ) reveal_type ( y ) call_this_function ( int_to_string , " A " ) call_this_function ( int_to_string , i " = A " ) } | [ " Revealed type [ - 1 ] : Revealed type for ` x ` is ` str ` . " ; " Revealed type [ - 1 ] : Revealed type for ` y ` is ` str ` . " ; " Incompatible parameter type [ 6 ] : In call ` call_this_function ` , for 2nd positional only \ parameter expected ` int ` but got ` str ` . " ; " Incompatible parameter type [ 6 ] : In call ` call_this_function ` , for 2nd parameter ` i ` \ expected ` int ` but got ` str ` . " ; ] ; assert_type_errors { | from typing import Protocol , Callable , TypeVar , overload , Union import pyre_extensions TParams = pyre_extensions . ParameterSpecification ( " TParams " ) TReturn = TypeVar ( " TReturn " ) def call_this_function ( __f : Callable [ TParams , TReturn ] , * args : TParams . args , ** kwargs : TParams . kwargs ) -> TReturn : return __f ( * args , ** kwargs ) @ overload def overloaded ( x : int ) -> str . . . : @ overload def overloaded ( x : str ) -> int . . . : def overloaded ( x : Union [ int , str ] ) -> Union [ int , str ] : if isinstance ( x , int ) : return " A " else : return 1 def foo ( ) -> None : x = call_this_function ( overloaded , 1 ) reveal_type ( x ) y = call_this_function ( overloaded , " A " ) reveal_type ( y ) call_this_function ( overloaded , 1 . 0 ) } | [ " Revealed type [ - 1 ] : Revealed type for ` x ` is ` str ` . " ; " Revealed type [ - 1 ] : Revealed type for ` y ` is ` int ` . " ; " Incompatible parameter type [ 6 ] : In call ` call_this_function ` , for 2nd positional only \ parameter expected ` int ` but got ` float ` . " ; ] ; assert_type_errors { | from typing import Protocol , Callable , TypeVar import pyre_extensions TParams = pyre_extensions . ParameterSpecification ( " TParams " ) TReturn = TypeVar ( " TReturn " ) def call_n_times ( __f : Callable [ TParams , None ] , __n : int , * args : TParams . args , ** kwargs : TParams . kwargs , ) -> None : for x in range ( __n ) : __f ( * args , ** kwargs ) def valid ( x : int , y : str ) -> None : . . . def invalid ( x : int , y : str ) -> int : . . . def foo ( ) -> None : call_n_times ( valid , 75 , 1 , " A " ) # invalid first argument call_n_times ( invalid , 75 , 1 , " A " ) # missing second argument call_n_times ( valid , y " = A " , x = 1 ) } | [ " Incompatible parameter type [ 6 ] : In call ` call_n_times ` , for 1st positional only parameter \ expected ` typing . Callable [ test . TParams , None ] ` but got ` typing . Callable ( invalid ) [ [ Named ( x , \ int ) , Named ( y , str ) ] , int ] ` . " ; " Missing argument [ 20 ] : Call ` call_n_times ` expects argument in position 1 . " ; ] ; assert_type_errors { | from typing import * from pyre_extensions import ParameterSpecification from pyre_extensions . type_variable_operators import Concatenate P = ParameterSpecification ( " P " ) R = TypeVar ( " R " ) class Client : . . . def with_client ( f : Callable [ Concatenate [ " Foo " , Client , P ] , R ] ) -> Callable [ Concatenate [ " Foo " , P ] , R ] : def inner ( __self : " Foo " , * args : P . args , ** kwargs : P . kwargs ) -> R : return f ( __self , Client ( ) , * args , ** kwargs ) return inner class Foo : @ with_client def takes_int_str ( self , client : Client , x : int , y : str ) -> int : # Use ` client ` here . return x + 7 reveal_type ( with_client ) x : Foo reveal_type ( x . takes_int_str ) x . takes_int_str ( 1 , " A " ) # Accepted x . takes_int_str ( " B " , 2 ) # Correctly rejected by the type checker } | [ " Revealed type [ - 1 ] : Revealed type for ` test . with_client ` is \ ` typing . Callable ( with_client ) [ [ Named ( f , \ typing . Callable [ pyre_extensions . type_variable_operators . Concatenate [ Foo , Client , test . P ] , \ Variable [ R ] ] ) ] , typing . Callable [ pyre_extensions . type_variable_operators . Concatenate [ Foo , \ test . P ] , Variable [ R ] ] ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` x . takes_int_str ` is \ ` BoundMethod [ typing . Callable [ [ Foo , Named ( x , int ) , Named ( y , str ) ] , int ] , Foo ] ` . " ; " Incompatible parameter type [ 6 ] : In anonymous call , for 1st positional only parameter \ expected ` int ` but got ` str ` . " ; " Incompatible parameter type [ 6 ] : In anonymous call , for 2nd positional only parameter \ expected ` str ` but got ` int ` . " ; ] ; assert_type_errors { | from abc import ABCMeta from typing import Protocol , Callable , TypeVar import pyre_extensions TParams = pyre_extensions . ParameterSpecification ( " TParams " ) TReturn = TypeVar ( " TReturn " ) class HasForward ( Protocol [ TParams , TReturn ] ) : forward : Callable [ TParams , TReturn ] class Model ( metaclass = ABCMeta ) : forward : Callable [ . . . , object ] def __call__ ( __self : HasForward [ TParams , TReturn ] , * args : TParams . args , ** kwargs : TParams . kwargs ) -> TReturn : # do some common stuff return_value = __self . forward ( * args , ** kwargs ) # do some more stuff return return_value class AModel ( Model ) : def forward ( self , x : int , y : str ) -> bool : . . . class BModel ( Model ) : def forward ( self , x : bool , * args : int ) -> str : . . . def foo ( ) -> None : # Correct usages x = AModel ( ) ( 1 , " A " ) reveal_type ( x ) y = AModel ( ) ( y " = A " , x = 5 ) reveal_type ( y ) # Incorrect second argument AModel ( ) ( 1 , 1 ) # Different model z = BModel ( ) ( True , 1 , 4 , 5 ) reveal_type ( z ) } | [ " Revealed type [ - 1 ] : Revealed type for ` x ` is ` bool ` . " ; " Revealed type [ - 1 ] : Revealed type for ` y ` is ` bool ` . " ; " Incompatible parameter type [ 6 ] : In call ` Model . __call__ ` , for 2nd positional only \ parameter expected ` str ` but got ` int ` . " ; " Revealed type [ - 1 ] : Revealed type for ` z ` is ` str ` . " ; ] ; assert_type_errors { | from pyre_extensions import ParameterSpecification from typing import Generic P = ParameterSpecification ( " P " ) class H ( Generic [ P ] ) : def f ( self , , / * args : P . args , ** kwargs : P . kwargs ) -> int : return 5 def foo ( x : H [ int , str ] ) -> None : reveal_type ( x . f . __call__ ) # incorrect x . f ( ) x . f ( " A " , 1 ) # correct x . f ( 1 , " A " ) } | [ " Revealed type [ - 1 ] : Revealed type for ` x . f . __call__ ` is ` typing . Callable [ [ int , str ] , int ] ` . " ; " Missing argument [ 20 ] : Call ` H . f ` expects argument in position 1 . " ; " Incompatible parameter type [ 6 ] : In call ` H . f ` , for 1st positional only parameter expected \ ` int ` but got ` str ` . " ; " Incompatible parameter type [ 6 ] : In call ` H . f ` , for 2nd positional only parameter expected \ ` str ` but got ` int ` . " ; ] ; assert_type_errors { | from typing import Callable import pyre_extensions TParams = pyre_extensions . ParameterSpecification ( " TParams " ) def outer ( f : Callable [ TParams , int ] ) -> None : def foo ( x : int , * args : TParams . args , ** kwargs : TParams . kwargs ) -> None : pass def bar ( __x : int , * args : TParams . args , ** kwargs : TParams . kwargs ) -> None : pass def baz ( x : int , , / * args : TParams . args , ** kwargs : TParams . kwargs ) -> None : pass reveal_type ( foo ) reveal_type ( bar ) reveal_type ( baz ) } | [ " Revealed type [ - 1 ] : Revealed type for ` foo ` is \ ` typing . Callable [ pyre_extensions . type_variable_operators . Concatenate [ int , test . TParams ] , \ None ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` bar ` is \ ` typing . Callable [ pyre_extensions . type_variable_operators . Concatenate [ int , test . TParams ] , \ None ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` baz ` is \ ` typing . Callable [ pyre_extensions . type_variable_operators . Concatenate [ int , test . TParams ] , \ None ] ` . " ; ] ; assert_type_errors { | from typing import Callable import pyre_extensions TParams = pyre_extensions . ParameterSpecification ( " TParams " ) def outer ( f : Callable [ TParams , int ] ) -> Callable [ TParams , None ] : def foo ( x : int , * args : TParams . args , ** kwargs : TParams . kwargs ) -> None : f ( * args , ** kwargs ) def bar ( * args : TParams . args , ** kwargs : TParams . kwargs ) -> None : foo ( 1 , * args , ** kwargs ) # Accepted foo ( x = 1 , * args , ** kwargs ) # Rejected return bar } | [ " Unexpected keyword [ 28 ] : Unexpected keyword argument ` x ` to anonymous call . " ] ; assert_type_errors { | from typing import Protocol , Callable , TypeVar , overload , Union import pyre_extensions TParams = pyre_extensions . ParameterSpecification ( " TParams " ) def doesnt_care_positional ( * args : object ) -> None : pass def doesnt_care_keywords ( ** kwargs : object ) -> None : pass def does_care_positional ( * args : int ) -> None : pass def does_care_keywords ( ** kwargs : int ) -> None : pass def outer ( f : Callable [ TParams , int ] ) -> Callable [ TParams , None ] : def foo ( * args : TParams . args , ** kwargs : TParams . kwargs ) -> None : doesnt_care_positional ( * args ) doesnt_care_keywords ( ** kwargs ) does_care_positional ( * args ) does_care_keywords ( ** kwargs ) f ( * args , ** kwargs ) return foo } | [ " Incompatible parameter type [ 6 ] : In call ` does_care_positional ` , for 1st positional only \ parameter expected ` int ` but got ` object ` . " ; " Incompatible parameter type [ 6 ] : In call ` does_care_keywords ` , for 1st positional only \ parameter expected ` int ` but got ` object ` . " ; ] ; ( )
let test_user_defined_parameter_specification_classes context = let assert_type_errors = assert_type_errors ~ context in assert_type_errors { | from typing import Callable , ParamSpec TParams = ParamSpec ( " TParams " ) def client ( f : Callable [ TParams , int ] ) -> None : def inner ( * args : TParams . args , ** kwargs : TParams . kwargs ) -> int : return f ( * args , ** kwargs ) } | [ ] ; assert_type_errors { | from typing import Callable from typing_extensions import ParamSpec TParams = ParamSpec ( " TParams " ) def client ( f : Callable [ TParams , int ] ) -> None : def inner ( * args : TParams . args , ** kwargs : TParams . kwargs ) -> int : return f ( * args , ** kwargs ) } | [ ] ; assert_type_errors { | from pyre_extensions import ParameterSpecification from typing import TypeVar , Generic , Callable TParams = ParameterSpecification ( " TParams " ) TReturn = TypeVar ( " TReturn " ) def function ( param : str ) -> str : . . . class MyClass ( Generic [ TParams , TReturn ] ) : f : Callable [ TParams , TReturn ] def __init__ ( self , f : Callable [ TParams , TReturn ] ) -> None : self . f = f def call ( __self , * args : TParams . args , ** kwargs : TParams . kwargs ) -> TReturn : f = __self . f # do some logging or something return f ( * args , ** kwargs ) def client ( f : Callable [ TParams , TReturn ] ) -> MyClass [ TParams , TReturn ] : return MyClass ( f ) def foo ( ) -> None : x = client ( function ) . call ( param " " ) = reveal_type ( x ) client ( function ) . call ( parm " " ) = } | [ " Revealed type [ - 1 ] : Revealed type for ` x ` is ` str ` . " ; " Unexpected keyword [ 28 ] : Unexpected keyword argument ` parm ` to call ` MyClass . call ` . " ; ] ; assert_type_errors { | from pyre_extensions import ParameterSpecification from typing import TypeVar , Generic , Callable TParams = ParameterSpecification ( " TParams " ) TReturn = TypeVar ( " TReturn " ) def client ( f : Callable [ TParams , TReturn ] ) -> None : def inner ( __x : int , * args : TParams . args , ** kwargs : TParams . kwargs ) -> TReturn : return f ( * args , ** kwargs ) reveal_type ( inner ) } | [ " Revealed type [ - 1 ] : Revealed type for ` inner ` is \ ` typing . Callable [ pyre_extensions . type_variable_operators . Concatenate [ int , test . TParams ] , \ Variable [ TReturn ] ] ` . " ; ] ; assert_type_errors { | from pyre_extensions import ParameterSpecification from typing import TypeVar , Generic , Callable , Protocol TParams = ParameterSpecification ( " TParams " ) TReturn = TypeVar ( " TReturn " ) class CallableReturningInt ( Protocol [ TParams ] ) : def __call__ ( __self , __f : int , * args : TParams . args , ** kwargs : TParams . kwargs ) -> int : . . . def remove_int_argument ( f : CallableReturningInt [ TParams ] ) -> Callable [ TParams , int ] : . . . def goof ( x : int , y : str ) -> int : return x def foo ( ) -> None : f = remove_int_argument ( goof ) reveal_type ( f ) } | [ " Revealed type [ - 1 ] : Revealed type for ` f ` is ` typing . Callable [ [ Named ( y , str ) ] , int ] ` . " ] ; assert_type_errors { | from pyre_extensions import ParameterSpecification from pyre_extensions . type_variable_operators import Concatenate from typing import TypeVar , Generic , Callable , Protocol TParams = ParameterSpecification ( " TParams " ) TReturn = TypeVar ( " TReturn " ) def remove_int_argument ( f : Callable [ Concatenate [ int , TParams ] , str ] ) -> Callable [ TParams , int ] : def inner ( * args : TParams . args , ** kwargs : TParams . kwargs ) -> int : s = f ( 75 , * args , ** kwargs ) return int ( s ) return inner def goof ( x : int , y : str ) -> str : return str ( x ) def foo ( ) -> None : f = remove_int_argument ( goof ) reveal_type ( f ) } | [ " Revealed type [ - 1 ] : Revealed type for ` f ` is ` typing . Callable [ [ Named ( y , str ) ] , int ] ` . " ] ; assert_type_errors { | from typing import Protocol from pyre_extensions import ParameterSpecification from typing import TypeVar , Generic , Callable TParams = ParameterSpecification ( " TParams " ) TReturn = TypeVar ( " TReturn " ) TSelf = TypeVar ( " TSelf " ) class ObjectMethod ( Protocol [ TSelf , TParams , TReturn ] ) : def __call__ ( __self , __other_self : TSelf , * args : TParams . args , ** kwargs : TParams . kwargs ) -> TReturn : . . . def track_assertion ( assertion : ObjectMethod [ " TestCommand " , TParams , None ] ) -> ObjectMethod [ " TestCommand " , TParams , int ] : def assert_test ( __self : " TestCommand " , * args : TParams . args , ** kwargs : TParams . kwargs ) -> int : assertion ( __self , * args , ** kwargs ) return 7 return assert_test class TestCommand : @ track_assertion def method ( self : " TestCommand " , x : int ) -> None : pass def foo ( ) -> None : m = TestCommand ( ) . method reveal_type ( m ) } | [ " Revealed type [ - 1 ] : Revealed type for ` m ` is ` ObjectMethod [ TestCommand , [ Named ( x , int ) ] , \ int ] ` . " ; ] ; assert_type_errors { | from typing import Protocol from pyre_extensions import ParameterSpecification from pyre_extensions . type_variable_operators import Concatenate from typing import TypeVar , Generic , Callable TParams = ParameterSpecification ( " TParams " ) TReturn = TypeVar ( " TReturn " ) TSelf = TypeVar ( " TSelf " ) def track_assertion ( assertion : Callable [ Concatenate [ " TestCommand " , TParams ] , None ] ) -> Callable [ Concatenate [ " TestCommand " , TParams ] , int ] : def assert_test ( __self : " TestCommand " , * args : TParams . args , ** kwargs : TParams . kwargs ) -> int : assertion ( __self , * args , ** kwargs ) return 7 return assert_test class TestCommand : @ track_assertion def method ( self : " TestCommand " , x : int ) -> None : pass def foo ( ) -> None : m = TestCommand ( ) . method reveal_type ( m ) } | [ " Revealed type [ - 1 ] : Revealed type for ` m ` is ` BoundMethod [ typing . Callable [ [ TestCommand , \ Named ( x , int ) ] , int ] , TestCommand ] ` . " ; ] ; assert_type_errors { | from pyre_extensions import ParameterSpecification from pyre_extensions . type_variable_operators import Concatenate from typing import TypeVar , Generic , Callable , Protocol TParams = ParameterSpecification ( " TParams " ) TReturn = TypeVar ( " TReturn " ) def add_on_argument ( f : Callable [ TParams , str ] ) -> Callable [ Concatenate [ str , TParams ] , int ] : def inner ( first : str , , / * args : TParams . args , ** kwargs : TParams . kwargs ) -> int : s = f ( * args , ** kwargs ) return int ( s ) return inner def goof ( x : int ) -> str : return str ( x ) def foo ( ) -> None : f = add_on_argument ( goof ) reveal_type ( f ) } | [ " Revealed type [ - 1 ] : Revealed type for ` f ` is ` typing . Callable [ [ str , Named ( x , int ) ] , int ] ` . " ] ; assert_type_errors { | from pyre_extensions import ParameterSpecification from typing import TypeVar , Generic , Callable TParams = ParameterSpecification ( " TParams " ) class MyClass ( Generic [ TParams ] ) : def __call__ ( __self , * args : TParams . args , ** kwargs : TParams . kwargs ) -> bool : . . . IntStrParamSpec = MyClass [ int , str ] def foo ( ) -> None : f : IntStrParamSpec reveal_type ( f ) f ( 1 , " hello " ) f ( " invalid " ) } | [ " Revealed type [ - 1 ] : Revealed type for ` f ` is ` MyClass [ [ int , str ] ] ` . " ; " Missing argument [ 20 ] : Call ` MyClass . __call__ ` expects argument in position 2 . " ; ] ; assert_type_errors { | from pyre_extensions import ParameterSpecification from typing import TypeVar , Generic , Callable , Protocol TParams = ParameterSpecification ( " TParams " ) class PrependIntProtocol ( Protocol [ TParams ] ) : def __call__ ( __self , __f : int , * args : TParams . args , ** kwargs : TParams . kwargs ) -> int : . . . IntBoolStrParamSpec = PrependIntProtocol [ bool , str ] def foo ( ) -> None : f : IntBoolStrParamSpec reveal_type ( f ) f ( 1 , True , " hello " ) f ( " invalid " ) } | [ " Revealed type [ - 1 ] : Revealed type for ` f ` is ` PrependIntProtocol [ [ bool , str ] ] ` . " ; " Missing argument [ 20 ] : Call ` PrependIntProtocol . __call__ ` expects argument in position 2 . " ; ] ; ( )
let test_duplicate_type_variables context = let assert_type_errors = assert_type_errors ~ context in assert_type_errors { | from typing import TypeVar , Generic T = TypeVar ( " T " ) S = TypeVar ( " S " ) class A ( Generic [ T , S , T ] ) : pass } | [ " Duplicate type variables [ 59 ] : Duplicate type variable ` T ` in Generic [ . . . ] . " ] ; assert_type_errors { | from typing import TypeVar , Protocol T = TypeVar ( " T " ) class A ( Protocol [ T , T , T ] ) : pass } | [ " Duplicate type variables [ 59 ] : Duplicate type variable ` T ` in Protocol [ . . . ] . " ] ; assert_type_errors { | from typing import Generic from pyre_extensions import ParameterSpecification P = ParameterSpecification ( " P " ) class A ( Generic [ P , P ] ) : pass } | [ " Duplicate type variables [ 59 ] : Duplicate type variable ` P ` in Generic [ . . . ] . " ] ; ( )
let test_generic_aliases context = let assert_type_errors = assert_type_errors ~ context in assert_type_errors { | from typing import List MyList = List [ int ] x : MyList reveal_type ( x ) reveal_type ( x [ 0 ] ) } | [ " Revealed type [ - 1 ] : Revealed type for ` x ` is ` List [ int ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` x [ 0 ] ` is ` int ` . " ; ] ; assert_type_errors { | from typing import Tuple , TypeVar T = TypeVar ( " T " ) Pair = Tuple [ T , T ] x : Pair [ str ] reveal_type ( x ) reveal_type ( x [ 0 ] ) } | [ " Revealed type [ - 1 ] : Revealed type for ` x ` is ` Tuple [ str , str ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` x [ 0 ] ` is ` str ` . " ; ] ; assert_type_errors { | from typing import TypeVar , Union T = TypeVar ( " T " ) UnionWithInt = Union [ T , int ] x : UnionWithInt [ str ] reveal_type ( x ) } | [ " Revealed type [ - 1 ] : Revealed type for ` x ` is ` Union [ int , str ] ` . " ] ; assert_type_errors { | from typing import List , Tuple , TypeVar , Union T = TypeVar ( " T " ) Alias1 = Union [ T , int ] Alias2 = Tuple [ T , Alias1 [ T ] ] Alias3 = List [ Alias2 [ T ] ] x : Alias3 [ str ] reveal_type ( x ) } | [ " Revealed type [ - 1 ] : Revealed type for ` x ` is ` List [ Tuple [ str , Union [ int , str ] ] ] ` . " ] ; assert_type_errors { | from typing import * T = TypeVar ( " T " ) MyList1 = List [ T ] MyList2 = MyList1 [ int ] MyList3 = MyList2 xs : MyList3 [ str ] reveal_type ( xs ) } | [ " Revealed type [ - 1 ] : Revealed type for ` xs ` is ` typing . List [ int ] ` . " ] ; let sources_exporting_generic_classes = [ { Test . handle = " foo . py " ; source = { | from typing import Generic , TypeVar T = TypeVar ( " T " ) class SomeGenericClass ( Generic [ T ] ) : . . . } ; | } ; { handle = " baz . py " ; source = { | from typing import Dict , Generic , Iterable , Optional , Sequence , Union , TypeVar from foo import SomeGenericClass } ; | } ; ] in assert_type_errors ~ update_environment_with : sources_exporting_generic_classes { | from baz import * from typing import List as MyList reveal_type ( Optional ) reveal_type ( Union ) reveal_type ( MyList ) reveal_type ( Iterable ) reveal_type ( SomeGenericClass ) } | [ " Revealed type [ - 1 ] : Revealed type for ` baz . Optional ` is ` typing . Type [ typing . Optional ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` baz . Union ` is ` typing . Type [ typing . Union ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` typing . List ` is ` typing . Type [ list ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` baz . Iterable ` is ` typing . Type [ typing . Iterable ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` baz . SomeGenericClass ` is \ ` typing . Type [ foo . SomeGenericClass ] ` . " ; ] ; assert_type_errors ~ update_environment_with : sources_exporting_generic_classes { | from baz import * from typing import List as MyList , TypeVar z : MyList [ int ] = [ " hello " ] z2 : Iterable [ int ] = [ " hello " ] z3 : SomeGenericClass [ int ] = [ " hello " ] } | [ " Incompatible variable type [ 9 ] : z is declared to have type ` MyList [ int ] ` but is used as \ type ` MyList [ str ] ` . " ; " Incompatible variable type [ 9 ] : z2 is declared to have type ` Iterable [ int ] ` but is used as \ type ` Iterable [ str ] ` . " ; " Incompatible variable type [ 9 ] : z3 is declared to have type ` SomeGenericClass [ int ] ` but is \ used as type ` MyList [ str ] ` . " ; ] ; assert_type_errors ~ update_environment_with : sources_exporting_generic_classes { | from baz import * x : Optional [ Dict [ str , int ] ] reveal_type ( x ) } | [ " Revealed type [ - 1 ] : Revealed type for ` x ` is ` typing . Optional [ typing . Dict [ str , int ] ] ` . " ] ; let sources_exporting_generic_classes = [ { Test . handle = " bar / baz . py " ; source = { | from typing import Callable } ; | } ; ] in assert_type_errors ~ update_environment_with : sources_exporting_generic_classes { | from bar . baz import Callable def foo ( ) -> None : reveal_type ( Callable ) f : Callable [ [ int ] , str ] y = f ( 1 ) reveal_type ( y ) } | [ " Revealed type [ - 1 ] : Revealed type for ` bar . baz . Callable ` is ` typing . Type [ typing . Callable ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` y ` is ` str ` . " ; ] ; assert_type_errors { | from typing import Callable C = Callable def foo ( ) -> None : f : C [ [ int ] , str ] reveal_type ( f ) } | [ " Invalid type parameters [ 24 ] : Generic type ` Callable ` expects 2 type parameters . " ; " Revealed type [ - 1 ] : Revealed type for ` f ` is ` typing . Callable [ [ int ] , str ] ` . " ; ] ; assert_type_errors { | from typing import Callable , Iterable , Iterator , TypeVar T = TypeVar ( " T " ) Predicate = Callable [ [ T ] , int ] def dropwhile ( predicate : Predicate [ T ] , iterable : Iterable [ T ] ) -> Iterator [ T ] : . . . def foo ( ) -> None : reveal_type ( dropwhile ) } | [ " Revealed type [ - 1 ] : Revealed type for ` test . dropwhile ` is \ ` typing . Callable ( dropwhile ) [ [ Named ( predicate , typing . Callable [ [ Variable [ T ] ] , int ] ) , \ Named ( iterable , Iterable [ Variable [ T ] ] ) ] , Iterator [ Variable [ T ] ] ] ` . " ; ] ; assert_type_errors { | from typing import TypeVar , Iterable as MyIterable , List as MyList T = TypeVar ( " T " ) class Base : . . . class Child ( Base ) : . . . xs : MyIterable [ Child ] # No error , since Iterable is covariant . ys : MyIterable [ Base ] = xs xs : MyList [ Child ] # Error because List is invariant . ys : MyList [ Base ] = xs } | [ " Incompatible variable type [ 9 ] : ys is declared to have type ` MyList [ Base ] ` but is used as \ type ` MyList [ Child ] ` . " ; ] ; assert_type_errors { | from typing import Tuple , TypeVar T = TypeVar ( " T " ) Pair = Tuple [ T , T ] y : Pair reveal_type ( y ) } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` Tuple [ typing . Any , typing . Any ] ` . " ] ; assert_type_errors { | from typing import Tuple , TypeVar T = TypeVar ( " T " ) Pair = Tuple [ T , T ] y : Pair [ int , str ] reveal_type ( y ) } | [ " Invalid type variable [ 34 ] : The type variable ` Variable [ T ] ` can only be used to annotate \ generic classes or functions . " ; " Revealed type [ - 1 ] : Revealed type for ` y ` is ` typing . Any ` . " ; ] ; assert_type_errors { | from typing import Tuple , TypeVar T1 = TypeVar ( " T1 " ) T2 = TypeVar ( " T2 " ) Pair = Tuple [ T1 , T2 ] y : Pair [ int ] reveal_type ( y ) y : Pair [ int , str ] reveal_type ( y ) } | [ " Invalid type variable [ 34 ] : The type variable ` Variable [ T1 ] ` can only be used to annotate \ generic classes or functions . " ; " Invalid type variable [ 34 ] : The type variable ` Variable [ T2 ] ` can only be used to annotate \ generic classes or functions . " ; " Revealed type [ - 1 ] : Revealed type for ` y ` is ` typing . Any ` . " ; " Revealed type [ - 1 ] : Revealed type for ` y ` is ` Tuple [ int , str ] ` . " ; ] ; assert_type_errors { | from typing import Any , Tuple , TypeVar T = TypeVar ( " T " ) Pair = Tuple [ str , int ] y : Pair reveal_type ( y ) y : Pair [ str ] reveal_type ( y ) } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` Tuple [ str , int ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` y ` is ` Tuple [ str , int ] ` . " ; ] ; assert_type_errors { | from typing import * T = TypeVar ( " T " ) MyList = List [ T ] def foo ( x : T , y : MyList ) -> MyList : return y foo ( 1 , [ ' hello ' ] ) foo ( ' some ' , [ ' hello ' ] ) reveal_type ( foo ( 1 , [ ' hello ' ] ) ) } | [ " Revealed type [ - 1 ] : Revealed type for ` test . foo ( 1 , [ " \ hello " ] ) ` \ is \ ` typing . List [ typing . Any ] ` . " ; ] ; assert_type_errors { | from typing import * MyList = List def foo ( x : MyList ) -> MyList : . . . reveal_type ( foo ) reveal_type ( foo ( [ ' hello ' ] ) ) } | [ " Invalid type parameters [ 24 ] : Generic type ` list ` expects 1 type parameter , use \ ` typing . List ` to avoid runtime subscripting errors . " ; " Revealed type [ - 1 ] : Revealed type for ` test . foo ` is ` typing . Callable ( foo ) [ [ Named ( x , \ typing . List [ typing . Any ] ) ] , typing . List [ typing . Any ] ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` test . foo ( [ " \ hello " ] ) ` \ is ` typing . List [ typing . Any ] ` . " ; ] ; assert_type_errors { | from typing import List , Tuple , TypeVar , Union T1 = TypeVar ( " T1 " ) T2 = TypeVar ( " T2 " ) T3 = TypeVar ( " T3 " ) Alias2Before3 = Tuple [ T1 , Union [ T2 , T3 ] , T2 ] Alias3Before2 = Tuple [ T1 , Union [ T3 , T2 ] , T2 ] x : Alias2Before3 [ int , str , bool ] reveal_type ( x ) y : Alias3Before2 [ int , str , bool ] reveal_type ( y ) } | [ " Revealed type [ - 1 ] : Revealed type for ` x ` is ` Tuple [ int , Union [ bool , str ] , str ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` y ` is ` Tuple [ int , Union [ bool , str ] , str ] ` . " ; ] ; ( )
let test_recursive_aliases context = let assert_type_errors = assert_type_errors ~ context in assert_type_errors { | from typing import Tuple , Union Tree = Union [ int , Tuple [ " Tree " , " Tree " ] ] x : Tree reveal_type ( x ) } | [ " Revealed type [ - 1 ] : Revealed type for ` x ` is ` test . Tree ( resolves to Union [ Tuple [ Tree , \ Tree ] , int ] ) ` . " ; ] ; assert_type_errors { | from typing import Tuple , Union Tree = Union [ int , Tuple [ " Tree " , " Tree " ] ] x : Tree some_int : int x = some_int tuple_int : Tuple [ int , int ] x = tuple_int tuple_tuple_int : Tuple [ Tuple [ int , int ] , int ] x = tuple_tuple_int } | [ ] ; assert_type_errors { | from typing import Tuple , Union Tree = Union [ int , Tuple [ " Tree " , " Tree " ] ] x : Tree x = 1 x = ( 2 , 3 ) x = ( ( 4 , 5 ) , ( 6 , 7 ) ) } | [ ] ; assert_type_errors { | from typing import Tuple , Union Tree = Union [ int , Tuple [ " Tree " , " Tree " ] ] x : Tree some_str : str x = some_str tuple_int_str : Tuple [ int , str ] x = tuple_int_str } | [ " Incompatible variable type [ 9 ] : x is declared to have type ` test . Tree ( resolves to \ Union [ Tuple [ Tree , Tree ] , int ] ) ` but is used as type ` str ` . " ; " Incompatible variable type [ 9 ] : x is declared to have type ` test . Tree ( resolves to \ Union [ Tuple [ Tree , Tree ] , int ] ) ` but is used as type ` Tuple [ int , str ] ` . " ; ] ; assert_type_errors { | from typing import Tuple , Union Tree = Union [ int , Tuple [ " Tree " , " Tree " ] ] x : Tree x = " hello " x = ( 1 , " hello " ) x = ( ( 2 , 3 ) , ( 4 , " hello " ) ) } | [ " Incompatible variable type [ 9 ] : x is declared to have type ` test . Tree ( resolves to \ Union [ Tuple [ Tree , Tree ] , int ] ) ` but is used as type ` str ` . " ; " Incompatible variable type [ 9 ] : x is declared to have type ` test . Tree ( resolves to \ Union [ Tuple [ Tree , Tree ] , int ] ) ` but is used as type ` Tuple [ int , str ] ` . " ; " Incompatible variable type [ 9 ] : x is declared to have type ` test . Tree ( resolves to \ Union [ Tuple [ Tree , Tree ] , int ] ) ` but is used as type ` Tuple [ Tuple [ int , int ] , Tuple [ int , \ str ] ] ` . " ; ] ; assert_type_errors { | from typing import Mapping , Union StringDict = Union [ str , Mapping [ str , " StringDict " ] ] valid : StringDict = { " hello " : { " world " : " from here " } } contains_int : StringDict = { " hello " : { " world " : 1 } } } | [ " Incompatible variable type [ 9 ] : contains_int is declared to have type ` test . StringDict \ ( resolves to Union [ Mapping [ str , StringDict ] , str ] ) ` but is used as type ` Dict [ str , \ Dict [ str , int ] ] ` . " ; ] ; assert_type_errors { | from typing import List , Tuple Tree = Tuple [ str , List [ " Tree " ] ] tree : Tree = ( " foo " , [ ] ) tree2 : Tree = ( " foo " , [ ( " branch1 " , [ ( " leaf1 " , [ ] ) ] ) , ( " leaf2 " , [ ] ) ] ) } | [ ] ; assert_type_errors { | from typing import List , Union X = List [ " X " ] def foo ( ) -> None : x : X = [ [ ] , [ [ ] , [ ] ] , [ ] ] } | [ ] ; assert_type_errors { | from typing import Mapping , Union StringMapping = Union [ str , Mapping [ str , " StringMapping " ] ] d : Mapping [ str , str ] d2 : StringMapping = d } | [ ] ; assert_type_errors { | from typing import Dict , Union StringDict = Union [ str , Dict [ str , " StringDict " ] ] d : Dict [ str , str ] d2 : StringDict = d } | [ " Incompatible variable type [ 9 ] : d2 is declared to have type ` test . StringDict ( resolves to \ Union [ Dict [ str , StringDict ] , str ] ) ` but is used as type ` Dict [ str , str ] ` . " ; ] ; assert_type_errors { | from typing import Tuple , Union X = Union [ int , Tuple [ int , " X " ] ] Y = Union [ int , Tuple [ int , " Y " ] ] x : X y : Y = x y2 : Y x2 : X = y2 } | [ ] ; assert_type_errors { | from typing import Tuple , Union X = Union [ int , Tuple [ int , " X " ] ] NotQuiteIsomorphicToX = Union [ int , Tuple [ str , " NotQuiteIsomorphicToX " ] ] x : X not_quite_isomorphic : NotQuiteIsomorphicToX = x not_quite_isomorphic2 : NotQuiteIsomorphicToX x2 : X = not_quite_isomorphic2 } | [ " Incompatible variable type [ 9 ] : not_quite_isomorphic is declared to have type \ ` test . NotQuiteIsomorphicToX ( resolves to Union [ Tuple [ str , NotQuiteIsomorphicToX ] , int ] ) ` \ but is used as type ` test . X ( resolves to Union [ Tuple [ int , X ] , int ] ) ` . " ; " Incompatible variable type [ 9 ] : x2 is declared to have type ` test . X ( resolves to \ Union [ Tuple [ int , X ] , int ] ) ` but is used as type ` test . NotQuiteIsomorphicToX ( resolves to \ Union [ Tuple [ str , NotQuiteIsomorphicToX ] , int ] ) ` . " ; ] ; assert_type_errors { | from typing import Tuple , Union X = Union [ int , Tuple [ int , " X " ] ] unrolled : Tuple [ int , X ] x : X = unrolled } | [ ] ; assert_type_errors { | from typing import Tuple , Union X = Union [ int , Tuple [ int , " X " ] ] unrolled : Tuple [ int , X ] unrolled2 : Tuple [ int , X ] = unrolled } | [ ] ; assert_type_errors { | from typing import Tuple , Union X = Union [ int , Tuple [ int , " X " ] ] unrolled_union : Union [ int , Tuple [ int , X ] ] x : X = unrolled_union x2 : X unrolled_union2 : Union [ int , Tuple [ int , X ] ] = x2 } | [ ] ; assert_type_errors { | from typing import Tuple , Union X = Union [ int , Tuple [ int , " X " ] ] x : X unrolled_multiple_times : Union [ int , Tuple [ int , Union [ int , Tuple [ int , X ] ] ] ] = x unrolled_multiple_times2 : Union [ int , Tuple [ int , Union [ int , Tuple [ int , X ] ] ] ] x2 : X = unrolled_multiple_times2 } | [ ] ; assert_type_errors { | from typing import Tuple , Union X = Union [ int , Tuple [ int , " X " ] ] unrolled_once : Union [ int , Tuple [ int , X ] ] unrolled_multiple_times : Union [ int , Tuple [ int , Union [ int , Tuple [ int , X ] ] ] ] unrolled_once = unrolled_multiple_times unrolled_once2 : Union [ int , Tuple [ int , X ] ] unrolled_multiple_times2 : Union [ int , Tuple [ int , Union [ int , Tuple [ int , X ] ] ] ] unrolled_multiple_times2 = unrolled_once2 } | [ ] ; assert_type_errors { | from typing import Tuple , Union X = Union [ int , Tuple [ int , " X " ] ] x : X y : Union [ int , Tuple [ int , int ] ] = x } | [ " Incompatible variable type [ 9 ] : y is declared to have type ` Union [ Tuple [ int , int ] , int ] ` \ but is used as type ` test . X ( resolves to Union [ Tuple [ int , X ] , int ] ) ` . " ; ] ; assert_type_errors { | from typing import Tuple , Union X = Union [ int , Tuple [ int , " X " ] ] def foo ( x : X , n : int ) -> X : result = x for i in range ( n ) : result = ( i , result ) reveal_type ( result ) reveal_type ( result ) return result } | [ " Revealed type [ - 1 ] : Revealed type for ` result ` is ` Tuple [ int , test . X ( resolves to \ Union [ Tuple [ int , X ] , int ] ) ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` result ` is ` test . X ( resolves to Union [ Tuple [ int , X ] , \ int ] ) ` . " ; ] ; assert_type_errors { | from typing import Tuple , Union Tree = Union [ int , Tuple [ " Tree " , " Tree " ] ] def foo ( tree : Tree , some_bool : bool ) -> Tree : if some_bool : x = 42 else : x = ( 1 , ( 2 , tree ) ) return x } | [ ] ; assert_type_errors { | from typing import Tuple , Union Tree = Union [ int , Tuple [ " Tree " , " Tree " ] ] Unrolled = Union [ int , Tuple [ Union [ int , Tuple [ " Unrolled " , " Unrolled " ] ] , " Unrolled " ] ] def foo ( some_bool : bool ) -> Tree : tree : Tree unrolled_tree : Unrolled if some_bool : x = tree else : x = unrolled_tree return x } | [ ] ; Type . RecursiveType . Namespace . reset ( ) ; assert_type_errors { | from typing import Tuple , Union Tree = Union [ int , Tuple [ " Tree " , " Tree " ] ] # str instead of int . Wrong = Union [ int , Tuple [ Union [ str , Tuple [ " Wrong " , " Wrong " ] ] , " Wrong " ] ] def foo ( some_bool : bool ) -> Tree : tree : Tree wrong_unrolled_tree : Wrong if some_bool : x = tree else : x = wrong_unrolled_tree return x } | [ " Incompatible return type [ 7 ] : Expected ` test . Tree ( resolves to Union [ Tuple [ Tree , Tree ] , \ int ] ) ` but got ` $ RecursiveType1 ( resolves to Union [ Tuple [ Union [ Tuple [ $ RecursiveType1 , \ $ RecursiveType1 ] , str ] , $ RecursiveType1 ] , Tuple [ $ RecursiveType1 , $ RecursiveType1 ] , int ] ) ` . " ; ] ; assert_type_errors { | from typing import Final from typing_extensions import Literal x : Final [ str ] = " x " y : Literal [ " y " ] = " y " reveal_type ( x ) reveal_type ( y ) } | [ " Revealed type [ - 1 ] : Revealed type for ` x ` is ` str ` ( inferred : \ ` typing_extensions . Literal [ ' x ' ] ` , final ) . " ; " Revealed type [ - 1 ] : Revealed type for ` y ` is ` typing_extensions . Literal [ ' y ' ] ` . " ; ] ; assert_type_errors { | x : str = " x " reveal_type ( x ) } | [ " Revealed type [ - 1 ] : Revealed type for ` x ` is ` str ` ( inferred : \ ` typing_extensions . Literal [ ' x ' ] ` ) . " ; ] ; assert_type_errors { | from typing_extensions import TypeAlias MyInt = int X : TypeAlias = " MyInt " y : X reveal_type ( y ) } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` int ` . " ] ; assert_type_errors { | from typing import List , Union X = List [ Union [ int , " X " ] ] def foo ( ) -> None : x : X y = x [ 0 ] reveal_type ( y ) } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` Union [ int , test . X ( resolves to List [ Union [ X , \ int ] ] ) ] ` . " ; ] ; assert_type_errors { | from typing import Dict , Union D = Dict [ str , Union [ str , " D " ] ] def foo ( d : D ) -> None : y = d [ " hello " ] reveal_type ( y ) if isinstance ( y , str ) : reveal_type ( y ) else : z = y [ " world " ] reveal_type ( z ) } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` Union [ str , test . D ( resolves to Dict [ str , \ Union [ D , str ] ] ) ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` y ` is ` str ` . " ; " Revealed type [ - 1 ] : Revealed type for ` z ` is ` Union [ str , test . D ( resolves to Dict [ str , \ Union [ D , str ] ] ) ] ` . " ; ] ; assert_type_errors { | from typing import Union D = Union [ int , " D " ] D2 = Union [ int , " D2 " ] def foo ( ) -> None : d : D reveal_type ( d ) d2 : D2 d = d2 } | [ " Missing global annotation [ 5 ] : Globally accessible variable ` D ` has no type specified . " ; " Missing global annotation [ 5 ] : Globally accessible variable ` D2 ` has no type specified . " ; " Undefined or invalid type [ 11 ] : Annotation ` D ` is not defined as a type . " ; " Revealed type [ - 1 ] : Revealed type for ` d ` is ` typing . Any ` . " ; " Undefined or invalid type [ 11 ] : Annotation ` D2 ` is not defined as a type . " ; ] ; assert_type_errors { | from typing import List , Union NestedList = List [ Union [ int , " NestedList " ] ] def pass_spurious_parameter ( x : NestedList [ int ] ) -> None : reveal_type ( x ) } | [ " Revealed type [ - 1 ] : Revealed type for ` x ` is ` test . NestedList ( resolves to \ List [ Union [ NestedList , int ] ] ) ` . " ; ] ; assert_type_errors { | from typing import Tuple , TypeVar , Union T = TypeVar ( " T " ) GenericTree = Union [ T , Tuple [ " GenericTree [ T ] " , " GenericTree [ T ] " ] ] def foo ( x : GenericTree [ int ] ) -> None : reveal_type ( x ) } | [ " Missing global annotation [ 5 ] : Globally accessible variable ` GenericTree ` has no type \ specified . " ; " Undefined or invalid type [ 11 ] : Annotation ` GenericTree ` is not defined as a type . " ; " Revealed type [ - 1 ] : Revealed type for ` x ` is ` unknown ` . " ; ] ; assert_type_errors { | from typing import List , Union X = List [ " X " ] Y = Union [ int , X ] def foo ( ) -> None : y : Y y == y reveal_type ( y ) } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` Union [ int , test . X ( resolves to List [ X ] ) ] ` . " ] ; assert_type_errors { | from typing import List , Sequence , Union class Foo : . . . X = Union [ Sequence [ " X " ] , List [ " X " ] ] Y = Union [ Foo , X ] def foo ( ) -> None : y : Y y == y reveal_type ( y ) } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` Union [ Foo , test . X ( resolves to Union [ List [ X ] , \ Sequence [ X ] ] ) ] ` . " ; ] ; assert_type_errors { | from typing import List , Sequence , Union class Foo : . . . X = Union [ Sequence [ " X " ] , List [ " X " ] ] Y = Union [ Foo , X ] Z = List [ Y ] def foo ( ) -> None : z : Z reveal_type ( z ) } | [ " Revealed type [ - 1 ] : Revealed type for ` z ` is ` List [ Union [ Foo , test . X ( resolves to \ Union [ List [ X ] , Sequence [ X ] ] ) ] ] ` . " ; ] ; ( )
let test_variadic_tuples context = let assert_type_errors = assert_type_errors ~ context in assert_type_errors { | from typing import Tuple from pyre_extensions import TypeVarTuple , Unpack Ts = TypeVarTuple ( " Ts " ) def foo ( x : Tuple [ int , Unpack [ Ts ] ] ) -> Tuple [ bool , Unpack [ Ts ] ] : . . . def bar ( ) -> None : x : Tuple [ int , str , bool ] y = foo ( x ) reveal_type ( y ) x2 : Tuple [ int ] y2 = foo ( x2 ) reveal_type ( y2 ) } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` Tuple [ bool , str , bool ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` y2 ` is ` Tuple [ bool ] ` . " ; ] ; assert_type_errors { | from typing import Tuple from pyre_extensions import TypeVarTuple , Unpack Ts = TypeVarTuple ( " Ts " ) def foo ( x : Tuple [ int , Unpack [ Ts ] , str ] ) -> Tuple [ bool , Unpack [ Ts ] ] : . . . def bar ( ) -> None : x : Tuple [ int ] foo ( x ) } | [ " Incompatible parameter type [ 6 ] : In call ` foo ` , for 1st positional only parameter expected \ ` typing . Tuple [ int , * test . Ts , str ] ` but got ` Tuple [ int ] ` . " ; ] ; assert_type_errors { | from typing import Tuple from pyre_extensions import TypeVarTuple , Unpack Ts = TypeVarTuple ( " Ts " ) def add_int ( xs : Tuple [ Unpack [ Ts ] ] ) -> Tuple [ int , Unpack [ Ts ] ] : . . . def remove_int ( xs : Tuple [ int , Unpack [ Ts ] ] ) -> Tuple [ Unpack [ Ts ] ] : . . . def generic_function ( xs : Tuple [ Unpack [ Ts ] ] ) -> None : y = remove_int ( add_int ( xs ) ) reveal_type ( y ) add_int ( remove_int ( xs ) ) } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` typing . Tuple [ * test . Ts ] ` . " ; " Incompatible parameter type [ 6 ] : In call ` remove_int ` , for 1st positional only parameter \ expected ` typing . Tuple [ int , * test . Ts ] ` but got ` typing . Tuple [ * test . Ts ] ` . " ; ] ; assert_type_errors { | from typing import Tuple from pyre_extensions import TypeVarTuple , Unpack Ts = TypeVarTuple ( " Ts " ) def expects_same_tuples ( x : Tuple [ Unpack [ Ts ] ] , y : Tuple [ Unpack [ Ts ] ] ) -> Tuple [ Unpack [ Ts ] ] : . . . def bar ( ) -> None : tuple1 : Tuple [ int , str ] tuple2 : Tuple [ bool , bool ] expects_same_tuples ( tuple1 , tuple2 ) } | [ " Incompatible parameter type [ 6 ] : In call ` expects_same_tuples ` , for 2nd positional only \ parameter expected ` typing . Tuple [ * test . Ts ] ` but got ` Tuple [ bool , bool ] ` . " ; ] ; assert_type_errors { | from typing import Tuple from pyre_extensions import TypeVarTuple , Unpack Ts = TypeVarTuple ( " Ts " ) def expects_same_tuples ( x : Tuple [ Unpack [ Ts ] ] , y : Tuple [ Unpack [ Ts ] ] ) -> Tuple [ Unpack [ Ts ] ] : . . . def bar ( ) -> None : tuple1 : Tuple [ int , str ] shorter_tuple : Tuple [ bool ] expects_same_tuples ( tuple1 , shorter_tuple ) } | [ " Incompatible parameter type [ 6 ] : In call ` expects_same_tuples ` , for 2nd positional only \ parameter expected ` typing . Tuple [ * test . Ts ] ` but got ` Tuple [ bool ] ` . " ; ] ; assert_type_errors { | from typing import Tuple from pyre_extensions import TypeVarTuple , Unpack Ts = TypeVarTuple ( " Ts " ) def expects_same_tuples ( x : Tuple [ Unpack [ Ts ] ] , y : Tuple [ Unpack [ Ts ] ] ) -> Tuple [ Unpack [ Ts ] ] : . . . def bar ( ) -> None : tuple1 : Tuple [ int , str ] shorter_tuple : Tuple [ bool ] expects_same_tuples ( tuple1 , shorter_tuple ) } | [ " Incompatible parameter type [ 6 ] : In call ` expects_same_tuples ` , for 2nd positional only \ parameter expected ` typing . Tuple [ * test . Ts ] ` but got ` Tuple [ bool ] ` . " ; ] ; assert_type_errors { | from typing import Tuple from pyre_extensions import TypeVarTuple , Unpack Ts = TypeVarTuple ( " Ts " ) def add_int ( xs : Tuple [ Unpack [ Tuple [ str , . . . ] ] ] ) -> Tuple [ int , Unpack [ Tuple [ str , . . . ] ] ] : . . . def foo ( ) -> None : xs : Tuple [ str , str ] y = add_int ( xs ) reveal_type ( y ) invalid : Tuple [ int , str ] add_int ( invalid ) } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` typing . Tuple [ int , * Tuple [ str , . . . ] ] ` . " ; " Incompatible parameter type [ 6 ] : In call ` add_int ` , for 1st positional only parameter \ expected ` typing . Tuple [ str , . . . ] ` but got ` Tuple [ int , str ] ` . " ; ] ; assert_type_errors { | from typing import Tuple from pyre_extensions import TypeVarTuple , Unpack Ts = TypeVarTuple ( " Ts " ) def foo ( xs : Tuple [ Unpack [ Ts ] ] ) -> Tuple [ Unpack [ Ts ] ] : . . . def baz ( ) -> None : unbounded_tuple : Tuple [ int , . . . ] y = foo ( unbounded_tuple ) reveal_type ( y ) } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` typing . Tuple [ int , . . . ] ` . " ] ; assert_type_errors { | from typing import Tuple , TypeVar from pyre_extensions import TypeVarTuple , Unpack T = TypeVar ( " T " ) Ts = TypeVarTuple ( " Ts " ) def foo ( xs : Tuple [ T , Unpack [ Tuple [ str , . . . ] ] ] ) -> T : . . . def baz ( ) -> None : some_tuple : Tuple [ int , str , str ] y = foo ( some_tuple ) reveal_type ( y ) invalid_tuple : Tuple [ int , str , int ] foo ( invalid_tuple ) } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` int ` . " ; " Incompatible parameter type [ 6 ] : In call ` foo ` , for 1st positional only parameter expected \ ` typing . Tuple [ Variable [ T ] , * Tuple [ str , . . . ] ] ` but got ` Tuple [ int , str , int ] ` . " ; ] ; assert_type_errors { | from typing import Any , Tuple , TypeVar from pyre_extensions import TypeVarTuple , Unpack Ts = TypeVarTuple ( " Ts " ) N = TypeVar ( " N " , bound = int ) def foo ( x : Tuple [ N , Unpack [ Ts ] ] ) -> Tuple [ Unpack [ Ts ] , N ] : . . . def bar ( ) -> None : x : Tuple [ Any , . . . ] y = foo ( x ) reveal_type ( y ) x2 : Tuple [ int , . . . ] y2 = foo ( x2 ) reveal_type ( y2 ) } | [ " Prohibited any [ 33 ] : Explicit annotation for ` x ` cannot contain ` Any ` . " ; " Revealed type [ - 1 ] : Revealed type for ` y ` is ` typing . Tuple [ * Tuple [ typing . Any , . . . ] , \ typing . Any ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` y2 ` is ` typing . Tuple [ * Tuple [ int , . . . ] , int ] ` . " ; ] ; assert_type_errors { | from typing import Any , Tuple , TypeVar from pyre_extensions import TypeVarTuple , Unpack Ts = TypeVarTuple ( " Ts " ) N = TypeVar ( " N " , bound = int ) def foo ( x : Tuple [ N , Unpack [ Ts ] ] ) -> Tuple [ Unpack [ Ts ] , N ] : . . . def bar ( ) -> None : x_error : Tuple [ str , . . . ] y_error = foo ( x_error ) reveal_type ( y_error ) } | [ " Incomplete type [ 37 ] : Type ` typing . Tuple [ * test . Ts , Variable [ N ( bound to int ) ] ] ` inferred \ for ` y_error ` is incomplete , add an explicit annotation . " ; " Incompatible parameter type [ 6 ] : In call ` foo ` , for 1st positional only parameter expected \ ` typing . Tuple [ Variable [ N ( bound to int ) ] , * test . Ts ] ` but got ` typing . Tuple [ str , . . . ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` y_error ` is ` typing . Tuple [ * Tuple [ typing . Any , . . . ] , \ typing . Any ] ` . " ; ] ; assert_type_errors { | from typing import Any , Tuple , TypeVar N = TypeVar ( " N " , bound = int ) def foo ( x : Tuple [ N ] ) -> Tuple [ N ] : . . . def bar ( ) -> None : x : Tuple [ int , . . . ] y = foo ( x ) reveal_type ( y ) x_error : Tuple [ str , . . . ] foo ( x_error ) } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` Tuple [ int ] ` . " ; " Incompatible parameter type [ 6 ] : In call ` foo ` , for 1st positional only parameter expected \ ` Tuple [ Variable [ N ( bound to int ) ] ] ` but got ` typing . Tuple [ str , . . . ] ` . " ; ] ; ( )
let test_variadic_classes context = let assert_type_errors = assert_type_errors ~ context in assert_type_errors { | from typing import Generic from pyre_extensions import TypeVarTuple , Unpack Ts = TypeVarTuple ( " Ts " ) class Tensor ( Generic [ Unpack [ Ts ] ] ) : . . . def add_bool ( x : Tensor [ int , Unpack [ Ts ] , str ] ) -> Tensor [ bool , Unpack [ Ts ] ] : . . . def foo ( ) -> None : x : Tensor [ int , bool , str ] y = add_bool ( x ) reveal_type ( y ) } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` Tensor [ bool , bool ] ` . " ] ; assert_type_errors { | from typing import Generic from pyre_extensions import TypeVarTuple , Unpack Ts = TypeVarTuple ( " Ts " ) class Tensor ( Generic [ Unpack [ Ts ] ] ) : . . . def expects_same_tensors ( x : Tensor [ Unpack [ Ts ] ] , y : Tensor [ Unpack [ Ts ] ] ) -> Tensor [ Unpack [ Ts ] ] : . . . def bar ( ) -> None : tensor : Tensor [ int , str ] tensor2 : Tensor [ bool , bool ] y = expects_same_tensors ( tensor , tensor2 ) reveal_type ( y ) } | [ " Incompatible parameter type [ 6 ] : In call ` expects_same_tensors ` , for 2nd positional only \ parameter expected ` Tensor [ * test . Ts ] ` but got ` Tensor [ bool , bool ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` y ` is ` Tensor [ int , str ] ` . " ; ] ; assert_type_errors { | from typing import Generic from pyre_extensions import TypeVarTuple , Unpack Ts = TypeVarTuple ( " Ts " ) class Tensor ( Generic [ Unpack [ Ts ] ] ) : . . . def expects_same_length ( xs : Tensor [ Unpack [ Ts ] ] , ys : Tensor [ Unpack [ Ts ] ] ) -> Tensor [ Unpack [ Ts ] ] : . . . def bar ( ) -> None : xs : Tensor [ int , str ] ys : Tensor [ bool ] expects_same_length ( xs , ys ) } | [ " Incompatible parameter type [ 6 ] : In call ` expects_same_length ` , for 2nd positional only \ parameter expected ` Tensor [ * test . Ts ] ` but got ` Tensor [ bool ] ` . " ; ] ; assert_type_errors { | from typing import Generic , List , Protocol , Tuple , TypeVar from pyre_extensions import TypeVarTuple , Unpack T = TypeVar ( " T " ) Ts = TypeVarTuple ( " Ts " ) class Tensor ( Generic [ T , Unpack [ Ts ] ] ) : . . . class Base : . . . class Child ( Base ) : . . . def foo ( x : Tensor [ float , Base , Base ] ) -> None : . . . def bar ( ) -> None : child : Tensor [ float , Child , Child ] foo ( child ) int_tensor : Tensor [ int , Base , Base ] foo ( int_tensor ) } | [ " Incompatible parameter type [ 6 ] : In call ` foo ` , for 1st positional only parameter expected \ ` Tensor [ float , Base , Base ] ` but got ` Tensor [ int , Base , Base ] ` . " ; ] ; assert_type_errors { | from typing import Generic , TypeVar from pyre_extensions import TypeVarTuple , Unpack from typing_extensions import Literal as L Ts = TypeVarTuple ( " Ts " ) Tin = TypeVar ( " Tin " ) Tout = TypeVar ( " Tout " ) class Tensor ( Generic [ Unpack [ Ts ] ] ) : . . . class Linear ( Generic [ Tin , Tout ] ) : " " " Transform the last dimension from Tin to Tout . " " " def __init__ ( self , in_dimension : Tin , out_dimension : Tout ) -> None : self . in_dimension = in_dimension self . out_dimension = out_dimension def __call__ ( self , x : Tensor [ Unpack [ Ts ] , Tin ] ) -> Tensor [ Unpack [ Ts ] , Tout ] : . . . def bar ( ) -> None : x : Tensor [ L [ 10 ] , L [ 20 ] ] layer1 = Linear ( 20 , 30 ) layer2 = Linear ( 30 , 40 ) layer3 = Linear ( 40 , 50 ) y = layer3 ( layer2 ( layer1 ( x ) ) ) reveal_type ( y ) shape_mismatch = ( 10 , 21 ) layer1 ( shape_mismatch ) } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` Tensor [ typing_extensions . Literal [ 10 ] , \ typing_extensions . Literal [ 50 ] ] ` . " ; " Incompatible parameter type [ 6 ] : In call ` Linear . __call__ ` , for 1st positional only \ parameter expected ` Tensor [ * test . Ts , typing_extensions . Literal [ 20 ] ] ` but got \ ` Tuple [ typing_extensions . Literal [ 10 ] , typing_extensions . Literal [ 21 ] ] ` . " ; ] ; assert_type_errors { | from typing import Generic from pyre_extensions import TypeVarTuple , Unpack Ts = TypeVarTuple ( " Ts " ) class Tensor ( Generic [ Unpack [ Ts ] ] ) : def some_method ( self , x : Tensor [ Unpack [ Ts ] ] ) -> None : . . . def bar ( ) -> None : xs : Tensor [ int , str ] xs . some_method ( xs ) } | [ ] ; assert_type_errors { | from typing import Generic , TypeVar from pyre_extensions import TypeVarTuple , Unpack T = TypeVar ( " T " ) Ts = TypeVarTuple ( " Ts " ) class Tensor ( Generic [ T , Unpack [ Ts ] ] ) : . . . def bar ( ) -> None : x = Tensor . __getitem__ reveal_type ( x ) } | [ " Revealed type [ - 1 ] : Revealed type for ` x ` is \ ` BoundMethod [ typing . Callable ( typing . GenericMeta . __getitem__ ) [ [ Named ( self , unknown ) , \ typing . Tuple [ typing . Type [ Variable [ T ] ] , typing . Any ] ] , typing . Type [ Tensor [ Variable [ T ] , \ typing . Any ] ] ] , typing . Type [ Tensor ] ] ` . " ; ] ; assert_type_errors { | from typing import Generic , List , Protocol , Tuple , TypeVar from pyre_extensions import TypeVarTuple , Unpack T = TypeVar ( " T " ) Ts = TypeVarTuple ( " Ts " ) class VariadicProtocol ( Protocol [ T , Unpack [ Ts ] ] ) : def foo ( self , x : Tuple [ T , Unpack [ Ts ] ] ) -> None : . . . class Tensor ( Generic [ Unpack [ Ts ] ] ) : " " " This implements VariadicProtocol with T = List [ int ] and Ts = Tuple [ Unpack [ Ts ] ] . " " " def foo ( self , x : Tuple [ List [ int ] , Unpack [ Ts ] ] ) -> None . . . : def accepts_variadic_protocol ( x : VariadicProtocol [ T , Unpack [ Ts ] ] ) -> VariadicProtocol [ T , Unpack [ Ts ] ] : . . . def bar ( ) -> None : x : Tensor [ int , str ] y = accepts_variadic_protocol ( x ) reveal_type ( y ) } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` VariadicProtocol [ List [ int ] , int , str ] ` . " ] ; assert_type_errors { | from typing import Generic , Tuple , TypeVar from pyre_extensions import TypeVarTuple , Unpack T = TypeVar ( " T " ) Ts = TypeVarTuple ( " Ts " ) class Tensor ( Generic [ T , Unpack [ Ts ] ] ) : def __init__ ( self , default : T , shape : Tuple [ Unpack [ Ts ] ] ) -> None : . . . class Base : . . . class Child ( Base ) : . . . def expects_base ( t : Tensor [ int , Base , Base ] ) -> None : . . . def bar ( ) -> None : expects_base ( Tensor ( 1 , ( Child ( ) , Child ( ) ) ) ) } | [ " Incompatible parameter type [ 6 ] : In call ` expects_base ` , for 1st positional only parameter \ expected ` Tensor [ int , Base , Base ] ` but got ` Tensor [ int , Child , Child ] ` . " ; ] ; assert_type_errors { | from typing import Generic , TypeVar from pyre_extensions import TypeVarTuple , Unpack from typing_extensions import Literal as L T = TypeVar ( " T " ) Ts = TypeVarTuple ( " Ts " ) class Tensor ( Generic [ T , Unpack [ Ts ] ] ) : . . . FloatTensor = Tensor [ float , Unpack [ Ts ] ] def bar ( ) -> None : x : FloatTensor [ L [ 10 ] , L [ 20 ] ] reveal_type ( x ) y : FloatTensor reveal_type ( y ) } | [ " Revealed type [ - 1 ] : Revealed type for ` x ` is ` Tensor [ float , typing_extensions . Literal [ 10 ] , \ typing_extensions . Literal [ 20 ] ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` y ` is ` Tensor [ float , * Tuple [ typing . Any , . . . ] ] ` . " ; ] ; assert_type_errors { | from typing import Generic , Tuple , TypeVar from pyre_extensions import TypeVarTuple , Unpack from typing_extensions import Literal as L T = TypeVar ( " T " ) Ts = TypeVarTuple ( " Ts " ) class Tensor ( Generic [ T , Unpack [ Ts ] ] ) : . . . def get_last_type ( t : Tensor [ float , Unpack [ Tuple [ int , . . . ] ] , T ] ) -> T : . . . def bar ( ) -> None : x : Tensor [ float , L [ 10 ] , L [ 20 ] ] y = get_last_type ( x ) reveal_type ( y ) } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` typing_extensions . Literal [ 20 ] ` . " ] ; assert_type_errors { | from typing import Generic , Tuple , TypeVar from pyre_extensions import TypeVarTuple , Unpack from typing_extensions import Literal as L T = TypeVar ( " T " ) Ts = TypeVarTuple ( " Ts " ) class Tensor ( Generic [ T , Unpack [ Ts ] ] ) : . . . # pyre - ignore [ 24 ] : Generic type ` Tensor ` expects at least 1 type parameter . def accept_arbitrary_tensor ( t : Tensor ) -> Tensor : . . . def bar ( ) -> None : x : Tensor [ float , L [ 10 ] , L [ 20 ] ] y = accept_arbitrary_tensor ( x ) reveal_type ( y ) # pyre - ignore [ 24 ] : Generic type ` Tensor ` expects at least 1 type parameter . no_parameters : Tensor accept_arbitrary_tensor ( no_parameters ) } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` Tensor [ typing . Any , * Tuple [ typing . Any , . . . ] ] ` . " ] ; assert_type_errors { | from typing import Generic , Tuple , TypeVar from pyre_extensions import TypeVarTuple , Unpack from typing_extensions import Literal as L T = TypeVar ( " T " ) Ts = TypeVarTuple ( " Ts " ) class Tensor ( Generic [ T , Unpack [ Ts ] ] ) : . . . def strip_last ( x : Tensor [ int , Unpack [ Ts ] , int ] ) -> Tensor [ int , Unpack [ Ts ] ] : . . . def bar ( ) -> None : invalid : Tensor [ int , L [ 10 ] , str ] y = strip_last ( invalid ) reveal_type ( y ) } | [ " Incomplete type [ 37 ] : Type ` Tensor [ int , * test . Ts ] ` inferred for ` y ` is incomplete , add an \ explicit annotation . " ; " Incompatible parameter type [ 6 ] : In call ` strip_last ` , for 1st positional only parameter \ expected ` Tensor [ int , * test . Ts , int ] ` but got ` Tensor [ int , int , str ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` y ` is ` Tensor [ int , * Tuple [ typing . Any , . . . ] ] ` . " ; ] ; assert_type_errors { | from typing import Callable , Generic , Tuple , TypeVar from pyre_extensions import ParameterSpecification , TypeVarTuple , Unpack from typing_extensions import Literal as L T = TypeVar ( " T " ) Ts = TypeVarTuple ( " Ts " ) TParams = ParameterSpecification ( " TParams " ) class Tensor ( Generic [ T , TParams , Unpack [ Ts ] ] ) : def __init__ ( self , f : Callable [ TParams , T ] , shape : Tuple [ Unpack [ Ts ] ] ) -> None : self . f = f self . shape = shape def bar ( ) -> None : tensor : Tensor [ float , [ int , str ] , int , str ] y = tensor . f ( * tensor . shape ) reveal_type ( y ) tensor . f ( " wrong argument " ) } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` float ` . " ; " Missing argument [ 20 ] : PositionalOnly call expects argument in position 1 . " ; ] ; ( )
let test_variadic_callables context = let assert_type_errors = assert_type_errors ~ context in assert_type_errors { | from typing import Callable , Tuple from pyre_extensions import TypeVarTuple , Unpack Ts = TypeVarTuple ( " Ts " ) def make_tuple ( leave_this_out : int , * args : Unpack [ Ts ] , message : str ) -> Tuple [ Unpack [ Ts ] , bool ] : . . . def foo ( ) -> None : y = make_tuple ( 1 , 2 , 3 , message " = hello " ) reveal_type ( y ) y2 = make_tuple ( 1 , message " = hello " ) reveal_type ( y2 ) } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` Tuple [ typing_extensions . Literal [ 2 ] , \ typing_extensions . Literal [ 3 ] , bool ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` y2 ` is ` Tuple [ bool ] ` . " ; ] ; assert_type_errors { | from typing import Callable , Tuple from pyre_extensions import TypeVarTuple , Unpack Ts = TypeVarTuple ( " Ts " ) def make_tuple ( leave_this_out : int , * args : Unpack [ Tuple [ int , Unpack [ Ts ] , str ] ] , message : str ) -> Tuple [ int , Unpack [ Ts ] , str ] : return args def foo ( ) -> None : y = make_tuple ( 1 , 2 , 3 , " has to end with a string " , message " = hello " ) reveal_type ( y ) } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` Tuple [ int , typing_extensions . Literal [ 3 ] , str ] ` . " ; ] ; assert_type_errors { | from typing import Callable , Tuple from pyre_extensions import TypeVarTuple , Unpack Ts = TypeVarTuple ( " Ts " ) def make_tuple ( * args : Unpack [ Tuple [ int , Unpack [ Ts ] , str ] ] ) -> None : . . . def foo ( x : Tuple [ Unpack [ Ts ] ] ) -> None : unbounded_tuple : Tuple [ int , . . . ] make_tuple ( 1 , * unbounded_tuple , " foo " ) make_tuple ( * unbounded_tuple , " foo " ) unbounded_str_tuple : Tuple [ str , . . . ] make_tuple ( * unbounded_str_tuple , " foo " ) } | [ " Invalid argument [ 32 ] : Argument types ` * Tuple [ str , . . . ] , typing_extensions . Literal [ ' foo ' ] ` \ are not compatible with expected variadic elements ` int , * test . Ts , str ` . " ; ] ; assert_type_errors { | from typing import Callable , Tuple from pyre_extensions import TypeVarTuple , Unpack Ts = TypeVarTuple ( " Ts " ) def make_tuple ( * args : Unpack [ Tuple [ int , Unpack [ Ts ] , str ] ] ) -> None : . . . def foo ( x : Tuple [ Unpack [ Ts ] ] ) -> None : make_tuple ( 1 , 2 ) make_tuple ( 1 , * x , * x , " foo " ) } | [ " Invalid argument [ 32 ] : Argument types ` typing_extensions . Literal [ 1 ] , \ typing_extensions . Literal [ 2 ] ` are not compatible with expected variadic elements ` int , \ * test . Ts , str ` . " ; " Invalid argument [ 32 ] : Variadic type variable ` int , * test . Ts , str ` cannot be made to \ contain ` typing_extensions . Literal [ 1 ] , * test . Ts , * test . Ts , \ typing_extensions . Literal [ ' foo ' ] ` ; concatenation of multiple variadic type variables is not \ yet implemented . " ; ] ; assert_type_errors { | from typing import Callable , Tuple , TypeVar from pyre_extensions import TypeVarTuple , Unpack Ts = TypeVarTuple ( " Ts " ) def strip_int_parameter ( f : Callable [ [ int , Unpack [ Ts ] ] , None ] ) -> Callable [ [ Unpack [ Ts ] ] , None ] : . . . def foo ( x : int , y : str , z : bool ) -> None : . . . def baz ( ) -> None : f = strip_int_parameter ( foo ) reveal_type ( f ) # Valid f ( " hello " , True ) # Error f ( " hello " ) } | [ " Revealed type [ - 1 ] : Revealed type for ` f ` is ` typing . Callable [ [ str , bool ] , None ] ` . " ; " Missing argument [ 20 ] : PositionalOnly call expects argument in position 1 . " ; ] ; assert_type_errors { | from typing import Callable , Tuple , TypeVar from pyre_extensions import TypeVarTuple , Unpack Ts = TypeVarTuple ( " Ts " ) def strip_int_parameter ( f : Callable [ [ int , Unpack [ Ts ] ] , None ] ) -> Callable [ [ Unpack [ Ts ] ] , None ] : . . . def no_leading_int ( y : str , z : bool ) -> None : . . . def foo ( ) -> None : strip_int_parameter ( no_leading_int ) } | [ " Incompatible parameter type [ 6 ] : In call ` strip_int_parameter ` , for 1st positional only \ parameter expected ` typing . Callable [ [ Variable ( int , * test . Ts ) ] , None ] ` but got \ ` typing . Callable ( no_leading_int ) [ [ Named ( y , str ) , Named ( z , bool ) ] , None ] ` . " ; ] ; assert_type_errors { | from typing import Callable , Generic , Tuple , TypeVar from pyre_extensions import TypeVarTuple , Unpack T = TypeVar ( " T " ) Ts = TypeVarTuple ( " Ts " ) class Tensor ( Generic [ Unpack [ Ts ] ] ) : def some_method ( self , * args : Unpack [ Ts ] ) -> Tuple [ Unpack [ Ts ] ] : . . . def bar ( ) -> None : x : Tensor [ int , str ] y = x . some_method ( 1 , " hello " ) reveal_type ( y ) x . some_method ( " invalid " ) } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` Tuple [ int , str ] ` . " ; " Missing argument [ 20 ] : Call ` Tensor . some_method ` expects argument in position 2 . " ; ] ; assert_type_errors { | from typing import Callable , Tuple , TypeVar from pyre_extensions import TypeVarTuple , Unpack Ts = TypeVarTuple ( " Ts " ) T = TypeVar ( " T " ) def apply ( f : Callable [ [ Unpack [ Ts ] ] , T ] , * args : Unpack [ Ts ] ) -> T : . . . def foo ( x : int , y : str , z : bool ) -> str : . . . def bar ( a : int , b : str , c : bool ) -> None : y = apply ( foo , a , b , c ) reveal_type ( y ) apply ( foo , a , b ) } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` str ` . " ; " Invalid argument [ 32 ] : Argument types ` int , str ` are not compatible with expected variadic \ elements ` * test . Ts ` . " ; ] ; assert_type_errors { | from typing import Callable , Tuple , TypeVar from pyre_extensions import TypeVarTuple , Unpack Ts = TypeVarTuple ( " Ts " ) T = TypeVar ( " T " ) def apply ( f : Callable [ [ Unpack [ Ts ] ] , T ] , * args : Unpack [ Ts ] ) -> T : . . . class Base : . . . class Child ( Base ) : . . . def expects_base ( x : int , y : str , z : Base ) -> str : . . . def expects_child ( x : int , y : str , z : Child ) -> str : . . . def bar ( ) -> None : child : Child apply ( expects_base , 1 , " hello " , child ) base : Base apply ( expects_child , 1 , " hello " , base ) } | [ " Invalid argument [ 32 ] : Argument types ` typing_extensions . Literal [ 1 ] , \ typing_extensions . Literal [ ' hello ' ] , test . Base ` are not compatible with expected variadic \ elements ` * test . Ts ` . " ; ] ; ( )
let ( ) = " typeVariable " >::: [ " check_bounded_variables " >:: test_check_bounded_variables ; " check_unbounded_variables " >:: test_check_unbounded_variables ; " check_variable_bindings " >:: test_check_variable_bindings ; " unbound_variables " >:: test_unbound_variables ; " distinguish " >:: test_distinguish ; " integer_variables " >:: test_integer_variables ; " nested_variable_error " >:: test_nested_variable_error ; " single_explicit_error " >:: test_single_explicit_error ; " callable_parameter_variadics " >:: test_callable_parameter_variadics ; " user_defined_parameter_variadics " >:: test_user_defined_parameter_specification_classes ; " duplicate_type_variables " >:: test_duplicate_type_variables ; " generic_aliases " >:: test_generic_aliases ; " recursive_aliases " >:: test_recursive_aliases ; " variadic_tuples " >:: test_variadic_tuples ; " variadic_classes " >:: test_variadic_classes ; " variadic_callables " >:: test_variadic_callables ; ] |> Test . run
module Make0 ( X : Named_intf . S0 ) : Typerepable . S with type t := X . t = struct module M = Make_typename . Make0 ( X ) let typerep_of_t = Typerep . Named ( M . named , None ) let typename_of_t = M . typename_of_t end
module Make1 ( X : Named_intf . S1 ) : Typerepable . S1 with type ' a t := ' a X . t = struct module M = Make_typename . Make1 ( X ) let typerep_of_t of_p1 = Typerep . Named ( M . named of_p1 , None ) let typename_of_t = M . typename_of_t end
module Make2 ( X : Named_intf . S2 ) : Typerepable . S2 with type ( ' a , ' b ) t := ( ' a , ' b ) X . t = struct module M = Make_typename . Make2 ( X ) let typerep_of_t of_p1 of_p2 = Typerep . Named ( M . named of_p1 of_p2 , None ) let typename_of_t = M . typename_of_t end
module Make3 ( X : Named_intf . S3 ) : Typerepable . S3 with type ( ' a , ' b , ' c ) t := ( ' a , ' b , ' c ) X . t = struct module M = Make_typename . Make3 ( X ) let typerep_of_t of_p1 of_p2 of_p3 = Typerep . Named ( M . named of_p1 of_p2 of_p3 , None ) let typename_of_t = M . typename_of_t end
module Make4 ( X : Named_intf . S4 ) : Typerepable . S4 with type ( ' a , ' b , ' c , ' d ) t := ( ' a , ' b , ' c , ' d ) X . t = struct module M = Make_typename . Make4 ( X ) let typerep_of_t of_p1 of_p2 of_p3 of_p4 = Typerep . Named ( M . named of_p1 of_p2 of_p3 of_p4 , None ) ; ; let typename_of_t = M . typename_of_t end
module Make5 ( X : Named_intf . S5 ) : Typerepable . S5 with type ( ' a , ' b , ' c , ' d , ' e ) t := ( ' a , ' b , ' c , ' d , ' e ) X . t = struct module M = Make_typename . Make5 ( X ) let typerep_of_t of_p1 of_p2 of_p3 of_p4 of_p5 = Typerep . Named ( M . named of_p1 of_p2 of_p3 of_p4 of_p5 , None ) ; ; let typename_of_t = M . typename_of_t end
type ' a writer = { size : ' a Size . sizer ; write : ' a Write . writer }
type ' a reader = { read : ' a Read . reader ; vtag_read : ( int -> ' a ) Read . reader }
type ' a t = { shape : Shape . t ; writer : ' a writer ; reader : ' a reader }
type ' a writer0 = ' a writer
type ' a reader0 = ' a reader
type ' a t0 = ' a t
module S1 = struct type ( ' a , ' b ) writer = ' a writer0 -> ' b writer0 type ( ' a , ' b ) reader = ' a reader0 -> ' b reader0 type ( ' a , ' b ) t = ' a t0 -> ' b t0 end
module S2 = struct type ( ' a , ' b , ' c ) writer = ' a writer0 -> ( ' b , ' c ) S1 . writer type ( ' a , ' b , ' c ) reader = ' a reader0 -> ( ' b , ' c ) S1 . reader type ( ' a , ' b , ' c ) t = ' a t0 -> ( ' b , ' c ) S1 . t end
module S3 = struct type ( ' a , ' b , ' c , ' d ) writer = ' a writer0 -> ( ' b , ' c , ' d ) S2 . writer type ( ' a , ' b , ' c , ' d ) reader = ' a reader0 -> ( ' b , ' c , ' d ) S2 . reader type ( ' a , ' b , ' c , ' d ) t = ' a t0 -> ( ' b , ' c , ' d ) S2 . t end
let variant_wrong_type name _buf ~ pos_ref _x = Common . raise_variant_wrong_type name ! pos_ref ; ;
let bin_writer_unit = { size = Size . bin_size_unit ; write = Write . bin_write_unit }
let bin_reader_unit = { read = Read . bin_read_unit ; vtag_read = variant_wrong_type " unit " } ; ;
let bin_unit = { shape = bin_shape_unit ; writer = bin_writer_unit ; reader = bin_reader_unit } ; ;
let bin_writer_bool = { size = Size . bin_size_bool ; write = Write . bin_write_bool }
let bin_reader_bool = { read = Read . bin_read_bool ; vtag_read = variant_wrong_type " bool " } ; ;
let bin_bool = { shape = bin_shape_bool ; writer = bin_writer_bool ; reader = bin_reader_bool } ; ;
let bin_writer_string = { size = Size . bin_size_string ; write = Write . bin_write_string }
let bin_reader_string = { read = Read . bin_read_string ; vtag_read = variant_wrong_type " string " } ; ;
let bin_string = { shape = bin_shape_string ; writer = bin_writer_string ; reader = bin_reader_string } ; ;
let bin_writer_bytes = { size = Size . bin_size_bytes ; write = Write . bin_write_bytes }
let bin_reader_bytes = { read = Read . bin_read_bytes ; vtag_read = variant_wrong_type " bytes " } ; ;
let bin_bytes = { shape = bin_shape_bytes ; writer = bin_writer_bytes ; reader = bin_reader_bytes } ; ;
let bin_writer_char = { size = Size . bin_size_char ; write = Write . bin_write_char }
let bin_reader_char = { read = Read . bin_read_char ; vtag_read = variant_wrong_type " char " } ; ;
let bin_char = { shape = bin_shape_char ; writer = bin_writer_char ; reader = bin_reader_char } ; ;
let bin_writer_int = { size = Size . bin_size_int ; write = Write . bin_write_int }
let bin_reader_int = { read = Read . bin_read_int ; vtag_read = variant_wrong_type " int " }
let bin_int = { shape = bin_shape_int ; writer = bin_writer_int ; reader = bin_reader_int }
let bin_writer_float = { size = Size . bin_size_float ; write = Write . bin_write_float }
let bin_reader_float = { read = Read . bin_read_float ; vtag_read = variant_wrong_type " float " } ; ;
let bin_float = { shape = bin_shape_float ; writer = bin_writer_float ; reader = bin_reader_float } ; ;
let bin_writer_int32 = { size = Size . bin_size_int32 ; write = Write . bin_write_int32 }
let bin_reader_int32 = { read = Read . bin_read_int32 ; vtag_read = variant_wrong_type " int32 " } ; ;
let bin_int32 = { shape = bin_shape_int32 ; writer = bin_writer_int32 ; reader = bin_reader_int32 } ; ;
let bin_writer_int64 = { size = Size . bin_size_int64 ; write = Write . bin_write_int64 }
let bin_reader_int64 = { read = Read . bin_read_int64 ; vtag_read = variant_wrong_type " int64 " } ; ;
let bin_int64 = { shape = bin_shape_int64 ; writer = bin_writer_int64 ; reader = bin_reader_int64 } ; ;
let bin_writer_nativeint = { size = Size . bin_size_nativeint ; write = Write . bin_write_nativeint } ; ;
let bin_reader_nativeint = { read = Read . bin_read_nativeint ; vtag_read = variant_wrong_type " nativeint " } ; ;
let bin_nativeint = { shape = bin_shape_nativeint ; writer = bin_writer_nativeint ; reader = bin_reader_nativeint } ; ;
let bin_writer_nat0 = { size = Size . bin_size_nat0 ; write = Write . bin_write_nat0 }
let bin_reader_nat0 = { read = Read . bin_read_nat0 ; vtag_read = variant_wrong_type " nat0 " } ; ;
let bin_nat0 = { shape = bin_shape_nat0 ; writer = bin_writer_nat0 ; reader = bin_reader_nat0 } ; ;
let bin_writer_ref bin_writer_el = { size = ( fun v -> Size . bin_size_ref bin_writer_el . size v ) ; write = ( fun buf ~ pos v -> Write . bin_write_ref bin_writer_el . write buf ~ pos v ) } ; ;
let bin_reader_ref bin_reader_el = { read = ( fun buf ~ pos_ref -> Read . bin_read_ref bin_reader_el . read buf ~ pos_ref ) ; vtag_read = variant_wrong_type " ref " } ; ;
let bin_shape_ref x1 = Shape . bin_shape_ref x1
let bin_ref bin_el = { shape = bin_shape_ref bin_el . shape ; writer = bin_writer_ref bin_el . writer ; reader = bin_reader_ref bin_el . reader } ; ;
let bin_writer_lazy bin_writer_el = { size = ( fun v -> Size . bin_size_lazy bin_writer_el . size v ) ; write = ( fun buf ~ pos v -> Write . bin_write_lazy bin_writer_el . write buf ~ pos v ) } ; ;
let bin_reader_lazy bin_reader_el = { read = ( fun buf ~ pos_ref -> Read . bin_read_lazy bin_reader_el . read buf ~ pos_ref ) ; vtag_read = variant_wrong_type " lazy " } ; ;
let bin_shape_lazy x1 = Shape . bin_shape_lazy x1