Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2320,25 +2320,44 @@ function isPrimitiveBackedEnumAndPrimitive(enumType: ClassType, primitiveType: C
return false;
}

function unwrapPrimitiveBackedEnumLiteral(classType: ClassType): ClassType {
function unwrapPrimitiveBackedEnumLiteral(classType: ClassType): ClassType | undefined {
const literalValue = classType.priv.literalValue;
if (!(literalValue instanceof EnumLiteral) || !isPrimitiveBackedEnumClass(classType)) {
return classType;
}

const itemType = literalValue.itemType;
if (isClassInstance(itemType) && itemType.priv.literalValue !== undefined) {
// A nested enum literal can still have an unknown underlying value.
let nestedType = itemType;
let recursionCount = 0;
while (nestedType.priv.literalValue instanceof EnumLiteral) {
if (recursionCount++ >= maxTypeRecursionCount) {
return undefined;
}

const nestedValueType = nestedType.priv.literalValue.itemType;
if (!isClassInstance(nestedValueType) || nestedValueType.priv.literalValue === undefined) {
return undefined;
}
nestedType = nestedValueType;
}

return itemType;
}

return classType;
return undefined;
}

function isLiteralValueEqualAtRuntime(type1: ClassType, type2: ClassType): boolean {
return ClassType.isLiteralValueSame(
unwrapPrimitiveBackedEnumLiteral(type1),
unwrapPrimitiveBackedEnumLiteral(type2)
);
function isLiteralValueEqualAtRuntime(type1: ClassType, type2: ClassType): boolean | undefined {
const unwrappedType1 = unwrapPrimitiveBackedEnumLiteral(type1);
const unwrappedType2 = unwrapPrimitiveBackedEnumLiteral(type2);

if (!unwrappedType1 || !unwrappedType2) {
return undefined;
}

return ClassType.isLiteralValueSame(unwrappedType1, unwrappedType2);
}

function getInnermostNewTypeBaseInstance(subtype: Type): Type | undefined {
Expand Down Expand Up @@ -2399,6 +2418,12 @@ export function narrowTypeForLiteralComparison(
const literalValueMatches = isPrimitiveBackedEnumComparison
? isLiteralValueEqualAtRuntime(subtype, literalType)
: ClassType.isLiteralValueSame(subtype, literalType);

if (literalValueMatches === undefined) {
// The outcome is unknown, so retain the subtype in both branches.
return subtype;
}

if (isPositiveTest) {
return literalValueMatches ? subtype : undefined;
}
Expand All @@ -2422,8 +2447,139 @@ export function narrowTypeForLiteralComparison(
// to the primitive literal type.
if (ClassType.isEnumClass(subtype)) {
const allLiteralTypes = enumerateLiteralsForType(evaluator, subtype);
const match = allLiteralTypes?.find((type) => isLiteralValueEqualAtRuntime(type, literalType));
return match ?? subtype;

if (allLiteralTypes) {
let match: ClassType | undefined;
let isIndeterminate = false;
const possibleMatches: ClassType[] = [];
const primitiveClass = ClassType.isBuiltIn(literalType, 'int') ? 'int' : 'str';

for (const type of allLiteralTypes) {
const isEqual = isLiteralValueEqualAtRuntime(type, literalType);
const valueType =
type.priv.literalValue instanceof EnumLiteral
? type.priv.literalValue.itemType
: undefined;
const value =
valueType && isClassInstance(valueType) ? valueType.priv.literalValue : undefined;
// Nested or converted values cannot prove a nonmatch.
const isDefiniteNonmatch =
isEqual === false &&
valueType !== undefined &&
isClassInstance(valueType) &&
ClassType.isBuiltIn(valueType, primitiveClass) &&
(primitiveClass === 'int'
? typeof value === 'number' || typeof value === 'bigint'
: typeof value === 'string');
if (!isDefiniteNonmatch) {
possibleMatches.push(type);
}
if (isEqual === undefined) {
isIndeterminate = true;
} else if (isEqual && !match) {
match = type;
}
}

if (isIndeterminate && possibleMatches.length < allLiteralTypes.length) {
if (ClassType.isEnumMemberSetMayBeIncomplete(subtype)) {
// Older typeshed versions mark IntEnum with @disjoint_base,
// which cannot add members to a direct subclass.
const baseClass =
subtype.shared.baseClasses.length === 1
? subtype.shared.baseClasses[0]
: undefined;
const declaration = subtype.shared.declaration;
const metaclass = subtype.shared.effectiveMetaclass;
if (
!baseClass ||
!isClass(baseClass) ||
!ClassType.isBuiltIn(baseClass, 'enum.IntEnum') ||
!ClassType.isEnumMemberSetMayBeIncomplete(baseClass) ||
declaration?.node.nodeType !== ParseNodeType.Class ||
declaration.node.d.decorators.length > 0 ||
!metaclass ||
!isInstantiableClass(metaclass) ||
!ClassType.isBuiltIn(metaclass, ['EnumMeta', 'EnumType'])
) {
return subtype;
}
}

if (!hasStandardEnumConstruction(subtype)) {
return subtype;
}

// Class creation hooks can add members after static enumeration.
const subclassHook = lookUpClassMember(
subtype,
'__init_subclass__',
MemberAccessFlags.SkipObjectBaseClass
);
if (
subclassHook &&
(!isClass(subclassHook.classType) || !ClassType.isBuiltIn(subclassHook.classType))
) {
return subtype;
}

for (const methodName of ['__eq__', '__ne__']) {
const member = lookUpObjectMember(
subtype,
methodName,
MemberAccessFlags.SkipObjectBaseClass | MemberAccessFlags.SkipInstanceMembers
);
if (
!member ||
!isClass(member.classType) ||
!ClassType.isBuiltIn(member.classType, primitiveClass)
) {
return subtype;
}
}

for (const symbol of ClassType.getSymbolTable(subtype).values()) {
const memberType = evaluator.getEffectiveTypeOfSymbol(symbol);
if (
someSubtypes(memberType, (type) => {
if (isAnyOrUnknown(type)) {
return true;
}
if (isClass(type)) {
if (TypeBase.isInstance(type)) {
if (
ClassType.isBuiltIn(type, primitiveClass) ||
ClassType.isBuiltIn(type, 'bool') ||
(type.priv.literalValue instanceof EnumLiteral &&
ClassType.isSameGenericClass(type, subtype))
) {
return false;
}
return !!lookUpObjectMember(type, '__set_name__');
}
const metaclass = type.shared.effectiveMetaclass;
return (
!metaclass ||
!isClass(metaclass) ||
!!lookUpClassMember(metaclass, '__set_name__')
);
}
return false;
})
) {
return subtype;
}
}

return addConditionToType(combineTypes(possibleMatches), getTypeCondition(subtype));
}

if (!isIndeterminate && match) {
return match;
}
}

return subtype;
}

// The reference type is the primitive type, so narrowing it to the
Expand All @@ -2442,7 +2598,7 @@ export function narrowTypeForLiteralComparison(
return combineTypes(
allLiteralTypes.filter((type) =>
isPrimitiveBackedEnumComparison
? !isLiteralValueEqualAtRuntime(type, literalType)
? isLiteralValueEqualAtRuntime(type, literalType) !== true
: !ClassType.isLiteralValueSame(type, literalType)
)
);
Expand Down
82 changes: 81 additions & 1 deletion packages/pyright-internal/src/tests/samples/enumNarrowing1.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This sample tests type narrowing for equality (== and !=) comparisons
# between IntEnum/StrEnum members and primitive literals or literal unions.

from enum import IntEnum, IntFlag, StrEnum
from enum import IntEnum, IntFlag, StrEnum, auto
from typing import Literal, Union, assert_type

class Priority(IntEnum):
Expand Down Expand Up @@ -77,3 +77,83 @@ def test_plain_enum(e: Priority, y: int):
# declared type rather than narrowing to the enum literal.
if y == Priority.HIGH:
assert_type(y, int)


def get_int() -> int:
return 30


class AutoValue(IntEnum):
# The underlying primitive values of these members are not known
# statically, so equality comparisons with int literals cannot be
# used for narrowing in either direction.
RED = auto()
GREEN = auto()


class ComputedValue(IntEnum):
FIRST = get_int()
SECOND = 40


def test_auto_member_equals_int_literal():
v = AutoValue.RED
if v == 1:
# At runtime AutoValue.RED == 1 is True, so this branch is reachable.
assert_type(v, Literal[AutoValue.RED])
else:
assert_type(v, Literal[AutoValue.RED])


def test_int_var_equals_auto_member(x: Literal[1, 3]):
if x == AutoValue.RED:
# At runtime x == AutoValue.RED is True when x is 1, so this branch
# is reachable and x cannot be narrowed.
assert_type(x, Literal[1, 3])
else:
assert_type(x, Literal[1, 3])


def test_auto_enum_var_equals_int_literal(v: AutoValue):
if v == 1:
assert_type(v, AutoValue)
else:
assert_type(v, Literal[AutoValue.RED, AutoValue.GREEN])


def test_computed_member_equals_int_literal():
v = ComputedValue.FIRST
if v == 30:
# At runtime this branch is reachable when get_int() returns 30.
assert_type(v, Literal[ComputedValue.FIRST])
else:
assert_type(v, Literal[ComputedValue.FIRST])


def test_computed_enum_var_equals_int_literal(v: ComputedValue):
if v == 40:
# SECOND has a known value that matches, but FIRST's value is
# unknown and could also be 40, so no narrowing can occur.
assert_type(v, ComputedValue)
else:
assert_type(v, Literal[ComputedValue.FIRST])


def test_match_int_literal_pattern(v: ComputedValue):
# A literal pattern goes through the same comparison. SECOND has a known
# value that matches, but FIRST's value is unknown and could be 40 too,
# so FIRST must survive into the matching case and the fallback.
match v:
case 40:
assert_type(v, ComputedValue)
case _:
assert_type(v, ComputedValue)


def test_match_auto_int_literal_pattern(v: AutoValue):
# Neither member's value is known, so a literal pattern narrows nothing.
match v:
case 1:
assert_type(v, AutoValue)
case _:
assert_type(v, AutoValue)
Loading
Loading