@zzzen/pyright-internal 1.2.0-dev.20240721 → 1.2.0-dev.20240728
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/analyzer/analyzerNodeInfo.js +2 -2
- package/dist/analyzer/analyzerNodeInfo.js.map +1 -1
- package/dist/analyzer/binder.js +8 -8
- package/dist/analyzer/binder.js.map +1 -1
- package/dist/analyzer/cacheManager.js +3 -0
- package/dist/analyzer/cacheManager.js.map +1 -1
- package/dist/analyzer/checker.d.ts +3 -2
- package/dist/analyzer/checker.js +115 -122
- package/dist/analyzer/checker.js.map +1 -1
- package/dist/analyzer/codeFlowEngine.d.ts +2 -2
- package/dist/analyzer/codeFlowEngine.js +49 -42
- package/dist/analyzer/codeFlowEngine.js.map +1 -1
- package/dist/analyzer/codeFlowTypes.js +1 -1
- package/dist/analyzer/codeFlowTypes.js.map +1 -1
- package/dist/analyzer/constraintSolver.d.ts +2 -2
- package/dist/analyzer/constraintSolver.js +164 -204
- package/dist/analyzer/constraintSolver.js.map +1 -1
- package/dist/analyzer/constructorTransform.d.ts +2 -2
- package/dist/analyzer/constructorTransform.js +15 -15
- package/dist/analyzer/constructorTransform.js.map +1 -1
- package/dist/analyzer/constructors.d.ts +2 -2
- package/dist/analyzer/constructors.js +28 -33
- package/dist/analyzer/constructors.js.map +1 -1
- package/dist/analyzer/dataClasses.d.ts +2 -2
- package/dist/analyzer/dataClasses.js +59 -36
- package/dist/analyzer/dataClasses.js.map +1 -1
- package/dist/analyzer/declaration.d.ts +9 -9
- package/dist/analyzer/declaration.js +9 -9
- package/dist/analyzer/declaration.js.map +1 -1
- package/dist/analyzer/declarationUtils.d.ts +1 -1
- package/dist/analyzer/declarationUtils.js +6 -6
- package/dist/analyzer/declarationUtils.js.map +1 -1
- package/dist/analyzer/decorators.js +4 -4
- package/dist/analyzer/decorators.js.map +1 -1
- package/dist/analyzer/docStringConversion.js +2 -2
- package/dist/analyzer/docStringConversion.js.map +1 -1
- package/dist/analyzer/enums.d.ts +2 -2
- package/dist/analyzer/enums.js +6 -6
- package/dist/analyzer/enums.js.map +1 -1
- package/dist/analyzer/functionTransform.d.ts +2 -2
- package/dist/analyzer/functionTransform.js +4 -4
- package/dist/analyzer/functionTransform.js.map +1 -1
- package/dist/analyzer/importResolver.js +5 -3
- package/dist/analyzer/importResolver.js.map +1 -1
- package/dist/analyzer/namedTuples.d.ts +3 -3
- package/dist/analyzer/namedTuples.js +21 -22
- package/dist/analyzer/namedTuples.js.map +1 -1
- package/dist/analyzer/operations.js +10 -10
- package/dist/analyzer/operations.js.map +1 -1
- package/dist/analyzer/packageTypeVerifier.js +8 -8
- package/dist/analyzer/packageTypeVerifier.js.map +1 -1
- package/dist/analyzer/parameterUtils.d.ts +10 -10
- package/dist/analyzer/parameterUtils.js +58 -58
- package/dist/analyzer/parameterUtils.js.map +1 -1
- package/dist/analyzer/parseTreeUtils.d.ts +6 -6
- package/dist/analyzer/parseTreeUtils.js +22 -22
- package/dist/analyzer/parseTreeUtils.js.map +1 -1
- package/dist/analyzer/patternMatching.js +108 -89
- package/dist/analyzer/patternMatching.js.map +1 -1
- package/dist/analyzer/program.js +11 -4
- package/dist/analyzer/program.js.map +1 -1
- package/dist/analyzer/properties.js +19 -19
- package/dist/analyzer/properties.js.map +1 -1
- package/dist/analyzer/protocols.js +14 -15
- package/dist/analyzer/protocols.js.map +1 -1
- package/dist/analyzer/pythonPathUtils.js +1 -1
- package/dist/analyzer/pythonPathUtils.js.map +1 -1
- package/dist/analyzer/service.d.ts +3 -7
- package/dist/analyzer/service.js +28 -30
- package/dist/analyzer/service.js.map +1 -1
- package/dist/analyzer/serviceUtils.d.ts +8 -0
- package/dist/analyzer/serviceUtils.js +31 -0
- package/dist/analyzer/serviceUtils.js.map +1 -0
- package/dist/analyzer/sourceFile.js +5 -19
- package/dist/analyzer/sourceFile.js.map +1 -1
- package/dist/analyzer/sourceMapper.d.ts +1 -1
- package/dist/analyzer/sourceMapper.js +4 -4
- package/dist/analyzer/sourceMapper.js.map +1 -1
- package/dist/analyzer/staticExpressions.js +1 -1
- package/dist/analyzer/staticExpressions.js.map +1 -1
- package/dist/analyzer/tracePrinter.js +4 -4
- package/dist/analyzer/tracePrinter.js.map +1 -1
- package/dist/analyzer/tuples.d.ts +9 -0
- package/dist/analyzer/tuples.js +252 -0
- package/dist/analyzer/tuples.js.map +1 -0
- package/dist/analyzer/typeComplexity.d.ts +2 -0
- package/dist/analyzer/typeComplexity.js +86 -0
- package/dist/analyzer/typeComplexity.js.map +1 -0
- package/dist/analyzer/typeEvaluator.js +1062 -1311
- package/dist/analyzer/typeEvaluator.js.map +1 -1
- package/dist/analyzer/typeEvaluatorTypes.d.ts +44 -40
- package/dist/analyzer/typeEvaluatorTypes.js +7 -1
- package/dist/analyzer/typeEvaluatorTypes.js.map +1 -1
- package/dist/analyzer/typeGuards.js +114 -85
- package/dist/analyzer/typeGuards.js.map +1 -1
- package/dist/analyzer/typePrinter.d.ts +1 -1
- package/dist/analyzer/typePrinter.js +54 -53
- package/dist/analyzer/typePrinter.js.map +1 -1
- package/dist/analyzer/typeStubWriter.d.ts +3 -3
- package/dist/analyzer/typeStubWriter.js +15 -15
- package/dist/analyzer/typeStubWriter.js.map +1 -1
- package/dist/analyzer/typeUtils.d.ts +20 -18
- package/dist/analyzer/typeUtils.js +316 -321
- package/dist/analyzer/typeUtils.js.map +1 -1
- package/dist/analyzer/typeVarContext.d.ts +24 -36
- package/dist/analyzer/typeVarContext.js +89 -193
- package/dist/analyzer/typeVarContext.js.map +1 -1
- package/dist/analyzer/typeWalker.js +4 -4
- package/dist/analyzer/typeWalker.js.map +1 -1
- package/dist/analyzer/typedDicts.d.ts +2 -2
- package/dist/analyzer/typedDicts.js +48 -52
- package/dist/analyzer/typedDicts.js.map +1 -1
- package/dist/analyzer/types.d.ts +79 -57
- package/dist/analyzer/types.js +125 -113
- package/dist/analyzer/types.js.map +1 -1
- package/dist/commands/dumpFileDebugInfoCommand.js +11 -11
- package/dist/commands/dumpFileDebugInfoCommand.js.map +1 -1
- package/dist/common/configOptions.d.ts +1 -0
- package/dist/common/configOptions.js +5 -0
- package/dist/common/configOptions.js.map +1 -1
- package/dist/common/diagnosticRules.d.ts +1 -0
- package/dist/common/diagnosticRules.js +1 -0
- package/dist/common/diagnosticRules.js.map +1 -1
- package/dist/common/fileSystem.d.ts +0 -1
- package/dist/common/fileSystem.js.map +1 -1
- package/dist/common/languageServerInterface.d.ts +13 -0
- package/dist/common/languageServerInterface.js +15 -1
- package/dist/common/languageServerInterface.js.map +1 -1
- package/dist/common/lspUtils.js +2 -2
- package/dist/common/memUtils.d.ts +4 -0
- package/dist/common/memUtils.js +33 -2
- package/dist/common/memUtils.js.map +1 -1
- package/dist/common/serviceKeys.d.ts +3 -0
- package/dist/common/serviceKeys.js +2 -0
- package/dist/common/serviceKeys.js.map +1 -1
- package/dist/common/serviceProviderExtensions.js +7 -0
- package/dist/common/serviceProviderExtensions.js.map +1 -1
- package/dist/languageServerBase.d.ts +4 -3
- package/dist/languageServerBase.js +14 -10
- package/dist/languageServerBase.js.map +1 -1
- package/dist/languageService/autoImporter.d.ts +3 -2
- package/dist/languageService/autoImporter.js +5 -1
- package/dist/languageService/autoImporter.js.map +1 -1
- package/dist/languageService/completionProvider.d.ts +5 -3
- package/dist/languageService/completionProvider.js +38 -37
- package/dist/languageService/completionProvider.js.map +1 -1
- package/dist/languageService/completionProviderUtils.d.ts +1 -1
- package/dist/languageService/completionProviderUtils.js +3 -3
- package/dist/languageService/completionProviderUtils.js.map +1 -1
- package/dist/languageService/fileWatcherDynamicFeature.js +2 -2
- package/dist/languageService/fileWatcherDynamicFeature.js.map +1 -1
- package/dist/languageService/hoverProvider.js +3 -3
- package/dist/languageService/hoverProvider.js.map +1 -1
- package/dist/languageService/referencesProvider.js +2 -2
- package/dist/languageService/signatureHelpProvider.js +1 -1
- package/dist/languageService/signatureHelpProvider.js.map +1 -1
- package/dist/languageService/tooltipUtils.js +3 -3
- package/dist/languageService/tooltipUtils.js.map +1 -1
- package/dist/localization/localize.d.ts +5 -0
- package/dist/localization/localize.js +6 -0
- package/dist/localization/localize.js.map +1 -1
- package/dist/localization/package.nls.cs.json +5 -1
- package/dist/localization/package.nls.de.json +5 -1
- package/dist/localization/package.nls.en-us.json +5 -0
- package/dist/localization/package.nls.es.json +5 -1
- package/dist/localization/package.nls.fr.json +5 -1
- package/dist/localization/package.nls.it.json +5 -1
- package/dist/localization/package.nls.ja.json +5 -1
- package/dist/localization/package.nls.ko.json +5 -1
- package/dist/localization/package.nls.pl.json +5 -1
- package/dist/localization/package.nls.pt-br.json +5 -1
- package/dist/localization/package.nls.qps-ploc.json +5 -1
- package/dist/localization/package.nls.ru.json +5 -1
- package/dist/localization/package.nls.tr.json +5 -1
- package/dist/localization/package.nls.zh-cn.json +5 -1
- package/dist/localization/package.nls.zh-tw.json +5 -1
- package/dist/parser/parseNodes.d.ts +9 -9
- package/dist/parser/parseNodes.js +20 -20
- package/dist/parser/parseNodes.js.map +1 -1
- package/dist/parser/parser.js +30 -30
- package/dist/parser/parser.js.map +1 -1
- package/dist/tests/checker.test.js +2 -2
- package/dist/tests/fourslash/signature.builtinDocstrings.fourslash.js +4 -21
- package/dist/tests/fourslash/signature.builtinDocstrings.fourslash.js.map +1 -1
- package/dist/tests/fourslash/signature.complicated.fourslash.js +1 -1
- package/dist/tests/fourslash/signature.complicated.fourslash.js.map +1 -1
- package/dist/tests/harness/fourslash/fourSlashParser.js +1 -1
- package/dist/tests/harness/fourslash/fourSlashParser.js.map +1 -1
- package/dist/tests/harness/fourslash/testStateUtils.js +2 -2
- package/dist/tests/harness/fourslash/testStateUtils.js.map +1 -1
- package/dist/tests/harness/vfs/filesystem.d.ts +0 -1
- package/dist/tests/harness/vfs/filesystem.js +0 -4
- package/dist/tests/harness/vfs/filesystem.js.map +1 -1
- package/dist/tests/importResolver.test.js +62 -15
- package/dist/tests/importResolver.test.js.map +1 -1
- package/dist/tests/lsp/languageServerTestUtils.js +1 -1
- package/dist/tests/lsp/languageServerTestUtils.js.map +1 -1
- package/dist/tests/typeEvaluator3.test.js +1 -1
- package/dist/tests/typeEvaluator4.test.js +9 -1
- package/dist/tests/typeEvaluator4.test.js.map +1 -1
- package/dist/tests/typeEvaluator5.test.js +8 -2
- package/dist/tests/typeEvaluator5.test.js.map +1 -1
- package/dist/tests/typeEvaluator6.test.js +72 -60
- package/dist/tests/typeEvaluator6.test.js.map +1 -1
- package/dist/tests/typeEvaluator7.test.js +2 -2
- package/dist/tests/typePrinter.test.js +13 -18
- package/dist/tests/typePrinter.test.js.map +1 -1
- package/dist/workspaceFactory.d.ts +3 -2
- package/dist/workspaceFactory.js +15 -2
- package/dist/workspaceFactory.js.map +1 -1
- package/package.json +1 -1
@@ -90,7 +90,7 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
90
90
|
ParseTreeUtils.isMatchingExpression(reference, leftExpression.d.leftExpr) &&
|
91
91
|
leftExpression.d.items.length === 1 &&
|
92
92
|
!leftExpression.d.trailingComma &&
|
93
|
-
leftExpression.d.items[0].d.argCategory === 0 /*
|
93
|
+
leftExpression.d.items[0].d.argCategory === 0 /* ArgCategory.Simple */ &&
|
94
94
|
!leftExpression.d.items[0].d.name &&
|
95
95
|
leftExpression.d.items[0].d.valueExpr.nodeType === 40 /* ParseNodeType.Number */ &&
|
96
96
|
leftExpression.d.items[0].d.valueExpr.d.isInteger &&
|
@@ -126,7 +126,7 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
126
126
|
// Look for "type(X) is Y", "type(X) is not Y", "type(X) == Y" or "type(X) != Y".
|
127
127
|
if (testExpression.d.leftExpr.nodeType === 9 /* ParseNodeType.Call */) {
|
128
128
|
if (testExpression.d.leftExpr.d.args.length === 1 &&
|
129
|
-
testExpression.d.leftExpr.d.args[0].d.argCategory === 0 /*
|
129
|
+
testExpression.d.leftExpr.d.args[0].d.argCategory === 0 /* ArgCategory.Simple */) {
|
130
130
|
const arg0Expr = testExpression.d.leftExpr.d.args[0].d.valueExpr;
|
131
131
|
if (ParseTreeUtils.isMatchingExpression(reference, arg0Expr)) {
|
132
132
|
const callType = evaluator.getTypeOfExpression(testExpression.d.leftExpr.d.leftExpr, 2 /* EvalFlags.CallBaseDefaults */).type;
|
@@ -175,7 +175,7 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
175
175
|
if (testExpression.d.leftExpr.nodeType === 27 /* ParseNodeType.Index */ &&
|
176
176
|
testExpression.d.leftExpr.d.items.length === 1 &&
|
177
177
|
!testExpression.d.leftExpr.d.trailingComma &&
|
178
|
-
testExpression.d.leftExpr.d.items[0].d.argCategory === 0 /*
|
178
|
+
testExpression.d.leftExpr.d.items[0].d.argCategory === 0 /* ArgCategory.Simple */ &&
|
179
179
|
ParseTreeUtils.isMatchingExpression(reference, testExpression.d.leftExpr.d.leftExpr)) {
|
180
180
|
const indexTypeResult = evaluator.getTypeOfExpression(testExpression.d.leftExpr.d.items[0].d.valueExpr);
|
181
181
|
const indexType = indexTypeResult.type;
|
@@ -241,7 +241,7 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
241
241
|
if (testExpression.d.leftExpr.nodeType === 27 /* ParseNodeType.Index */ &&
|
242
242
|
testExpression.d.leftExpr.d.items.length === 1 &&
|
243
243
|
!testExpression.d.leftExpr.d.trailingComma &&
|
244
|
-
testExpression.d.leftExpr.d.items[0].d.argCategory === 0 /*
|
244
|
+
testExpression.d.leftExpr.d.items[0].d.argCategory === 0 /* ArgCategory.Simple */ &&
|
245
245
|
ParseTreeUtils.isMatchingExpression(reference, testExpression.d.leftExpr.d.leftExpr)) {
|
246
246
|
const indexTypeResult = evaluator.getTypeOfExpression(testExpression.d.leftExpr.d.items[0].d.valueExpr);
|
247
247
|
const indexType = indexTypeResult.type;
|
@@ -489,10 +489,11 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
489
489
|
const functionReturnTypeResult = evaluator.getTypeOfExpression(testExpression);
|
490
490
|
const functionReturnType = functionReturnTypeResult.type;
|
491
491
|
if ((0, types_1.isClassInstance)(functionReturnType) &&
|
492
|
-
types_1.ClassType.isBuiltIn(functionReturnType, '
|
493
|
-
functionReturnType.priv.
|
494
|
-
|
495
|
-
const
|
492
|
+
types_1.ClassType.isBuiltIn(functionReturnType, ['TypeGuard', 'TypeIs']) &&
|
493
|
+
functionReturnType.priv.typeArgs &&
|
494
|
+
functionReturnType.priv.typeArgs.length > 0) {
|
495
|
+
const isStrictTypeGuard = types_1.ClassType.isBuiltIn(functionReturnType, 'TypeIs');
|
496
|
+
const typeGuardType = functionReturnType.priv.typeArgs[0];
|
496
497
|
const isIncomplete = !!callTypeResult.isIncomplete || !!functionReturnTypeResult.isIncomplete;
|
497
498
|
return (type) => {
|
498
499
|
return {
|
@@ -596,14 +597,14 @@ function getDeclsForLocalVar(evaluator, name, reachableFrom, requireUnique) {
|
|
596
597
|
return undefined;
|
597
598
|
}
|
598
599
|
if (decls.length === 0 ||
|
599
|
-
decls.some((decl) => decl.type !== 1 /* DeclarationType.Variable */ && decl.type !== 2 /* DeclarationType.
|
600
|
+
decls.some((decl) => decl.type !== 1 /* DeclarationType.Variable */ && decl.type !== 2 /* DeclarationType.Param */)) {
|
600
601
|
return undefined;
|
601
602
|
}
|
602
603
|
// If there are any assignments within different scopes (e.g. via a "global" or
|
603
604
|
// "nonlocal" reference), don't consider it a local variable.
|
604
605
|
let prevDeclScope;
|
605
606
|
if (decls.some((decl) => {
|
606
|
-
const nodeToConsider = decl.type === 2 /* DeclarationType.
|
607
|
+
const nodeToConsider = decl.type === 2 /* DeclarationType.Param */ ? decl.node.d.name : decl.node;
|
607
608
|
const declScopeNode = ParseTreeUtils.getExecutionScopeNode(nodeToConsider);
|
608
609
|
if (prevDeclScope && declScopeNode !== prevDeclScope) {
|
609
610
|
return true;
|
@@ -624,7 +625,17 @@ function narrowTypeForUserDefinedTypeGuard(evaluator, type, typeGuardType, isPos
|
|
624
625
|
// For non-strict type guards, always narrow to the typeGuardType
|
625
626
|
// in the positive case and don't narrow in the negative case.
|
626
627
|
if (!isStrictTypeGuard) {
|
627
|
-
|
628
|
+
let result = type;
|
629
|
+
if (isPositiveTest) {
|
630
|
+
result = typeGuardType;
|
631
|
+
// If the type guard is a non-constrained TypeVar, add a
|
632
|
+
// condition to the resulting type.
|
633
|
+
if ((0, types_1.isTypeVar)(type) && !(0, types_1.isParamSpec)(type) && type.shared.constraints.length === 0) {
|
634
|
+
result = (0, typeUtils_1.addConditionToType)(result, [{ typeVar: type, constraintIndex: 0 }]);
|
635
|
+
}
|
636
|
+
return result;
|
637
|
+
}
|
638
|
+
return result;
|
628
639
|
}
|
629
640
|
const filterTypes = [];
|
630
641
|
(0, typeUtils_1.doForEachSubtype)(typeGuardType, (typeGuardSubtype) => {
|
@@ -655,14 +666,14 @@ function narrowTypeForTruthiness(evaluator, type, isPositiveTest) {
|
|
655
666
|
function narrowTupleTypeForIsNone(evaluator, type, isPositiveTest, indexValue) {
|
656
667
|
return evaluator.mapSubtypesExpandTypeVars(type, /* options */ undefined, (subtype) => {
|
657
668
|
const tupleType = (0, typeUtils_1.getSpecializedTupleType)(subtype);
|
658
|
-
if (!tupleType || (0, typeUtils_1.isUnboundedTupleClass)(tupleType) || !tupleType.priv.
|
669
|
+
if (!tupleType || (0, typeUtils_1.isUnboundedTupleClass)(tupleType) || !tupleType.priv.tupleTypeArgs) {
|
659
670
|
return subtype;
|
660
671
|
}
|
661
|
-
const tupleLength = tupleType.priv.
|
672
|
+
const tupleLength = tupleType.priv.tupleTypeArgs.length;
|
662
673
|
if (indexValue < 0 || indexValue >= tupleLength) {
|
663
674
|
return subtype;
|
664
675
|
}
|
665
|
-
const typeOfEntry = evaluator.makeTopLevelTypeVarsConcrete(tupleType.priv.
|
676
|
+
const typeOfEntry = evaluator.makeTopLevelTypeVarsConcrete(tupleType.priv.tupleTypeArgs[indexValue].type);
|
666
677
|
if (isPositiveTest) {
|
667
678
|
if (!evaluator.assignType(typeOfEntry, evaluator.getNoneType())) {
|
668
679
|
return undefined;
|
@@ -781,8 +792,8 @@ function getIsInstanceClassTypes(evaluator, argType) {
|
|
781
792
|
}
|
782
793
|
else if ((0, types_1.isFunction)(subtype) &&
|
783
794
|
subtype.shared.parameters.length === 2 &&
|
784
|
-
subtype.shared.parameters[0].category === 1 /*
|
785
|
-
subtype.shared.parameters[1].category === 2 /*
|
795
|
+
subtype.shared.parameters[0].category === 1 /* ParamCategory.ArgsList */ &&
|
796
|
+
subtype.shared.parameters[1].category === 2 /* ParamCategory.KwargsDict */) {
|
786
797
|
classTypeList.push(subtype);
|
787
798
|
}
|
788
799
|
else {
|
@@ -795,8 +806,8 @@ function getIsInstanceClassTypes(evaluator, argType) {
|
|
795
806
|
return;
|
796
807
|
}
|
797
808
|
if ((0, types_1.isClass)(type) && types_1.TypeBase.isInstance(type) && (0, typeUtils_1.isTupleClass)(type)) {
|
798
|
-
if (type.priv.
|
799
|
-
type.priv.
|
809
|
+
if (type.priv.tupleTypeArgs) {
|
810
|
+
type.priv.tupleTypeArgs.forEach((tupleEntry) => {
|
800
811
|
addClassTypesRecursive(tupleEntry.type, recursionCount + 1);
|
801
812
|
});
|
802
813
|
}
|
@@ -826,8 +837,11 @@ function isIsinstanceFilterSuperclass(evaluator, varType, concreteVarType, filte
|
|
826
837
|
return true;
|
827
838
|
}
|
828
839
|
if (isInstanceCheck) {
|
840
|
+
// We convert both types to instances in case they are protocol
|
841
|
+
// classes. A protocol class isn't allowed to be assigned to
|
842
|
+
// type[T], so this would otherwise fail.
|
829
843
|
if (types_1.ClassType.isProtocolClass(concreteFilterType) &&
|
830
|
-
evaluator.assignType(concreteFilterType, concreteVarType)) {
|
844
|
+
evaluator.assignType(types_1.ClassType.cloneAsInstance(concreteFilterType), types_1.ClassType.cloneAsInstance(concreteVarType))) {
|
831
845
|
return true;
|
832
846
|
}
|
833
847
|
}
|
@@ -845,7 +859,11 @@ function isIsinstanceFilterSubclass(evaluator, varType, concreteFilterType, isIn
|
|
845
859
|
return true;
|
846
860
|
}
|
847
861
|
if (isInstanceCheck) {
|
848
|
-
|
862
|
+
// We convert both types to instances in case they are protocol
|
863
|
+
// classes. A protocol class isn't allowed to be assigned to
|
864
|
+
// type[T], so this would otherwise fail.
|
865
|
+
if (types_1.ClassType.isProtocolClass(varType) &&
|
866
|
+
evaluator.assignType(types_1.ClassType.cloneAsInstance(varType), types_1.ClassType.cloneAsInstance(concreteFilterType))) {
|
849
867
|
return true;
|
850
868
|
}
|
851
869
|
}
|
@@ -894,10 +912,8 @@ function narrowTypeForIsInstanceInternal(evaluator, type, filterTypes, isInstanc
|
|
894
912
|
// If the class was implicitly specialized (e.g. because its type
|
895
913
|
// parameters have default values), replace the default type arguments
|
896
914
|
// with Unknown.
|
897
|
-
if (concreteFilterType.priv.
|
898
|
-
concreteFilterType = (0, typeUtils_1.specializeWithUnknownTypeArgs)(types_1.ClassType.
|
899
|
-
/* typeArguments */ undefined,
|
900
|
-
/* isTypeArgumentExplicit */ false), evaluator.getTupleClassType());
|
915
|
+
if (concreteFilterType.priv.typeArgs && !concreteFilterType.priv.isTypeArgExplicit) {
|
916
|
+
concreteFilterType = (0, typeUtils_1.specializeWithUnknownTypeArgs)(types_1.ClassType.specialize(concreteFilterType, /* typeArgs */ undefined), evaluator.getTupleClassType());
|
901
917
|
}
|
902
918
|
filterIsSuperclass = isIsinstanceFilterSuperclass(evaluator, varType, concreteVarType, filterType, concreteFilterType, isInstanceCheck);
|
903
919
|
filterIsSubclass = isIsinstanceFilterSubclass(evaluator, concreteVarType, concreteFilterType, isInstanceCheck);
|
@@ -933,7 +949,7 @@ function narrowTypeForIsInstanceInternal(evaluator, type, filterTypes, isInstanc
|
|
933
949
|
}
|
934
950
|
}
|
935
951
|
else if (filterIsSubclass) {
|
936
|
-
if (evaluator.assignType(concreteVarType, concreteFilterType,
|
952
|
+
if (evaluator.assignType((0, typeUtils_1.convertToInstance)(concreteVarType), (0, typeUtils_1.convertToInstance)(concreteFilterType),
|
937
953
|
/* diag */ undefined,
|
938
954
|
/* destTypeVarContext */ undefined,
|
939
955
|
/* srcTypeVarContext */ undefined, 1024 /* AssignTypeFlags.IgnoreTypeVarScope */ | 16384 /* AssignTypeFlags.AllowIsinstanceSpecialForms */)) {
|
@@ -944,15 +960,13 @@ function narrowTypeForIsInstanceInternal(evaluator, type, filterTypes, isInstanc
|
|
944
960
|
// important because a specialized version of the filter cannot
|
945
961
|
// be passed to isinstance or issubclass.
|
946
962
|
if ((0, types_1.isClass)(filterType)) {
|
947
|
-
if (types_1.ClassType.isSpecialBuiltIn(filterType) ||
|
948
|
-
filterType.
|
949
|
-
if (!filterType.priv.isTypeArgumentExplicit &&
|
963
|
+
if (types_1.ClassType.isSpecialBuiltIn(filterType) || filterType.shared.typeParams.length > 0) {
|
964
|
+
if (!filterType.priv.isTypeArgExplicit &&
|
950
965
|
!types_1.ClassType.isSameGenericClass(concreteVarType, filterType)) {
|
951
966
|
const typeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(filterType));
|
952
|
-
const unspecializedFilterType = types_1.ClassType.
|
953
|
-
/*
|
954
|
-
|
955
|
-
if ((0, constraintSolver_1.addConstraintsForExpectedType)(evaluator, unspecializedFilterType, concreteVarType, typeVarContext,
|
967
|
+
const unspecializedFilterType = types_1.ClassType.specialize(filterType,
|
968
|
+
/* typeArg */ undefined);
|
969
|
+
if ((0, constraintSolver_1.addConstraintsForExpectedType)(evaluator, (0, typeUtils_1.convertToInstance)(unspecializedFilterType), (0, typeUtils_1.convertToInstance)(concreteVarType), typeVarContext,
|
956
970
|
/* liveTypeVarScopes */ undefined, errorNode.start)) {
|
957
971
|
specializedFilterType = (0, typeUtils_1.applySolvedTypeVars)(unspecializedFilterType, typeVarContext, {
|
958
972
|
unknownIfNotFound: true,
|
@@ -966,6 +980,16 @@ function narrowTypeForIsInstanceInternal(evaluator, type, filterTypes, isInstanc
|
|
966
980
|
filteredTypes.push((0, typeUtils_1.addConditionToType)(specializedFilterType, conditions));
|
967
981
|
}
|
968
982
|
}
|
983
|
+
else if (types_1.ClassType.isSameGenericClass(concreteVarType, concreteFilterType)) {
|
984
|
+
// Don't attempt to narrow in this case.
|
985
|
+
if (((_a = concreteVarType.priv) === null || _a === void 0 ? void 0 : _a.literalValue) === undefined &&
|
986
|
+
((_b = concreteFilterType.priv) === null || _b === void 0 ? void 0 : _b.literalValue) === undefined) {
|
987
|
+
const intersection = intersectSameClassType(evaluator, concreteVarType, concreteFilterType);
|
988
|
+
filteredTypes.push(intersection !== null && intersection !== void 0 ? intersection : varType);
|
989
|
+
// Don't attempt to narrow in the negative direction.
|
990
|
+
isClassRelationshipIndeterminate = true;
|
991
|
+
}
|
992
|
+
}
|
969
993
|
else if (allowIntersections &&
|
970
994
|
!types_1.ClassType.isFinal(concreteVarType) &&
|
971
995
|
!types_1.ClassType.isFinal(concreteFilterType)) {
|
@@ -974,39 +998,16 @@ function narrowTypeForIsInstanceInternal(evaluator, type, filterTypes, isInstanc
|
|
974
998
|
// be a mix-in class used with the other. In this case, we'll
|
975
999
|
// synthesize a new class type that represents an intersection of
|
976
1000
|
// the two types.
|
977
|
-
|
978
|
-
|
979
|
-
|
980
|
-
let effectiveMetaclass = concreteVarType.shared.effectiveMetaclass;
|
981
|
-
if (concreteFilterType.shared.effectiveMetaclass) {
|
982
|
-
if (!effectiveMetaclass ||
|
983
|
-
evaluator.assignType(effectiveMetaclass, concreteFilterType.shared.effectiveMetaclass)) {
|
984
|
-
effectiveMetaclass = concreteFilterType.shared.effectiveMetaclass;
|
985
|
-
}
|
986
|
-
}
|
987
|
-
let newClassType = types_1.ClassType.createInstantiable(className, ParseTreeUtils.getClassFullName(errorNode, fileInfo.moduleName, className), fileInfo.moduleName, fileInfo.fileUri, 0 /* ClassTypeFlags.None */, ParseTreeUtils.getTypeSourceId(errorNode),
|
988
|
-
/* declaredMetaclass */ undefined, effectiveMetaclass, concreteVarType.shared.docString);
|
989
|
-
newClassType.shared.baseClasses = [
|
990
|
-
types_1.ClassType.cloneAsInstantiable(concreteVarType),
|
991
|
-
concreteFilterType,
|
992
|
-
];
|
993
|
-
(0, typeUtils_1.computeMroLinearization)(newClassType);
|
994
|
-
newClassType = (0, typeUtils_1.addConditionToType)(newClassType, (_a = concreteFilterType.props) === null || _a === void 0 ? void 0 : _a.condition);
|
995
|
-
if ((0, types_1.isTypeVar)(varType) &&
|
996
|
-
!varType.shared.isParamSpec &&
|
997
|
-
varType.shared.constraints.length === 0) {
|
998
|
-
newClassType = (0, typeUtils_1.addConditionToType)(newClassType, [
|
999
|
-
{ typeVar: varType, constraintIndex: 0 },
|
1000
|
-
]);
|
1001
|
-
}
|
1002
|
-
let newClassInstanceType = types_1.ClassType.cloneAsInstance(newClassType);
|
1003
|
-
if ((_b = concreteVarType.props) === null || _b === void 0 ? void 0 : _b.condition) {
|
1004
|
-
newClassInstanceType = (0, typeUtils_1.addConditionToType)(newClassInstanceType, (_c = concreteVarType.props) === null || _c === void 0 ? void 0 : _c.condition);
|
1001
|
+
let newClassType = evaluator.createSubclass(errorNode, concreteVarType, concreteFilterType);
|
1002
|
+
if ((0, types_1.isTypeVar)(varType) && !(0, types_1.isParamSpec)(varType) && varType.shared.constraints.length === 0) {
|
1003
|
+
newClassType = (0, typeUtils_1.addConditionToType)(newClassType, [{ typeVar: varType, constraintIndex: 0 }]);
|
1005
1004
|
}
|
1005
|
+
let newClassObjType = types_1.ClassType.cloneAsInstance(newClassType);
|
1006
|
+
newClassObjType = (0, typeUtils_1.addConditionToType)(newClassObjType, (_c = concreteVarType.props) === null || _c === void 0 ? void 0 : _c.condition);
|
1006
1007
|
// If this is a issubclass check, we do a double conversion from instantiable
|
1007
1008
|
// to instance back to instantiable to make sure that the includeSubclasses flag
|
1008
1009
|
// gets cleared.
|
1009
|
-
filteredTypes.push(isInstanceCheck ?
|
1010
|
+
filteredTypes.push(isInstanceCheck ? newClassObjType : types_1.ClassType.cloneAsInstantiable(newClassObjType));
|
1010
1011
|
}
|
1011
1012
|
}
|
1012
1013
|
}
|
@@ -1119,7 +1120,7 @@ function narrowTypeForIsInstanceInternal(evaluator, type, filterTypes, isInstanc
|
|
1119
1120
|
// This will normally be treated as type[Any], which is compatible with
|
1120
1121
|
// any metaclass, but we specifically want to treat type as the class
|
1121
1122
|
// type[object] in this case.
|
1122
|
-
if (types_1.ClassType.isBuiltIn(filterMetaclass, 'type') && !filterMetaclass.priv.
|
1123
|
+
if (types_1.ClassType.isBuiltIn(filterMetaclass, 'type') && !filterMetaclass.priv.isTypeArgExplicit) {
|
1123
1124
|
if (!types_1.ClassType.isBuiltIn(metaclassType, 'type')) {
|
1124
1125
|
isMetaclassOverlap = false;
|
1125
1126
|
}
|
@@ -1254,8 +1255,9 @@ function narrowTypeForIsInstanceInternal(evaluator, type, filterTypes, isInstanc
|
|
1254
1255
|
if ((0, types_1.isInstantiableClass)(subtype) || isSubtypeMetaclass) {
|
1255
1256
|
// Handle the special case of isinstance(x, metaclass).
|
1256
1257
|
const includesMetaclassType = filterTypes.some((classType) => (0, typeUtils_1.isInstantiableMetaclass)(classType));
|
1258
|
+
const includesObject = filterTypes.some((classType) => (0, types_1.isInstantiableClass)(classType) && types_1.ClassType.isBuiltIn(classType, 'object'));
|
1257
1259
|
if (isPositiveTest) {
|
1258
|
-
return includesMetaclassType ? negativeFallback : undefined;
|
1260
|
+
return includesMetaclassType || includesObject ? negativeFallback : undefined;
|
1259
1261
|
}
|
1260
1262
|
else {
|
1261
1263
|
return includesMetaclassType ? undefined : negativeFallback;
|
@@ -1297,6 +1299,35 @@ function narrowTypeForIsInstanceInternal(evaluator, type, filterTypes, isInstanc
|
|
1297
1299
|
}
|
1298
1300
|
return filteredType;
|
1299
1301
|
}
|
1302
|
+
// This function assumes that the caller has already verified that the two
|
1303
|
+
// types are the same class and are not literals. It also assumes that the
|
1304
|
+
// caller has verified that type1 is not assignable to type2 or vice versa.
|
1305
|
+
// Returns undefined if there is no intersection between the two types.
|
1306
|
+
function intersectSameClassType(evaluator, type1, type2) {
|
1307
|
+
var _a, _b;
|
1308
|
+
(0, debug_1.assert)((0, types_1.isInstantiableClass)(type1) && (0, types_1.isInstantiableClass)(type2));
|
1309
|
+
(0, debug_1.assert)(types_1.ClassType.isSameGenericClass(type1, type2));
|
1310
|
+
(0, debug_1.assert)(((_a = type1.priv) === null || _a === void 0 ? void 0 : _a.literalValue) === undefined);
|
1311
|
+
(0, debug_1.assert)(((_b = type2.priv) === null || _b === void 0 ? void 0 : _b.literalValue) === undefined);
|
1312
|
+
// Handle tuples specially.
|
1313
|
+
if (types_1.ClassType.isBuiltIn(type1, 'tuple')) {
|
1314
|
+
return intersectTupleTypes(type1, type1);
|
1315
|
+
}
|
1316
|
+
// Indicate that there is no intersection.
|
1317
|
+
return undefined;
|
1318
|
+
}
|
1319
|
+
function intersectTupleTypes(type1, type2) {
|
1320
|
+
var _a, _b;
|
1321
|
+
if (!type2.priv.tupleTypeArgs || (0, typeUtils_1.isTupleGradualForm)(type2)) {
|
1322
|
+
return (0, typeUtils_1.addConditionToType)(type1, (_a = type2.props) === null || _a === void 0 ? void 0 : _a.condition);
|
1323
|
+
}
|
1324
|
+
if (!type1.priv.tupleTypeArgs || (0, typeUtils_1.isTupleGradualForm)(type1)) {
|
1325
|
+
return (0, typeUtils_1.addConditionToType)(type2, (_b = type1.props) === null || _b === void 0 ? void 0 : _b.condition);
|
1326
|
+
}
|
1327
|
+
// For now, don't attempt to narrow in this case.
|
1328
|
+
// TODO - add more sophisticated logic here.
|
1329
|
+
return undefined;
|
1330
|
+
}
|
1300
1331
|
// Attempts to narrow a union of tuples based on their known length.
|
1301
1332
|
function narrowTypeForTupleLength(evaluator, referenceType, lengthValue, isPositiveTest, isLessThanCheck) {
|
1302
1333
|
return (0, typeUtils_1.mapSubtypes)(referenceType, (subtype) => {
|
@@ -1304,23 +1335,23 @@ function narrowTypeForTupleLength(evaluator, referenceType, lengthValue, isPosit
|
|
1304
1335
|
// If it's not a tuple, we can't narrow it.
|
1305
1336
|
if (!(0, types_1.isClassInstance)(concreteSubtype) ||
|
1306
1337
|
!(0, typeUtils_1.isTupleClass)(concreteSubtype) ||
|
1307
|
-
!concreteSubtype.priv.
|
1338
|
+
!concreteSubtype.priv.tupleTypeArgs) {
|
1308
1339
|
return subtype;
|
1309
1340
|
}
|
1310
1341
|
// If the tuple contains a variadic TypeVar, we can't narrow it.
|
1311
|
-
if (concreteSubtype.priv.
|
1342
|
+
if (concreteSubtype.priv.tupleTypeArgs.some((typeArg) => (0, types_1.isUnpackedTypeVarTuple)(typeArg.type))) {
|
1312
1343
|
return subtype;
|
1313
1344
|
}
|
1314
1345
|
// If the tuple contains no unbounded elements, then we know its length exactly.
|
1315
|
-
if (!concreteSubtype.priv.
|
1346
|
+
if (!concreteSubtype.priv.tupleTypeArgs.some((typeArg) => typeArg.isUnbounded)) {
|
1316
1347
|
const tupleLengthMatches = isLessThanCheck
|
1317
|
-
? concreteSubtype.priv.
|
1318
|
-
: concreteSubtype.priv.
|
1348
|
+
? concreteSubtype.priv.tupleTypeArgs.length < lengthValue
|
1349
|
+
: concreteSubtype.priv.tupleTypeArgs.length === lengthValue;
|
1319
1350
|
return tupleLengthMatches === isPositiveTest ? subtype : undefined;
|
1320
1351
|
}
|
1321
1352
|
// The tuple contains a "...". We'll expand this into as many elements as
|
1322
1353
|
// necessary to match the lengthValue.
|
1323
|
-
const elementsToAdd = lengthValue - concreteSubtype.priv.
|
1354
|
+
const elementsToAdd = lengthValue - concreteSubtype.priv.tupleTypeArgs.length + 1;
|
1324
1355
|
if (!isLessThanCheck) {
|
1325
1356
|
// If the specified length is smaller than the minimum length of this tuple,
|
1326
1357
|
// we can rule it out for a positive test and rule it in for a negative test.
|
@@ -1360,7 +1391,7 @@ function narrowTypeForTupleLength(evaluator, referenceType, lengthValue, isPosit
|
|
1360
1391
|
// to) the unbounded element.
|
1361
1392
|
function expandUnboundedTupleElement(tupleType, elementsToAdd, keepUnbounded) {
|
1362
1393
|
const tupleTypeArgs = [];
|
1363
|
-
tupleType.priv.
|
1394
|
+
tupleType.priv.tupleTypeArgs.forEach((typeArg) => {
|
1364
1395
|
if (!typeArg.isUnbounded) {
|
1365
1396
|
tupleTypeArgs.push(typeArg);
|
1366
1397
|
}
|
@@ -1388,13 +1419,13 @@ function narrowTypeForContainerType(evaluator, referenceType, containerType, isP
|
|
1388
1419
|
// with a known length.
|
1389
1420
|
if (!(0, types_1.isClassInstance)(containerType) ||
|
1390
1421
|
!types_1.ClassType.isBuiltIn(containerType, 'tuple') ||
|
1391
|
-
!containerType.priv.
|
1422
|
+
!containerType.priv.tupleTypeArgs) {
|
1392
1423
|
return referenceType;
|
1393
1424
|
}
|
1394
1425
|
// Determine which tuple types can be eliminated. Only "None" and
|
1395
1426
|
// literal types can be handled here.
|
1396
1427
|
const typesToEliminate = [];
|
1397
|
-
containerType.priv.
|
1428
|
+
containerType.priv.tupleTypeArgs.forEach((tupleEntry) => {
|
1398
1429
|
if (!tupleEntry.isUnbounded) {
|
1399
1430
|
if ((0, typeUtils_1.isNoneInstance)(tupleEntry.type)) {
|
1400
1431
|
typesToEliminate.push(tupleEntry.type);
|
@@ -1429,12 +1460,12 @@ function getElementTypeForContainerNarrowing(containerType) {
|
|
1429
1460
|
if (!(0, types_1.isClassInstance)(containerType) || !types_1.ClassType.isBuiltIn(containerType, supportedContainers)) {
|
1430
1461
|
return undefined;
|
1431
1462
|
}
|
1432
|
-
if (!containerType.priv.
|
1463
|
+
if (!containerType.priv.typeArgs || containerType.priv.typeArgs.length < 1) {
|
1433
1464
|
return undefined;
|
1434
1465
|
}
|
1435
|
-
let elementType = containerType.priv.
|
1436
|
-
if ((0, typeUtils_1.isTupleClass)(containerType) && containerType.priv.
|
1437
|
-
elementType = (0, types_1.combineTypes)(containerType.priv.
|
1466
|
+
let elementType = containerType.priv.typeArgs[0];
|
1467
|
+
if ((0, typeUtils_1.isTupleClass)(containerType) && containerType.priv.tupleTypeArgs) {
|
1468
|
+
elementType = (0, types_1.combineTypes)(containerType.priv.tupleTypeArgs.map((t) => t.type));
|
1438
1469
|
}
|
1439
1470
|
return elementType;
|
1440
1471
|
}
|
@@ -1584,10 +1615,8 @@ function narrowTypeForDiscriminatedTupleComparison(evaluator, referenceType, ind
|
|
1584
1615
|
typeof indexLiteralType.priv.literalValue === 'number' &&
|
1585
1616
|
(0, types_1.isClassInstance)(literalType)) {
|
1586
1617
|
const indexValue = indexLiteralType.priv.literalValue;
|
1587
|
-
if (subtype.priv.
|
1588
|
-
indexValue
|
1589
|
-
indexValue < subtype.priv.tupleTypeArguments.length) {
|
1590
|
-
const tupleEntryType = (_a = subtype.priv.tupleTypeArguments[indexValue]) === null || _a === void 0 ? void 0 : _a.type;
|
1618
|
+
if (subtype.priv.tupleTypeArgs && indexValue >= 0 && indexValue < subtype.priv.tupleTypeArgs.length) {
|
1619
|
+
const tupleEntryType = (_a = subtype.priv.tupleTypeArgs[indexValue]) === null || _a === void 0 ? void 0 : _a.type;
|
1591
1620
|
if (tupleEntryType && (0, typeUtils_1.isLiteralTypeOrUnion)(tupleEntryType)) {
|
1592
1621
|
if (isPositiveTest) {
|
1593
1622
|
return evaluator.assignType(tupleEntryType, literalType) ? subtype : undefined;
|
@@ -1733,8 +1762,8 @@ function narrowTypeForClassComparison(evaluator, referenceType, classType, isPos
|
|
1733
1762
|
types_1.TypeBase.isInstance(subtype) &&
|
1734
1763
|
types_1.ClassType.isBuiltIn(concreteSubtype, 'type')) {
|
1735
1764
|
concreteSubtype =
|
1736
|
-
concreteSubtype.priv.
|
1737
|
-
? (0, typeUtils_1.convertToInstantiable)(concreteSubtype.priv.
|
1765
|
+
concreteSubtype.priv.typeArgs && concreteSubtype.priv.typeArgs.length > 0
|
1766
|
+
? (0, typeUtils_1.convertToInstantiable)(concreteSubtype.priv.typeArgs[0])
|
1738
1767
|
: types_1.UnknownType.create();
|
1739
1768
|
}
|
1740
1769
|
if ((0, types_1.isAnyOrUnknown)(concreteSubtype)) {
|
@@ -1894,9 +1923,9 @@ function narrowTypeForCallable(evaluator, type, isPositiveTest, errorNode, allow
|
|
1894
1923
|
newClassType = (0, typeUtils_1.addConditionToType)(newClassType, (_a = subtype.props) === null || _a === void 0 ? void 0 : _a.condition);
|
1895
1924
|
// Add a __call__ method to the new class.
|
1896
1925
|
const callMethod = types_1.FunctionType.createSynthesizedInstance('__call__');
|
1897
|
-
const selfParam = types_1.FunctionParam.create(0 /*
|
1898
|
-
types_1.FunctionType.
|
1899
|
-
types_1.FunctionType.
|
1926
|
+
const selfParam = types_1.FunctionParam.create(0 /* ParamCategory.Simple */, types_1.ClassType.cloneAsInstance(newClassType), types_1.FunctionParamFlags.TypeDeclared, 'self');
|
1927
|
+
types_1.FunctionType.addParam(callMethod, selfParam);
|
1928
|
+
types_1.FunctionType.addDefaultParams(callMethod);
|
1900
1929
|
callMethod.shared.declaredReturnType = types_1.UnknownType.create();
|
1901
1930
|
types_1.ClassType.getSymbolTable(newClassType).set('__call__', symbol_1.Symbol.createWithType(4 /* SymbolFlags.ClassMember */, callMethod));
|
1902
1931
|
return types_1.ClassType.cloneAsInstance(newClassType);
|