Skip to content

Commit

Permalink
Fixed a bug that masked an error (false negative) under certain circu…
Browse files Browse the repository at this point in the history
…mstances when evaluating a lambda. This addresses microsoft#7012. (microsoft#7059)
  • Loading branch information
erictraut committed Jan 20, 2024
1 parent 50f677c commit af44054
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14059,6 +14059,8 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
},
{
dependentType: expectedType,
allowDiagnostics:
!forceSpeculative && !isDiagnosticSuppressedForNode(node) && !inferenceContext?.isTypeIncomplete,
}
);

Expand Down
14 changes: 13 additions & 1 deletion packages/pyright-internal/src/tests/samples/lambda4.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This sample tests the case where a lambda is assigned to
# a union type that contains multiple callables.

from typing import Callable, Protocol
from typing import Callable, Protocol, TypeVar


U1 = Callable[[int, str], bool] | Callable[[str], bool]
Expand Down Expand Up @@ -68,3 +68,15 @@ def accepts_u2(cb: U2) -> U2:
accepts_u2(callback_1)
accepts_u2(callback_2)
accepts_u2(callback_3)


T = TypeVar("T")

Takes = Callable[[T], object]

U3 = Takes[Takes[int]] | Takes[Takes[str]]


def accepts_u3(u: U3):
# This should generate an error.
u(lambda v: v.lower())
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/tests/typeEvaluator1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ test('Lambda3', () => {
test('Lambda4', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['lambda4.py']);

TestUtils.validateResults(analysisResults, 1);
TestUtils.validateResults(analysisResults, 2);
});

test('Lambda5', () => {
Expand Down

0 comments on commit af44054

Please sign in to comment.