Skip to content

Commit

Permalink
Rename top-level preprocessing functions
Browse files Browse the repository at this point in the history
Summary:
There's currently a `Preprocessing.preprocess` function that is
both named and commented as though it does "all of preprocessing", which
is misleading - it does everything except wildcard expansion, which is
effectively part of preprocessing. And the names `preprocess_phase0` and
`preprocess_phase1` aren't really descriptive.

This diff renames them all to make the existing API clearer.

I came across this while considering moving wildcard exports into the
`Preprocessing` module, although I've realized that `DecoratorPreprocessing`
is also involved and complicates things, I'll need to figure out how much
this matters.

Reviewed By: grievejia

Differential Revision: D52521369

fbshipit-source-id: 188d16c7d4bfcc9c577b651c0628f19890eb5636
  • Loading branch information
stroxler authored and facebook-github-bot committed Jan 5, 2024
1 parent 65f0f49 commit 399b22d
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 19 deletions.
6 changes: 3 additions & 3 deletions source/analysis/astEnvironment.ml
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,13 @@ module OutgoingDataComputation = struct


let get_processed_source (Queries.{ get_raw_source; _ } as queries) qualifier =
let preprocessing = Preprocessing.preprocess_phase1 in
(* Preprocessing a module depends on the module itself is implicitly assumed in `update`. No
need to explicitly record the dependency. *)
get_raw_source qualifier
>>| function
| Result.Ok source ->
expand_wildcard_imports queries source
|> preprocessing
|> Preprocessing.preprocess_after_wildcards
|> DecoratorPreprocessing.preprocess_source ~get_source:(fun qualifier ->
get_raw_source qualifier >>= Result.ok)
| Result.Error
Expand All @@ -175,7 +174,8 @@ module OutgoingDataComputation = struct
let fallback_source = ["import typing"; "def __getattr__(name: str) -> typing.Any: ..."] in
let typecheck_flags = Source.TypecheckFlags.parse ~qualifier fallback_source in
let statements = Parser.parse_exn ~relative fallback_source in
Parsing.create_source ~typecheck_flags ~module_path statements |> preprocessing
Parsing.create_source ~typecheck_flags ~module_path statements
|> Preprocessing.preprocess_after_wildcards
end

module ReadOnly = struct
Expand Down
2 changes: 1 addition & 1 deletion source/analysis/parsing.ml
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ let load_and_parse ~controls ~get_raw_code module_path =
EnvironmentControls.python_version_info controls
in
Preprocessing.replace_version_specific_code ~major_version ~minor_version ~micro_version source
|> Preprocessing.preprocess_phase0
|> Preprocessing.preprocess_before_wildcards
in
match get_raw_code module_path with
| Ok raw_code ->
Expand Down
16 changes: 9 additions & 7 deletions source/analysis/preprocessing.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
We break this into two phases:
* phase0: transformations that don't depend on expanding wildcard imports (e.g., selecting
platform-specific code)
* preprocess_before_wildcards: transformations that don't depend on expanding wildcard imports
(e.g., selecting platform-specific code)
* phase1: transformations that depend on expanding wildcard imports (e.g., expanding functional
TypedDict declarations, since the `TypedDict` may be imported via wildcard) *)
* preprocess_after_wildcards: transformations that depend on expanding wildcard imports (e.g.,
expanding functional TypedDict declarations, since the `TypedDict` may be imported via
wildcard) *)

open Core
open Ast
Expand Down Expand Up @@ -4812,7 +4813,7 @@ let expand_enum_functional_syntax
{ source with Source.statements }


let preprocess_phase0 source =
let preprocess_before_wildcards source =
source
|> expand_relative_imports
|> replace_platform_specific_code
Expand All @@ -4821,7 +4822,7 @@ let preprocess_phase0 source =
|> expand_import_python_calls


let preprocess_phase1 source =
let preprocess_after_wildcards source =
source
|> expand_new_types
|> populate_unbound_names
Expand All @@ -4843,4 +4844,5 @@ let preprocess_phase1 source =
|> populate_captures


let preprocess source = preprocess_phase0 source |> preprocess_phase1
let preprocess_no_wildcards source =
preprocess_before_wildcards source |> preprocess_after_wildcards
10 changes: 5 additions & 5 deletions source/analysis/preprocessing.mli
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ val toplevel_expand_tuple_assign : Statement.Assign.t Node.t -> Statement.Assign
val dequalify_map : Source.t -> Reference.t Reference.Map.t

(* Steps that may affect wildcard imports, excluding version-specific code replacement *)
val preprocess_phase0 : Source.t -> Source.t
val preprocess_before_wildcards : Source.t -> Source.t

(* Steps that does not affect wildcard imports *)
val preprocess_phase1 : Source.t -> Source.t
(* Steps that do not affect wildcard imports *)
val preprocess_after_wildcards : Source.t -> Source.t

(* Phase0 followed by Phase1 *)
val preprocess : Source.t -> Source.t
(* All preprocessing steps, skipping wildcard expansion *)
val preprocess_no_wildcards : Source.t -> Source.t

(* Following are exposed for testing only *)

Expand Down
2 changes: 1 addition & 1 deletion source/analysis/test/typeCheckTest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ let test_forward_expression context =
annotation
=
let expression =
let expression = parse expression |> Preprocessing.preprocess in
let expression = parse expression |> Preprocessing.preprocess_no_wildcards in
expression
|> function
| { Source.statements = [{ Node.value = Expression expression; _ }]; _ } -> expression
Expand Down
2 changes: 1 addition & 1 deletion source/analysis/type.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4311,7 +4311,7 @@ let rec create_logic ~resolve_aliases ~variable_aliases { Node.value = expressio
let parsed =
Parser.parse_exn [value]
|> Source.create
|> Preprocessing.preprocess
|> Preprocessing.preprocess_no_wildcards
|> Source.statements
in
match parsed with
Expand Down
2 changes: 1 addition & 1 deletion source/test/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ let parse ?(handle = "") ?(coerce_special_methods = false) source =
let parse_single_statement ?(preprocess = false) ?(coerce_special_methods = false) ?handle source =
let source =
if preprocess then
Preprocessing.preprocess (parse ?handle ~coerce_special_methods source)
Preprocessing.preprocess_no_wildcards (parse ?handle ~coerce_special_methods source)
else
parse ~coerce_special_methods source
in
Expand Down

0 comments on commit 399b22d

Please sign in to comment.