Skip to content

Commit

Permalink
Prefer first overload
Browse files Browse the repository at this point in the history
Summary:
This isn't actually specified yet, but it looks like the behavior of picking the first overload that matches is going to be specified soon (python/typing#253).
For some reason we thought we needed to reverse the list to make this work, but evidently it wasn't reversed, so we were reversing it.

Reviewed By: sinancepel

Differential Revision: D14717465

fbshipit-source-id: 646dd01ff50b709a3ed55a9905fe39611d935286
  • Loading branch information
mrkmndz authored and facebook-github-bot committed Apr 3, 2019
1 parent 6d8cec5 commit 6432d6e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 33 deletions.
15 changes: 2 additions & 13 deletions analysis/annotatedClass.ml
Original file line number Diff line number Diff line change
Expand Up @@ -699,20 +699,9 @@ let attributes
attributes
({ Node.value = ({ Class.name = parent_name; _ } as definition); _ } as parent) =
let collect_attributes attributes attribute =
let reverse_define_order
{ Node.value = { Statement.Attribute.defines; _ } as attribute_value; location } =
{
Node.location;
value = {
attribute_value with Statement.Attribute.defines = defines >>| List.rev
};
}
in
let attribute_node =
(* Map fold returns the reverse order; order matters when constructing callables *)
attribute
|> reverse_define_order
|> Attribute.create
Attribute.create
attribute
~resolution
~parent
~instantiated
Expand Down
14 changes: 10 additions & 4 deletions analysis/test/annotatedClassTest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -596,15 +596,19 @@ let test_implements _ =
def len() -> int: pass
|}
DoesNotImplement;
(* Not sure whats meant to happen here... *)
assert_conforms
{|
class List():
def empty() -> bool: pass
@typing.overload
def length(x: int) -> str: pass
def length() -> str: pass
def length(x: int) -> int: pass
@typing.overload
def length() -> int: pass
@typing.overload
def length(x: int) -> str: pass
@typing.overload
def length() -> str: pass
|}
{|
class Sized(typing.Protocol):
Expand All @@ -618,8 +622,10 @@ let test_implements _ =
def empty() -> bool: pass
@typing.overload
def length(x: int) -> str: pass
def length() -> str: pass
@typing.overload
def length(x: int) -> int: pass
@typing.overload
def length() -> str: pass
|}
{|
class Sized(typing.Protocol):
Expand Down Expand Up @@ -1151,7 +1157,7 @@ let test_class_attributes _ =
~expected_attribute:(
create_expected_attribute
"baz"
"typing.Callable('Attributes.baz')[[Named(x, str)], str]");
"typing.Callable('Attributes.baz')[[Named(x, int)], int]");
assert_attribute
~parent
~parent_instantiated_type:(Type.meta (Type.Primitive "Attributes"))
Expand Down
10 changes: 2 additions & 8 deletions analysis/test/integration/signatureSelectionTest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -432,10 +432,7 @@ let test_check_function_parameters _ =
def f(d: typing.Dict[int, int], x) -> None:
d.update({ 1: x })
|}
[
"Incompatible parameter type [6]: Expected `typing.Iterable[typing.Tuple[int, int]]` " ^
"for 1st anonymous parameter to call `dict.update` but got `typing.Dict[int, typing.Any]`."
];
[];

assert_default_type_errors
{|
Expand Down Expand Up @@ -713,10 +710,7 @@ let test_check_function_overloads _ =
def herp(x: Foo) -> int:
return x.derp(True)
|}
[
"Incompatible return type [7]: Expected `int` but got `str`.";
"Missing argument [20]: Call `Foo.derp` expects argument `y`.";
];
["Missing argument [20]: Call `Foo.derp` expects argument `y`."];

assert_type_errors
{|
Expand Down
8 changes: 4 additions & 4 deletions analysis/test/lookupTest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -540,12 +540,12 @@ let test_lookup_unbound _ =
"3:2-3:3/typing.List[]";
"3:6-3:21/typing.List[]";
"4:15-4:16/typing.List[]";
"4:22-4:23/typing.Callable(list.__getitem__)[..., unknown]\
[[[Named(s, slice)], typing.List[Variable[_T]]][[Named(i, int)], Variable[_T]]]";
"4:22-4:23/typing.Callable(list.__getitem__)[..., unknown][[[Named(i, int)], Variable[_T]]" ^
"[[Named(s, slice)], typing.List[Variable[_T]]]]";
"4:22-4:23/typing.List[]";
"4:24-4:25/typing_extensions.Literal[1]";
"4:7-4:8/typing.Callable(list.__getitem__)[..., unknown][[[Named(s, slice)], \
typing.List[Variable[_T]]][[Named(i, int)], Variable[_T]]]";
"4:7-4:8/typing.Callable(list.__getitem__)[..., unknown][[[Named(i, int)], Variable[_T]]" ^
"[[Named(s, slice)], typing.List[Variable[_T]]]]";
"4:7-4:8/typing.List[]";
"4:9-4:10/typing_extensions.Literal[0]";
"5:2-5:3/typing.Callable(identity)[[Named(x, Variable[_T])], Variable[_T]]";
Expand Down
8 changes: 4 additions & 4 deletions analysis/test/typeCheckTest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1808,14 +1808,14 @@ let test_forward_access _ =
overloads = [
overload ~return_annotation:Type.integer ~name:"x" (Type.literal_integer 0);
overload ~return_annotation:Type.string ~name:"x" (Type.literal_integer 1);
overload
~return_annotation:(Type.Tuple (Unbounded (Type.union [Type.integer; Type.string])))
~name:"x"
(Type.Primitive "slice");
overload
~return_annotation:(Type.union [Type.integer; Type.string])
~name:"x"
(Type.integer);
overload
~return_annotation:(Type.Tuple (Unbounded (Type.union [Type.integer; Type.string])))
~name:"x"
(Type.Primitive "slice");
];
implicit = None;
};
Expand Down

0 comments on commit 6432d6e

Please sign in to comment.