Skip to content

Commit

Permalink
Fixed bug that results in false positive error under certain circumst…
Browse files Browse the repository at this point in the history
…ances that involve unions of TypeVars in an invariant context. This addresses #6957. (#7709)
  • Loading branch information
erictraut authored Apr 16, 2024
1 parent 8a7aed5 commit 8912c88
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 22 deletions.
42 changes: 28 additions & 14 deletions packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24187,22 +24187,36 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
} else if (remainingDestSubtypes.length === remainingSrcSubtypes.length) {
// If the number of remaining source subtypes is the same as the number
// of dest TypeVars, try to assign each source subtype to its own dest TypeVar.
remainingDestSubtypes.forEach((destSubtype, index) => {
const srcSubtype = remainingSrcSubtypes[index];
if (
!assignType(
destSubtype,
srcSubtype,
diag?.createAddendum(),
destTypeVarContext,
srcTypeVarContext,
flags,
recursionCount
)
) {
const reorderedDestSubtypes = [...remainingDestSubtypes];

for (let srcIndex = 0; srcIndex < remainingSrcSubtypes.length; srcIndex++) {
let foundMatchForSrc = false;

for (let destIndex = 0; destIndex < reorderedDestSubtypes.length; destIndex++) {
if (
assignType(
reorderedDestSubtypes[destIndex],
remainingSrcSubtypes[srcIndex],
diag?.createAddendum(),
destTypeVarContext,
srcTypeVarContext,
flags,
recursionCount
)
) {
foundMatchForSrc = true;
// Move the matched dest TypeVar to the end of the list so the other
// dest TypeVars have a better chance of being assigned to.
reorderedDestSubtypes.push(...reorderedDestSubtypes.splice(destIndex, 1));
break;
}
}

if (!foundMatchForSrc) {
canUseFastPath = false;
break;
}
});
}

// We can avoid checking the source subtypes that have already been checked.
sortedSrcTypes = remainingSrcSubtypes;
Expand Down
21 changes: 13 additions & 8 deletions packages/pyright-internal/src/tests/samples/solver24.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

V = TypeVar("V")
V_co = TypeVar("V_co", covariant=True)
T = TypeVar("T")
U = TypeVar("U")


Expand All @@ -30,24 +31,28 @@ def func1(a: ClassA[V], b: ClassA[U], c: bool) -> ClassB[V | U]:
return r


class ClassC(Generic[AnyStr]):
...
class ClassC(Generic[AnyStr]): ...


class ClassD(Iterator[ClassC[AnyStr]], Protocol):
...
class ClassD(Iterator[ClassC[AnyStr]], Protocol): ...


GenericPath: TypeAlias = AnyStr | PathLike[AnyStr]


def func2(iter: Iterable[object]) -> bool:
...
def func2(iter: Iterable[object]) -> bool: ...


def func3(path: GenericPath[AnyStr]) -> ClassD[AnyStr]:
...
def func3(path: GenericPath[AnyStr]) -> ClassD[AnyStr]: ...


def func4(val: str):
func2(func3(val))


def func5(a: dict[T, U], b: list[T | U]):
pass


def func6(a: dict[str, int], b: list[str | int]):
func5(a, b)

0 comments on commit 8912c88

Please sign in to comment.