Skip to content

Commit

Permalink
Fixed a false positive error when solving type variables in a call th…
Browse files Browse the repository at this point in the history
…at involves a lambda. This addresses #8714. (#8730)
  • Loading branch information
erictraut authored Aug 10, 2024
1 parent f85e257 commit 4fc5de5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
6 changes: 1 addition & 5 deletions packages/pyright-internal/src/analyzer/constraintSolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,10 +530,6 @@ function getTypeVarType(
}
}

if (useLowerBoundOnly) {
return entry.lowerBound;
}

let result: Type | undefined;

let lowerBound = entry.lowerBound;
Expand All @@ -558,7 +554,7 @@ function getTypeVarType(
}

result = lowerBound;
} else {
} else if (!useLowerBoundOnly) {
result = entry.upperBound;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11500,7 +11500,7 @@ export function createTypeEvaluator(

// If we skipped a bare type var during the first pass, add
// another pass to ensure that we handle all of the type variables.
if (i === 0 && argResult.skippedBareTypeVarExpectedType) {
if (i === 0 && passCount < 2 && argResult.skippedBareTypeVarExpectedType) {
passCount++;
}
});
Expand Down
16 changes: 16 additions & 0 deletions packages/pyright-internal/src/tests/samples/solver40.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# This sample involves solving type variables that provide a lambda
# its expected type.

# pyright: strict

from typing import Callable, TypeVar

T = TypeVar("T")
U = TypeVar("U")


def func1(lst: list[T], init: U, f: Callable[[U, T], U]) -> U:
...


y = func1([1], 1, lambda x, y: x * y)
6 changes: 6 additions & 0 deletions packages/pyright-internal/src/tests/typeEvaluator2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,12 @@ test('Solver39', () => {
TestUtils.validateResults(analysisResults, 0);
});

test('Solver40', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['solver40.py']);

TestUtils.validateResults(analysisResults, 0);
});

test('SolverScoring1', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['solverScoring1.py']);

Expand Down

0 comments on commit 4fc5de5

Please sign in to comment.