Fixed bug that results in parameter types being converted to Any when converting a NewType or dataclass constructor to a callable. This addresses #8116. (#8117)

This commit is contained in:
Eric Traut 2024-06-11 09:08:46 -07:00 committed by GitHub
parent 2b0a1d92dd
commit 0edcd2eecf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 24 additions and 7 deletions

View File

@ -326,10 +326,7 @@ export function assignTypeToTypeVar(
)
) {
// The srcType is narrower than the current wideTypeBound, so replace it.
// If it's Any, don't replace it because Any is the narrowest type already.
if (!isAnyOrUnknown(curWideTypeBound)) {
newWideTypeBound = adjSrcType;
}
newWideTypeBound = adjSrcType;
} else if (
!evaluator.assignType(
adjSrcType,

View File

@ -1087,8 +1087,6 @@ function createFunctionFromInitMethod(
const convertedInit = FunctionType.clone(boundInit);
convertedInit.details.declaredReturnType = boundInit.strippedFirstParamType ?? selfType ?? objectType;
convertedInit.details.name = '';
convertedInit.details.fullName = '';
if (convertedInit.specializedTypes) {
convertedInit.specializedTypes.returnType = selfType ?? objectType;

View File

@ -32,6 +32,7 @@ import {
TypeBase,
TypeVarType,
UnknownType,
Variance,
} from './types';
import {
applySolvedTypeVars,
@ -778,7 +779,7 @@ function createProtocolTypeVarContext(
);
} else if (destType.typeArguments && index < destType.typeArguments.length) {
let typeArg = destType.typeArguments[index];
let flags = AssignTypeFlags.PopulatingExpectedType;
let flags: AssignTypeFlags;
let hasUnsolvedTypeVars = requiresSpecialization(typeArg);
// If the type argument has unsolved TypeVars, see if they have
@ -787,6 +788,15 @@ function createProtocolTypeVarContext(
typeArg = applySolvedTypeVars(typeArg, destTypeVarContext, { useNarrowBoundOnly: true });
flags = AssignTypeFlags.Default;
hasUnsolvedTypeVars = requiresSpecialization(typeArg);
} else {
flags = AssignTypeFlags.PopulatingExpectedType;
const variance = TypeVarType.getVariance(typeParam);
if (variance === Variance.Invariant) {
flags |= AssignTypeFlags.EnforceInvariance;
} else if (variance === Variance.Contravariant) {
flags |= AssignTypeFlags.ReverseTypeVarMatching;
}
}
if (!hasUnsolvedTypeVars) {

View File

@ -135,3 +135,15 @@ def func3(t: type[object]):
reveal_type(
cast_to_callable(t), expected_text="(*args: Any, **kwargs: Any) -> object"
)
@dataclass
class G:
value: int
def func4(c: Callable[[T1], T2]) -> Callable[[T1], T2]:
return c
reveal_type(func4(G), expected_text="(int) -> G")