Fixed recent regression that results in a false positive error under certain circumstances when yield expression includes a call to a constructor. This addresses #8552. (#8553)

This commit is contained in:
Eric Traut 2024-07-26 09:49:37 -07:00 committed by GitHub
parent 28b452ac0f
commit a1f24db683
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 3 deletions

View File

@ -14268,13 +14268,15 @@ export function createTypeEvaluator(
if (enclosingFunction) {
const functionTypeInfo = getTypeOfFunction(enclosingFunction);
if (functionTypeInfo) {
const returnType = FunctionType.getEffectiveReturnType(functionTypeInfo.functionType);
let returnType = FunctionType.getEffectiveReturnType(functionTypeInfo.functionType);
if (returnType) {
const liveScopeIds = ParseTreeUtils.getTypeVarScopesForNode(node);
returnType = updateTypeWithInternalTypeVars(returnType, liveScopeIds);
expectedYieldType = getGeneratorYieldType(returnType, !!enclosingFunction.d.isAsync);
const generatorTypeArgs = getGeneratorTypeArgs(returnType);
if (generatorTypeArgs && generatorTypeArgs.length >= 2) {
const liveScopeIds = ParseTreeUtils.getTypeVarScopesForNode(node);
sentType = updateTypeWithInternalTypeVars(generatorTypeArgs[1], liveScopeIds);
}
}

View File

@ -1,7 +1,20 @@
# This sample tests various type checking operations relating to
# generator functions (those with a "yield" method).
from typing import Any, Awaitable, Generator, Iterable, Iterator, Protocol, TypedDict
from typing import (
Any,
Awaitable,
Generator,
Generic,
Iterable,
Iterator,
NamedTuple,
Protocol,
TypedDict,
TypeVar,
)
T = TypeVar("T")
class ClassA:
@ -21,6 +34,18 @@ class ClassC:
pass
class NT1(NamedTuple, Generic[T]):
value: T
class ClassD(Generic[T]):
def __init__(self, obj: T) -> None:
self.obj = obj
def ingest(self) -> Generator[NT1[T], None, None]:
yield NT1(self.obj)
def generator1() -> Generator[ClassA, ClassB, ClassC]:
cont = ClassB()
while cont.shouldContinue():