text
stringlengths 0
601k
|
---|
let test_parse_type_variable_declarations _ = let assert_parses_declaration expression expected = assert_equal ( Some expected ) ( Type . Variable . parse_declaration ( parse_single_expression expression ) ~ target ( : Reference . create " target " ) ) in let assert_declaration_does_not_parse expression = assert_equal None ( Type . Variable . parse_declaration ( parse_single_expression expression ) ~ target ( : Reference . create " target " ) ) in assert_parses_declaration " pyre_extensions . ParameterSpecification ( ' Tparams ' ) " ( Type . Variable . ParameterVariadic ( Type . Variable . Variadic . Parameters . create " target " ) ) ; assert_declaration_does_not_parse " pyre_extensions . ParameterSpecification ( ' Tparams ' , int , str ) " ; assert_parses_declaration " pyre_extensions . TypeVarTuple ( ' Ts ' ) " ( Type . Variable . TupleVariadic ( Type . Variable . Variadic . Tuple . create " target " ) ) ; assert_declaration_does_not_parse " pyre_extensions . TypeVarTuple ( ' Ts ' , covariant = True ) " ; ( ) |
let test_starred_annotation_expression _ = let assert_starred_expression concatenation expression = assert_equal ~ printer : Expression . show ~ cmp ( : fun left right -> Expression . location_insensitive_compare left right = 0 ) ( parse_single_expression ~ coerce_special_methods : true expression ) ( Type . OrderedTypes . to_starred_annotation_expression ~ expression : Type . expression concatenation ) in let variadic = Type . Variable . Variadic . Tuple . create " Ts " in assert_starred_expression ( Type . OrderedTypes . Concatenation . create ~ prefix [ ] : ~ suffix [ ] : variadic ) " pyre_extensions . Unpack [ Ts ] " ; assert_starred_expression ( Type . OrderedTypes . Concatenation . create ~ prefix [ : Type . integer ] ~ suffix [ : Type . string ] variadic ) " pyre_extensions . Unpack [ ( int , pyre_extensions . Unpack [ Ts ] , str ) ] " ; ( ) |
let test_concatenation_from_unpack_expression _ = let variadic = Type . Variable . Variadic . Tuple . create " Ts " in let assert_concatenation expression concatenation = let parse_annotation expression = let aliases ? replace_unbound_parameters_with_any : _ = function | " Ts " -> Some ( Type . VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | _ -> None in Type . create ~ aliases ( parse_single_expression ~ preprocess : true ( Expression . show expression ) ) in assert_equal ~ printer [ :% show : Type . t Type . OrderedTypes . Concatenation . t option ] ~ cmp [ :% equal : Type . t Type . OrderedTypes . Concatenation . t option ] concatenation ( Type . OrderedTypes . concatenation_from_unpack_expression ~ parse_annotation ( parse_single_expression ~ coerce_special_methods : true expression ) ) in assert_concatenation " pyre_extensions . Unpack [ Ts ] " ( Some ( Type . OrderedTypes . Concatenation . create ~ prefix [ ] : ~ suffix [ ] : variadic ) ) ; assert_concatenation " pyre_extensions . Unpack [ typing . Tuple [ int , pyre_extensions . Unpack [ Ts ] , str ] ] " ( Some ( Type . OrderedTypes . Concatenation . create ~ prefix [ : Type . integer ] ~ suffix [ : Type . string ] variadic ) ) ; assert_concatenation " int " None ; ( ) |
let test_broadcast _ = let assert_broadcast left_type right_type expected = assert_equal ~ printer [ :% show : Type . t ] ~ cmp [ :% equal : Type . t ] ( Type . OrderedTypes . broadcast left_type right_type ) expected ; assert_equal ~ printer [ :% show : Type . t ] ~ cmp [ :% equal : Type . t ] ( Type . OrderedTypes . broadcast right_type left_type ) expected in let literal_tuple input = Type . tuple ( List . map ~ f : Type . literal_integer input ) in let unbounded_int = Type . Tuple ( Concatenation ( Type . OrderedTypes . Concatenation . create_from_unbounded_element Type . integer ) ) in let unbounded_any = Type . Tuple ( Concatenation ( Type . OrderedTypes . Concatenation . create_from_unbounded_element Type . Any ) ) in let x = Type . Variable ( Type . Variable . Unary . create ~ constraints ( : Type . Record . Variable . Bound ( Type . Primitive " int " ) ) " x " ) in let y = Type . Variable ( Type . Variable . Unary . create ~ constraints ( : Type . Record . Variable . Bound ( Type . Primitive " int " ) ) " y " ) in let not_bound_to_int = Type . Variable ( Type . Variable . Unary . create " not_bound_to_int " ) in let variadic = Type . Variable . Variadic . Tuple . create " Ts " in let variadic2 = Type . Variable . Variadic . Tuple . create " Ts2 " in let variadic_t = Type . OrderedTypes . Concatenation . create variadic in let variadic2_t = Type . OrderedTypes . Concatenation . create variadic2 in let x_and_unbounded_int = Type . OrderedTypes . Concatenation . create_from_unbounded_element ~ prefix [ : x ] Type . integer in let x_and_variadic = Type . OrderedTypes . Concatenation . create ~ prefix [ : x ] variadic in let broadcast_error left right = Type . Parametric { name = " pyre_extensions . BroadcastError " ; parameters = [ Type . Parameter . Single left ; Type . Parameter . Single right ] ; } in assert_broadcast ( literal_tuple [ 1 ] ) ( literal_tuple [ 5 ] ) ( literal_tuple [ 5 ] ) ; assert_broadcast ( literal_tuple [ 5 ] ) ( literal_tuple [ 3 ] ) ( broadcast_error ( literal_tuple [ 3 ] ) ( literal_tuple [ 5 ] ) ) ; assert_broadcast ( Type . tuple [ ] ) ( literal_tuple [ 5 ] ) ( literal_tuple [ 5 ] ) ; assert_broadcast ( literal_tuple [ 1 ; 3 ] ) Type . Any Any ; assert_broadcast ( literal_tuple [ 1 ; 3 ] ) ( Type . tuple [ Any ; Type . literal_integer 1 ] ) ( Type . tuple [ Any ; Type . literal_integer 3 ] ) ; assert_broadcast ( literal_tuple [ 1 ; 3 ] ) unbounded_any unbounded_any ; assert_broadcast Type . integer ( literal_tuple [ 1 ; 3 ] ) Bottom ; assert_broadcast ( Type . tuple [ Type . literal_integer 1 ; Type . integer ] ) ( literal_tuple [ 1 ; 3 ] ) ( Type . tuple [ Type . literal_integer 1 ; Type . integer ] ) ; assert_broadcast ( literal_tuple [ 1 ; 3 ] ) unbounded_int unbounded_int ; assert_broadcast ( Type . tuple [ Type . string ] ) unbounded_int Bottom ; assert_broadcast ( Type . Tuple ( Concatenation x_and_variadic ) ) ( Type . Tuple ( Concatenation x_and_variadic ) ) ( Type . Tuple ( Concatenation x_and_variadic ) ) ; assert_broadcast ( Type . Tuple ( Concatenation x_and_variadic ) ) ( Type . Tuple ( Concatenation ( Type . OrderedTypes . Concatenation . create ~ prefix [ : x ] variadic2 ) ) ) ( Type . Tuple ( Concatenation ( Type . OrderedTypes . Concatenation . create_from_concatenation_against_concatenation ~ compare_t : Type . compare x_and_variadic ( Type . OrderedTypes . Concatenation . create ~ prefix [ : x ] variadic2 ) ) ) ) ; assert_broadcast ( Type . Tuple ( Concatenation x_and_unbounded_int ) ) ( Type . Tuple ( Concatenation x_and_unbounded_int ) ) ( Type . Tuple ( Concatenation x_and_unbounded_int ) ) ; assert_broadcast ( Type . Tuple ( Concatenation x_and_unbounded_int ) ) ( Type . Tuple ( Concatenation ( Type . OrderedTypes . Concatenation . create_from_unbounded_element ~ prefix [ : x ] Type . Any ) ) ) ( Type . Tuple ( Concatenation ( Type . OrderedTypes . Concatenation . create_from_concatenation_against_concatenation ~ compare_t : Type . compare x_and_unbounded_int ( Type . OrderedTypes . Concatenation . create_from_unbounded_element ~ prefix [ : x ] Type . Any ) ) ) ) ; let first_concrete_against_concatenation = Type . OrderedTypes . Concatenation . create_from_concrete_against_concatenation ~ prefix [ : x ] ~ suffix [ ] : ~ concrete [ : y ] ~ concatenation : variadic_t in let second_concrete_against_concatenation = Type . OrderedTypes . Concatenation . create_from_concrete_against_concatenation ~ prefix [ : y ] ~ suffix [ ] : ~ concrete [ : y ] ~ concatenation : variadic_t in assert_broadcast ( Type . Tuple ( Concatenation first_concrete_against_concatenation ) ) ( Type . Tuple ( Concatenation first_concrete_against_concatenation ) ) ( Type . Tuple ( Concatenation first_concrete_against_concatenation ) ) ; assert_broadcast ( Type . Tuple ( Concatenation first_concrete_against_concatenation ) ) ( Type . Tuple ( Concatenation second_concrete_against_concatenation ) ) ( Type . Tuple ( Concatenation ( Type . OrderedTypes . Concatenation . create_from_concatenation_against_concatenation ~ compare_t : Type . compare first_concrete_against_concatenation second_concrete_against_concatenation ) ) ) ; let first_concatenation_against_concatenation = Type . OrderedTypes . Concatenation . create_from_concatenation_against_concatenation ~ prefix [ : x ] ~ suffix [ ] : ~ compare_t : Type . compare variadic_t variadic_t in let second_concatenation_against_concatenation = Type . OrderedTypes . Concatenation . create_from_concatenation_against_concatenation ~ prefix [ : x ] ~ suffix [ ] : ~ compare_t : Type . compare variadic_t variadic2_t in assert_broadcast ( Type . Tuple ( Concatenation first_concatenation_against_concatenation ) ) ( Type . Tuple ( Concatenation second_concatenation_against_concatenation ) ) ( Type . Tuple ( Concatenation ( Type . OrderedTypes . Concatenation . create_from_concatenation_against_concatenation ~ compare_t : Type . compare first_concatenation_against_concatenation second_concatenation_against_concatenation ) ) ) ; assert_broadcast ( Type . tuple [ x ] ) ( literal_tuple [ 1 ] ) ( Type . tuple [ x ] ) ; assert_broadcast ( Type . tuple [ x ] ) ( Type . tuple [ x ] ) ( Type . tuple [ x ] ) ; assert_broadcast ( Type . tuple [ x ; y ] ) ( Type . tuple [ y ; x ] ) ( Type . Tuple ( Concatenation ( Type . OrderedTypes . Concatenation . create_from_concrete_against_concrete ~ prefix [ ] : ~ suffix [ ] : ~ compare_t : Type . compare ~ left [ : y ; x ] ~ right [ : x ; y ] ) ) ) ; assert_broadcast ( Type . tuple [ x ] ) ( Type . tuple [ Type . literal_integer 5 ] ) ( Type . Tuple ( Concatenation ( Type . OrderedTypes . Concatenation . create_from_concrete_against_concrete ~ prefix [ ] : ~ suffix [ ] : ~ compare_t : Type . compare ~ left [ : x ] ~ right [ : Type . literal_integer 5 ] ) ) ) ; assert_broadcast ( Type . tuple [ not_bound_to_int ] ) ( Type . tuple [ not_bound_to_int ] ) ( broadcast_error ( Type . tuple [ not_bound_to_int ] ) ( Type . tuple [ not_bound_to_int ] ) ) ; let left , right = literal_tuple [ 1 ; 3 ; 5 ] , literal_tuple [ 1 ; 3 ] in assert_broadcast left right ( broadcast_error right left ) ; assert_broadcast ( literal_tuple [ 1 ; 3 ; 5 ] ) ( literal_tuple [ 5 ; 3 ; 1 ] ) ( literal_tuple [ 5 ; 3 ; 5 ] ) ; assert_broadcast ( literal_tuple [ 1 ; 3 ; 5 ] ) ( literal_tuple [ 3 ; 5 ] ) ( literal_tuple [ 1 ; 3 ; 5 ] ) ; assert_broadcast ( literal_tuple [ 5 ; 3 ; 1 ] ) ( literal_tuple [ 3 ; 1 ] ) ( literal_tuple [ 5 ; 3 ; 1 ] ) ; ( ) |
let test_split_ordered_types _ = let variadic = Type . Variable . Variadic . Tuple . create " Ts " in let assert_split ( ? split_both_ways = true ) left right expected = let aliases ? replace_unbound_parameters_with_any : _ = function | " Ts " -> Some ( Type . VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | _ -> None in let left = match Type . create ~ aliases ( parse_single_expression ~ preprocess : true ( " typing . Tuple " ^ left ) ) with | Type . Tuple ordered_type -> ordered_type | _ -> failwith " expected tuple elements " in let right = match Type . create ~ aliases ( parse_single_expression ~ preprocess : true ( " typing . Tuple " ^ right ) ) with | Type . Tuple ordered_type -> ordered_type | _ -> failwith " expected tuple elements " in assert_equal ~ printer [ :% show : Type . t Type . OrderedTypes . ordered_type_split option ] expected ( Type . OrderedTypes . split_matching_elements_by_length left right ) ; if split_both_ways then let flip_splits { Type . Record . OrderedTypes . prefix_pairs ; middle_pair ; suffix_pairs } = let swap ( a , b ) = b , a in { Type . Record . OrderedTypes . prefix_pairs = List . map prefix_pairs ~ f : swap ; middle_pair = swap middle_pair ; suffix_pairs = List . map suffix_pairs ~ f : swap ; } in assert_equal ~ printer [ :% show : Type . t Type . OrderedTypes . ordered_type_split option ] ( expected >>| flip_splits ) ( Type . OrderedTypes . split_matching_elements_by_length right left ) in let open Type . OrderedTypes in assert_split " [ int , str ] " " [ int , str ] " ( Some { prefix_pairs = [ Type . integer , Type . integer ; Type . string , Type . string ] ; middle_pair = Concrete [ ] , Concrete [ ] ; suffix_pairs = [ ] ; } ) ; assert_split " [ int , str , bool , int , str ] " " [ int , pyre_extensions . Unpack [ Ts ] , int , str ] " ( Some { prefix_pairs = [ Type . integer , Type . integer ] ; middle_pair = ( Concrete [ Type . string ; Type . bool ] , Concatenation ( Type . OrderedTypes . Concatenation . create ~ prefix [ ] : ~ suffix [ ] : variadic ) ) ; suffix_pairs = [ Type . integer , Type . integer ; Type . string , Type . string ] ; } ) ; assert_split " [ int ] " " [ int , str , pyre_extensions . Unpack [ Ts ] ] " None ; assert_split " [ str ] " " [ pyre_extensions . Unpack [ Ts ] , int , str ] " None ; assert_split " [ int , int ] " " [ int , pyre_extensions . Unpack [ Ts ] , int , str ] " None ; assert_split " [ int , int ] " " [ int , pyre_extensions . Unpack [ Ts ] , int , str ] " None ; assert_split " [ int , int , str ] " " [ int , pyre_extensions . Unpack [ Ts ] , int , str ] " ( Some { prefix_pairs = [ Type . integer , Type . integer ] ; middle_pair = ( Concrete [ ] , Concatenation ( Type . OrderedTypes . Concatenation . create ~ prefix [ ] : ~ suffix [ ] : variadic ) ) ; suffix_pairs = [ Type . integer , Type . integer ; Type . string , Type . string ] ; } ) ; assert_split " [ int , pyre_extensions . Unpack [ Ts ] , bool ] " " [ int , pyre_extensions . Unpack [ Ts ] , int ] " ( Some { prefix_pairs = [ Type . integer , Type . integer ] ; middle_pair = ( Concatenation ( Type . OrderedTypes . Concatenation . create ~ prefix [ ] : ~ suffix [ ] : variadic ) , Concatenation ( Type . OrderedTypes . Concatenation . create ~ prefix [ ] : ~ suffix [ ] : variadic ) ) ; suffix_pairs = [ Type . bool , Type . integer ] ; } ) ; assert_split " [ int , str , pyre_extensions . Unpack [ Ts ] , bool ] " " [ int , pyre_extensions . Unpack [ Ts ] , str , int ] " ( Some { prefix_pairs = [ Type . integer , Type . integer ] ; middle_pair = ( Concatenation ( Type . OrderedTypes . Concatenation . create ~ prefix [ : Type . string ] ~ suffix [ ] : variadic ) , Concatenation ( Type . OrderedTypes . Concatenation . create ~ prefix [ ] : ~ suffix [ : Type . string ] variadic ) ) ; suffix_pairs = [ Type . bool , Type . integer ] ; } ) ; assert_split " [ pyre_extensions . Unpack [ Ts ] , str ] " " [ int , pyre_extensions . Unpack [ Ts ] ] " ( Some { prefix_pairs = [ ] ; middle_pair = ( Concatenation ( Type . OrderedTypes . Concatenation . create ~ prefix [ ] : ~ suffix [ : Type . string ] variadic ) , Concatenation ( Type . OrderedTypes . Concatenation . create ~ prefix [ : Type . integer ] ~ suffix [ ] : variadic ) ) ; suffix_pairs = [ ] ; } ) ; assert_split " [ int , str , pyre_extensions . Unpack [ typing . Tuple [ str , . . . ] ] , str , bool ] " " [ int , pyre_extensions . Unpack [ typing . Tuple [ int , . . . ] ] , bool ] " ( Some { prefix_pairs = [ Type . integer , Type . integer ; Type . string , Type . integer ] ; middle_pair = Concrete [ Type . string ] , Concrete [ Type . integer ] ; suffix_pairs = [ Type . string , Type . integer ; Type . bool , Type . bool ] ; } ) ; assert_split " [ int , pyre_extensions . Unpack [ typing . Tuple [ str , . . . ] ] , bool ] " " [ int , str , pyre_extensions . Unpack [ typing . Tuple [ str , . . . ] ] , str , bool ] " ( Some { prefix_pairs = [ Type . integer , Type . integer ; Type . string , Type . string ] ; middle_pair = Concrete [ Type . string ] , Concrete [ Type . string ] ; suffix_pairs = [ Type . string , Type . string ; Type . bool , Type . bool ] ; } ) ; assert_split " [ int , str , pyre_extensions . Unpack [ typing . Tuple [ str , . . . ] ] , bool ] " " [ int , pyre_extensions . Unpack [ typing . Tuple [ str , . . . ] ] , str , bool ] " ( Some { prefix_pairs = [ Type . integer , Type . integer ; Type . string , Type . string ] ; middle_pair = Concrete [ Type . string ] , Concrete [ Type . string ] ; suffix_pairs = [ Type . string , Type . string ; Type . bool , Type . bool ] ; } ) ; assert_split " [ pyre_extensions . Unpack [ typing . Tuple [ str , . . . ] ] , int ] " " [ int , pyre_extensions . Unpack [ Ts ] , int ] " ( Some { prefix_pairs = [ Type . string , Type . integer ] ; middle_pair = ( Concatenation ( Type . OrderedTypes . Concatenation . create_from_unbounded_element Type . string ) , Concatenation ( Type . OrderedTypes . Concatenation . create variadic ) ) ; suffix_pairs = [ Type . integer , Type . integer ] ; } ) ; assert_split " [ int , int , pyre_extensions . Unpack [ typing . Tuple [ str , . . . ] ] , int ] " " [ int , pyre_extensions . Unpack [ Ts ] , int ] " ( Some { prefix_pairs = [ Type . integer , Type . integer ] ; middle_pair = ( Concatenation ( Type . OrderedTypes . Concatenation . create_from_unbounded_element ~ prefix [ : Type . integer ] Type . string ) , Concatenation ( Type . OrderedTypes . Concatenation . create variadic ) ) ; suffix_pairs = [ Type . integer , Type . integer ] ; } ) ; assert_split " [ pyre_extensions . Unpack [ typing . Tuple [ str , . . . ] ] ] " " [ pyre_extensions . Unpack [ Ts ] , str ] " ( Some { prefix_pairs = [ ] ; middle_pair = ( Concatenation ( Type . OrderedTypes . Concatenation . create_from_unbounded_element Type . string ) , Concatenation ( Type . OrderedTypes . Concatenation . create variadic ) ) ; suffix_pairs = [ Type . string , Type . string ] ; } ) ; assert_split " [ pyre_extensions . Unpack [ typing . Tuple [ str , . . . ] ] , int ] " " [ int , pyre_extensions . Unpack [ Ts ] , str , int ] " ( Some { prefix_pairs = [ Type . string , Type . integer ] ; middle_pair = ( Concatenation ( Type . OrderedTypes . Concatenation . create_from_unbounded_element Type . string ) , Concatenation ( Type . OrderedTypes . Concatenation . create variadic ) ) ; suffix_pairs = [ Type . string , Type . string ; Type . integer , Type . integer ] ; } ) ; assert_split " [ str , pyre_extensions . Unpack [ typing . Tuple [ str , . . . ] ] , int ] " " [ pyre_extensions . Unpack [ Ts ] , str , int ] " ( Some { prefix_pairs = [ ] ; middle_pair = ( Concatenation ( Type . OrderedTypes . Concatenation . create_from_unbounded_element ~ prefix [ : Type . string ] Type . string ) , Concatenation ( Type . OrderedTypes . Concatenation . create variadic ) ) ; suffix_pairs = [ Type . string , Type . string ; Type . integer , Type . integer ] ; } ) ; assert_split " [ pyre_extensions . Unpack [ typing . Tuple [ str , . . . ] ] ] " " [ str , str ] " ( Some { prefix_pairs = [ Type . string , Type . string ; Type . string , Type . string ] ; middle_pair = Concrete [ ] , Concrete [ ] ; suffix_pairs = [ ] ; } ) ; assert_split " [ str , str , pyre_extensions . Unpack [ typing . Tuple [ int , . . . ] ] ] " " [ str , str ] " ( Some { prefix_pairs = [ Type . string , Type . string ; Type . string , Type . string ] ; middle_pair = Concrete [ ] , Concrete [ ] ; suffix_pairs = [ ] ; } ) ; assert_split " [ str , pyre_extensions . Unpack [ typing . Tuple [ int , . . . ] ] , str ] " " [ str , bool ] " ( Some { prefix_pairs = [ Type . string , Type . string ; Type . string , Type . bool ] ; middle_pair = Concrete [ ] , Concrete [ ] ; suffix_pairs = [ ] ; } ) ; assert_split " [ str , str , str , pyre_extensions . Unpack [ typing . Tuple [ int , . . . ] ] ] " " [ str , str ] " None ; assert_split " [ str , . . . ] " " [ int ] " ( Some { prefix_pairs = [ Type . string , Type . integer ] ; middle_pair = Concrete [ ] , Concrete [ ] ; suffix_pairs = [ ] ; } ) ; ( ) |
let test_coalesce_ordered_types _ = let variadic = Type . Variable . Variadic . Tuple . create " Ts " in let assert_coalesce ordered_types expected = let aliases ? replace_unbound_parameters_with_any : _ = function | " Ts " -> Some ( Type . VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | _ -> None in let parse_ordered_type type_ = match Type . create ~ aliases ( parse_single_expression ~ preprocess : true ( " typing . Tuple " ^ type_ ) ) with | Type . Tuple ordered_type -> ordered_type | _ -> failwith " expected tuple elements " in let ordered_types = List . map ordered_types ~ f : parse_ordered_type in let expected = expected >>| parse_ordered_type in assert_equal ~ printer [ :% show : Type . t Type . OrderedTypes . record option ] expected ( Type . OrderedTypes . coalesce_ordered_types ordered_types ) in assert_coalesce [ " [ int , str ] " ; " [ bool , bool ] " ] ( Some " [ int , str , bool , bool ] " ) ; assert_coalesce [ " [ int , str ] " ; " [ int , . . . ] " ; " [ bool , bool ] " ] ( Some " [ int , str , pyre_extensions . Unpack [ typing . Tuple [ int , . . . ] ] , bool , bool ] " ) ; assert_coalesce [ " [ int , str ] " ; " [ pyre_extensions . Unpack [ Ts ] ] " ; " [ bool , bool ] " ] ( Some " [ int , str , pyre_extensions . Unpack [ Ts ] , bool , bool ] " ) ; assert_coalesce [ " [ int , . . . ] " ; " [ pyre_extensions . Unpack [ Ts ] ] " ] None ; assert_coalesce [ " [ int , . . . ] " ; " [ int , . . . ] " ] ( Some " [ int , . . . ] " ) ; assert_coalesce [ " [ int , . . . ] " ; " [ str , . . . ] " ] ( Some " [ typing . Union [ int , str ] , . . . ] " ) ; assert_coalesce [ " [ int , int ] " ; " [ int , str , pyre_extensions . Unpack [ typing . Tuple [ int , . . . ] ] , bool , bool ] " ; " [ bool , bool ] " ; " [ int , str , pyre_extensions . Unpack [ typing . Tuple [ str , . . . ] ] , bool , bool ] " ; " [ bool , bool ] " ; " [ int , str , pyre_extensions . Unpack [ typing . Tuple [ str , . . . ] ] , bool , bool ] " ; " [ bool , bool ] " ; ] ( Some " [ int , int , int , str , pyre_extensions . Unpack [ typing . Tuple [ typing . Union [ int , bool , str ] , \ . . . ] ] , bool , bool , bool , bool ] " ) ; ( ) |
let test_drop_prefix_ordered_type _ = let open Type . OrderedTypes in let assert_drop_prefix ~ length actual expected_tuple = let variadic = Type . Variable . Variadic . Tuple . create " Ts " in let aliases ? replace_unbound_parameters_with_any : _ = function | " Ts " -> Some ( Type . VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | _ -> None in let extract_ordered_type string = match parse_single_expression string |> Type . create ~ aliases with | Type . Tuple ordered_type -> ordered_type | _ -> failwith " expected tuple " in assert_equal ~ cmp [ :% equal : Type . t record option ] ~ printer [ :% show : Type . t record option ] ( expected_tuple >>| extract_ordered_type ) ( extract_ordered_type actual |> Type . OrderedTypes . drop_prefix ~ length ) in assert_drop_prefix ~ length : 0 " typing . Tuple [ int , str ] " ( Some " typing . Tuple [ int , str ] " ) ; assert_drop_prefix ~ length : 2 " typing . Tuple [ int , str ] " ( Some " typing . Tuple [ ( ) ] " ) ; assert_drop_prefix ~ length : 2 " typing . Tuple [ int , str , bool , int ] " ( Some " typing . Tuple [ bool , int ] " ) ; assert_drop_prefix ~ length : 3 " typing . Tuple [ int , str ] " None ; assert_drop_prefix ~ length : 0 " typing . Tuple [ int , str , pyre_extensions . Unpack [ typing . Tuple [ int , . . . ] ] ] " ( Some " typing . Tuple [ int , str , pyre_extensions . Unpack [ typing . Tuple [ int , . . . ] ] ] " ) ; assert_drop_prefix ~ length : 1 " typing . Tuple [ int , str , pyre_extensions . Unpack [ typing . Tuple [ int , . . . ] ] , str ] " ( Some " typing . Tuple [ str , pyre_extensions . Unpack [ typing . Tuple [ int , . . . ] ] , str ] " ) ; assert_drop_prefix ~ length : 2 " typing . Tuple [ int , str , pyre_extensions . Unpack [ typing . Tuple [ int , . . . ] ] ] " ( Some " typing . Tuple [ int , . . . ] " ) ; assert_drop_prefix ~ length : 2 " typing . Tuple [ int , str , pyre_extensions . Unpack [ Ts ] ] " ( Some " typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] " ) ; assert_drop_prefix ~ length : 3 " typing . Tuple [ int , str , pyre_extensions . Unpack [ typing . Tuple [ int , . . . ] ] ] " ( Some " typing . Tuple [ int , . . . ] " ) ; assert_drop_prefix ~ length : 3 " typing . Tuple [ int , str , pyre_extensions . Unpack [ Ts ] ] " None ; ( ) |
let test_index_ordered_type _ = let assert_index ~ python_index tuple expected = let variadic = Type . Variable . Variadic . Tuple . create " Ts " in let aliases ? replace_unbound_parameters_with_any : _ = function | " Ts " -> Some ( Type . VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | _ -> None in let extract_ordered_type string = match parse_single_expression string |> Type . create ~ aliases with | Type . Tuple ordered_type -> ordered_type | _ -> failwith " expected tuple " in assert_equal ~ cmp [ :% equal : Type . t option ] ~ printer [ :% show : Type . t option ] ( expected >>| parse_single_expression >>| Type . create ~ aliases ) ( extract_ordered_type tuple |> Type . OrderedTypes . index ~ python_index ) in assert_index ~ python_index : 0 " typing . Tuple [ int , str ] " ( Some " int " ) ; assert_index ~ python_index : 1 " typing . Tuple [ int , str ] " ( Some " str " ) ; assert_index ~ python_index ( :- 1 ) " typing . Tuple [ int , str ] " ( Some " str " ) ; assert_index ~ python_index ( :- 2 ) " typing . Tuple [ int , str ] " ( Some " int " ) ; assert_index ~ python_index : 2 " typing . Tuple [ int , str ] " None ; assert_index ~ python_index ( :- 3 ) " typing . Tuple [ int , str ] " None ; assert_index ~ python_index : 0 " typing . Tuple [ int , str , pyre_extensions . Unpack [ typing . Tuple [ bool , . . . ] ] ] " ( Some " int " ) ; assert_index ~ python_index : 2 " typing . Tuple [ int , str , pyre_extensions . Unpack [ typing . Tuple [ bool , . . . ] ] ] " ( Some " bool " ) ; assert_index ~ python_index : 99 " typing . Tuple [ int , str , pyre_extensions . Unpack [ typing . Tuple [ bool , . . . ] ] ] " ( Some " bool " ) ; assert_index ~ python_index ( :- 1 ) " typing . Tuple [ int , str , pyre_extensions . Unpack [ typing . Tuple [ bool , . . . ] ] , str ] " ( Some " str " ) ; assert_index ~ python_index ( :- 2 ) " typing . Tuple [ int , str , pyre_extensions . Unpack [ typing . Tuple [ bool , . . . ] ] , str ] " ( Some " bool " ) ; assert_index ~ python_index ( :- 99 ) " typing . Tuple [ int , str , pyre_extensions . Unpack [ typing . Tuple [ bool , . . . ] ] , str ] " ( Some " bool " ) ; assert_index ~ python_index : 1 " typing . Tuple [ int , str , pyre_extensions . Unpack [ Ts ] , bool ] " ( Some " str " ) ; assert_index ~ python_index ( :- 1 ) " typing . Tuple [ int , str , pyre_extensions . Unpack [ Ts ] , bool ] " ( Some " bool " ) ; assert_index ~ python_index : 2 " typing . Tuple [ int , str , pyre_extensions . Unpack [ Ts ] , bool ] " None ; assert_index ~ python_index ( :- 2 ) " typing . Tuple [ int , str , pyre_extensions . Unpack [ Ts ] , bool ] " None ; ( ) |
let test_zip_variables_with_parameters _ = let unary = Type . Variable . Unary . create " T " in let unary2 = Type . Variable . Unary . create " T2 " in let variadic = Type . Variable . Variadic . Tuple . create " Ts " in let variadic2 = Type . Variable . Variadic . Tuple . create " Ts2 " in let parameter_variadic = Type . Variable . Variadic . Parameters . create " TParams " in let assert_zipped ~ generic_class ~ instantiation expected = let aliases ? replace_unbound_parameters_with_any : _ = function | " T " -> Some ( Type . TypeAlias ( Type . Variable unary ) ) | " T2 " -> Some ( Type . TypeAlias ( Type . Variable unary2 ) ) | " Ts " -> Some ( Type . VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | " Ts2 " -> Some ( Type . VariableAlias ( Type . Variable . TupleVariadic variadic2 ) ) | " TParams " -> Some ( Type . VariableAlias ( Type . Variable . ParameterVariadic parameter_variadic ) ) | _ -> None in let parameters = match Type . create ~ aliases ( parse_single_expression ~ preprocess : true instantiation ) with | Type . Parametric { parameters ; _ } -> parameters | _ -> failwith " expected Parametric " in let variables = match Type . create ~ aliases ( parse_single_expression ~ preprocess : true generic_class ) with | Type . Parametric { parameters ; _ } -> let variables = List . map ~ f : Type . Parameter . to_variable parameters |> Option . all in Option . value_exn variables | _ -> failwith " expected Parametric " in assert_equal ~ printer [ :% show : Type . Variable . variable_zip_result list option ] ~ cmp [ :% equal : Type . Variable . variable_zip_result list option ] expected ( Type . Variable . zip_variables_with_parameters_including_mismatches ~ parameters variables ) in assert_zipped ~ generic_class " : Generic [ T , T2 ] " ~ instantiation " : Foo [ int , str ] " ( Some [ { variable_pair = Type . Variable . UnaryPair ( unary , Type . integer ) ; received_parameter = Single Type . integer ; } ; { variable_pair = Type . Variable . UnaryPair ( unary2 , Type . string ) ; received_parameter = Single Type . string ; } ; ] ) ; assert_zipped ~ generic_class " : Generic [ T , T2 ] " ~ instantiation " : Foo [ int ] " None ; assert_zipped ~ generic_class " : Generic [ T , TParams ] " ~ instantiation " : Foo [ int , TParams ] " ( Some [ { variable_pair = Type . Variable . UnaryPair ( unary , Type . integer ) ; received_parameter = Single Type . integer ; } ; { variable_pair = Type . Variable . ParameterVariadicPair ( parameter_variadic , Type . Callable . ParameterVariadicTypeVariable ( empty_head parameter_variadic ) ) ; received_parameter = CallableParameters ( Type . Variable . Variadic . Parameters . self_reference parameter_variadic ) ; } ; ] ) ; assert_zipped ~ generic_class " : Generic [ T , TParams ] " ~ instantiation " : Foo [ int ] " None ; assert_zipped ~ generic_class " : Generic [ T , TParams ] " ~ instantiation " : Foo [ TParams , int ] " ( Some [ { variable_pair = Type . Variable . UnaryPair ( unary , Type . Any ) ; received_parameter = CallableParameters ( Type . Variable . Variadic . Parameters . self_reference parameter_variadic ) ; } ; { variable_pair = Type . Variable . ParameterVariadicPair ( parameter_variadic , Undefined ) ; received_parameter = Single Type . integer ; } ; ] ) ; assert_zipped ~ generic_class " : Generic [ TParams ] " ~ instantiation " : Foo [ int , str ] " ( Some [ { variable_pair = Type . Variable . ParameterVariadicPair ( parameter_variadic , Defined ( Type . Callable . prepend_anonymous_parameters ~ head [ : Type . integer ; Type . string ] ~ tail [ ] ) : ) ; received_parameter = CallableParameters ( Defined ( Type . Callable . prepend_anonymous_parameters ~ head [ : Type . integer ; Type . string ] ~ tail [ ] ) ) ; : } ; ] ) ; assert_zipped ~ generic_class " : Generic [ T , TParams ] " ~ instantiation " : Foo [ int , [ str , bool ] ] " ( Some [ { variable_pair = Type . Variable . UnaryPair ( unary , Type . integer ) ; received_parameter = Single Type . integer ; } ; { variable_pair = Type . Variable . ParameterVariadicPair ( parameter_variadic , Defined [ PositionalOnly { index = 0 ; annotation = Type . string ; default = false } ; PositionalOnly { index = 1 ; annotation = Type . bool ; default = false } ; ] ) ; received_parameter = CallableParameters ( Defined [ PositionalOnly { index = 0 ; annotation = Type . string ; default = false } ; PositionalOnly { index = 1 ; annotation = Type . bool ; default = false } ; ] ) ; } ; ] ) ; assert_zipped ~ generic_class " : Generic [ pyre_extensions . Unpack [ Ts ] ] " ~ instantiation " : Foo [ pyre_extensions . Unpack [ Ts ] ] " ( Some [ { variable_pair = Type . Variable . TupleVariadicPair ( variadic , Concatenation ( Type . OrderedTypes . Concatenation . create variadic ) ) ; received_parameter = Single ( Tuple ( Concatenation ( Type . OrderedTypes . Concatenation . create variadic ) ) ) ; } ; ] ) ; assert_zipped ~ generic_class " : Generic [ T , pyre_extensions . Unpack [ Ts ] ] " ~ instantiation " : Foo [ int ] " ( Some [ { variable_pair = Type . Variable . UnaryPair ( unary , Type . integer ) ; received_parameter = Single Type . integer ; } ; { variable_pair = Type . Variable . TupleVariadicPair ( variadic , Concrete [ ] ) ; received_parameter = Single ( Tuple ( Concrete [ ] ) ) ; } ; ] ) ; assert_zipped ~ generic_class " : Generic [ T , pyre_extensions . Unpack [ Ts ] ] " ~ instantiation " : Foo [ int , str , int , bool ] " ( Some [ { variable_pair = Type . Variable . UnaryPair ( unary , Type . integer ) ; received_parameter = Single Type . integer ; } ; { variable_pair = Type . Variable . TupleVariadicPair ( variadic , Concrete [ Type . string ; Type . integer ; Type . bool ] ) ; received_parameter = Single ( Tuple ( Concrete [ Type . string ; Type . integer ; Type . bool ] ) ) ; } ; ] ) ; assert_zipped ~ generic_class " : Generic [ T , pyre_extensions . Unpack [ Ts ] , T2 ] " ~ instantiation " : Foo [ int , str , pyre_extensions . Unpack [ Ts2 ] , bool , str ] " ( Some [ { variable_pair = Type . Variable . UnaryPair ( unary , Type . integer ) ; received_parameter = Single Type . integer ; } ; { variable_pair = Type . Variable . TupleVariadicPair ( variadic , Concatenation ( Type . OrderedTypes . Concatenation . create ~ prefix [ : Type . string ] ~ suffix [ : Type . bool ] variadic2 ) ) ; received_parameter = Single ( Tuple ( Concatenation ( Type . OrderedTypes . Concatenation . create ~ prefix [ : Type . string ] ~ suffix [ : Type . bool ] variadic2 ) ) ) ; } ; { variable_pair = Type . Variable . UnaryPair ( unary2 , Type . string ) ; received_parameter = Single Type . string ; } ; ] ) ; assert_zipped ~ generic_class " : Generic [ T , TParams , pyre_extensions . Unpack [ Ts ] ] " ~ instantiation " : Foo [ int , TParams , str , bool ] " ( Some [ { variable_pair = Type . Variable . UnaryPair ( unary , Type . integer ) ; received_parameter = Single Type . integer ; } ; { variable_pair = Type . Variable . ParameterVariadicPair ( parameter_variadic , Type . Callable . ParameterVariadicTypeVariable ( empty_head parameter_variadic ) ) ; received_parameter = CallableParameters ( Type . Variable . Variadic . Parameters . self_reference parameter_variadic ) ; } ; { variable_pair = Type . Variable . TupleVariadicPair ( variadic , Concrete [ Type . string ; Type . bool ] ) ; received_parameter = Single ( Tuple ( Concrete [ Type . string ; Type . bool ] ) ) ; } ; ] ) ; assert_zipped ~ generic_class " : Generic [ T , TParams , pyre_extensions . Unpack [ Ts ] ] " ~ instantiation " : Foo [ int , TParams , str , pyre_extensions . Unpack [ Ts2 ] ] " ( Some [ { variable_pair = Type . Variable . UnaryPair ( unary , Type . integer ) ; received_parameter = Single Type . integer ; } ; { variable_pair = Type . Variable . ParameterVariadicPair ( parameter_variadic , Type . Callable . ParameterVariadicTypeVariable ( empty_head parameter_variadic ) ) ; received_parameter = CallableParameters ( Type . Variable . Variadic . Parameters . self_reference parameter_variadic ) ; } ; { variable_pair = Type . Variable . TupleVariadicPair ( variadic , Concatenation ( Type . OrderedTypes . Concatenation . create ~ prefix [ : Type . string ] ~ suffix [ ] : variadic2 ) ) ; received_parameter = Single ( Tuple ( Concatenation ( Type . OrderedTypes . Concatenation . create ~ prefix [ : Type . string ] ~ suffix [ ] : variadic2 ) ) ) ; } ; ] ) ; assert_zipped ~ generic_class " : Generic [ T ] " ~ instantiation " : Foo [ pyre_extensions . Unpack [ Ts ] ] " ( Some [ { variable_pair = Type . Variable . UnaryPair ( unary , Type . Any ) ; received_parameter = Unpacked ( Type . OrderedTypes . Concatenation . create_unpackable variadic ) ; } ; ] ) ; assert_zipped ~ generic_class " : Generic [ TParams ] " ~ instantiation " : Foo [ pyre_extensions . Unpack [ Ts ] ] " ( Some [ { variable_pair = Type . Variable . ParameterVariadicPair ( parameter_variadic , Undefined ) ; received_parameter = Unpacked ( Type . OrderedTypes . Concatenation . create_unpackable variadic ) ; } ; ] ) ; assert_zipped ~ generic_class " : Generic [ pyre_extensions . Unpack [ Ts ] ] " ~ instantiation " : Foo [ TParams ] " ( Some [ { variable_pair = Type . Variable . TupleVariadicPair ( variadic , Type . Variable . Variadic . Tuple . any ) ; received_parameter = Single ( Type . parametric Type . Variable . Variadic . Tuple . synthetic_class_name_for_error [ CallableParameters ( Type . Variable . Variadic . Parameters . self_reference parameter_variadic ) ; ] ) ; } ; ] ) ; assert_zipped ~ generic_class " : Generic [ T , pyre_extensions . Unpack [ Ts ] ] " ~ instantiation " : Foo [ pyre_extensions . Unpack [ Ts2 ] ] " None ; assert_zipped ~ generic_class " : Generic [ pyre_extensions . Unpack [ Ts ] , pyre_extensions . Unpack [ Ts2 ] ] " ~ instantiation " : Foo [ int , str ] " None ; ( ) |
let test_zip_on_two_parameter_lists _ = let unary = Type . Variable . Unary . create " T " in let unary2 = Type . Variable . Unary . create " T2 " in let variadic = Type . Variable . Variadic . Tuple . create " Ts " in let variadic2 = Type . Variable . Variadic . Tuple . create " Ts2 " in let parameter_variadic = Type . Variable . Variadic . Parameters . create " TParams " in let assert_zipped ~ generic_class ~ left ~ right expected = let aliases ? replace_unbound_parameters_with_any : _ = function | " T " -> Some ( Type . TypeAlias ( Type . Variable unary ) ) | " T2 " -> Some ( Type . TypeAlias ( Type . Variable unary2 ) ) | " Ts " -> Some ( Type . VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | " Ts2 " -> Some ( Type . VariableAlias ( Type . Variable . TupleVariadic variadic2 ) ) | " TParams " -> Some ( Type . VariableAlias ( Type . Variable . ParameterVariadic parameter_variadic ) ) | _ -> None in let left_parameters = match Type . create ~ aliases ( parse_single_expression ~ preprocess : true left ) with | Type . Parametric { parameters ; _ } -> parameters | _ -> failwith " expected Parametric " in let right_parameters = match Type . create ~ aliases ( parse_single_expression ~ preprocess : true right ) with | Type . Parametric { parameters ; _ } -> parameters | _ -> failwith " expected Parametric " in let variables = match Type . create ~ aliases ( parse_single_expression ~ preprocess : true generic_class ) with | Type . Parametric { parameters ; _ } -> let variables = List . map ~ f : Type . Parameter . to_variable parameters |> Option . all in Option . value_exn variables | _ -> failwith " expected Parametric " in assert_equal ~ printer [ :% show : ( Type . Variable . pair * Type . Variable . pair ) list option ] ~ cmp [ :% equal : ( Type . Variable . pair * Type . Variable . pair ) list option ] expected ( Type . Variable . zip_variables_with_two_parameter_lists ~ left_parameters ~ right_parameters variables ) in assert_zipped ~ generic_class " : Generic [ T ] " ~ left " : Child [ int ] " ~ right " : Base [ str ] " ( Some [ Type . Variable . UnaryPair ( unary , Type . integer ) , Type . Variable . UnaryPair ( unary , Type . string ) ] ) ; assert_zipped ~ generic_class " : Generic [ T ] " ~ left " : Child [ int ] " ~ right " : Base [ str , bool ] " None ; ( ) |
let test_union_upper_bound _ = let assert_union_upper_bound map expected = assert_equal ~ printer : Type . show ~ cmp : Type . equal ( Type . OrderedTypes . union_upper_bound map ) expected in assert_union_upper_bound ( Concrete [ Type . integer ; Type . string ; Type . bool ] ) ( Type . union [ Type . integer ; Type . string ; Type . bool ] ) ; let variadic = Type . Variable . Variadic . Tuple . create " Ts " in assert_union_upper_bound ( Concatenation ( Type . OrderedTypes . Concatenation . create variadic ) ) Type . object_primitive ; assert_union_upper_bound ( Concatenation ( Type . OrderedTypes . Concatenation . create ~ prefix [ : Type . integer ] ~ suffix [ : Type . string ] variadic ) ) Type . object_primitive ; assert_union_upper_bound ( Type . OrderedTypes . create_unbounded_concatenation Type . integer ) Type . integer ; assert_union_upper_bound ( Concatenation ( Type . OrderedTypes . Concatenation . create_from_unbounded_element ~ prefix [ : Type . integer ] ~ suffix [ : Type . bool ] Type . string ) ) ( Type . union [ Type . integer ; Type . string ; Type . bool ] ) ; ( ) |
let test_infer_transform _ = let assert_transform ~ expected ~ annotation = assert_equal ( Type . infer_transform annotation ) expected in assert_transform ~ annotation ( : Type . parametric " _PathLike " [ ! Type . Primitive " string " ] ) ~ expected ( : Type . parametric " PathLike " [ ! Type . Primitive " string " ] ) ; assert_transform ~ annotation ( : Type . parametric " typing . Dict " [ ! Type . Bottom ; Type . Bottom ] ) ~ expected ( : Type . parametric " dict " [ ! Type . Any ; Type . Any ] ) ; assert_transform ~ annotation : ( Type . Tuple ( Concrete [ Type . Primitive " string " ; Type . Primitive " string " ; Type . Primitive " string " ] ) ) ~ expected : ( Type . Tuple ( Type . OrderedTypes . create_unbounded_concatenation ( Type . Primitive " string " ) ) ) ; assert_transform ~ annotation ( : Type . Tuple ( Concrete [ Type . Primitive " string " ; Type . Primitive " string " ] ) ) ~ expected ( : Type . Tuple ( Concrete [ Type . Primitive " string " ; Type . Primitive " string " ] ) ) ; assert_transform ~ annotation : ( Type . parametric " Union " [ ! Type . Primitive " string " ; Type . Primitive " string " ; Type . Primitive " int " ] ) ~ expected ( : Type . Union [ Type . Primitive " int " ; Type . Primitive " string " ] ) ; assert_transform ~ annotation ( : Type . parametric " Union " [ ! Type . Primitive " string " ; Type . Primitive " string " ] ) ~ expected ( : Type . Primitive " string " ) ; assert_transform ~ annotation ( : Type . parametric " Union " [ ! Type . NoneType ; Type . Primitive " string " ] ) ~ expected ( : Type . optional ( Type . Primitive " string " ) ) |
let test_fields_from_constructor _ = let assert_fields ~ constructor ~ expected = assert_equal ~ printer [ :% show : Type . t Type . Record . TypedDictionary . typed_dictionary_field list option ] expected ( Type . TypedDictionary . fields_from_constructor constructor ) in let fields = [ { Type . Record . TypedDictionary . name = " name " ; annotation = Type . string ; required = true } ; { Type . Record . TypedDictionary . name = " year " ; annotation = Type . integer ; required = false } ; ] in let non_constructor = match Type . Callable . create ~ annotation : Type . integer ( ) with | Type . Callable callable -> callable | _ -> failwith " expected callable " in assert_fields ~ constructor : non_constructor ~ expected : None ; assert_fields ~ constructor ( : Type . TypedDictionary . constructor ~ name " : Movie " ~ fields ) ~ expected ( : Some fields ) ; assert_fields ~ constructor ( : Type . TypedDictionary . constructor ~ name " : Movie " ~ fields ) ~ expected ( : Some fields ) ; ( ) |
let test_is_unit_test _ = let assert_is_unit_test name expected = Type . Primitive . is_unit_test name |> assert_equal expected in assert_is_unit_test " unittest . TestCase " true ; assert_is_unit_test " unittest . case . TestCase " true ; assert_is_unit_test " a . TestCase " false ; ( ) |
let polynomial_show_normal = Type . Polynomial . show_normal ~ show_variable : Type . polynomial_show_variable ~ show_type : Type . pp |
let polynomial_add = Type . Polynomial . add ~ compare_t : Type . compare |
let polynomial_subtract = Type . Polynomial . subtract ~ compare_t : Type . compare |
let polynomial_multiply = Type . Polynomial . multiply ~ compare_t : Type . compare |
let polynomial_divide = Type . Polynomial . divide ~ compare_t : Type . compare |
let test_polynomial_create_from_list _ = let assert_create given expected = let given = polynomial_create_from_variables_list given in assert_equal ~ printer : Fn . id expected ( polynomial_show_normal given ) in let x = Type . Variable . Unary . create " x " in let y = Type . Variable . Unary . create " y " in let z = Type . Variable . Unary . create " z " in assert_create [ ] " 0 " ; assert_create [ 1 , [ ] ] " 1 " ; assert_create [ 1 , [ x , 1 ] ] " x " ; assert_create [ 3 , [ x , 1 ] ] " 3x " ; assert_create [ 3 , [ x , 2 ] ] " 3x ^ 2 " ; assert_create [ 5 , [ y , 1 ; z , 1 ; x , 1 ] ] " 5xyz " ; assert_create [ 5 , [ y , 2 ; z , 1 ; x , 3 ] ] " 5x ^ 3y ^ 2z " ; assert_create [ 4 , [ ] ; 3 , [ x , 2 ] ] " 4 + 3x ^ 2 " ; assert_create [ 3 , [ x , 2 ] ; 4 , [ y , 2 ] ] " 3x ^ 2 + 4y ^ 2 " ; assert_create [ 3 , [ x , 2 ; y , 1 ] ; 4 , [ y , 2 ] ] " 4y ^ 2 + 3x ^ 2y " ; assert_create [ 1 , [ y , 1 ] ; 1 , [ x , 1 ] ; 2 , [ ] ] " 2 + x + y " ; assert_create [ 2 , [ y , 1 ] ; 1 , [ x , 1 ] ; 1 , [ x , 2 ; y , 1 ] ; 1 , [ z , 1 ] ; 1 , [ y , 1 ; z , 1 ; x , 1 ] ; 1 , [ x , 1 ; y , 2 ] ; 2 , [ ] ; ] " 2 + x + 2y + z + xyz + xy ^ 2 + x ^ 2y " ; ( ) |
let test_polynomial_to_type _ = let x = Type . Variable . Unary . create " x " in let y = Type . Variable . Unary . create " y " in let aliases ? replace_unbound_parameters_with_any : _ = function | " x " -> Some ( Type . TypeAlias ( Type . Variable x ) ) | " y " -> Some ( Type . TypeAlias ( Type . Variable y ) ) | _ -> None in let assert_to_type given expected = let expected_type = Type . create ~ aliases ( parse_single_expression ~ preprocess : true expected ) in let given = polynomial_create_from_variables_list given in let result = Type . polynomial_to_type given in assert_equal ~ printer : Type . show ~ cmp : Type . equal expected_type result in assert_to_type [ ] " typing_extensions . Literal [ 0 ] " ; assert_to_type [ 2 , [ ] ] " typing_extensions . Literal [ 2 ] " ; assert_to_type [ 1 , [ x , 1 ] ] " x " ; assert_to_type [ 2 , [ x , 1 ; y , 3 ] ] " pyre_extensions . Multiply [ pyre_extensions . Multiply [ typing_extensions . Literal [ 2 ] , \ pyre_extensions . Multiply [ x , y ] ] , pyre_extensions . Multiply [ y , y ] ] " ; ( ) |
let test_polynomial_replace _ = let x = Type . Variable . Unary . create " x " in let y = Type . Variable . Unary . create " y " in let z = Type . Variable . Unary . create " z " in let w = Type . Variable . Unary . create " w " in let assert_polynomial_replace given ~ replacements ( : variable , by ) expected = let replaced = Type . Polynomial . replace ~ compare_t : Type . compare ( polynomial_create_from_variables_list given ) ~ by ( : polynomial_create_from_variables_list by ) ~ variable ( : Type . Monomial . create_variable variable ) in let expected_polynomial = polynomial_create_from_variables_list expected in assert_equal ~ printer : Fn . id ( polynomial_show_normal expected_polynomial ) ( polynomial_show_normal replaced ) in assert_polynomial_replace [ ] ~ replacements ( : x , [ 1 , [ y , 1 ] ] ) [ ] ; assert_polynomial_replace [ 2 , [ x , 1 ] ] ~ replacements ( : x , [ ] ) [ ] ; assert_polynomial_replace [ 2 , [ x , 1 ] ] ~ replacements ( : x , [ 1 , [ x , 1 ] ] ) [ 2 , [ x , 1 ] ] ; assert_polynomial_replace [ 3 , [ y , 2 ] ] ~ replacements ( : x , [ ] ) [ 3 , [ y , 2 ] ] ; assert_polynomial_replace [ 2 , [ x , 1 ] ] ~ replacements ( : x , [ 3 , [ y , 1 ] ] ) [ 6 , [ y , 1 ] ] ; assert_polynomial_replace [ 5 , [ x , 2 ] ] ~ replacements ( : x , [ 2 , [ y , 3 ] ] ) [ 20 , [ y , 6 ] ] ; assert_polynomial_replace [ 2 , [ x , 2 ; y , 3 ] ] ~ replacements ( : y , [ 2 , [ z , 4 ] ] ) [ 16 , [ x , 2 ; z , 12 ] ] ; assert_polynomial_replace [ 2 , [ x , 1 ] ] ~ replacements ( : x , [ 3 , [ y , 2 ; z , 1 ] ; 6 , [ x , 3 ] ] ) [ 6 , [ y , 2 ; z , 1 ] ; 12 , [ x , 3 ] ] ; assert_polynomial_replace [ 4 , [ x , 2 ; y , 3 ; z , 1 ] ] ~ replacements ( : x , [ 2 , [ x , 2 ] ; 3 , [ y , 1 ; z , 3 ] ] ) [ 16 , [ x , 4 ; y , 3 ; z , 1 ] ; 48 , [ x , 2 ; y , 4 ; z , 4 ] ; 36 , [ y , 5 ; z , 7 ] ] ; assert_polynomial_replace [ 2 , [ x , 1 ; y , 1 ] ; 3 , [ z , 1 ] ] ~ replacements ( : z , [ ] ) [ 2 , [ x , 1 ; y , 1 ] ] ; assert_polynomial_replace [ 2 , [ x , 2 ; y , 3 ] ; 3 , [ y , 2 ; z , 1 ] ] ~ replacements ( : y , [ 2 , [ ] ] ) [ 16 , [ x , 2 ] ; 12 , [ z , 1 ] ] ; assert_polynomial_replace [ 8 , [ x , 1 ; y , 2 ; z , 2 ] ; 3 , [ x , 3 ; y , 2 ; z , 1 ] ] ~ replacements ( : z , [ 3 , [ x , 2 ] ] ) [ 81 , [ x , 5 ; y , 2 ] ] ; assert_polynomial_replace [ 3 , [ y , 1 ; z , 2 ] ; 2 , [ x , 2 ; y , 1 ; z , 1 ] ] ~ replacements ( : z , [ 2 , [ x , 2 ] ; 5 , [ y , 4 ] ] ) [ 16 , [ x , 4 ; y , 1 ] ; 70 , [ x , 2 ; y , 5 ] ; 75 , [ y , 9 ] ] ; assert_polynomial_replace [ 3 , [ x , 1 ; y , 1 ; z , 2 ] ; 2 , [ x , 2 ; y , 1 ; z , 1 ] ; 2 , [ w , 2 ] ] ~ replacements ( : x , [ 2 , [ x , 2 ] ; 5 , [ y , 4 ; z , 1 ] ] ) [ 6 , [ x , 2 ; y , 1 ; z , 2 ] ; 15 , [ y , 5 ; z , 3 ] ; 8 , [ x , 4 ; y , 1 ; z , 1 ] ; 40 , [ x , 2 ; y , 5 ; z , 2 ] ; 50 , [ y , 9 ; z , 3 ] ; 2 , [ w , 2 ] ; ] ; ( ) |
let test_add_polynomials _ = let assert_add given1 given2 expected = let given1 = polynomial_create_from_variables_list given1 in let given2 = polynomial_create_from_variables_list given2 in assert_equal ~ printer : Fn . id expected ( polynomial_show_normal ( polynomial_add given1 given2 ) ) ; assert_equal ~ printer : Fn . id expected ( polynomial_show_normal ( polynomial_add given2 given1 ) ) in let x = Type . Variable . Unary . create " x " in let y = Type . Variable . Unary . create " y " in let z = Type . Variable . Unary . create " z " in assert_add [ 3 , [ ] ] [ 2 , [ ] ] " 5 " ; assert_add [ 3 , [ ] ] [ - 3 , [ ] ; 2 , [ x , 2 ] ] " 2x ^ 2 " ; assert_add [ 1 , [ ] ; 3 , [ x , 1 ] ; 2 , [ y , 1 ] ] [ 2 , [ ] ; 1 , [ x , 1 ] ; 1 , [ z , 1 ] ] " 3 + 4x + 2y + z " ; assert_add [ 1 , [ ] ; 1 , [ x , 1 ; y , 2 ] ] [ 1 , [ x , 2 ; y , 1 ] ] " 1 + xy ^ 2 + x ^ 2y " ; ( ) |
let test_subtract_polynomials _ = let assert_subtract given1 given2 expected = let given1 = polynomial_create_from_variables_list given1 in let given2 = polynomial_create_from_variables_list given2 in assert_equal ~ printer : Fn . id expected ( polynomial_show_normal ( polynomial_subtract given1 given2 ) ) in let x = Type . Variable . Unary . create " x " in let y = Type . Variable . Unary . create " y " in let z = Type . Variable . Unary . create " z " in assert_subtract [ ] [ 3 , [ ] ] " - 3 " ; assert_subtract [ 3 , [ ] ] [ 3 , [ ] ] " 0 " ; assert_subtract [ 3 , [ x , 1 ] ] [ 3 , [ x , 1 ] ] " 0 " ; assert_subtract [ ] [ 1 , [ x , 1 ] ] " - x " ; assert_subtract [ 2 , [ ] ] [ 3 , [ ] ] " - 1 " ; assert_subtract [ 1 , [ ] ; 3 , [ x , 1 ] ; 2 , [ y , 1 ] ] [ 2 , [ ] ; 1 , [ x , 1 ] ; 1 , [ z , 1 ] ] " - 1 + 2x + 2y + - z " ; ( ) |
let test_multiply_polynomial _ = let assert_multiply given1 given2 expected = let given1 = polynomial_create_from_variables_list given1 in let given2 = polynomial_create_from_variables_list given2 in assert_equal ~ printer : Fn . id expected ( polynomial_show_normal ( polynomial_multiply given1 given2 ) ) ; assert_equal ~ printer : Fn . id expected ( polynomial_show_normal ( polynomial_multiply given2 given1 ) ) in let x = Type . Variable . Unary . create " x " in let y = Type . Variable . Unary . create " y " in let z = Type . Variable . Unary . create " z " in assert_multiply [ 1 , [ ] ] [ ] " 0 " ; assert_multiply [ 1 , [ x , 1 ] ] [ ] " 0 " ; assert_multiply [ 2 , [ ] ] [ 4 , [ ] ] " 8 " ; assert_multiply [ 2 , [ y , 1 ] ] [ 1 , [ z , 1 ] ] " 2yz " ; assert_multiply [ 2 , [ y , 1 ] ] [ 1 , [ x , 1 ] ] " 2xy " ; assert_multiply [ 2 , [ y , 1 ] ] [ 1 , [ x , 1 ; z , 1 ] ] " 2xyz " ; assert_multiply [ 3 , [ ] ; 1 , [ x , 1 ] ] [ 1 , [ y , 1 ] ] " 3y + xy " ; assert_multiply [ 1 , [ x , 1 ] ; 3 , [ z , 1 ] ] [ 2 , [ ] ; 1 , [ y , 2 ] ] " 2x + 6z + xy ^ 2 + 3y ^ 2z " ; ( ) |
let test_divide_polynomial _ = let assert_divide given1 given2 expected = let given1 = polynomial_create_from_variables_list given1 in let given2 = polynomial_create_from_variables_list given2 in assert_equal ~ printer : Fn . id expected ( polynomial_show_normal ( polynomial_divide given1 given2 ) ) in let x = Type . Variable . Unary . create " x " in let y = Type . Variable . Unary . create " y " in let z = Type . Variable . Unary . create " z " in assert_divide [ 1 , [ x , 1 ] ] [ ] " 0 " ; assert_divide [ 2 , [ ] ] [ 2 , [ ] ] " 1 " ; assert_divide [ 4 , [ ] ] [ 2 , [ ] ] " 2 " ; assert_divide [ - 3 , [ ] ] [ 2 , [ ] ] " - 2 " ; assert_divide [ 2 , [ y , 1 ] ] [ 1 , [ z , 1 ] ] " ( 2y // z ) " ; assert_divide [ 2 , [ x , 1 ] ; 1 , [ y , 1 ] ] [ 1 , [ y , 1 ] ; 1 , [ z , 1 ] ] " ( ( 2x + y ) ( // y + z ) ) " ; assert_divide [ 2 , [ y , 1 ] ] [ 2 , [ x , 1 ] ] " ( y // x ) " ; assert_divide [ 2 , [ y , 1 ; z , 1 ] ] [ 1 , [ x , 1 ; z , 1 ] ] " ( 2y // x ) " ; assert_divide [ 2 , [ y , 1 ; z , 2 ] ] [ 1 , [ z , 1 ] ] " 2yz " ; assert_divide [ 2 , [ x , 3 ; y , 1 ] ; 2 , [ x , 2 ; z , 1 ] ] [ 4 , [ x , 2 ; y , 1 ] ] " ( ( z + xy ) // 2y ) " ; ( ) |
let test_parameter_create _ = assert_equal ( Type . Callable . Parameter . create [ { Type . Callable . Parameter . name = " __ " ; annotation = Type . integer ; default = false } ] ) [ Type . Callable . Parameter . PositionalOnly { index = 0 ; annotation = Type . integer ; default = false } ; ] |
let test_resolve_getitem_callee _ = let open Expression in let assert_resolved_getitem_callee ( ? resolve_aliases = Fn . id ) actual expected = let parse_callee given = match parse_single_expression given |> Node . value with | Expression . Call { callee = { Node . value = callee ; _ } ; _ } -> callee | _ -> failwith " expected Call " in assert_equal ~ cmp ( : fun left right -> Expression . location_insensitive_compare ( Node . create_with_default_location left ) ( Node . create_with_default_location right ) = 0 ) ~ printer [ :% show : Expression . expression ] ( parse_callee expected ) ( Type . Callable . resolve_getitem_callee ~ resolve_aliases ( parse_callee actual ) ) in assert_resolved_getitem_callee " NotAlias [ int ] " " NotAlias [ int ] " ; assert_resolved_getitem_callee " typing . Callable [ [ int ] , str ] " " typing . Callable [ [ int ] , str ] " ; assert_resolved_getitem_callee " typing . Callable [ [ int ] , str ] [ [ int ] , str ] " " typing . Callable [ [ int ] , str ] [ [ int ] , str ] " ; assert_resolved_getitem_callee " typing . Callable [ [ int ] , str ] [ [ int ] , str ] [ [ int ] , str ] " " typing . Callable [ [ int ] , str ] [ [ int ] , str ] [ [ int ] , str ] " ; assert_resolved_getitem_callee ~ resolve_aliases ( : function | Type . Primitive " bar . baz . Callable " -> Type . Primitive " typing . Callable " | annotation -> annotation ) " bar . baz . Callable [ [ int ] , str ] " " typing . Callable [ [ int ] , str ] " ; assert_resolved_getitem_callee ~ resolve_aliases ( : function | Type . Primitive " bar . baz . Callable " -> Type . Callable . create ~ annotation : Type . Any ~ parameters : Undefined ( ) | annotation -> annotation ) " bar . baz . Callable [ [ int ] , str ] " " typing . Callable [ [ int ] , str ] " ; assert_resolved_getitem_callee ~ resolve_aliases ( : function | Type . Primitive " bar . baz . CallableAlias " -> Type . Callable . create ~ annotation : Type . integer ~ parameters : ( Defined [ PositionalOnly { index = 0 ; annotation = Type . variable " T " ; default = false } ] ) ( ) | annotation -> annotation ) " bar . baz . CallableAlias [ str ] " " bar . baz . CallableAlias [ str ] " ; assert_resolved_getitem_callee ~ resolve_aliases ( : function | Type . Primitive " bar . baz . Callable " -> Type . Primitive " typing . Callable " | annotation -> annotation ) " bar . baz . Callable [ [ int ] , str ] [ [ int ] , str ] [ [ int ] , str ] " " typing . Callable [ [ int ] , str ] [ [ int ] , str ] [ [ int ] , str ] " ; assert_resolved_getitem_callee ~ resolve_aliases ( : function | Type . Primitive " bar . baz . Callable " -> Type . Primitive " typing . Callable " | annotation -> annotation ) " not_an_alias . Callable [ [ int ] , str ] " " not_an_alias . Callable [ [ int ] , str ] " ; assert_resolved_getitem_callee ~ resolve_aliases ( : function | Type . Primitive " bar . baz . Callable " -> Type . Primitive " typing . Callable " | annotation -> annotation ) " Foo [ int ] . Callable [ [ int ] , str ] " " Foo [ int ] . Callable [ [ int ] , str ] " ; ( ) |
let test_resolve_class _ = let assert_resolved_class annotation expected = assert_equal ~ printer ( : fun x -> [ % sexp_of : Type . class_data list option ] x |> Sexp . to_string_hum ) expected ( Type . resolve_class annotation ) in assert_resolved_class Type . Any ( Some [ ] ) ; assert_resolved_class ( Type . meta Type . integer ) ( Some [ { instantiated = Type . integer ; accessed_through_class = true ; class_name = " int " } ] ) ; assert_resolved_class ( Type . optional Type . integer ) ( Some [ { instantiated = Type . optional Type . integer ; accessed_through_class = false ; class_name = " typing . Optional " ; } ; ] ) ; assert_resolved_class ( Type . union [ Type . integer ; Type . string ] ) ( Some [ { instantiated = Type . integer ; accessed_through_class = false ; class_name = " int " } ; { instantiated = Type . string ; accessed_through_class = false ; class_name = " str " } ; ] ) ; assert_resolved_class ( Type . union [ Type . Primitive " Foo " ; Type . list Type . integer ] ) ( Some [ { instantiated = Type . list Type . integer ; accessed_through_class = false ; class_name = " list " ; } ; { instantiated = Type . Primitive " Foo " ; accessed_through_class = false ; class_name = " Foo " } ; ] ) ; assert_resolved_class ( Type . union [ Type . Primitive " Foo " ; Type . list Type . integer ] ) ( Some [ { instantiated = Type . list Type . integer ; accessed_through_class = false ; class_name = " list " ; } ; { instantiated = Type . Primitive " Foo " ; accessed_through_class = false ; class_name = " Foo " } ; ] ) ; let tree_annotation = Type . RecursiveType . create ~ name " : Tree " ~ body ( : Type . union [ Type . integer ; Type . tuple [ Type . Primitive " Foo " ; Type . Primitive " Tree " ] ] ) in assert_resolved_class tree_annotation ( Some [ { instantiated = Type . integer ; accessed_through_class = false ; class_name = " int " } ; { instantiated = Type . tuple [ Type . Primitive " Foo " ; tree_annotation ] ; accessed_through_class = false ; class_name = " tuple " ; } ; ] ) ; let recursive_list = Type . RecursiveType . create ~ name " : RecursiveList " ~ body ( : Type . list ( Type . union [ Type . integer ; Type . Primitive " RecursiveList " ] ) ) in assert_resolved_class recursive_list ( Some [ { instantiated = Type . list ( Type . union [ Type . integer ; recursive_list ] ) ; accessed_through_class = false ; class_name = " list " ; } ; ] ) ; let directly_recursive_type = Type . RecursiveType . create ~ name " : Tree " ~ body ( : Type . union [ Type . integer ; Type . Primitive " Tree " ] ) in assert_resolved_class directly_recursive_type ( Some [ { instantiated = Type . integer ; accessed_through_class = false ; class_name = " int " } ] ) ; ( ) |
let test_show _ = let assert_show given ~ expected_full ~ expected_concise = assert_equal ~ cmp : String . equal ~ printer : Fn . id ( [ % show : Type . t ] given ) expected_full ; assert_equal ~ cmp : String . equal ~ printer : Fn . id ( Type . show_concise given ) expected_concise in let callable1 = Type . Callable . create ~ parameters ( : make_callable_from_arguments [ Type . integer ] ) ~ annotation : Type . string ( ) in let callable2 = Type . Callable . create ~ parameters ( : make_callable_from_arguments [ Type . string ] ) ~ annotation : Type . bool ( ) in let ts = Type . Variable . Variadic . Tuple . create " Ts " in assert_show ( Type . TypeOperation ( Compose ( Type . OrderedTypes . Concrete [ callable1 ; callable2 ] ) ) ) ~ expected_full : " pyre_extensions . Compose [ typing . Callable [ [ int ] , str ] , typing . Callable [ [ str ] , bool ] ] " ~ expected_concise " : Compose [ ( int ) -> str , ( str ) -> bool ] " ; assert_show ( Type . TypeOperation ( Compose ( Type . OrderedTypes . Concatenation ( Type . OrderedTypes . Concatenation . create ~ prefix [ : callable1 ] ts ) ) ) ) ~ expected_full " : pyre_extensions . Compose [ typing . Callable [ [ int ] , str ] , * Ts ] " ~ expected_concise " : Compose [ ( int ) -> str , * Ts ] " ; assert_show ( Type . TypeOperation ( Compose ( Type . OrderedTypes . Concatenation ( Type . OrderedTypes . Concatenation . create_from_unbounded_element ~ prefix [ : callable1 ] callable2 ) ) ) ) ~ expected_full : " pyre_extensions . Compose [ typing . Callable [ [ int ] , str ] , * Tuple [ typing . Callable [ [ str ] , bool ] , \ . . . ] ] " ~ expected_concise " : Compose [ ( int ) -> str , * Tuple [ ( str ) -> bool , . . . ] ] " ; assert_show ( Type . TypeOperation ( Compose ( Type . OrderedTypes . Concatenation ( Type . OrderedTypes . Concatenation . create_from_concrete_against_concatenation ~ prefix [ ] : ~ suffix [ ] : ~ concrete [ : callable1 ] ~ concatenation ( : Type . OrderedTypes . Concatenation . create ~ prefix [ : callable2 ] ts ) ) ) ) ) ~ expected_full : " pyre_extensions . Compose [ * Broadcast [ typing . Tuple [ typing . Callable [ [ int ] , str ] ] , \ typing . Tuple [ typing . Callable [ [ str ] , bool ] , * Ts ] ] ] " ~ expected_concise : " Compose [ * Broadcast [ typing . Tuple [ ( int ) -> str ] , typing . Tuple [ ( str ) -> bool , * Ts ] ] ] " ; ( ) |
let ( ) = " type " >::: [ " create " >:: test_create ; " create_callable " >:: test_create_callable ; " create_alias " >:: test_create_alias ; " create_type_operator " >:: test_create_type_operator ; " create_variadic_tuple " >:: test_create_variadic_tuple ; " resolve_aliases " >:: test_resolve_aliases ; " instantiate " >:: test_instantiate ; " expression " >:: test_expression ; " concise " >:: test_concise ; " weaken_literals " >:: test_weaken_literals ; " union " >:: test_union ; " primitives " >:: test_primitives ; " elements " >:: test_elements ; " exists " >:: test_exists ; " contains_callable " >:: test_contains_callable ; " contains_any " >:: test_contains_any ; " expression_contains_any " >:: test_expression_contains_any ; " is_concrete " >:: test_is_concrete ; " is_not_instantiated " >:: test_is_not_instantiated ; " is_meta " >:: test_is_meta ; " is_none " >:: test_is_none ; " is_type_alias " >:: test_is_type_alias ; " create_recursive_type " >:: test_create_recursive_type ; " unfold_recursive_type " >:: test_unfold_recursive_type ; " contains_unknown " >:: test_contains_unknown ; " contains_undefined " >:: test_contains_undefined ; " is_resolved " >:: test_is_resolved ; " is_iterator " >:: test_is_iterator ; " class_name " >:: test_class_name ; " optional_value " >:: test_optional_value ; " dequalify " >:: test_dequalify ; " variables " >:: test_variables ; " lambda " >:: test_lambda ; " visit " >:: test_visit ; " collapse_escaped_variable_unions " >:: test_collapse_escaped_variable_unions ; " namespace_insensitive_compare " >:: test_namespace_insensitive_compare ; " namespace " >:: test_namespace ; " mark_all_variables_as_bound " >:: test_mark_all_variables_as_bound ; " mark_all_variables_as_free " >:: test_mark_all_variables_as_free ; " namespace_all_free_variables " >:: test_namespace_all_free_variables ; " mark_all_free_variables_as_escaped " >:: test_mark_all_free_variables_as_escaped ; " contains_escaped_free_variable " >:: test_contains_escaped_free_variable ; " convert_all_escaped_free_variables_to_anys " >:: test_convert_all_escaped_free_variables_to_anys ; " int_expression_create " >:: test_int_expression_create ; " replace_all " >:: test_replace_all ; " product_replace_variadic " >:: test_product_replace_variadic ; " less_or_equal_polynomial " >:: test_less_or_equal ; " collect_all " >:: test_collect_all ; " parse_type_variable_declarations " >:: test_parse_type_variable_declarations ; " starred_annotation_expression " >:: test_starred_annotation_expression ; " concatenation_from_unpack_expression " >:: test_concatenation_from_unpack_expression ; " broadcast " >:: test_broadcast ; " split_ordered_types " >:: test_split_ordered_types ; " coalesce_ordered_types " >:: test_coalesce_ordered_types ; " drop_prefix_ordered_type " >:: test_drop_prefix_ordered_type ; " index_ordered_type " >:: test_index_ordered_type ; " zip_variables_with_parameters " >:: test_zip_variables_with_parameters ; " zip_on_two_parameter_lists " >:: test_zip_on_two_parameter_lists ; " union_upper_bound " >:: test_union_upper_bound ; " infer_transform " >:: test_infer_transform ; " fields_from_constructor " >:: test_fields_from_constructor ; " map_callable_annotation " >:: test_map_callable_annotation ; " type_parameters_for_bounded_tuple_union " >:: test_type_parameters_for_bounded_tuple_union ; " polynomial_create_from_list " >:: test_polynomial_create_from_list ; " polynomial_to_type " >:: test_polynomial_to_type ; " polynomial_replace " >:: test_polynomial_replace ; " add_polynomials " >:: test_add_polynomials ; " subtract_polynomials " >:: test_subtract_polynomials ; " multiply_polynomial " >:: test_multiply_polynomial ; " divide_polynomial " >:: test_divide_polynomial ; " resolve_class " >:: test_resolve_class ; " show " >:: test_show ; ] |> Test . run ; " primitive " >::: [ " is unit test " >:: test_is_unit_test ] |> Test . run ; " callable " >::: [ " from_overloads " >:: test_from_overloads ; " with_return_annotation " >:: test_with_return_annotation ; " overload_parameters " >:: test_overload_parameters ; " parameter_create " >:: test_parameter_create ; " resolve_getitem_callee " >:: test_resolve_getitem_callee ; ] |> Test . run |
type error = Unbound_type_variable of string | Undefined_type_constructor of Path . t | Type_arity_mismatch of Longident . t * int * int | Bound_type_variable of string | Recursive_type | Unbound_row_variable of Longident . t | Type_mismatch of Ctype . Unification_trace . t | Alias_type_mismatch of Ctype . Unification_trace . t | Present_has_conjunction of string | Present_has_no_type of string | Constructor_mismatch of type_expr * type_expr | Not_a_variant of type_expr | Variant_tags of string * string | Invalid_variable_name of string | Cannot_quantify of string * type_expr | Multiple_constraints_on_type of Longident . t | Method_mismatch of string * type_expr * type_expr | Opened_object of Path . t option | Not_an_object of type_expr |
type variable_context = int * type_expr TyVarMap . t |
let transl_modtype_longident = ref ( fun _ -> assert false ) |
let transl_modtype = ref ( fun _ -> assert false ) |
let create_package_mty fake loc env ( p , l ) = let l = List . sort ( fun ( s1 , _t1 ) ( s2 , _t2 ) -> if s1 . txt = s2 . txt then raise ( Error ( loc , env , Multiple_constraints_on_type s1 . txt ) ) ; compare s1 . txt s2 . txt ) l in l , List . fold_left ( fun mty ( s , t ) -> let d = { ptype_name = mkloc ( Longident . last s . txt ) s . loc ; ptype_params = [ ] ; ptype_cstrs = [ ] ; ptype_kind = Ptype_abstract ; ptype_private = Asttypes . Public ; ptype_manifest = if fake then None else Some t ; ptype_attributes = [ ] ; ptype_loc = loc } in Ast_helper . Mty . mk ~ loc ( Pmty_with ( mty , [ Pwith_type ( { txt = s . txt ; loc } , d ) ] ) ) ) ( Ast_helper . Mty . mk ~ loc ( Pmty_ident p ) ) l |
let type_variables = ref ( TyVarMap . empty : type_expr TyVarMap . t ) |
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 } ; ; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.