text
stringlengths
0
601k
type uint32 = float object method byteLength : int readonly_prop method slice : int -> int -> arrayBuffer t meth method slice_toEnd : int -> arrayBuffer t meth end
let arrayBuffer : ( int -> arrayBuffer t ) constr = Js . Unsafe . global . ## _ArrayBuffer object method buffer : arrayBuffer t readonly_prop method byteOffset : int readonly_prop method byteLength : int readonly_prop end object inherit arrayBufferView method _BYTES_PER_ELEMENT : int readonly_prop method length : int r...
type int8Array = ( int , Bigarray . int8_signed_elt ) typedArray
type uint8Array = ( int , Bigarray . int8_unsigned_elt ) typedArray
type int16Array = ( int , Bigarray . int16_signed_elt ) typedArray
type uint16Array = ( int , Bigarray . int16_unsigned_elt ) typedArray
type int32Array = ( int32 , Bigarray . int32_elt ) typedArray
type uint32Array = ( int32 , Bigarray . int32_elt ) typedArray
type float32Array = ( float , Bigarray . float32_elt ) typedArray
type float64Array = ( float , Bigarray . float64_elt ) typedArray = " caml_ba_kind_of_typed_array " ( ' a , ' b , Bigarray . c_layout ) Bigarray . Genarray . t -> ( ' a , ' b ) typedArray t = " caml_ba_to_typed_array " ( ' a , ' b ) typedArray t -> ( ' a , ' b , Bigarray . c_layout ) Bigarray . Genarray . t = " caml_ba...
let set : ( ' a , ' b ) typedArray t -> int -> ' a -> unit = fun a i v -> array_set ( Unsafe . coerce a ) i v
let get : ( ' a , ' b ) typedArray t -> int -> ' a optdef = fun a i -> Js . Unsafe . get a i
let unsafe_get : ( ' a , ' b ) typedArray t -> int -> ' a = fun a i -> Js . Unsafe . get a i object inherit arrayBufferView method getInt8 : int -> int meth method getUint8 : int -> int meth method getInt16 : int -> int meth method getInt16_ : int -> bool t -> int meth method getUint16 : int -> int meth method getUint1...
module Bigstring = struct type t = ( char , Bigarray . int8_unsigned_elt , Bigarray . c_layout ) Bigarray . Array1 . t external to_arrayBuffer : t -> arrayBuffer Js . t = " bigstring_to_array_buffer " external to_uint8Array : t -> uint8Array Js . t = " bigstring_to_typed_array " external of_arrayBuffer : arrayBuffer Js...
module String = struct external of_uint8Array : uint8Array Js . t -> string = " caml_string_of_array " let of_arrayBuffer ab = let uint8 = new % js uint8Array_fromBuffer ab in of_uint8Array uint8 end
module AnalysisErrorValue = struct type t = Error . t list let prefix = Prefix . make ( ) let description = " Raw analysis errors " end
module LocalAnnotationsValue = struct type t = LocalAnnotationMap . ReadOnly . t let prefix = Prefix . make ( ) let description = " Node type resolution " end
module RawErrors = Memory . FirstClass . NoCache . Make ( SharedMemoryKeys . ReferenceKey ) ( AnalysisErrorValue )
module LocalAnnotations = Memory . FirstClass . WithCache . Make ( SharedMemoryKeys . ReferenceKey ) ( LocalAnnotationsValue )
type t = { global_environment : AnnotatedGlobalEnvironment . t ; set_errors : Reference . t -> Error . t list -> unit ; set_local_annotations : Reference . t -> LocalAnnotationMap . ReadOnly . t -> unit ; invalidate : Reference . t list -> unit ; get_errors : Reference . t -> Error . t list ; get_local_annotations : Re...
let global_environment { global_environment ; _ } = global_environment
let ast_environment { global_environment ; _ } = AnnotatedGlobalEnvironment . ast_environment global_environment
let module_tracker type_environment = ast_environment type_environment |> AstEnvironment . module_tracker
let set_errors { set_errors ; _ } = set_errors
let get_errors { get_errors ; _ } = get_errors
let set_local_annotations { set_local_annotations ; _ } = set_local_annotations
let get_local_annotations { get_local_annotations ; _ } = get_local_annotations
let invalidate { invalidate ; _ } = invalidate
let create global_environment = let raw_errors = RawErrors . create ( ) in let local_annotations = LocalAnnotations . create ( ) in let get_errors reference = RawErrors . get raw_errors reference |> Option . value ~ default [ ] : in let set_errors = RawErrors . add raw_errors in let get_local_annotations = LocalAnnotat...
module ReadOnly = struct type t = { global_environment : AnnotatedGlobalEnvironment . ReadOnly . t ; get_errors : Reference . t -> Error . t list ; get_local_annotations : Reference . t -> LocalAnnotationMap . ReadOnly . t option ; } let create ( ? get_errors = fun _ -> [ ] ) ( ? get_local_annotations = fun _ -> None )...
let read_only { global_environment ; get_errors ; get_local_annotations ; _ } = ReadOnly . create ~ get_errors ~ get_local_annotations ( AnnotatedGlobalEnvironment . read_only global_environment )
let test_type_guard context = let assert_type_errors = assert_type_errors ~ context in let assert_default_type_errors = assert_default_type_errors ~ context in assert_default_type_errors { | from typing import Any , List , TypeGuard def is_str_list ( val : List [ Any ] ) -> TypeGuard [ List [ str ] ] : " " " Determines...
let test_multiple_arguments context = let assert_type_errors = assert_type_errors ~ context in let assert_default_type_errors = assert_default_type_errors ~ context in assert_type_errors { | from typing import TypeGuard , List def is_str_list ( val : List [ int | str ] , allow_empty : bool ) -> TypeGuard [ List [ str ]...
let test_methods context = let assert_type_errors = assert_type_errors ~ context in assert_type_errors { | from typing import List , TypeGuard class Foo : def is_str_list ( self , val : List [ int | str ] ) -> TypeGuard [ List [ str ] ] : return all ( isinstance ( x , str ) for x in val ) def bar ( self , xs : List [ i...
let test_callback context = let assert_type_errors = assert_type_errors ~ context in let assert_default_type_errors = assert_default_type_errors ~ context in assert_type_errors { | from typing import Callable , TypeGuard def simple_fn ( x : bool , is_int : Callable [ [ object ] , TypeGuard [ int ] ] ) -> None : val : i...
let test_return_type context = let assert_type_errors = assert_type_errors ~ context in let assert_default_type_errors = assert_default_type_errors ~ context in assert_type_errors { | from typing import List , TypeGuard def is_str_list ( val : List [ int | str ] ) -> TypeGuard [ List [ str ] ] : return all ( isinstance...
let test_walrus_operator context = let assert_type_errors = assert_type_errors ~ context in assert_type_errors { | from typing import TypeGuard def typeguard_fn ( val : int | str ) -> TypeGuard [ int ] : return True def foo ( x : int | str ) -> None : if typeguard_fn ( bar := x ) : reveal_type ( bar ) } | [ " Revealed ...
let test_boolean_operators context = let assert_type_errors = assert_type_errors ~ context in let assert_default_type_errors = assert_default_type_errors ~ context in assert_type_errors { | from typing import TypeGuard def typeguard_fn ( val : int | str ) -> TypeGuard [ int ] : return True def foo ( val : int | str ) -...
let test_misc context = let assert_type_errors = assert_type_errors ~ context in assert_type_errors { | from typing import TypeGuard , Union def typeguard_fn ( val : int | str ) -> TypeGuard [ int ] : return True def bool_fn ( val : int | str ) -> bool : return True def first_int ( x : int | str ) -> None : y = x if ty...
let ( ) = " type_guard " >::: [ " test_type_guard " >:: test_type_guard ; " test_multiple_arguments " >:: test_multiple_arguments ; " test_methods " >:: test_methods ; " test_callback " >:: test_callback ; " test_return_type " >:: test_return_type ; " test_walrus_operator " >:: test_walrus_operator ; " test_boolean_ope...
let filters : ( Sigs_util . plugin_name , Sigs_util . plugin ) Hashtbl . t = Hashtbl . create 30
let show_code = ref false
let print_collect_mtyps = ref false
let register ? filter ? position = function | ( name , transform ) -> if Hashtbl . mem filters name then eprintf " Warning :% s filter already exists . " !@ name else Hashtbl . add filters name { transform ; position ; filter }
let show_modules = function | ( ) -> ( Hashtbl . iter ( function | key -> ( function | _ -> Format . printf " % s @ " key ) ) filters ; print_newline ( ) )
let plugin_add = function | plugin -> ( ( try let v = Hashtbl . find filters plugin in function | ( ) -> if not @@ ( List . exists ( function | ( n , _ ) -> n = plugin ) ( ! State . current_filters ) ) then Ref . modify State . current_filters ( function | x -> cons ( plugin , v ) x ) else eprintf " < Warning > plugin ...
let plugin_remove = function | plugin -> Ref . modify State . current_filters ( function | x -> Listf . remove plugin x ) object inherit Objs . map method get_cur_mtyps : Sigs_util . mtyps method get_cur_and_types : Sigs_util . and_types method update_cur_and_types : ( Sigs_util . and_types -> Sigs_util . and_types ) -...
let make_filter = function | ( s , code ) -> let f = function | ( ` StExp ( _loc , ` Lid ( _ , s ' ) ) : Astf . stru ) when s = s ' -> Fill . stru _loc code | e -> e in ( ( " filter_ " ^ s ) , ( ( Objs . map_stru f ) # stru ) )
let iterate_code = function | sloc -> ( function | mtyps -> ( function | ( _ , ( x : Sigs_util . plugin ) ) -> ( function | acc -> let mtyps = match x . filter with | Some x -> Sigs_util . apply_filter x mtyps | None -> mtyps in let code = x . transform mtyps in ( match ( ( x . position ) , code ) with | ( Some x , Som...
let traversal = function | ( ) -> ( object ( self : ' self_type ) inherit Objs . map as super val mtyps_stack = ( Stack . create ( ) : Sigs_util . mtyps Stack . t ) val mutable cur_and_types = ( [ ] : Sigs_util . and_types ) val mutable and_group = false method get_cur_mtyps : Sigs_util . mtyps = Stack . top mtyps_stac...
let genenrate_type_code = function | _loc -> ( function | tdl -> ( function | ( ns : Astf . strings ) -> ( let x : Astf . stru = ` Type ( _loc , tdl ) in let ns = list_of_app ns [ ] in let filters = List . map ( function | ` Str ( sloc , n ) -> ( match Hashtblf . find_opt filters n with | None -> Locf . failf sloc " % ...
let ( ) = Ast2pt . generate_type_code := genenrate_type_code
let printf_output = if ( try Sys . getenv " DEBUG " = " 1 " with Not_found -> false ) then Format . std_formatter else Format . make_formatter ( fun _ _ _ -> ( ) ) ( fun ( ) -> ( ) )
let printf x = Format . fprintf printf_output x
let type_implicit_instance : ( Env . t -> Typedtree . module_expr -> Path . t -> Longident . t list -> type_expr list -> Typedtree . module_expr * type_expr list ) ref = ref ( fun _ -> assert false ) ) *
type pending_implicit = { implicit_id : Ident . t ; implicit_env : Env . t ; implicit_loc : Location . t ; implicit_type : Path . t * Longident . t list * type_expr list ; mutable implicit_constraints : ( type_expr * type_expr ) list ; implicit_argument : argument ; }
let unlink env unlink_on = let path_table = Hashtbl . create 7 in let add_constraint register path ty tyvar = let instance_list = try Hashtbl . find path_table path with Not_found -> [ ] in try let eq_args ( ty ' , _tyvar ' ) = TypeOps . equal ty ty ' || Ctype . equal env false [ ty ] [ ty ' ] in let _ty ' , tyvar ' = ...
let pending_implicits : pending_implicit list list ref = ref [ ]
let rec has_implicit ty = match ( repr ty ) . desc with | Tarrow ( Tarr_implicit id , _ , _ , _ ) -> true | Tarrow ( _ , _ , rhs , _ ) -> has_implicit rhs | _ -> false
let instantiate_one_implicit loc env id ty_arg ty_res = let inst = match ( repr ty_arg ) . desc with | Tpackage ( p , nl , tl ) -> { implicit_id = id ; implicit_env = env ; implicit_loc = loc ; implicit_type = ( p , nl , tl ) ; implicit_constraints = [ ] ; implicit_argument = { arg_flag = Tapp_implicit ; arg_expression...
let pack_implicit_ref : ( pending_implicit -> Path . t -> Typedtree . expression ) ref = ref ( fun _ _ -> assert false )
let pack_implicit inst path = ! pack_implicit_ref inst path
module Link = struct let to_path inst path = let subst = Subst . add_module inst . implicit_id path Subst . identity in List . iter ( fun ( ty , tyvar ) -> let ty = Subst . type_expr subst ty in let tyvar = Subst . type_expr subst tyvar in unify inst . implicit_env ty tyvar ) inst . implicit_constraints ; let expr = pa...
let reunify_constraint inst = let reunify ( ty , tyvar ) = try unify inst . implicit_env ty tyvar with _ -> ( ) in List . iter reunify inst . implicit_constraints
let reunify_constraints ( ) = List . iter ( List . iter reunify_constraint ) ! pending_implicits
let add_pending_implicits insts = pending_implicits := insts :: ! pending_implicits
let reset_pending_implicits ( ) = pending_implicits := [ ]
let generalize_implicits_ref : ( unit -> unit ) ref = ref ( fun ( ) -> assert false )
let generalize_implicits ( ) = ! generalize_implicits_ref ( )
let type_to_string type_ = type_ |> Format . asprintf " % a " Type . pp
let type_to_reference type_ = type_ |> type_to_string |> Reference . create
let expression_to_json expression = ` String ( expression |> Expression . sanitized |> Expression . show )
module SerializableReference = struct type t = Reference . t [ @@ deriving compare , sexp , hash , show ] let to_yojson reference = ` String ( Reference . show_sanitized reference ) module Map = Reference . Map . Tree module Set = Set . Make ( Reference ) end
module DefaultValue = struct type t = Expression . t option [ @@ deriving show ] let to_yojson value = value >>| expression_to_json |> Option . value ~ default ` : Null end
module AnnotationLocation = struct type t = { qualifier : SerializableReference . t ; path : string ; line : int ; } [ @@ deriving show , compare , to_yojson ] let create ~ lookup ~ qualifier ~ line = { qualifier ; path = lookup qualifier |> Option . value ~ default " " ; :* line } let from_location ~ lookup ~ qualifie...
module SerializableType = struct type t = Type . t [ @@ deriving show ] let to_yojson type_ = ` String ( type_to_string type_ ) end
module TypeAnnotation = struct type t = | Inferred of SerializableType . t | Given of Expression . t | Missing [ @@ deriving show ] let is_inferred = function | Inferred _ -> true | Given _ | Missing -> false let from_given maybe_expression = match maybe_expression with | Some expression -> Given expression | None -> M...
module AnnotationsByName = struct module Base = struct module type S = sig type t [ @@ deriving show , compare , to_yojson ] val identifying_name : t -> SerializableReference . t end module Make ( Value : S ) = struct type t = Value . t SerializableReference . Map . t let empty = SerializableReference . Map . empty let...
module GlobalAnnotation = struct module Value = struct type t = { name : SerializableReference . t ; location : AnnotationLocation . t ; annotation : SerializableType . t ; } [ @@ deriving show , to_yojson ] let compare { location = left ; _ } { location = right ; _ } = AnnotationLocation . compare left right let quali...
module AttributeAnnotation = struct module Value = struct type t = { parent : SerializableReference . t ; name : SerializableReference . t ; location : AnnotationLocation . t ; annotation : SerializableType . t ; } [ @@ deriving show , to_yojson ] let compare { location = left ; _ } { location = right ; _ } = Annotatio...
module DefineAnnotation = struct module Parameters = struct module Value = struct type t = { name : SerializableReference . t ; annotation : TypeAnnotation . t ; value : DefaultValue . t ; index : int ; } [ @@ deriving show , to_yojson ] let compare { index = left ; _ } { index = right ; _ } = Int . compare left right ...
module Inference = struct type target = | Return | Parameter of { name : Reference . t } | Global of { name : Reference . t ; location : Location . WithModule . t ; } | Attribute of { parent : Reference . t ; name : Reference . t ; location : Location . WithModule . t ; } [ @@ deriving show ] type raw = { type_ : Type ...
module LocalResult = struct type t = { globals : GlobalAnnotation . ByName . t ; attributes : AttributeAnnotation . ByName . t ; define : DefineAnnotation . t ; } [ @@ deriving show , to_yojson ] let define_name { define = { name ; _ } ; _ } = name let from_signature ~ global_resolution ~ lookup ~ qualifier { Node . va...
module GlobalResult = struct module DefineAnnotationsByName = struct module Value = struct type t = DefineAnnotation . t [ @@ deriving show , compare , to_yojson ] let identifying_name { DefineAnnotation . name ; _ } = name end include AnnotationsByName . Base . Make ( Value ) end type t = { globals : GlobalAnnotation ...
let environment_data scratch_project = let { ScratchProject . BuiltGlobalEnvironment . global_environment ; _ } = scratch_project |> ScratchProject . build_global_environment in global_environment , scratch_project . configuration
let type_inference_result ~ context ~ test_source = let scratch_project = ScratchProject . setup ~ context [ " test . py " , test_source ] in let global_environment , configuration = scratch_project |> environment_data in let scheduler = Test . mock_scheduler ( ) in let global_result = Service . Infer . run_infer ~ con...
let assert_json_equal ~ context ~ expected result = let expected = Yojson . Safe . from_string expected in assert_equal ~ ctxt : context ~ printer : Yojson . Safe . pretty_to_string ~ msg " : GlobalResult json " expected result
let assert_global_result ~ context ~ test_source ~ expected ( ) = let result = type_inference_result ~ context ~ test_source in result |> TypeInference . Data . GlobalResult . to_yojson |> assert_json_equal ~ context ~ expected
let serialization_test context = assert_global_result ~ context ~ test_source : { | import functools from sqlalchemy import Integer x = 1 + 1 class C : x = 1 + 1 def no_errors ( x : int ) -> int : return x @ functools . lru_cache ( 4 ) def needs_return ( y : C , x : Integer ) : return ( x , y ) } | ~ expected : { | { "...
let duplicate_define_test context = assert_global_result ~ context ~ test_source : { | import typing @ typing . overload def f ( x : int , y ) -> int : y = " y " return x @ typing . overload def f ( x : str , y ) -> str : y = " y " return x @ typing . overload def f ( x : float , y ) -> float : y = " y " return x } | ~...
let suppress_test context = assert_global_result ~ context ~ test_source { :| x = None class Foo : x = None } | ~ expected : { | { " globals " : [ ] , " attributes " : [ ] , " defines " : [ ] } } | ( )
let attribute_widen_test context = assert_global_result ~ context ~ test_source : { | class Foo : x = None def __init__ ( self ) -> None : self . x = 1 + 1 } | ~ expected : { | { " globals " : [ ] , " attributes " : [ { " parent " : " test . Foo " , " name " : " x " , " location " : { " qualifier " : " test " , " path ...
let ( ) = " typeInferenceAnalysisTest " >::: [ " serialization " >:: serialization_test ; " duplicates " >:: duplicate_define_test ; " suppress " >:: suppress_test ; " attribute_widen " >:: attribute_widen_test ; ] |> Test . run
let refine_local ~ resolution ~ name ~ annotation = match name_to_reference name with | Some reference -> Resolution . refine_local resolution ~ reference ~ annotation | None -> resolution
module ErrorMap = struct type key = { location : Location . t ; kind : int ; } [ @@ deriving compare , sexp ] module Map = Map . Make ( struct type nonrec t = key let compare = compare_key let sexp_of_t = sexp_of_key let t_of_sexp = key_of_sexp end ) let add ~ errors ( { Error . location = { Location . WithModule . sta...
module type Context = sig val configuration : Configuration . Analysis . t val qualifier : Reference . t val define : Define . t Node . t val resolution_fixpoint : LocalAnnotationMap . t option val error_map : TypeCheck . LocalErrorMap . t option end
module type Signature = sig type t [ @@ deriving eq ] val create : ? bottom : bool -> resolution : Resolution . t -> unit -> t val initial : resolution : Resolution . t -> t val initial_forward : resolution : Resolution . t -> t val initial_backward : forward : t -> t val widen_resolution_with_snapshots : t -> t includ...
module State ( Context : Context ) = struct module TypeCheckContext = struct let qualifier = Context . qualifier let debug = Context . configuration . debug let constraint_solving_style = Configuration . Analysis . default_constraint_solving_style let define = Context . define let resolution_fixpoint = Context . resolu...
let infer_local ~ configuration ~ global_resolution ~ source { : Source . source_path = { ModulePath . qualifier ; _ } ; _ } ~ define : ( { Node . location ; value = { Define . signature = { name ; _ } ; _ } as define } as define_node ) = let module State = State ( struct let configuration = configuration let qualifier...
let infer_parameters_from_parent ~ global_resolution ~ source { : Source . source_path = { ModulePath . qualifier ; _ } ; _ } ~ define ( { : Node . value = { Define . signature = { parent ; parameters ; _ } ; _ } ; _ } as define ) = let overridden_callable = parent >>| Reference . show >>= GlobalResolution . overrides ...