Skip to content

Commit

Permalink
Strip resolve_literal from all public APIs
Browse files Browse the repository at this point in the history
Summary:
The `resolve_literal` function is a helper in attributeResolution.ml that
is used in two ways:
- to handle some cases of resolving a global (in particular, it handles
  globals with no annotation, where we can *sometimes* infer the type)
- to handle some cases of figuring out an attribute in an attribute
  table; I'm actually not 100% clear when Pyre allows implicit types
  with no annotation (I think we're stricter than with globals) but it does get
  called in some cases.

But it is *never* used directly - it's exposed exclusively through attribute table
and global signature analysis. We should get rid of it from the public API.

This change was motivated partly because it does need a `~variable_map` for scoped
type vars (because in the attribute table case there might be type vars in scope
from containing classes / functions), but in the global case it would never need
scoped tyep vars. I was going to try to massage the API to somehow reflect this,
and then I realized that it's actually unneeded.

Reviewed By: yangdanny97

Differential Revision: D61337081

fbshipit-source-id: af47da848b2faa5bab44014846cbb21d05cf6235
  • Loading branch information
stroxler authored and facebook-github-bot committed Aug 19, 2024
1 parent 51c5b81 commit f61d6ae
Show file tree
Hide file tree
Showing 5 changed files with 0 additions and 104 deletions.
2 changes: 0 additions & 2 deletions source/analysis/attributeResolution.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5293,8 +5293,6 @@ module ReadOnly = struct

let constraints = add_all_caches_and_empty_cycle_detections (fun o -> o#constraints)

let resolve_literal = add_all_caches_and_empty_cycle_detections (fun o -> o#resolve_literal)

let resolve_define = add_all_caches_and_empty_cycle_detections (fun o -> o#resolve_define)

let resolve_mutable_literals =
Expand Down
7 changes: 0 additions & 7 deletions source/analysis/attributeResolution.mli
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,6 @@ module AttributeReadOnly : sig
unit ->
TypeConstraints.Solution.t

val resolve_literal
: t ->
?dependency:DependencyKey.registered ->
variable_map:(?replace_unbound_parameters_with_any:bool -> string -> Type.Variable.t option) ->
Expression.expression Node.t ->
Type.t

val resolve_define
: t ->
?dependency:DependencyKey.registered ->
Expand Down
4 changes: 0 additions & 4 deletions source/analysis/globalResolution.ml
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,6 @@ let check_invalid_type_parameters ({ dependency; _ } as resolution) =
?dependency


let resolve_literal ({ dependency; _ } as resolution) =
AttributeResolution.ReadOnly.resolve_literal ?dependency (attribute_resolution resolution)


let attribute_names
({ dependency; _ } as resolution)
?(transitive = false)
Expand Down
6 changes: 0 additions & 6 deletions source/analysis/globalResolution.mli
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,6 @@ val check_invalid_type_parameters
Type.t ->
AttributeResolution.type_parameters_mismatch list * Type.t

val resolve_literal
: t ->
variable_map:(?replace_unbound_parameters_with_any:bool -> string -> Type.Variable.t option) ->
Expression.t ->
Type.t

val attribute_names
: t ->
?transitive:bool ->
Expand Down
85 changes: 0 additions & 85 deletions source/analysis/test/resolutionTest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -225,90 +225,6 @@ let test_parse_reference context =
assert_parse_reference "typing.List" (Type.Primitive "list")


let test_resolve_literal context =
let resolution =
make_resolution
~context
{|
class C:
def __init__(self) -> None:
pass
T = typing.TypeVar("T")
class G(typing.Generic[T]):
def __init__(self, x: T) -> None:
pass
def foo()->int:
...
i = 1
j = foo()
s = 'asdf'
t = 1, 1.0
none = None
awaitable: typing.Awaitable[int]
|}
|> Resolution.global_resolution
in
let assert_resolve_literal source expected =
let expression =
match parse_single_statement source with
| { Node.value = Statement.Expression expression; _ } -> expression
| _ -> failwith "No Assign to parse"
in
assert_equal
~printer:Type.show
expected
(GlobalResolution.resolve_literal ~variable_map:Type.empty_variable_map resolution expression)
in
assert_resolve_literal "i" Type.Any;
assert_resolve_literal "await i" Type.Any;
assert_resolve_literal "await awaitable" Type.Any;
assert_resolve_literal "\"\"" Type.string;
assert_resolve_literal "1" Type.integer;
assert_resolve_literal "1+1" Type.Any;
assert_resolve_literal "j" Type.Any;
assert_resolve_literal "foo()" Type.Any;
assert_resolve_literal "C()" (Type.Primitive "C");
assert_resolve_literal "G(7)" Type.Any;
assert_resolve_literal "C" (Type.meta (Type.Primitive "C"));
assert_resolve_literal "G" Type.Any;

(* None *)
assert_resolve_literal "None" Type.Any;
assert_resolve_literal "[None]" (Type.list Type.Any);

(* Dictionary *)
assert_resolve_literal "{'a': 1}" (Type.dictionary ~key:Type.string ~value:Type.integer);
assert_resolve_literal "{'a': i}" (Type.dictionary ~key:Type.string ~value:Type.Any);
assert_resolve_literal "{'a': [], 'b': [1]}" (Type.dictionary ~key:Type.string ~value:Type.Any);
assert_resolve_literal "{**foo}" (Type.dictionary ~key:Type.Any ~value:Type.Any);
assert_resolve_literal "{'a': 1, **foo}" (Type.dictionary ~key:Type.Any ~value:Type.Any);

(* Boolean Operator *)
assert_resolve_literal "1 or 2" Type.integer;
assert_resolve_literal "True or 1" Type.integer;
assert_resolve_literal "True or i" Type.Any;

(* List *)
assert_resolve_literal "[1]" (Type.list Type.integer);
assert_resolve_literal "[1, 'string']" (Type.list (Type.Union [Type.integer; Type.string]));
assert_resolve_literal "[1, i]" (Type.list Type.Any);

(* Set *)
assert_resolve_literal "{1}" (Type.set Type.integer);
assert_resolve_literal "{1, 'string'}" (Type.set (Type.Union [Type.integer; Type.string]));
assert_resolve_literal "{1, i}" (Type.set Type.Any);

(* Tuple *)
assert_resolve_literal "(1,)" (Type.Tuple (Concrete [Type.integer]));
assert_resolve_literal "(1, 'string')" (Type.Tuple (Concrete [Type.integer; Type.string]));
assert_resolve_literal "(1, i)" (Type.Tuple (Concrete [Type.integer; Type.Any]));

(* Ternary *)
assert_resolve_literal "1 if x else 2" Type.integer;
assert_resolve_literal "'hi' if x else 1" (Type.union [Type.string; Type.integer]);
assert_resolve_literal "1 if i else i" Type.Any


let test_get_typed_dictionary context =
let resolution =
make_resolution
Expand Down Expand Up @@ -594,7 +510,6 @@ let () =
"parse_annotation_no_validation_on_class_lookup_failure"
>:: test_parse_annotation_for_no_validation_on_class_lookup_failure_environment;
"parse_reference" >:: test_parse_reference;
"resolve_literal" >:: test_resolve_literal;
"get_typed_dictionary " >:: test_get_typed_dictionary;
test_fallback_attribute;
]
Expand Down

0 comments on commit f61d6ae

Please sign in to comment.