Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue6335 #6357

Merged
merged 3 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ import {
isCodeFlowSupportedForReference,
wildcardImportReferenceKey,
} from './codeFlowTypes';
import { assignTypeToTypeVar, populateTypeVarContextBasedOnExpectedType } from './constraintSolver';
import { assignTypeToTypeVar, populateTypeVarContextBasedOnExpectedType, updateTypeVarType } from './constraintSolver';
import { createFunctionFromConstructor, validateConstructorArguments } from './constructors';
import {
applyDataClassClassBehaviorOverrides,
Expand Down Expand Up @@ -22051,11 +22051,16 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
const srcTypeArgs = curSrcType.typeArguments;
for (let i = 0; i < destType.details.typeParameters.length; i++) {
const typeArgType = i < srcTypeArgs.length ? srcTypeArgs[i] : UnknownType.create();
destTypeVarContext.setTypeVarType(
destType.details.typeParameters[i],
/* narrowBound */ undefined,
/* narrowBoundNoLiterals */ undefined,
typeArgType
const typeParam = destType.details.typeParameters[i];
const variance = TypeVarType.getVariance(typeParam);

updateTypeVarType(
evaluatorInterface,
destTypeVarContext,
typeParam,
variance !== Variance.Contravariant ? typeArgType : undefined,
variance !== Variance.Covariant ? typeArgType : undefined,
/* forceRetainLiterals */ true
);
}
}
Expand Down
9 changes: 9 additions & 0 deletions packages/pyright-internal/src/tests/samples/constructor2.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Literal,
Mapping,
Protocol,
Self,
TypeVar,
)

Expand Down Expand Up @@ -174,3 +175,11 @@ def s18():

c2: dict[Literal["A", "B"], Literal[3, 4]] = {"A": 3}
reveal_type(c2, expected_text="dict[Literal['A', 'B'], Literal[3, 4]]")


class Plant(Generic[_T1]):
def __new__(cls, o: _T1) -> Self:
...


plant: Plant[float] = Plant(0)
Loading