Fixed a bug that resulted in a missing type error when a generic function returned a Callable type that used a TypeVar in its parameter types.

This commit is contained in:
Eric Traut 2021-12-29 17:15:54 -07:00
parent 2377d1b86f
commit 9466ee48d3
4 changed files with 30 additions and 8 deletions

View File

@ -792,7 +792,7 @@ export class Checker extends ParseTreeWalker {
declaredReturnType,
returnType,
diagAddendum,
/* typeVarMap */ undefined,
new TypeVarMap(),
CanAssignFlags.AllowBoolTypeGuard
)
) {

View File

@ -20394,13 +20394,6 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
}
if (srcFunction) {
if (typeVarMap) {
const scopeId = getTypeVarScopeId(destType);
if (scopeId !== WildcardTypeVarScopeId) {
typeVarMap.addSolveForScope(scopeId);
}
}
if (
canAssignFunction(
destType,

View File

@ -0,0 +1,23 @@
# This sample tests the case where a generic function
# returns a generic Callable.
from typing import Callable, TypeVar
_T = TypeVar("_T")
def func1(val1: _T) -> Callable[[_T], None]:
def f(a: str):
...
# This should generate an error because str isn't
# compatible with _T.
return f
def func2(val1: _T) -> Callable[[_T], None]:
def f(a: _T):
...
return f

View File

@ -799,6 +799,12 @@ test('GenericTypes77', () => {
TestUtils.validateResults(analysisResults, 1);
});
test('GenericTypes78', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['genericTypes78.py']);
TestUtils.validateResults(analysisResults, 1);
});
test('Protocol1', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['protocol1.py']);