Added special-case handling for EnumMeta.

This commit is contained in:
Eric Traut 2019-10-11 20:02:36 -07:00
parent 31cf310f15
commit 6f47d92e18
2 changed files with 21 additions and 0 deletions

View File

@ -449,6 +449,13 @@ export function canAssignType(destType: Type, srcType: Type, diag: DiagnosticAdd
if (isAnyOrUnknown(metaclass)) {
return true;
} else if (metaclass.category === TypeCategory.Class) {
// Handle EnumMeta, which requires special-case handling because
// of the way it's defined in enum.pyi. The type var _T must be
// manually set to the corresponding enum object type.
if (typeVarMap && ClassType.isBuiltIn(metaclass, 'EnumMeta')) {
typeVarMap.set('_T', ObjectType.create(srcType));
}
return _canAssignClass(destClassType, metaclass,
diag, typeVarMap, flags, recursionCount + 1, false);
}

View File

@ -1,6 +1,7 @@
# This sample tests the type checker's handling of Enum.
from enum import Enum, IntEnum
from typing import List
TestEnum1 = Enum("TestEnum1", "A B C D")
@ -32,3 +33,16 @@ b = TestEnum3.B
# This should generate an error because "Z" isn't
# a valid member.
z = TestEnum3.Z
# Test that enum classes are iterable.
def requires_enum3_list(a: List[TestEnum3]):
return
list1 = list(TestEnum3)
requires_enum3_list(list1)
list2 = [i for i in TestEnum3]
requires_enum3_list(list2)
num_items_in_enum3 = len(TestEnum3)