Skip to content

Commit

Permalink
Let the typechecker recognize type variables used within type aliases
Browse files Browse the repository at this point in the history
Summary: As shown in the unittests, we would like to collect type variables within type aliases and add them to the resolution, so that the typechecker recognizes them as type annotations.

Reviewed By: stroxler

Differential Revision: D62763697

fbshipit-source-id: e6b368fc2d139003e5face0272896a3acc3857bb
  • Loading branch information
Zeina Migeed authored and facebook-github-bot committed Sep 19, 2024
1 parent 0bd1ccd commit cda981f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
3 changes: 0 additions & 3 deletions source/analysis/test/integration/typeVariableTest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2263,7 +2263,6 @@ let test_generic_aliases =
|}
[
"Undefined or invalid type [11]: Annotation `T` is not defined as a type.";
"Parsing failure [404]: PEP 695 type params are unsupported";
"Revealed type [-1]: Revealed type for `test.func(x)` is \
`typing.Union[typing.List[int], typing.Set[int]]`.";
Expand Down Expand Up @@ -2302,8 +2301,6 @@ let test_generic_aliases =
reveal_type(apply_callback)
|}
[
"Undefined or invalid type [11]: Annotation `S1` is not defined as a type.";
"Undefined or invalid type [11]: Annotation `S2` is not defined as a type.";
"Parsing failure [404]: PEP 695 type params are unsupported";
"Revealed type [-1]: Revealed type for `test.apply_callback` is \
`typing.Callable(apply_callback)[[Named(callback, typing.Callable[[int], \
Expand Down
31 changes: 19 additions & 12 deletions source/analysis/typeCheck.ml
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,18 @@ module State (Context : Context) = struct
List.fold mismatches ~f:emit_error ~init:errors


let get_type_params_as_variables type_params =
List.map type_params ~f:(fun { Node.value; _ } ->
match value with
| Ast.Expression.TypeParam.TypeVar { name; _ } ->
Type.Variable.TypeVarVariable
(Type.Variable.TypeVar.create ~constraints:Unconstrained name)
| Ast.Expression.TypeParam.TypeVarTuple name ->
Type.Variable.TypeVarTupleVariable (Type.Variable.TypeVarTuple.create name)
| Ast.Expression.TypeParam.ParamSpec name ->
Type.Variable.ParamSpecVariable (Type.Variable.ParamSpec.create name))


let add_invalid_type_parameters_errors ~resolution ~location ~errors annotation =
let mismatches, annotation =
GlobalResolution.validate_and_sanitize_type_arguments resolution annotation
Expand Down Expand Up @@ -5758,6 +5770,11 @@ module State (Context : Context) = struct
forward_assignment ~resolution ~location ~target ~annotation:None ~value:(Some call)
(* TODO(T196994965): handle type alias *)
| TypeAlias { TypeAlias.name; type_params; value } ->
let resolution =
get_type_params_as_variables type_params
|> List.fold ~init:resolution ~f:(fun resolution variable ->
Resolution.add_type_variable resolution ~variable)
in
(* TODO: remove after PEP 695 is supported *)
let type_params_errors =
match type_params with
Expand Down Expand Up @@ -6352,20 +6369,10 @@ module State (Context : Context) = struct
Context.define
in
(* collect type parameters for functions *)
let type_param_names =
List.map type_params ~f:(fun { Node.value; _ } ->
match value with
| Ast.Expression.TypeParam.TypeVar { name; _ } ->
Type.Variable.TypeVarVariable
(Type.Variable.TypeVar.create ~constraints:Unconstrained name)
| Ast.Expression.TypeParam.TypeVarTuple name ->
Type.Variable.TypeVarTupleVariable (Type.Variable.TypeVarTuple.create name)
| Ast.Expression.TypeParam.ParamSpec name ->
Type.Variable.ParamSpecVariable (Type.Variable.ParamSpec.create name))
in
let type_params = get_type_params_as_variables type_params in
(* Add them to the resolution *)
let resolution =
type_param_names
type_params
|> List.fold ~init:resolution ~f:(fun resolution variable ->
Resolution.add_type_variable resolution ~variable)
in
Expand Down

0 comments on commit cda981f

Please sign in to comment.