Fixed bug that results in a false positive error when using a callback protocol with a function-scoped type variable. This addresses #8177. (#8207)

This commit is contained in:
Eric Traut 2024-06-23 14:57:32 +02:00 committed by GitHub
parent ee12ebcccf
commit 6ffb60bd53
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 29 additions and 2 deletions

View File

@ -962,7 +962,7 @@ export class Checker extends ParseTreeWalker {
declaredReturnType,
returnType,
diagAddendum,
new TypeVarContext(),
/* destTypeVarContext */ undefined,
/* srcTypeVarContext */ undefined,
AssignTypeFlags.AllowBoolTypeGuard
)

View File

@ -24166,7 +24166,7 @@ export function createTypeEvaluator(
destType,
concreteSrcType,
diag?.createAddendum(),
destTypeVarContext ?? new TypeVarContext(getTypeVarScopeId(destType)),
destTypeVarContext ?? new TypeVarContext(getTypeVarScopeIds(destType)),
srcTypeVarContext ?? new TypeVarContext(getTypeVarScopeIds(concreteSrcType)),
flags,
recursionCount

View File

@ -0,0 +1,21 @@
# This sample tests the case where a callback protocol uses a function-
# scoped type variable.
from typing import Generic, Protocol, TypeVar
T = TypeVar("T")
T_co = TypeVar("T_co", covariant=True)
U_co = TypeVar("U_co", covariant=True)
class A(Generic[T_co, U_co]): ...
class BProto(Protocol):
def __call__(self) -> A[list[T], T]: ...
def func1() -> BProto:
def make_a() -> A[list[T], T]: ...
return make_a

View File

@ -73,6 +73,12 @@ test('CallbackProtocol10', () => {
TestUtils.validateResults(analysisResults, 0);
});
test('CallbackProtocol11', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['callbackProtocol11.py']);
TestUtils.validateResults(analysisResults, 0);
});
test('Assignment1', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['assignment1.py']);