From 47ddf6075ab97debc350f8ef3e61143022580de0 Mon Sep 17 00:00:00 2001 From: Zeina Migeed Date: Tue, 20 Aug 2024 23:46:19 -0700 Subject: [PATCH] Do not store type parameters as target 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 --- source/analysis/classHierarchy.ml | 8 ++++---- source/analysis/classHierarchy.mli | 2 +- source/analysis/classHierarchyEnvironment.ml | 7 +++---- source/analysis/test/classHierarchyEnvironmentTest.ml | 11 ++++++++--- source/analysis/test/environmentTest.ml | 2 +- source/test/test.ml | 8 ++++++-- 6 files changed, 23 insertions(+), 15 deletions(-) diff --git a/source/analysis/classHierarchy.ml b/source/analysis/classHierarchy.ml index 4108601984..434bb4baf6 100644 --- a/source/analysis/classHierarchy.ml +++ b/source/analysis/classHierarchy.ml @@ -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 @@ -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 = diff --git a/source/analysis/classHierarchy.mli b/source/analysis/classHierarchy.mli index 6f5a6680fc..3d32aa47cf 100644 --- a/source/analysis/classHierarchy.mli +++ b/source/analysis/classHierarchy.mli @@ -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 diff --git a/source/analysis/classHierarchyEnvironment.ml b/source/analysis/classHierarchyEnvironment.ml index 8ad9c1b7f0..0edf902f6a 100644 --- a/source/analysis/classHierarchyEnvironment.ml +++ b/source/analysis/classHierarchyEnvironment.ml @@ -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 @@ -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 diff --git a/source/analysis/test/classHierarchyEnvironmentTest.ml b/source/analysis/test/classHierarchyEnvironmentTest.ml index 919ff5075a..c8ac45c2d9 100644 --- a/source/analysis/test/classHierarchyEnvironmentTest.ml +++ b/source/analysis/test/classHierarchyEnvironmentTest.ml @@ -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 @@ -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 @@ -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 diff --git a/source/analysis/test/environmentTest.ml b/source/analysis/test/environmentTest.ml index 6fd5dad954..e50d78f6e3 100644 --- a/source/analysis/test/environmentTest.ml +++ b/source/analysis/test/environmentTest.ml @@ -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; }; () diff --git a/source/test/test.ml b/source/test/test.ml index 0c22b01833..91ca90574b 100644 --- a/source/test/test.ml +++ b/source/test/test.ml @@ -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 @@ -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