Skip to content

Commit

Permalink
Fix t.Set undefined type spurious error
Browse files Browse the repository at this point in the history
Summary:
Looks like the latest binary release has fixed one part of the original issue - previously, `from typing import Set` would not throw this spurious error while `import typing as t` and referencing `t.Set` would.

Now, it's boiled down to a pretty clear issue where we are looking up the annotation `typing.Set` and cannot find it in the type hierarchy.

Looking a little closer, we `typing.Set` is not in our class hierarchy because `set` is, and we map the equivalence for other types like `typing.List -> list` in Type create. Adding this mapping fixes this issue.

typing.pyi: https://github.com/python/typeshed/blob/master/stdlib/3/typing.pyi
class hierarchy from env with typeshed included: P142551722

Reviewed By: grievejia

Differential Revision: D23726491

fbshipit-source-id: 15db093f5be6222b02ff4c4f3ae7ba221c3d82fc
  • Loading branch information
shannonzhu authored and facebook-github-bot committed Sep 16, 2020
1 parent 6798c63 commit 3e4a790
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
9 changes: 9 additions & 0 deletions analysis/test/integration/annotationTest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,15 @@ let test_check_undefined_type context =
"Unbound name [10]: Name `Herp` is used but not defined in the current scope.";
];

assert_strict_type_errors
{|
import typing as t

def foo() -> t.Set:
return set()
|}
["Invalid type parameters [24]: Generic type `set` expects 1 type parameter."];

(* Attributes *)
assert_type_errors
{|
Expand Down
1 change: 1 addition & 0 deletions analysis/test/typeTest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ let test_create _ =
(* Check renaming. *)
assert_create "typing.List[int]" (Type.list Type.integer);
assert_create "typing.List" (Primitive "list");
assert_create "typing.Set" (Primitive "set");
assert_create
"typing.DefaultDict[int, str]"
(Type.parametric "collections.defaultdict" ![Type.integer; Type.string]);
Expand Down
1 change: 1 addition & 0 deletions analysis/type.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2276,6 +2276,7 @@ let primitive_substitution_map =
"typing.Dict", Primitive "dict";
"typing.List", Primitive "list";
"typing.OrderedDict", Primitive "collections.OrderedDict";
"typing.Set", Primitive "set";
"typing.Tuple", Primitive "tuple";
"typing.Type", Primitive "type";
"typing_extensions.Protocol", Primitive "typing.Protocol";
Expand Down

0 comments on commit 3e4a790

Please sign in to comment.