Skip to content

Commit

Permalink
Rename ParameterVariadicTypeVariable to FromParamSpec
Browse files Browse the repository at this point in the history
Summary:
This type, which was frequently written out as the ultra-verbose name
`Type.Callable.ParameterVariadicTypeVariable`, actually represents a parameters
"list" that includes a ParamSpec (either a bare param spec like
`Callable[P, R]` or a concatenation like `Callable[Concatenate[T, P], R]`).

The name `FromParamSpec` seems much clearer; I say "'from' param spec" to
emphasize that it is built from a ParamSpec, but it could potentially
concatenate concrete positional-only args to the left.

Reviewed By: migeed-z

Differential Revision: D59026652

fbshipit-source-id: 6d5c174123dbd5827647bb0eacb6d0a47db5bff7
  • Loading branch information
stroxler authored and facebook-github-bot committed Jun 26, 2024
1 parent 786d9c1 commit ed8cb98
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 117 deletions.
7 changes: 2 additions & 5 deletions source/analysis/analysisError.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1791,9 +1791,7 @@ let rec messages ~concise ~signature location kind =
let arguments = if has_arguments then "(...)" else "" in
let recurse = messages ~concise ~signature location in
let has_param_spec_variable = function
| Type.Callable { implementation = { parameters = ParameterVariadicTypeVariable _; _ }; _ }
->
true
| Type.Callable { implementation = { parameters = FromParamSpec _; _ }; _ } -> true
| _ -> false
in
match reason, reason >>| recurse >>= List.hd with
Expand Down Expand Up @@ -2490,8 +2488,7 @@ let rec messages ~concise ~signature location kind =
"See https://pyre-check.org/docs/errors/#62-non-literal-string for more details.";
]
| NotCallable
(Type.Callable { implementation = { parameters = ParameterVariadicTypeVariable _; _ }; _ } as
annotation) ->
(Type.Callable { implementation = { parameters = FromParamSpec _; _ }; _ } as annotation) ->
[
Format.asprintf
"`%a` cannot be safely called because the types and kinds of its parameters depend on a \
Expand Down
2 changes: 1 addition & 1 deletion source/analysis/annotatedCallable.ml
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ let create_overload_without_applying_decorators
|> Option.all
in
match parsed_head with
| Some head -> ParameterVariadicTypeVariable { head; variable }
| Some head -> FromParamSpec { head; variable }
| None -> default ())
| None -> default ())
| _ -> Defined (List.map parameters ~f:parse)
Expand Down
2 changes: 1 addition & 1 deletion source/analysis/annotatedDefine.ml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ let decorate
placed_single_star, new_parameter :: sofar
in
List.fold parameters ~f:convert ~init:(false, []) |> snd |> List.rev
| ParameterVariadicTypeVariable _
| FromParamSpec _
| Undefined ->
original_parameters
in
Expand Down
12 changes: 5 additions & 7 deletions source/analysis/attributeResolution.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1307,8 +1307,7 @@ module SignatureSelection = struct
|> check_arguments_against_parameters ~callable
|> fun signature_match -> [signature_match]
| Undefined -> [base_signature_match]
| ParameterVariadicTypeVariable { head; variable } when Type.Variable.ParamSpec.is_free variable
-> (
| FromParamSpec { head; variable } when Type.Variable.ParamSpec.is_free variable -> (
(* Handle callables where an early parameter binds a ParamSpec and later parameters expect
the corresponding arguments.
Expand Down Expand Up @@ -1379,7 +1378,7 @@ module SignatureSelection = struct
|> function
| [] -> [head_signature]
| nonempty -> nonempty)
| ParameterVariadicTypeVariable { head; variable } -> (
| FromParamSpec { head; variable } -> (
(* The ParamSpec variable `P` is in scope, so the only valid arguments are `*args` and
`**kwargs` that have "type" `P.args` and `P.kwargs` respectively. If the ParamSpec has a
`head` prefix of parameters, check for any prefix arguments. *)
Expand All @@ -1405,7 +1404,7 @@ module SignatureSelection = struct
[
{
base_signature_match with
reasons = { arity = [CallingParameterVariadicTypeVariable]; annotation = [] };
reasons = { arity = [CallingFromParamSpec]; annotation = [] };
};
])

Expand Down Expand Up @@ -1448,7 +1447,7 @@ module SignatureSelection = struct
| [], reason :: reasons ->
let importance = function
| AbstractClassInstantiation _ -> 1
| CallingParameterVariadicTypeVariable -> 1
| CallingFromParamSpec -> 1
| InvalidKeywordArgument _ -> 0
| InvalidVariableArgument _ -> 0
| Mismatches _ -> -1
Expand Down Expand Up @@ -2127,8 +2126,7 @@ let partial_apply_self { Type.Callable.implementation; overloads; _ } ~order ~se
let parameters =
match parameters with
| Type.Callable.Defined (_ :: parameters) -> Type.Callable.Defined parameters
| ParameterVariadicTypeVariable { head = _ :: head; variable } ->
ParameterVariadicTypeVariable { head; variable }
| FromParamSpec { head = _ :: head; variable } -> FromParamSpec { head; variable }
| _ -> parameters
in
{ Type.Callable.annotation; parameters }
Expand Down
29 changes: 12 additions & 17 deletions source/analysis/constraintsSet.ml
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,8 @@ module Make (OrderedConstraints : OrderedConstraintsType) = struct
the Python type system at the moment. *)
| Defined _, Undefined -> [initial_constraints]
| Undefined, Defined _ -> [initial_constraints]
| ( ParameterVariadicTypeVariable { head = left_head; variable = left_variable },
ParameterVariadicTypeVariable { head = right_head; variable = right_variable } )
| ( FromParamSpec { head = left_head; variable = left_variable },
FromParamSpec { head = right_head; variable = right_variable } )
when Type.Variable.ParamSpec.is_free left_variable
&& Type.Variable.ParamSpec.is_free right_variable ->
let add_parameter_specification_bounds constraints =
Expand All @@ -323,14 +323,12 @@ module Make (OrderedConstraints : OrderedConstraintsType) = struct
~order
~pair:
(Type.Variable.ParamSpecPair
( right_variable,
ParameterVariadicTypeVariable { head = []; variable = left_variable } ))
(right_variable, FromParamSpec { head = []; variable = left_variable }))
>>= OrderedConstraints.add_lower_bound
~order
~pair:
(Type.Variable.ParamSpecPair
( left_variable,
ParameterVariadicTypeVariable { head = []; variable = right_variable } ))
(left_variable, FromParamSpec { head = []; variable = right_variable }))
|> Option.to_list
in
solve_ordered_types_less_or_equal
Expand All @@ -339,23 +337,22 @@ module Make (OrderedConstraints : OrderedConstraintsType) = struct
~right:(Type.OrderedTypes.Concrete left_head)
~constraints:initial_constraints
|> List.concat_map ~f:add_parameter_specification_bounds
| bound, ParameterVariadicTypeVariable { head = []; variable }
when Type.Variable.ParamSpec.is_free variable ->
| bound, FromParamSpec { head = []; variable } when Type.Variable.ParamSpec.is_free variable
->
let pair = Type.Variable.ParamSpecPair (variable, bound) in
OrderedConstraints.add_upper_bound initial_constraints ~order ~pair |> Option.to_list
| bound, ParameterVariadicTypeVariable { head; variable }
when Type.Variable.ParamSpec.is_free variable ->
| bound, FromParamSpec { head; variable } when Type.Variable.ParamSpec.is_free variable ->
let constraints, remainder =
match bound with
| Undefined -> [initial_constraints], Undefined
| ParameterVariadicTypeVariable { head = left_head; variable = left_variable } ->
| FromParamSpec { head = left_head; variable = left_variable } ->
let paired, remainder = List.split_n left_head (List.length head) in
( solve_ordered_types_less_or_equal
order
~left:(Type.OrderedTypes.Concrete paired)
~right:(Type.OrderedTypes.Concrete head)
~constraints:initial_constraints,
ParameterVariadicTypeVariable { head = remainder; variable = left_variable } )
FromParamSpec { head = remainder; variable = left_variable } )
| Defined defined ->
let paired, remainder = List.split_n defined (List.length head) in
( solve_parameters
Expand All @@ -366,8 +363,8 @@ module Make (OrderedConstraints : OrderedConstraintsType) = struct
in
let pair = Type.Variable.ParamSpecPair (variable, remainder) in
List.filter_map constraints ~f:(OrderedConstraints.add_upper_bound ~order ~pair)
| ParameterVariadicTypeVariable left, ParameterVariadicTypeVariable right
when Type.Callable.equal_parameter_variadic_type_variable Type.equal left right ->
| FromParamSpec left, FromParamSpec right
when Type.Callable.equal_params_from_param_spec Type.equal left right ->
[initial_constraints]
| _, _ -> impossible
with
Expand Down Expand Up @@ -1071,9 +1068,7 @@ module Make (OrderedConstraints : OrderedConstraintsType) = struct
|> fun instantiated -> [Type.Parameter.Single instantiated]
| ParamSpecVariable variable ->
TypeConstraints.Solution.instantiate_single_param_spec solution variable
|> Option.value
~default:
(Type.Callable.ParameterVariadicTypeVariable { head = []; variable })
|> Option.value ~default:(Type.Callable.FromParamSpec { head = []; variable })
|> fun instantiated -> [Type.Parameter.CallableParameters instantiated]
| TypeVarTupleVariable variadic ->
TypeConstraints.Solution.instantiate_ordered_types
Expand Down
2 changes: 1 addition & 1 deletion source/analysis/signatureSelectionTypes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type reason =
class_name: Reference.t;
abstract_methods: string list;
}
| CallingParameterVariadicTypeVariable
| CallingFromParamSpec
| InvalidKeywordArgument of invalid_argument Node.t
| InvalidVariableArgument of invalid_argument Node.t
| Mismatches of mismatch_reason list
Expand Down
4 changes: 2 additions & 2 deletions source/analysis/test/annotatedSignatureTest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ let test_unresolved_select =
{
annotation = Type.integer;
parameters =
ParameterVariadicTypeVariable
FromParamSpec
{
head = [];
variable =
Expand All @@ -937,7 +937,7 @@ let test_unresolved_select =
(NotFound
{
closest_return_annotation = Type.integer;
reason = Some SignatureSelectionTypes.CallingParameterVariadicTypeVariable;
reason = Some SignatureSelectionTypes.CallingFromParamSpec;
});
]

Expand Down
2 changes: 1 addition & 1 deletion source/analysis/test/constraintsSetTest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ let make_assert_functions context =
None
else
Some
(Type.Callable.ParameterVariadicTypeVariable
(Type.Callable.FromParamSpec
{ head = []; variable = Type.Variable.ParamSpec.mark_as_bound variable })
in
let mark_tuple_variadic variable =
Expand Down
3 changes: 1 addition & 2 deletions source/analysis/test/globalResolutionTest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1227,8 +1227,7 @@ let test_invalid_type_parameters context =
"test.Foo"
[
CallableParameters
(ParameterVariadicTypeVariable
{ Type.Callable.head = []; variable = parameter_variadic });
(FromParamSpec { Type.Callable.head = []; variable = parameter_variadic });
])
~expected_transformed_type:
(Type.parametric
Expand Down
30 changes: 8 additions & 22 deletions source/analysis/test/typeConstraintsTest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,7 @@ let test_multiple_variable_solution _ =
assert_solution
~sequentially_applied_bounds:
[
`Lower
(ParamSpecPair
(parameters_a, Type.Callable.ParameterVariadicTypeVariable (empty_head parameters_b)));
`Lower (ParamSpecPair (parameters_a, Type.Callable.FromParamSpec (empty_head parameters_b)));
`Lower (ParamSpecPair (parameters_b, empty_parameters));
]
(Some
Expand All @@ -422,12 +420,8 @@ let test_multiple_variable_solution _ =
assert_solution
~sequentially_applied_bounds:
[
`Lower
(ParamSpecPair
(parameters_a, Type.Callable.ParameterVariadicTypeVariable (empty_head parameters_b)));
`Lower
(ParamSpecPair
(parameters_b, Type.Callable.ParameterVariadicTypeVariable (empty_head parameters_a)));
`Lower (ParamSpecPair (parameters_a, Type.Callable.FromParamSpec (empty_head parameters_b)));
`Lower (ParamSpecPair (parameters_b, Type.Callable.FromParamSpec (empty_head parameters_a)));
]
None;
let parameters_with_unconstrained_a =
Expand Down Expand Up @@ -461,7 +455,7 @@ let test_multiple_variable_solution _ =
(TypeVarPair
( unconstrained_a,
Type.Callable.create
~parameters:(Type.Callable.ParameterVariadicTypeVariable (empty_head parameters_a))
~parameters:(Type.Callable.FromParamSpec (empty_head parameters_a))
~annotation:Type.integer
() ));
]
Expand Down Expand Up @@ -625,18 +619,10 @@ let test_partial_solution _ =
~variables:[Type.Variable.ParamSpecVariable parameters_a]
~bounds:
[
`Lower
(ParamSpecPair
(parameters_a, Type.Callable.ParameterVariadicTypeVariable (empty_head parameters_b)));
`Lower
(ParamSpecPair
(parameters_b, Type.Callable.ParameterVariadicTypeVariable (empty_head parameters_a)));
`Lower (ParamSpecPair (parameters_a, Type.Callable.FromParamSpec (empty_head parameters_b)));
`Lower (ParamSpecPair (parameters_b, Type.Callable.FromParamSpec (empty_head parameters_a)));
]
(Some
[
ParamSpecPair
(parameters_a, Type.Callable.ParameterVariadicTypeVariable (empty_head parameters_b));
])
(Some [ParamSpecPair (parameters_a, Type.Callable.FromParamSpec (empty_head parameters_b))])
(Some []);

(* Variadic tuples. *)
Expand Down Expand Up @@ -719,7 +705,7 @@ let test_exists _ =
let constraints_with_parameters_b =
let pair =
Type.Variable.ParamSpecPair
(parameters_a, Type.Callable.ParameterVariadicTypeVariable (empty_head parameters_b))
(parameters_a, Type.Callable.FromParamSpec (empty_head parameters_b))
in
DiamondOrderedConstraints.add_lower_bound TypeConstraints.empty ~order ~pair
|> fun constraints_option -> Option.value_exn constraints_option
Expand Down
Loading

0 comments on commit ed8cb98

Please sign in to comment.