Skip to content

Commit

Permalink
fix Any not always removing everything else from the union
Browse files Browse the repository at this point in the history
  • Loading branch information
DetachHead committed Jun 30, 2024
1 parent 3968ff0 commit 3a9ab80
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions packages/pyright-internal/src/analyzer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3378,6 +3378,7 @@ export function combineTypes(subtypes: Type[], maxSubtypeCount?: number, evaluat

expandedTypes.forEach((subtype) => {
let shouldAddType = false;
const existingAnyType = newUnionType.subtypes.find(isAnyOrUnknown);
if (
// if an evaluator isn't specified, don't do the redundant type check
!evaluator ||
Expand All @@ -3386,7 +3387,13 @@ export function combineTypes(subtypes: Type[], maxSubtypeCount?: number, evaluat
isTypeVar(subtype) ||
// no types have been added to the union yet which causes its type to be Never, which would break
// the redundant type check
!newUnionType.subtypes.length ||
!newUnionType.subtypes.length
) {
shouldAddType = true;
} else if (existingAnyType || isAnyOrUnknown(subtype)) {
newUnionType = UnionType.create();
UnionType.addType(newUnionType, existingAnyType ?? (subtype as UnionableType));
} else if (
// i cant figure out how to check whether a special form is assignable, for now we just skip the
// redundant check on special forms
subtype.specialForm ||
Expand All @@ -3412,11 +3419,6 @@ export function combineTypes(subtypes: Type[], maxSubtypeCount?: number, evaluat
}
}
}
} else if (isAnyOrUnknown(subtype)) {
// if the new type is Any or Unknown, it infects the whole union, so we remove everything else and turn it
// into Any
newUnionType = UnionType.create();
UnionType.addType(newUnionType, subtype);
}
if (shouldAddType) {
if (!newUnionType.subtypes.length) {
Expand Down

0 comments on commit 3a9ab80

Please sign in to comment.