mirror of
https://github.com/microsoft/pyright.git
synced 2024-10-26 10:55:06 +03:00
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:
parent
2b0a1d92dd
commit
0edcd2eecf
@ -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,
|
||||
|
@ -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;
|
||||
|
@ -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) {
|
||||
|
@ -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")
|
||||
|
Loading…
Reference in New Issue
Block a user