From 3e4a79028c84e51ca5039be15ba2aeed65bdb7e7 Mon Sep 17 00:00:00 2001 From: Shannon Zhu Date: Wed, 16 Sep 2020 11:12:06 -0700 Subject: [PATCH] Fix t.Set undefined type spurious error 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 --- analysis/test/integration/annotationTest.ml | 9 +++++++++ analysis/test/typeTest.ml | 1 + analysis/type.ml | 1 + 3 files changed, 11 insertions(+) diff --git a/analysis/test/integration/annotationTest.ml b/analysis/test/integration/annotationTest.ml index fc606abd943..6f37c283de5 100644 --- a/analysis/test/integration/annotationTest.ml +++ b/analysis/test/integration/annotationTest.ml @@ -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 {| diff --git a/analysis/test/typeTest.ml b/analysis/test/typeTest.ml index f0029931f3b..cc88bd194ae 100644 --- a/analysis/test/typeTest.ml +++ b/analysis/test/typeTest.ml @@ -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]); diff --git a/analysis/type.ml b/analysis/type.ml index eee3744e846..e6bb39f4dbf 100644 --- a/analysis/type.ml +++ b/analysis/type.ml @@ -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";