Skip to content

Commit

Permalink
Change the type of Type.create so that it doesn't deal with variable …
Browse files Browse the repository at this point in the history
…declarations.

Summary:
We continue to detangle type aliases and variable aliases for PEP695.

This diff does more refactoring for RawAlias.t, which is the TypeAlias and VariableAlias variant. Our  ultimate goal is parsing type variables as variables, not type aliases.

After this diff, we will stack another diff so that in attributeResolution, we are working with variables rather than variable declarations.

Reviewed By: stroxler

Differential Revision: D60075938

fbshipit-source-id: 16126d0c2291793b8f77226a099dc3154c14634b
  • Loading branch information
migeed-z authored and facebook-github-bot committed Jul 23, 2024
1 parent e08d3dd commit b90d79c
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 37 deletions.
2 changes: 1 addition & 1 deletion source/analysis/annotatedBases.mli
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ open Ast

val base_is_from_placeholder_stub
: Expression.t ->
variables:(string -> Type.Variable.Declaration.t option) ->
variables:(string -> Type.Variable.t option) ->
aliases:(?replace_unbound_parameters_with_any:bool -> string -> Type.t option) ->
is_from_empty_stub:(Reference.t -> bool) ->
bool
11 changes: 10 additions & 1 deletion source/analysis/classHierarchyEnvironment.ml
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,16 @@ module Edges = Environment.EnvironmentTable.WithCache (struct

let variable_aliases name =
match queries.get_type_alias ?replace_unbound_parameters_with_any:(Some true) name with
| Some (TypeAliasEnvironment.RawAlias.VariableAlias variable) -> Some variable
| Some (TypeAliasEnvironment.RawAlias.VariableAlias variable) ->
let type_variables =
Type.Variable.of_declaration
~create_type:
(Type.create
~aliases:Type.resolved_empty_aliases
~variables:Type.resolved_empty_variables)
variable
in
Some type_variables
| _ -> None
in

Expand Down
6 changes: 1 addition & 5 deletions source/analysis/globalResolution.mli
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,7 @@ val location_of_global : t -> Reference.t -> Location.WithModule.t option

val class_hierarchy : t -> (module ClassHierarchy.Handler)

val base_is_from_placeholder_stub
: (string -> Type.Variable.Declaration.t option) ->
t ->
Expression.t ->
bool
val base_is_from_placeholder_stub : (string -> Type.Variable.t option) -> t -> Expression.t -> bool

val parse_reference : ?allow_untracked:bool -> t -> Reference.t -> Type.t

Expand Down
11 changes: 10 additions & 1 deletion source/analysis/resolution.ml
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,16 @@ let get_type_alias resolution =

let variables resolution name =
match get_type_alias ?replace_unbound_parameters_with_any:(Some true) resolution name with
| Some (TypeAliasEnvironment.RawAlias.VariableAlias variable) -> Some variable
| Some (TypeAliasEnvironment.RawAlias.VariableAlias variable) ->
let type_variables =
Type.Variable.of_declaration
~create_type:
(Type.create
~aliases:Type.resolved_empty_aliases
~variables:Type.resolved_empty_variables)
variable
in
Some type_variables
| _ -> None


Expand Down
2 changes: 1 addition & 1 deletion source/analysis/resolution.mli
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ val is_consistent_with : t -> Type.t -> Type.t -> expression:Ast.Expression.t op

val global_resolution : t -> GlobalResolution.t

val variables : t -> string -> Type.Variable.Declaration.t option
val variables : t -> string -> Type.Variable.t option

(* Attribute defined by `__getattr__`. *)
val fallback_attribute
Expand Down
12 changes: 11 additions & 1 deletion source/analysis/test/constraintsSetTest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@ let ( ! ) concretes = List.map concretes ~f:(fun single -> Type.Parameter.Single

let variable_aliases aliases name =
match aliases ?replace_unbound_parameters_with_any:(Some true) name with
| Some (TypeAliasEnvironment.RawAlias.VariableAlias variable) -> Some variable
| Some (TypeAliasEnvironment.RawAlias.VariableAlias variable) ->
let type_variables =
Type.Variable.of_declaration
~create_type:
(Type.create
~aliases:Type.resolved_empty_aliases
~variables:Type.resolved_empty_variables)
variable
in

Some type_variables
| _ -> None


Expand Down
2 changes: 1 addition & 1 deletion source/analysis/test/globalResolutionTest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1109,7 +1109,7 @@ let test_invalid_type_parameters =
let parse annotation =
let variable_aliases name =
match name with
| "Ts" -> Some (Type.Variable.Declaration.DTypeVarTuple { name = "Ts" })
| "Ts" -> Some (Type.Variable.TypeVarTupleVariable (Type.Variable.TypeVarTuple.create name))
| _ -> None
in
parse_single_expression ~preprocess:true annotation
Expand Down
2 changes: 1 addition & 1 deletion source/analysis/test/typeOrderTest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ let ( ! ) concretes = List.map concretes ~f:(fun single -> Type.Parameter.Single

let variable_aliases name =
match name with
| "Ts" -> Some (Type.Variable.Declaration.DTypeVarTuple { name = "Ts" })
| "Ts" -> Some (Type.Variable.TypeVarTupleVariable (Type.Variable.TypeVarTuple.create name))
| _ -> None


Expand Down
11 changes: 10 additions & 1 deletion source/analysis/test/typeTest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,16 @@ let make_callable_from_arguments annotations =

let make_variables ~aliases name =
match aliases name with
| Some (TypeAliasEnvironment.RawAlias.VariableAlias variable) -> Some variable
| Some (TypeAliasEnvironment.RawAlias.VariableAlias variable) ->
let type_variables =
Type.Variable.of_declaration
~create_type:
(Type.create
~aliases:Type.resolved_empty_aliases
~variables:Type.resolved_empty_variables)
variable
in
Some type_variables
| _ -> None


Expand Down
18 changes: 10 additions & 8 deletions source/analysis/type.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4071,6 +4071,8 @@ let create_literal = function

let resolved_empty_aliases ?replace_unbound_parameters_with_any:_ _ = None

let resolved_empty_variables _ = None

let alternate_name_to_canonical_name_map =
[
"typing.ChainMap", "collections.ChainMap";
Expand Down Expand Up @@ -4111,12 +4113,10 @@ let parameters_from_unpacked_annotation annotation ~get_variable =

let rec create_logic ~resolve_aliases ~variable_aliases { Node.value = expression; _ } =
let create_logic = create_logic ~resolve_aliases ~variable_aliases in
let get_variable identifier =
variable_aliases identifier >>| Variable.of_declaration ~create_type:create_logic
in
(* let get_variable identifier = variable_aliases identifier ~create_type:create_logic in *)
let substitute_parameter_variadic = function
| Primitive name -> (
match get_variable name with
match variable_aliases name with
| Some (Record.Variable.ParamSpecVariable variable) ->
Some { Record.Callable.variable; head = [] }
| _ -> None)
Expand Down Expand Up @@ -4181,7 +4181,9 @@ let rec create_logic ~resolve_aliases ~variable_aliases { Node.value = expressio
List.map tail ~f:(fun annotation ->
create_logic (Node.create_with_default_location annotation))
in
OrderedTypes.concatenation_from_annotations ~get_variable elements
OrderedTypes.concatenation_from_annotations
~get_variable:variable_aliases
elements
>>| (fun concatenation -> CallableParamType.Concatenation concatenation)
|> Option.value
~default:
Expand Down Expand Up @@ -4373,7 +4375,7 @@ let rec create_logic ~resolve_aliases ~variable_aliases { Node.value = expressio
]
| element -> (
let parsed = create_logic element in
match parameters_from_unpacked_annotation ~get_variable parsed with
match parameters_from_unpacked_annotation ~get_variable:variable_aliases parsed with
| Some parameters -> parameters
| None -> (
match substitute_parameter_variadic parsed with
Expand Down Expand Up @@ -4434,7 +4436,7 @@ let rec create_logic ~resolve_aliases ~variable_aliases { Node.value = expressio
create_parametric ~base ~subscript_index)
in
let resolve_variables_then_aliases alias_name =
match get_variable alias_name with
match variable_aliases alias_name with
| Some (Record.Variable.TypeVarVariable variable) -> Variable variable
| _ -> Primitive alias_name |> resolve_aliases
in
Expand Down Expand Up @@ -4588,7 +4590,7 @@ let rec create_logic ~resolve_aliases ~variable_aliases { Node.value = expressio
|> Option.all
in
all_positional_only_parameters
>>= OrderedTypes.concatenation_from_annotations ~get_variable
>>= OrderedTypes.concatenation_from_annotations ~get_variable:variable_aliases
>>| (fun concatenation ->
{ overload with parameters = Defined [Variable (Concatenation concatenation)] })
|> Option.value ~default:overload
Expand Down
4 changes: 3 additions & 1 deletion source/analysis/type.mli
Original file line number Diff line number Diff line change
Expand Up @@ -977,13 +977,15 @@ val resolve_aliases
t

val create
: variables:(string -> Variable.Declaration.t option) ->
: variables:(string -> Variable.t option) ->
aliases:(?replace_unbound_parameters_with_any:bool -> Primitive.t -> t option) ->
Expression.t ->
t

val resolved_empty_aliases : ?replace_unbound_parameters_with_any:bool -> Primitive.t -> t option

val resolved_empty_variables : string -> Variable.t option

val infer_transform : t -> t

val contains_prohibited_any : t -> bool
Expand Down
55 changes: 41 additions & 14 deletions source/analysis/typeAliasEnvironment.ml
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,14 @@ module IncomingDataComputation = struct
target: Reference.t;
}

let unchecked_resolve ~unparsed ~target map =
let aliases ?replace_unbound_parameters_with_any:_ name = Map.find map name in
let unchecked_resolve ~unparsed ~target ~variables ~aliases =
let resolved_aliases ?replace_unbound_parameters_with_any:_ name =
match aliases name with
| Some (RawAlias.TypeAlias t) -> Some t
| _ -> None
in
let variable_aliases name =
match aliases ?replace_unbound_parameters_with_any:(Some true) name with
| Some (RawAlias.VariableAlias variable) -> Some variable
| _ -> None
in

match Type.create ~variables:variable_aliases ~aliases:resolved_aliases unparsed with
match Type.create ~variables ~aliases:resolved_aliases unparsed with
| Type.Variable variable -> Type.Variable { variable with name = Reference.show target }
| annotation -> annotation

Expand All @@ -80,7 +74,10 @@ module IncomingDataComputation = struct
Queries.{ class_exists; module_exists; is_from_empty_stub; _ }
{ value; target }
=
let value_annotation = unchecked_resolve ~unparsed:value ~target String.Map.empty in
let aliases ?replace_unbound_parameters_with_any:_ _name = None in
let value_annotation =
unchecked_resolve ~unparsed:value ~target ~variables:Type.resolved_empty_variables ~aliases
in
let dependencies = String.Hash_set.create () in
let module TrackedTransform = Type.VisitWithTransform.Make (struct
type state = unit
Expand Down Expand Up @@ -165,7 +162,6 @@ module IncomingDataComputation = struct
| Some variable -> Some (VariableAlias variable)
| _ ->
let variable_aliases _ = None in

let value = Type.preprocess_alias_value value |> delocalize in
let value_annotation =
Type.create
Expand Down Expand Up @@ -259,8 +255,33 @@ module IncomingDataComputation = struct
|> List.map ~f:solve_pair
|> Option.all
>>| String.Map.of_alist_exn
>>| UnresolvedAlias.unchecked_resolve ~target:current ~unparsed
>>| fun alias -> RawAlias.TypeAlias alias)
>>| fun map ->
let aliases ?replace_unbound_parameters_with_any:_ name = Map.find map name in

let resolved_aliases ?replace_unbound_parameters_with_any name =
match aliases ?replace_unbound_parameters_with_any name with
| Some (RawAlias.TypeAlias t) -> Some t
| _ -> None
in

let rec variable_aliases name =
match aliases name with
| Some (RawAlias.VariableAlias variable) ->
let type_variables =
Type.Variable.of_declaration
~create_type:
(Type.create ~aliases:resolved_aliases ~variables:variable_aliases)
variable
in
Some type_variables
| _ -> None
in
UnresolvedAlias.unchecked_resolve
~target:current
~unparsed
~variables:variable_aliases
~aliases
|> fun alias -> RawAlias.TypeAlias alias)
in
extract_alias queries current
>>= resolve_after_resolving_dependencies
Expand Down Expand Up @@ -304,10 +325,16 @@ module OutgoingDataComputation = struct
in
let variable_aliases name =
match aliases ?replace_unbound_parameters_with_any:(Some true) name with
| Some (RawAlias.VariableAlias variable) -> Some variable
| Some (RawAlias.VariableAlias variable) ->
let type_variables =
Type.Variable.of_declaration
~create_type:
(Type.create ~aliases:resolved_aliases ~variables:Type.resolved_empty_variables)
variable
in
Some type_variables
| _ -> None
in

Type.create ~variables:variable_aliases ~aliases:resolved_aliases expression
in
let annotation =
Expand Down
1 change: 0 additions & 1 deletion source/analysis/typeCheck.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5953,7 +5953,6 @@ module State (Context : Context) = struct
let initial ~resolution =
let global_resolution = Resolution.global_resolution resolution in

(* let aliases = GlobalResolution.get_type_alias global_resolution in *)
let variables = Resolution.variables resolution in

let {
Expand Down

0 comments on commit b90d79c

Please sign in to comment.