Skip to content

Commit

Permalink
Do not store type parameters as target
Browse files Browse the repository at this point in the history
Summary:
In this diff, we simplify the representation for storing type parameters in preparation for supporting type variables for classes in PEP695.

Two things I need to follow up on:

1- Do we have to synthesize a target in parents_and_generic_of_target?

2- we should represent type parameters as Type.Variable.t list option

Reviewed By: stroxler

Differential Revision: D61570813

fbshipit-source-id: 1b3f267673f84a892f3ed942b131237374471fe7
  • Loading branch information
migeed-z authored and facebook-github-bot committed Aug 21, 2024
1 parent b180604 commit 47ddf60
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 15 deletions.
8 changes: 4 additions & 4 deletions source/analysis/classHierarchy.ml
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ module Edges = struct
(* The instantiation of `typing.Generic` that the class inherits from but is not necessarily
listed explicitly as a parent. It needs to be stored separately because this class may not
take part in MRO computation. *)
generic_base: Target.t option;
parameters_as_generic_base_arguments: Type.Argument.t list option;
}
[@@deriving sexp, compare]
end
Expand All @@ -145,10 +145,10 @@ let parents_of (module Handler : Handler) target =

let parents_and_generic_of_target (module Handler : Handler) target =
Handler.edges target
>>= fun { parents; generic_base; _ } ->
match generic_base with
>>= fun { parents; parameters_as_generic_base_arguments; _ } ->
match parameters_as_generic_base_arguments with
| None -> Some parents
| Some base -> Some (List.append parents [base])
| Some base -> Some (List.append parents [{ target = generic_primitive; arguments = base }])


let is_instantiated (module Handler : Handler) annotation =
Expand Down
2 changes: 1 addition & 1 deletion source/analysis/classHierarchy.mli
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ end
module Edges : sig
type t = {
parents: Target.t list;
generic_base: Target.t option;
parameters_as_generic_base_arguments: Type.Argument.t list option;
}
[@@deriving sexp, compare]
end
Expand Down
7 changes: 3 additions & 4 deletions source/analysis/classHierarchyEnvironment.ml
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ module IncomingDataComputation = struct
|> deduplicate
|> remove_extra_edges_to_object
in
let generic_base =
let parameters_as_generic_base_arguments =
let open Option in
let parsed_bases =
List.map
Expand All @@ -200,10 +200,9 @@ module IncomingDataComputation = struct
in
compute_generic_base parsed_bases
>>= fun base ->
extract_supertype (Type.expression base)
>>= fun (name, arguments) -> Some { ClassHierarchy.Target.target = name; arguments }
extract_supertype (Type.expression base) >>= fun (_, arguments) -> Some arguments
in
Some { ClassHierarchy.Edges.parents; generic_base }
Some { ClassHierarchy.Edges.parents; parameters_as_generic_base_arguments }
end

module OutgoingDataComputation = struct
Expand Down
11 changes: 8 additions & 3 deletions source/analysis/test/classHierarchyEnvironmentTest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ let test_simple_registration context =
List.map
~f:(fun name -> { ClassHierarchy.Target.target = name; arguments = [] })
expected_edges;
generic_base = None;
parameters_as_generic_base_arguments = None;
}
in
assert_equal
Expand Down Expand Up @@ -64,11 +64,16 @@ let test_parents_and_inferred_generic_base context =
arguments = List.map concretes ~f:(fun single -> Type.Argument.Single single);
}
in
let create_argument_list (_, concretes) =
List.map concretes ~f:(fun single -> Type.Argument.Single single)
in

let expected =
Some
{
ClassHierarchy.Edges.parents = List.map expected_parents ~f:create_target;
generic_base = Option.map expected_inferred_generic_base ~f:create_target;
parameters_as_generic_base_arguments =
Option.map expected_inferred_generic_base ~f:create_argument_list;
}
in
assert_equal
Expand Down Expand Up @@ -325,7 +330,7 @@ let test_updates context =
ClassHierarchy.Edges.parents =
List.map expectation ~f:(fun name ->
{ ClassHierarchy.Target.target = name; arguments = [] });
generic_base = None;
parameters_as_generic_base_arguments = None;
})
in
ClassHierarchyEnvironment.ReadOnly.get_edges read_only ~dependency class_name
Expand Down
2 changes: 1 addition & 1 deletion source/analysis/test/environmentTest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1143,7 +1143,7 @@ let test_remove_extra_edges_to_object context =
(find_unsafe Handler.edges "test.Zero")
{
ClassHierarchy.Edges.parents = [{ ClassHierarchy.Target.target = "test.One"; arguments = [] }];
generic_base = None;
parameters_as_generic_base_arguments = None;
};
()
Expand Down
8 changes: 6 additions & 2 deletions source/test/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3671,7 +3671,11 @@ module MockClassHierarchyHandler = struct
let new_target = { ClassHierarchy.Target.target = successor; arguments } in
let predecessor_edges =
match Hashtbl.find edges predecessor with
| None -> { ClassHierarchy.Edges.parents = [new_target]; generic_base = None }
| None ->
{
ClassHierarchy.Edges.parents = [new_target];
parameters_as_generic_base_arguments = None;
}
| Some ({ ClassHierarchy.Edges.parents; _ } as edges) ->
{ edges with parents = new_target :: parents }
in
Expand All @@ -3683,5 +3687,5 @@ module MockClassHierarchyHandler = struct
Hashtbl.set
order.edges
~key:annotation
~data:{ ClassHierarchy.Edges.parents = []; generic_base = None }
~data:{ ClassHierarchy.Edges.parents = []; parameters_as_generic_base_arguments = None }
end

0 comments on commit 47ddf60

Please sign in to comment.