Fixed bug that leads to a false negative when calling a callback function that returns a callable with a generic parameter type. This addresses #7718. (#7965)

This commit is contained in:
Eric Traut 2024-05-21 01:47:44 -07:00 committed by GitHub
parent 582ead0aa0
commit 1027400ac0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 18 additions and 19 deletions

View File

@ -21958,7 +21958,11 @@ export function createTypeEvaluator(
) {
const specializedReturnType = FunctionType.getSpecializedReturnType(type, /* includeInferred */ false);
if (specializedReturnType && !isUnknown(specializedReturnType)) {
return adjustCallableReturnType(type, specializedReturnType, /* liveTypeVarScopes */ []);
const liveTypeVarScopes = callSiteInfo?.errorNode
? ParseTreeUtils.getTypeVarScopesForNode(callSiteInfo?.errorNode)
: [];
return adjustCallableReturnType(type, specializedReturnType, liveTypeVarScopes);
}
if (inferTypeIfNeeded) {

View File

@ -7,28 +7,23 @@ T = TypeVar("T")
U = TypeVar("U")
def foo(value: T) -> T:
...
def func1(value: T) -> T: ...
def bar(values: Sequence[T]) -> T:
...
def func2(values: Sequence[T]) -> T: ...
def baz(
value: T,
callback: Callable[[T], U],
) -> U:
...
def func3(value: T, callback: Callable[[T], U]) -> U: ...
def qux(
values: Sequence[T],
callback: Callable[[Sequence[T]], U],
) -> U:
...
def func4(values: Sequence[T], callback: Callable[[Sequence[T]], U]) -> U: ...
reveal_type(baz(1.0, foo), expected_text="float")
reveal_type(qux([1.0], foo), expected_text="Sequence[float]")
reveal_type(qux([1.0], bar), expected_text="float")
reveal_type(func3(1.0, func1), expected_text="float")
reveal_type(func4([1.0], func1), expected_text="Sequence[float]")
reveal_type(func4([1.0], func2), expected_text="float")
def func5(obj: object, cb: Callable[[], Callable[[T], object]]) -> None:
# This should generate an error.
cb()(obj)

View File

@ -654,7 +654,7 @@ test('Solver15', () => {
test('Solver16', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['solver16.py']);
TestUtils.validateResults(analysisResults, 0);
TestUtils.validateResults(analysisResults, 1);
});
test('Solver17', () => {