@zzzen/pyright-internal 1.2.0-dev.20230521 → 1.2.0-dev.20230604

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.
Files changed (110) hide show
  1. package/dist/analyzer/checker.js +4 -4
  2. package/dist/analyzer/checker.js.map +1 -1
  3. package/dist/analyzer/commentUtils.js +15 -16
  4. package/dist/analyzer/commentUtils.js.map +1 -1
  5. package/dist/analyzer/constraintSolver.d.ts +1 -1
  6. package/dist/analyzer/constraintSolver.js +24 -8
  7. package/dist/analyzer/constraintSolver.js.map +1 -1
  8. package/dist/analyzer/constructorTransform.js +3 -3
  9. package/dist/analyzer/constructorTransform.js.map +1 -1
  10. package/dist/analyzer/constructors.js +153 -132
  11. package/dist/analyzer/constructors.js.map +1 -1
  12. package/dist/analyzer/dataClasses.js +1 -1
  13. package/dist/analyzer/dataClasses.js.map +1 -1
  14. package/dist/analyzer/docStringConversion.js +1 -1
  15. package/dist/analyzer/enums.js +1 -1
  16. package/dist/analyzer/enums.js.map +1 -1
  17. package/dist/analyzer/parameterUtils.js +7 -7
  18. package/dist/analyzer/parameterUtils.js.map +1 -1
  19. package/dist/analyzer/parseTreeUtils.d.ts +1 -0
  20. package/dist/analyzer/parseTreeUtils.js +21 -6
  21. package/dist/analyzer/parseTreeUtils.js.map +1 -1
  22. package/dist/analyzer/patternMatching.js +2 -1
  23. package/dist/analyzer/patternMatching.js.map +1 -1
  24. package/dist/analyzer/program.js +9 -4
  25. package/dist/analyzer/program.js.map +1 -1
  26. package/dist/analyzer/service.js +8 -0
  27. package/dist/analyzer/service.js.map +1 -1
  28. package/dist/analyzer/sourceFile.d.ts +3 -2
  29. package/dist/analyzer/sourceFile.js +204 -190
  30. package/dist/analyzer/sourceFile.js.map +1 -1
  31. package/dist/analyzer/typeCacheUtils.d.ts +9 -2
  32. package/dist/analyzer/typeCacheUtils.js +52 -9
  33. package/dist/analyzer/typeCacheUtils.js.map +1 -1
  34. package/dist/analyzer/typeEvaluator.js +220 -128
  35. package/dist/analyzer/typeEvaluator.js.map +1 -1
  36. package/dist/analyzer/typeGuards.js +3 -3
  37. package/dist/analyzer/typeGuards.js.map +1 -1
  38. package/dist/analyzer/typePrinter.js +9 -9
  39. package/dist/analyzer/typePrinter.js.map +1 -1
  40. package/dist/analyzer/typeStubWriter.js +2 -2
  41. package/dist/analyzer/typeStubWriter.js.map +1 -1
  42. package/dist/analyzer/typeUtils.d.ts +6 -6
  43. package/dist/analyzer/typeUtils.js +145 -159
  44. package/dist/analyzer/typeUtils.js.map +1 -1
  45. package/dist/analyzer/typeVarContext.js +12 -1
  46. package/dist/analyzer/typeVarContext.js.map +1 -1
  47. package/dist/analyzer/typeWalker.d.ts +22 -0
  48. package/dist/analyzer/typeWalker.js +164 -0
  49. package/dist/analyzer/typeWalker.js.map +1 -0
  50. package/dist/analyzer/typedDicts.js +2 -2
  51. package/dist/analyzer/typedDicts.js.map +1 -1
  52. package/dist/analyzer/types.d.ts +5 -2
  53. package/dist/analyzer/types.js +39 -22
  54. package/dist/analyzer/types.js.map +1 -1
  55. package/dist/backgroundAnalysisBase.d.ts +4 -2
  56. package/dist/commands/dumpFileDebugInfoCommand.js +2 -2
  57. package/dist/common/commandLineOptions.d.ts +1 -0
  58. package/dist/common/commandLineOptions.js.map +1 -1
  59. package/dist/common/textRange.d.ts +3 -3
  60. package/dist/common/textRange.js +17 -8
  61. package/dist/common/textRange.js.map +1 -1
  62. package/dist/common/uriParser.d.ts +1 -0
  63. package/dist/common/uriParser.js +9 -0
  64. package/dist/common/uriParser.js.map +1 -1
  65. package/dist/languageServerBase.js +4 -2
  66. package/dist/languageServerBase.js.map +1 -1
  67. package/dist/languageService/analyzerServiceExecutor.js +1 -0
  68. package/dist/languageService/analyzerServiceExecutor.js.map +1 -1
  69. package/dist/languageService/completionProvider.js +5 -5
  70. package/dist/languageService/completionProvider.js.map +1 -1
  71. package/dist/languageService/renameProvider.d.ts +3 -3
  72. package/dist/languageService/renameProvider.js +6 -6
  73. package/dist/languageService/renameProvider.js.map +1 -1
  74. package/dist/localization/localize.d.ts +5 -0
  75. package/dist/localization/localize.js +3 -0
  76. package/dist/localization/localize.js.map +1 -1
  77. package/dist/localization/package.nls.en-us.json +3 -0
  78. package/dist/parser/parseNodes.d.ts +11 -3
  79. package/dist/parser/parseNodes.js +7 -9
  80. package/dist/parser/parseNodes.js.map +1 -1
  81. package/dist/parser/parser.js +13 -10
  82. package/dist/parser/parser.js.map +1 -1
  83. package/dist/parser/tokenizer.js +8 -7
  84. package/dist/parser/tokenizer.js.map +1 -1
  85. package/dist/tests/docStringConversion.test.js +44 -0
  86. package/dist/tests/docStringConversion.test.js.map +1 -1
  87. package/dist/tests/fourslash/fourslash.d.ts +1 -1
  88. package/dist/tests/fourslash/rename.function.untitledFile.fourslash.d.ts +1 -0
  89. package/dist/tests/fourslash/rename.function.untitledFile.fourslash.js +17 -0
  90. package/dist/tests/fourslash/rename.function.untitledFile.fourslash.js.map +1 -0
  91. package/dist/tests/harness/fourslash/testState.d.ts +1 -1
  92. package/dist/tests/harness/fourslash/testState.js +2 -2
  93. package/dist/tests/harness/fourslash/testState.js.map +1 -1
  94. package/dist/tests/service.test.js +10 -0
  95. package/dist/tests/service.test.js.map +1 -1
  96. package/dist/tests/tokenizer.test.js +8 -0
  97. package/dist/tests/tokenizer.test.js.map +1 -1
  98. package/dist/tests/typeEvaluator1.test.js +5 -1
  99. package/dist/tests/typeEvaluator1.test.js.map +1 -1
  100. package/dist/tests/typeEvaluator2.test.js +12 -0
  101. package/dist/tests/typeEvaluator2.test.js.map +1 -1
  102. package/dist/tests/typeEvaluator3.test.js +17 -1
  103. package/dist/tests/typeEvaluator3.test.js.map +1 -1
  104. package/dist/tests/typeEvaluator4.test.js +4 -0
  105. package/dist/tests/typeEvaluator4.test.js.map +1 -1
  106. package/dist/tests/typePrinter.test.js +3 -3
  107. package/dist/tests/typePrinter.test.js.map +1 -1
  108. package/dist/workspaceFactory.js +4 -4
  109. package/dist/workspaceFactory.js.map +1 -1
  110. package/package.json +1 -1
@@ -42,6 +42,7 @@ export declare const enum AssignTypeFlags {
42
42
  }
43
43
  export interface ApplyTypeVarOptions {
44
44
  unknownIfNotFound?: boolean;
45
+ unknownExemptTypeVars?: TypeVarType[];
45
46
  useUnknownOverDefault?: boolean;
46
47
  useNarrowBoundOnly?: boolean;
47
48
  eliminateUnsolvedInUnions?: boolean;
@@ -87,8 +88,7 @@ export declare function getTypeVarScopeIds(type: Type): TypeVarScopeId[] | TypeV
87
88
  export declare function getSpecializedTupleType(type: Type): ClassType | undefined;
88
89
  export declare function isLiteralType(type: ClassType): boolean;
89
90
  export declare function isLiteralTypeOrUnion(type: Type): boolean;
90
- export declare function containsType(type: Type, predicate: (t: Type) => boolean, includeTypeArgs?: boolean, recursionCount?: number): boolean;
91
- export declare function containsLiteralType(type: Type, includeTypeArgs?: boolean, recursionCount?: number): boolean;
91
+ export declare function containsLiteralType(type: Type, includeTypeArgs?: boolean): boolean;
92
92
  export declare function getLiteralTypeClassName(type: Type): string | undefined;
93
93
  export declare function getUnionSubtypeCount(type: Type): number;
94
94
  export declare function isEllipsisType(type: Type): boolean;
@@ -106,7 +106,7 @@ export declare function applySourceContextTypeVars(destContext: TypeVarContext,
106
106
  export declare function applySourceContextTypeVarsToSignature(destSignature: TypeVarSignatureContext, srcContext: TypeVarContext): void;
107
107
  export declare function applyInScopePlaceholders(typeVarContext: TypeVarContext): void;
108
108
  export declare function validateTypeVarDefault(typeVar: TypeVarType, liveTypeParams: TypeVarType[], invalidTypeVars: Set<string>): void;
109
- export declare function transformExpectedType(expectedType: Type, liveTypeVarScopes: TypeVarScopeId[]): Type;
109
+ export declare function transformExpectedType(expectedType: Type, liveTypeVarScopes: TypeVarScopeId[], usageOffset: number | undefined): Type;
110
110
  export declare function getProtocolSymbols(classType: ClassType): Map<string, ClassMember>;
111
111
  export declare function getProtocolSymbolsRecursive(classType: ClassType, symbolMap: Map<string, ClassMember>, classFlags?: ClassTypeFlags, recursionCount?: number): void;
112
112
  export declare function getContainerDepth(type: Type, recursionCount?: number): number;
@@ -134,12 +134,12 @@ export declare function convertToInstance(type: Type, includeSubclasses?: boolea
134
134
  export declare function convertToInstantiable(type: Type): Type;
135
135
  export declare function getMembersForClass(classType: ClassType, symbolTable: SymbolTable, includeInstanceVars: boolean): void;
136
136
  export declare function getMembersForModule(moduleType: ModuleType, symbolTable: SymbolTable): void;
137
- export declare function containsAnyOrUnknown(type: Type): boolean;
138
- export declare function containsUnknown(type: Type): boolean;
139
- export declare function isPartlyUnknown(type: Type, allowUnknownTypeArgsForClasses?: boolean, recursionCount?: number): boolean;
137
+ export declare function containsAnyOrUnknown(type: Type, recurse: boolean): AnyType | UnknownType | undefined;
138
+ export declare function isPartlyUnknown(type: Type, allowUnknownTypeArgsForClasses?: boolean): boolean;
140
139
  export declare function explodeGenericClass(classType: ClassType): Type;
141
140
  export declare function combineSameSizedTuples(type: Type, tupleType: Type | undefined): Type;
142
141
  export declare function specializeTupleClass(classType: ClassType, typeArgs: TupleTypeArgument[], isTypeArgumentExplicit?: boolean, isUnpackedTuple?: boolean): ClassType;
142
+ export declare function getCommonErasedType(types: Type[]): ClassType | undefined;
143
143
  export declare function removeParamSpecVariadicsFromSignature(type: FunctionType | OverloadedFunctionType): FunctionType | OverloadedFunctionType;
144
144
  export declare function removeParamSpecVariadicsFromFunction(type: FunctionType): FunctionType;
145
145
  export declare function getGeneratorTypeArgs(returnType: Type): Type[] | undefined;
@@ -8,14 +8,15 @@
8
8
  * Functions that operate on Type objects.
9
9
  */
10
10
  Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.lookUpObjectMember = exports.getContainerDepth = exports.getProtocolSymbolsRecursive = exports.getProtocolSymbols = exports.transformExpectedType = exports.validateTypeVarDefault = exports.applyInScopePlaceholders = exports.applySourceContextTypeVarsToSignature = exports.applySourceContextTypeVars = exports.applySolvedTypeVars = exports.ensureFunctionSignaturesAreUnique = exports.populateTypeVarContextForSelfType = exports.partiallySpecializeType = exports.isUnboundedTupleClass = exports.isTupleClass = exports.isMaybeDescriptorInstance = exports.isDescriptorInstance = exports.isCallableType = exports.isProperty = exports.isEllipsisType = exports.getUnionSubtypeCount = exports.getLiteralTypeClassName = exports.containsLiteralType = exports.containsType = exports.isLiteralTypeOrUnion = exports.isLiteralType = exports.getSpecializedTupleType = exports.getTypeVarScopeIds = exports.getTypeVarScopeId = exports.transformPossibleRecursiveTypeAlias = exports.isTypeAliasRecursive = exports.isTypeAliasPlaceholder = exports.getTypeCondition = exports.addConditionToType = exports.getFullNameOfType = exports.derivesFromAnyOrUnknown = exports.isUnionableType = exports.preserveUnknown = exports.areTypesSame = exports.doForEachSubtype = exports.sortTypes = exports.mapSubtypes = exports.makeInferenceContext = exports.isTypeVarSame = exports.isIncompleteUnknown = exports.isOptionalType = exports.UniqueSignatureTracker = exports.AssignTypeFlags = exports.ClassIteratorFlags = exports.ClassMemberLookupFlags = void 0;
12
- exports.convertParamSpecValueToType = exports.convertTypeToParamSpecValue = exports.getDeclaringModulesForType = exports.computeMroLinearization = exports.isVarianceOfTypeArgumentCompatible = exports.combineVariances = exports.requiresSpecialization = exports.requiresTypeArguments = exports.getGeneratorTypeArgs = exports.removeParamSpecVariadicsFromFunction = exports.removeParamSpecVariadicsFromSignature = exports.specializeTupleClass = exports.combineSameSizedTuples = exports.explodeGenericClass = exports.isPartlyUnknown = exports.containsUnknown = exports.containsAnyOrUnknown = exports.getMembersForModule = exports.getMembersForClass = exports.convertToInstantiable = exports.convertToInstance = exports.isEffectivelyInstantiable = exports.isMetaclassInstance = exports.isInstantiableMetaclass = exports.getGeneratorYieldType = exports.getDeclaredGeneratorReturnType = exports.synthesizeTypeVarForSelfCls = exports.derivesFromClassRecursive = exports.specializeForBaseClass = exports.buildTypeVarContext = exports.buildTypeVarContextFromSpecializedClass = exports.setTypeArgumentsRecursive = exports.specializeClassType = exports.isTypeVarLimitedToCallable = exports.getTypeVarArgumentsRecursive = exports.addTypeVarsToListIfUnique = exports.getClassFieldsRecursive = exports.getClassIterator = exports.getClassMemberIterator = exports.lookUpClassMember = void 0;
11
+ exports.lookUpClassMember = exports.lookUpObjectMember = exports.getContainerDepth = exports.getProtocolSymbolsRecursive = exports.getProtocolSymbols = exports.transformExpectedType = exports.validateTypeVarDefault = exports.applyInScopePlaceholders = exports.applySourceContextTypeVarsToSignature = exports.applySourceContextTypeVars = exports.applySolvedTypeVars = exports.ensureFunctionSignaturesAreUnique = exports.populateTypeVarContextForSelfType = exports.partiallySpecializeType = exports.isUnboundedTupleClass = exports.isTupleClass = exports.isMaybeDescriptorInstance = exports.isDescriptorInstance = exports.isCallableType = exports.isProperty = exports.isEllipsisType = exports.getUnionSubtypeCount = exports.getLiteralTypeClassName = exports.containsLiteralType = exports.isLiteralTypeOrUnion = exports.isLiteralType = exports.getSpecializedTupleType = exports.getTypeVarScopeIds = exports.getTypeVarScopeId = exports.transformPossibleRecursiveTypeAlias = exports.isTypeAliasRecursive = exports.isTypeAliasPlaceholder = exports.getTypeCondition = exports.addConditionToType = exports.getFullNameOfType = exports.derivesFromAnyOrUnknown = exports.isUnionableType = exports.preserveUnknown = exports.areTypesSame = exports.doForEachSubtype = exports.sortTypes = exports.mapSubtypes = exports.makeInferenceContext = exports.isTypeVarSame = exports.isIncompleteUnknown = exports.isOptionalType = exports.UniqueSignatureTracker = exports.AssignTypeFlags = exports.ClassIteratorFlags = exports.ClassMemberLookupFlags = void 0;
12
+ exports.convertParamSpecValueToType = exports.convertTypeToParamSpecValue = exports.getDeclaringModulesForType = exports.computeMroLinearization = exports.isVarianceOfTypeArgumentCompatible = exports.combineVariances = exports.requiresSpecialization = exports.requiresTypeArguments = exports.getGeneratorTypeArgs = exports.removeParamSpecVariadicsFromFunction = exports.removeParamSpecVariadicsFromSignature = exports.getCommonErasedType = exports.specializeTupleClass = exports.combineSameSizedTuples = exports.explodeGenericClass = exports.isPartlyUnknown = exports.containsAnyOrUnknown = exports.getMembersForModule = exports.getMembersForClass = exports.convertToInstantiable = exports.convertToInstance = exports.isEffectivelyInstantiable = exports.isMetaclassInstance = exports.isInstantiableMetaclass = exports.getGeneratorYieldType = exports.getDeclaredGeneratorReturnType = exports.synthesizeTypeVarForSelfCls = exports.derivesFromClassRecursive = exports.specializeForBaseClass = exports.buildTypeVarContext = exports.buildTypeVarContextFromSpecializedClass = exports.setTypeArgumentsRecursive = exports.specializeClassType = exports.isTypeVarLimitedToCallable = exports.getTypeVarArgumentsRecursive = exports.addTypeVarsToListIfUnique = exports.getClassFieldsRecursive = exports.getClassIterator = exports.getClassMemberIterator = void 0;
13
13
  const collectionUtils_1 = require("../common/collectionUtils");
14
14
  const debug_1 = require("../common/debug");
15
15
  const symbol_1 = require("./symbol");
16
16
  const symbolUtils_1 = require("./symbolUtils");
17
17
  const types_1 = require("./types");
18
18
  const typeVarContext_1 = require("./typeVarContext");
19
+ const typeWalker_1 = require("./typeWalker");
19
20
  var ClassMemberLookupFlags;
20
21
  (function (ClassMemberLookupFlags) {
21
22
  ClassMemberLookupFlags[ClassMemberLookupFlags["Default"] = 0] = "Default";
@@ -579,49 +580,28 @@ function isLiteralTypeOrUnion(type) {
579
580
  return false;
580
581
  }
581
582
  exports.isLiteralTypeOrUnion = isLiteralTypeOrUnion;
582
- function containsType(type, predicate, includeTypeArgs = false, recursionCount = 0) {
583
- var _a;
584
- if (recursionCount > types_1.maxTypeRecursionCount) {
585
- return false;
586
- }
587
- recursionCount++;
588
- if (predicate(type)) {
589
- return true;
590
- }
591
- if (includeTypeArgs && (0, types_1.isClass)(type)) {
592
- const typeArgs = ((_a = type.tupleTypeArguments) === null || _a === void 0 ? void 0 : _a.map((t) => t.type)) || type.typeArguments;
593
- if (typeArgs) {
594
- return typeArgs.some((typeArg) => containsType(typeArg, predicate, includeTypeArgs, recursionCount));
595
- }
596
- }
597
- if ((0, types_1.isUnion)(type)) {
598
- return type.subtypes.some((subtype) => containsType(subtype, predicate, includeTypeArgs, recursionCount));
599
- }
600
- if ((0, types_1.isOverloadedFunction)(type)) {
601
- return type.overloads.some((overload) => containsType(overload, predicate, includeTypeArgs, recursionCount));
602
- }
603
- if ((0, types_1.isFunction)(type)) {
604
- const returnType = types_1.FunctionType.getSpecializedReturnType(type);
605
- if (returnType && containsType(returnType, predicate, includeTypeArgs, recursionCount)) {
606
- return true;
583
+ function containsLiteralType(type, includeTypeArgs = false) {
584
+ class ContainsLiteralTypeWalker extends typeWalker_1.TypeWalker {
585
+ constructor(_includeTypeArgs) {
586
+ super();
587
+ this._includeTypeArgs = _includeTypeArgs;
588
+ this.foundLiteral = false;
607
589
  }
608
- for (let i = 0; i < type.details.parameters.length; i++) {
609
- const paramType = types_1.FunctionType.getEffectiveParameterType(type, i);
610
- if (containsType(paramType, predicate, includeTypeArgs, recursionCount)) {
611
- return true;
590
+ visitClass(classType) {
591
+ if ((0, types_1.isClassInstance)(classType)) {
592
+ if (isLiteralType(classType) || types_1.ClassType.isBuiltIn(classType, 'LiteralString')) {
593
+ this.foundLiteral = true;
594
+ this.cancelWalk();
595
+ }
596
+ }
597
+ if (this._includeTypeArgs) {
598
+ super.visitClass(classType);
612
599
  }
613
600
  }
614
601
  }
615
- return false;
616
- }
617
- exports.containsType = containsType;
618
- function containsLiteralType(type, includeTypeArgs = false, recursionCount = 0) {
619
- return containsType(type, (t) => {
620
- if (!(0, types_1.isClassInstance)(t)) {
621
- return false;
622
- }
623
- return isLiteralType(t) || types_1.ClassType.isBuiltIn(t, 'LiteralString');
624
- }, includeTypeArgs, recursionCount);
602
+ const walker = new ContainsLiteralTypeWalker(includeTypeArgs);
603
+ walker.walk(type);
604
+ return walker.foundLiteral;
625
605
  }
626
606
  exports.containsLiteralType = containsLiteralType;
627
607
  // If all of the subtypes are literals with the same built-in class (e.g.
@@ -841,8 +821,8 @@ exports.validateTypeVarDefault = validateTypeVarDefault;
841
821
  // expected type uses TypeVars that are not part of the context of the
842
822
  // class we are constructing. We'll replace these type variables with dummy
843
823
  // type variables.
844
- function transformExpectedType(expectedType, liveTypeVarScopes) {
845
- const transformer = new ExpectedTypeTransformer(liveTypeVarScopes);
824
+ function transformExpectedType(expectedType, liveTypeVarScopes, usageOffset) {
825
+ const transformer = new ExpectedTypeTransformer(liveTypeVarScopes, usageOffset);
846
826
  return transformer.apply(expectedType, 0);
847
827
  }
848
828
  exports.transformExpectedType = transformExpectedType;
@@ -1527,7 +1507,7 @@ exports.isEffectivelyInstantiable = isEffectivelyInstantiable;
1527
1507
  function convertToInstance(type, includeSubclasses = true) {
1528
1508
  var _a;
1529
1509
  // See if we've already performed this conversion and cached it.
1530
- if ((_a = type.cached) === null || _a === void 0 ? void 0 : _a.instanceType) {
1510
+ if (((_a = type.cached) === null || _a === void 0 ? void 0 : _a.instanceType) && includeSubclasses) {
1531
1511
  return type.cached.instanceType;
1532
1512
  }
1533
1513
  let result = mapSubtypes(type, (subtype) => {
@@ -1570,7 +1550,7 @@ function convertToInstance(type, includeSubclasses = true) {
1570
1550
  if (type.typeAliasInfo && type !== result) {
1571
1551
  result = types_1.TypeBase.cloneForTypeAlias(result, type.typeAliasInfo.name, type.typeAliasInfo.fullName, type.typeAliasInfo.typeVarScopeId, type.typeAliasInfo.typeParameters, type.typeAliasInfo.typeArguments);
1572
1552
  }
1573
- if (type !== result) {
1553
+ if (type !== result && includeSubclasses) {
1574
1554
  // Cache the converted value for next time.
1575
1555
  if (!type.cached) {
1576
1556
  type.cached = {};
@@ -1679,93 +1659,60 @@ function getMembersForModule(moduleType, symbolTable) {
1679
1659
  });
1680
1660
  }
1681
1661
  exports.getMembersForModule = getMembersForModule;
1682
- // Determines if the type is an Any or Unknown or a union that
1683
- // contains one of these. It does not look at type arguments.
1684
- function containsAnyOrUnknown(type) {
1685
- let foundAnyOrUnknown = false;
1686
- doForEachSubtype(type, (subtype) => {
1687
- if ((0, types_1.isAnyOrUnknown)(subtype)) {
1688
- foundAnyOrUnknown = true;
1662
+ // Determines if the type contains an Any or Unknown type. If so,
1663
+ // it returns the Any or Unknown type. Unknowns are preferred over
1664
+ // Any if both are present. If recurse is true, it will recurse
1665
+ // through type arguments and parameters.
1666
+ function containsAnyOrUnknown(type, recurse) {
1667
+ class AnyOrUnknownWalker extends typeWalker_1.TypeWalker {
1668
+ constructor(_recurse) {
1669
+ super();
1670
+ this._recurse = _recurse;
1689
1671
  }
1690
- });
1691
- return foundAnyOrUnknown;
1692
- }
1693
- exports.containsAnyOrUnknown = containsAnyOrUnknown;
1694
- // Determines if the type is an Unknown or a union that contains an Unknown.
1695
- // It does not look at type arguments.
1696
- function containsUnknown(type) {
1697
- let foundUnknown = false;
1698
- doForEachSubtype(type, (subtype) => {
1699
- if ((0, types_1.isUnknown)(subtype)) {
1700
- foundUnknown = true;
1672
+ visitUnknown(type) {
1673
+ this.anyOrUnknownType = this.anyOrUnknownType ? preserveUnknown(this.anyOrUnknownType, type) : type;
1701
1674
  }
1702
- });
1703
- return foundUnknown;
1704
- }
1705
- exports.containsUnknown = containsUnknown;
1706
- // Determines if any part of the type contains "Unknown", including any type arguments.
1707
- function isPartlyUnknown(type, allowUnknownTypeArgsForClasses = false, recursionCount = 0) {
1708
- var _a, _b;
1709
- if (recursionCount > types_1.maxTypeRecursionCount) {
1710
- return false;
1711
- }
1712
- recursionCount++;
1713
- if ((0, types_1.isUnknown)(type)) {
1714
- return true;
1715
- }
1716
- // If this is a generic type alias, see if any of its type arguments
1717
- // are either unspecified or are partially known.
1718
- if ((_a = type.typeAliasInfo) === null || _a === void 0 ? void 0 : _a.typeArguments) {
1719
- if (type.typeAliasInfo.typeArguments.some((typeArg) => isPartlyUnknown(typeArg, allowUnknownTypeArgsForClasses, recursionCount))) {
1720
- return true;
1675
+ visitAny(type) {
1676
+ this.anyOrUnknownType = this.anyOrUnknownType ? preserveUnknown(this.anyOrUnknownType, type) : type;
1721
1677
  }
1722
- }
1723
- // See if a union contains an unknown type.
1724
- if ((0, types_1.isUnion)(type)) {
1725
- return ((0, types_1.findSubtype)(type, (subtype) => isPartlyUnknown(subtype, allowUnknownTypeArgsForClasses, recursionCount)) !==
1726
- undefined);
1727
- }
1728
- // See if an object or class has an unknown type argument.
1729
- if ((0, types_1.isClass)(type)) {
1730
- if (types_1.TypeBase.isInstance(type)) {
1731
- allowUnknownTypeArgsForClasses = false;
1732
- }
1733
- if (!allowUnknownTypeArgsForClasses && !types_1.ClassType.isPseudoGenericClass(type)) {
1734
- const typeArgs = ((_b = type.tupleTypeArguments) === null || _b === void 0 ? void 0 : _b.map((t) => t.type)) || type.typeArguments;
1735
- if (typeArgs) {
1736
- for (const argType of typeArgs) {
1737
- if (isPartlyUnknown(argType, allowUnknownTypeArgsForClasses, recursionCount)) {
1738
- return true;
1739
- }
1740
- }
1678
+ visitClass(type) {
1679
+ if (this._recurse) {
1680
+ super.visitClass(type);
1741
1681
  }
1742
1682
  }
1743
- return false;
1744
- }
1745
- // See if a function has an unknown type.
1746
- if ((0, types_1.isOverloadedFunction)(type)) {
1747
- return types_1.OverloadedFunctionType.getOverloads(type).some((overload) => {
1748
- return isPartlyUnknown(overload, /* allowUnknownTypeArgsForClasses */ false, recursionCount);
1749
- });
1750
- }
1751
- if ((0, types_1.isFunction)(type)) {
1752
- for (let i = 0; i < type.details.parameters.length; i++) {
1753
- // Ignore parameters such as "*" that have no name.
1754
- if (type.details.parameters[i].name) {
1755
- const paramType = types_1.FunctionType.getEffectiveParameterType(type, i);
1756
- if (isPartlyUnknown(paramType, /* allowUnknownTypeArgsForClasses */ false, recursionCount)) {
1757
- return true;
1758
- }
1683
+ visitFunction(type) {
1684
+ if (this._recurse) {
1685
+ super.visitFunction(type);
1759
1686
  }
1760
1687
  }
1761
- if (type.details.declaredReturnType &&
1762
- !types_1.FunctionType.isParamSpecValue(type) &&
1763
- isPartlyUnknown(type.details.declaredReturnType, /* allowUnknownTypeArgsForClasses */ false, recursionCount)) {
1764
- return true;
1688
+ }
1689
+ const walker = new AnyOrUnknownWalker(recurse);
1690
+ walker.walk(type);
1691
+ return walker.anyOrUnknownType;
1692
+ }
1693
+ exports.containsAnyOrUnknown = containsAnyOrUnknown;
1694
+ // Determines if any part of the type contains "Unknown", including any type arguments.
1695
+ function isPartlyUnknown(type, allowUnknownTypeArgsForClasses = false) {
1696
+ class PartlyUnknownWalker extends typeWalker_1.TypeWalker {
1697
+ constructor(_allowUnknownTypeArgsForClasses) {
1698
+ super();
1699
+ this._allowUnknownTypeArgsForClasses = _allowUnknownTypeArgsForClasses;
1700
+ this.isPartlyUnknown = false;
1701
+ }
1702
+ visitUnknown(type) {
1703
+ this.isPartlyUnknown = true;
1704
+ // No need to keep walking.
1705
+ this.cancelWalk();
1706
+ }
1707
+ visitClass(type) {
1708
+ if (!this._allowUnknownTypeArgsForClasses || !types_1.TypeBase.isInstantiable(type)) {
1709
+ super.visitClass(type);
1710
+ }
1765
1711
  }
1766
- return false;
1767
1712
  }
1768
- return false;
1713
+ const walker = new PartlyUnknownWalker(allowUnknownTypeArgsForClasses);
1714
+ walker.walk(type);
1715
+ return walker.isPartlyUnknown;
1769
1716
  }
1770
1717
  exports.isPartlyUnknown = isPartlyUnknown;
1771
1718
  // If the specified type is a generic class with a single type argument
@@ -1853,6 +1800,31 @@ function specializeTupleClass(classType, typeArgs, isTypeArgumentExplicit = true
1853
1800
  return clonedClassType;
1854
1801
  }
1855
1802
  exports.specializeTupleClass = specializeTupleClass;
1803
+ // If the array contains two or more class types that are all the same
1804
+ // generic type, return the common "erased" type — a type that
1805
+ // replaces all of the type arguments with Unknown.
1806
+ function getCommonErasedType(types) {
1807
+ if (types.length < 2) {
1808
+ return undefined;
1809
+ }
1810
+ if (!(0, types_1.isClass)(types[0])) {
1811
+ return undefined;
1812
+ }
1813
+ for (let i = 1; i < types.length; i++) {
1814
+ const candidate = types[i];
1815
+ if (!(0, types_1.isClass)(candidate) ||
1816
+ types_1.TypeBase.isInstance(candidate) !== types_1.TypeBase.isInstance(types[0]) ||
1817
+ !types_1.ClassType.isSameGenericClass(types[0], candidate)) {
1818
+ return undefined;
1819
+ }
1820
+ }
1821
+ if (isTupleClass(types[0])) {
1822
+ return specializeTupleClass(types[0], [{ type: types_1.UnknownType.create(), isUnbounded: true }]);
1823
+ }
1824
+ return types_1.ClassType.cloneForSpecialization(types[0], types[0].details.typeParameters.map((t) => types_1.UnknownType.create()),
1825
+ /* isTypeArgumentExplicit */ true);
1826
+ }
1827
+ exports.getCommonErasedType = getCommonErasedType;
1856
1828
  // If the type is a function or overloaded function that has a paramSpec
1857
1829
  // associated with it and P.args and P.kwargs at the end of the signature,
1858
1830
  // it removes these parameters from the function.
@@ -1879,8 +1851,8 @@ function removeParamSpecVariadicsFromFunction(type) {
1879
1851
  }
1880
1852
  const argsParam = type.details.parameters[paramCount - 2];
1881
1853
  const kwargsParam = type.details.parameters[paramCount - 1];
1882
- if (argsParam.category !== 1 /* VarArgList */ ||
1883
- kwargsParam.category !== 2 /* VarArgDictionary */ ||
1854
+ if (argsParam.category !== 1 /* ArgsList */ ||
1855
+ kwargsParam.category !== 2 /* KwargsDict */ ||
1884
1856
  !(0, types_1.isParamSpec)(argsParam.type) ||
1885
1857
  !(0, types_1.isParamSpec)(kwargsParam.type) ||
1886
1858
  !(0, types_1.isTypeSame)(argsParam.type, kwargsParam.type)) {
@@ -1956,13 +1928,30 @@ function requiresSpecialization(type, ignorePseudoGeneric = false, ignoreSelf =
1956
1928
  return false;
1957
1929
  }
1958
1930
  recursionCount++;
1931
+ // Is the answer cached?
1932
+ const canUseCache = !ignorePseudoGeneric && !ignoreSelf;
1933
+ if (canUseCache && ((_a = type.cached) === null || _a === void 0 ? void 0 : _a.requiresSpecialization) !== undefined) {
1934
+ return type.cached.requiresSpecialization;
1935
+ }
1936
+ const result = _requiresSpecialization(type, ignorePseudoGeneric, ignoreSelf, recursionCount);
1937
+ if (canUseCache) {
1938
+ if (type.cached === undefined) {
1939
+ type.cached = {};
1940
+ }
1941
+ type.cached.requiresSpecialization = result;
1942
+ }
1943
+ return result;
1944
+ }
1945
+ exports.requiresSpecialization = requiresSpecialization;
1946
+ function _requiresSpecialization(type, ignorePseudoGeneric = false, ignoreSelf = false, recursionCount = 0) {
1947
+ var _a;
1959
1948
  switch (type.category) {
1960
1949
  case 7 /* Class */: {
1961
1950
  if (types_1.ClassType.isPseudoGenericClass(type) && ignorePseudoGeneric) {
1962
1951
  return false;
1963
1952
  }
1964
1953
  if (type.typeArguments) {
1965
- return (type.typeArguments.find((typeArg) => requiresSpecialization(typeArg, ignorePseudoGeneric, ignoreSelf, recursionCount)) !== undefined);
1954
+ return type.typeArguments.some((typeArg) => requiresSpecialization(typeArg, ignorePseudoGeneric, ignoreSelf, recursionCount));
1966
1955
  }
1967
1956
  return types_1.ClassType.getTypeParameters(type).length > 0;
1968
1957
  }
@@ -1991,10 +1980,10 @@ function requiresSpecialization(type, ignorePseudoGeneric = false, ignoreSelf =
1991
1980
  return false;
1992
1981
  }
1993
1982
  case 6 /* OverloadedFunction */: {
1994
- return (type.overloads.find((overload) => requiresSpecialization(overload, ignorePseudoGeneric, ignoreSelf, recursionCount)) !== undefined);
1983
+ return type.overloads.some((overload) => requiresSpecialization(overload, ignorePseudoGeneric, ignoreSelf, recursionCount));
1995
1984
  }
1996
1985
  case 9 /* Union */: {
1997
- return ((0, types_1.findSubtype)(type, (subtype) => requiresSpecialization(subtype, ignorePseudoGeneric, ignoreSelf, recursionCount)) !== undefined);
1986
+ return type.subtypes.some((subtype) => requiresSpecialization(subtype, ignorePseudoGeneric, ignoreSelf, recursionCount));
1998
1987
  }
1999
1988
  case 10 /* TypeVar */: {
2000
1989
  // Most TypeVar types need to be specialized.
@@ -2013,7 +2002,6 @@ function requiresSpecialization(type, ignorePseudoGeneric = false, ignoreSelf =
2013
2002
  }
2014
2003
  return false;
2015
2004
  }
2016
- exports.requiresSpecialization = requiresSpecialization;
2017
2005
  // Combines two variances to produce a resulting variance.
2018
2006
  function combineVariances(variance1, variance2) {
2019
2007
  if (variance1 === 1 /* Unknown */) {
@@ -2141,17 +2129,8 @@ function computeMroLinearization(classType) {
2141
2129
  // entries of the specified class. This is used once the class has been
2142
2130
  // added to the MRO.
2143
2131
  function filterClass(classToFilter, classLists) {
2144
- if ((classToFilter === null || classToFilter === void 0 ? void 0 : classToFilter.category) !== 7 /* Class */) {
2145
- // These "fail" calls are in place to diagnose a sporadic crash that
2146
- // we are seeing in the pylance telemetry. Delete these once we
2147
- // fix the underlying problem.
2148
- (0, debug_1.fail)(`Corrupted class type when computing MRO for ${classType.details.name}: ${JSON.stringify(classToFilter)}`);
2149
- }
2150
2132
  for (let i = 0; i < classLists.length; i++) {
2151
2133
  classLists[i] = classLists[i].filter((value) => {
2152
- if ((value === null || value === void 0 ? void 0 : value.category) === undefined) {
2153
- (0, debug_1.fail)(`Unexpected undefined type when computing MRO for ${classType.details.name}: ${JSON.stringify(classLists)}`);
2154
- }
2155
2134
  return !(0, types_1.isInstantiableClass)(value) || !types_1.ClassType.isSameGenericClass(value, classToFilter);
2156
2135
  });
2157
2136
  }
@@ -2283,7 +2262,7 @@ function convertParamSpecValueToType(paramSpecValue, omitParamSpec = false) {
2283
2262
  // If the ParamSpec has a position-only separator as its only parameter,
2284
2263
  // treat it as though there are no parameters.
2285
2264
  const onlyParam = paramSpecValue.details.parameters[0];
2286
- if (onlyParam.category === 0 /* Simple */ && !onlyParam.name) {
2265
+ if ((0, types_1.isPositionOnlySeparator)(onlyParam)) {
2287
2266
  hasParameters = false;
2288
2267
  }
2289
2268
  }
@@ -2480,14 +2459,14 @@ class TypeVarTransformer {
2480
2459
  : type;
2481
2460
  }
2482
2461
  transformTypeVarsInClassType(classType, recursionCount) {
2462
+ const typeParams = types_1.ClassType.getTypeParameters(classType);
2483
2463
  // Handle the common case where the class has no type parameters.
2484
- if (types_1.ClassType.getTypeParameters(classType).length === 0 && !types_1.ClassType.isSpecialBuiltIn(classType)) {
2464
+ if (typeParams.length === 0 && !types_1.ClassType.isSpecialBuiltIn(classType)) {
2485
2465
  return classType;
2486
2466
  }
2487
2467
  let newTypeArgs;
2488
2468
  let newTupleTypeArgs;
2489
2469
  let specializationNeeded = false;
2490
- const typeParams = types_1.ClassType.getTypeParameters(classType);
2491
2470
  const transformParamSpec = (paramSpec) => {
2492
2471
  const paramSpecValue = this.transformParamSpec(paramSpec, recursionCount);
2493
2472
  if (paramSpecValue) {
@@ -2631,8 +2610,8 @@ class TypeVarTransformer {
2631
2610
  const kwargsParam = functionType.details.parameters[functionType.details.parameters.length - 1];
2632
2611
  const argsParamType = types_1.FunctionType.getEffectiveParameterType(functionType, functionType.details.parameters.length - 2);
2633
2612
  const kwargsParamType = types_1.FunctionType.getEffectiveParameterType(functionType, functionType.details.parameters.length - 1);
2634
- if (argsParam.category === 1 /* VarArgList */ &&
2635
- kwargsParam.category === 2 /* VarArgDictionary */ &&
2613
+ if (argsParam.category === 1 /* ArgsList */ &&
2614
+ kwargsParam.category === 2 /* KwargsDict */ &&
2636
2615
  (0, types_1.isParamSpec)(argsParamType) &&
2637
2616
  (0, types_1.isParamSpec)(kwargsParamType) &&
2638
2617
  (0, types_1.isTypeSame)(argsParamType, kwargsParamType)) {
@@ -2667,7 +2646,7 @@ class TypeVarTransformer {
2667
2646
  specializedDefaultArgs.push(defaultArgType);
2668
2647
  if (variadicParamIndex === undefined &&
2669
2648
  (0, types_1.isVariadicTypeVar)(paramType) &&
2670
- functionType.details.parameters[i].category === 1 /* VarArgList */) {
2649
+ functionType.details.parameters[i].category === 1 /* ArgsList */) {
2671
2650
  variadicParamIndex = i;
2672
2651
  if ((0, types_1.isClassInstance)(specializedType) &&
2673
2652
  isTupleClass(specializedType) &&
@@ -2707,9 +2686,7 @@ class TypeVarTransformer {
2707
2686
  // Unpack the tuple into individual parameters.
2708
2687
  variadicTypesToUnpack.forEach((unpackedType) => {
2709
2688
  types_1.FunctionType.addParameter(newFunctionType, {
2710
- category: unpackedType.isUnbounded
2711
- ? 1 /* VarArgList */
2712
- : 0 /* Simple */,
2689
+ category: unpackedType.isUnbounded ? 1 /* ArgsList */ : 0 /* Simple */,
2713
2690
  name: `__p${newFunctionType.details.parameters.length}`,
2714
2691
  isNameSynthesized: true,
2715
2692
  type: unpackedType.type,
@@ -2728,17 +2705,17 @@ class TypeVarTransformer {
2728
2705
  }
2729
2706
  else {
2730
2707
  const param = { ...functionType.details.parameters[index] };
2731
- if (param.category === 1 /* VarArgList */ && !param.name) {
2708
+ if ((0, types_1.isKeywordOnlySeparator)(param)) {
2732
2709
  insertKeywordOnlySeparator = false;
2733
2710
  }
2734
- else if (param.category === 2 /* VarArgDictionary */) {
2711
+ else if (param.category === 2 /* KwargsDict */) {
2735
2712
  insertKeywordOnlySeparator = false;
2736
2713
  }
2737
2714
  // Insert a keyword-only separator parameter if we previously
2738
2715
  // unpacked a variadic TypeVar.
2739
2716
  if (param.category === 0 /* Simple */ && param.name && insertKeywordOnlySeparator) {
2740
2717
  types_1.FunctionType.addParameter(newFunctionType, {
2741
- category: 1 /* VarArgList */,
2718
+ category: 1 /* ArgsList */,
2742
2719
  type: types_1.UnknownType.create(),
2743
2720
  });
2744
2721
  insertKeywordOnlySeparator = false;
@@ -2797,10 +2774,14 @@ class UniqueFunctionSignatureTransformer extends TypeVarTransformer {
2797
2774
  // Create new type variables with the same scope but with
2798
2775
  // different (unique) names.
2799
2776
  sourceType.details.typeParameters.forEach((typeParam) => {
2800
- const replacement = types_1.TypeVarType.cloneForNewName(typeParam, `${typeParam.details.name}(${existingSignature.count})`);
2777
+ let replacement = types_1.TypeVarType.cloneForNewName(typeParam, `${typeParam.details.name}(${existingSignature.count})`);
2778
+ if (replacement.details.isParamSpec) {
2779
+ replacement = convertTypeToParamSpecValue(replacement);
2780
+ }
2801
2781
  typeVarContext.setTypeVarType(typeParam, replacement);
2802
- updatedSourceType = applySolvedTypeVars(sourceType, typeVarContext);
2803
2782
  });
2783
+ updatedSourceType = applySolvedTypeVars(sourceType, typeVarContext);
2784
+ (0, debug_1.assert)((0, types_1.isFunction)(updatedSourceType) || (0, types_1.isOverloadedFunction)(updatedSourceType));
2804
2785
  }
2805
2786
  this._signatureTracker.addSignature(sourceType);
2806
2787
  return updatedSourceType;
@@ -2816,7 +2797,7 @@ class ApplySolvedTypeVarsTransformer extends TypeVarTransformer {
2816
2797
  this._isSolvingDefaultType = false;
2817
2798
  }
2818
2799
  transformTypeVar(typeVar, recursionCount) {
2819
- var _a;
2800
+ var _a, _b;
2820
2801
  const signatureContext = this._typeVarContext.getSignatureContext((_a = this._activeTypeVarSignatureContextIndex) !== null && _a !== void 0 ? _a : 0);
2821
2802
  // If the type variable is unrelated to the scopes we're solving,
2822
2803
  // don't transform that type variable.
@@ -2852,7 +2833,11 @@ class ApplySolvedTypeVarsTransformer extends TypeVarTransformer {
2852
2833
  // default or Unknown.
2853
2834
  let useDefaultOrUnknown = false;
2854
2835
  if (this._options.unknownIfNotFound && !this._typeVarContext.hasSolveForScope(types_1.WildcardTypeVarScopeId)) {
2855
- useDefaultOrUnknown = true;
2836
+ const exemptTypeVars = (_b = this._options.unknownExemptTypeVars) !== null && _b !== void 0 ? _b : [];
2837
+ const typeVarInstance = types_1.TypeBase.isInstance(typeVar) ? typeVar : types_1.TypeVarType.cloneAsInstance(typeVar);
2838
+ if (!exemptTypeVars.some((t) => (0, types_1.isTypeSame)(t, typeVarInstance))) {
2839
+ useDefaultOrUnknown = true;
2840
+ }
2856
2841
  }
2857
2842
  else if (this._options.applyInScopePlaceholders && typeVar.isInScopePlaceholder) {
2858
2843
  useDefaultOrUnknown = true;
@@ -2907,7 +2892,7 @@ class ApplySolvedTypeVarsTransformer extends TypeVarTransformer {
2907
2892
  const signatureContext = this._typeVarContext.getSignatureContext((_a = this._activeTypeVarSignatureContextIndex) !== null && _a !== void 0 ? _a : 0);
2908
2893
  const typeVarType = signatureContext.getTypeVarType(preTransform);
2909
2894
  // Did the TypeVar remain unsolved?
2910
- if (!typeVarType) {
2895
+ if (!typeVarType || ((0, types_1.isTypeVar)(typeVarType) && typeVarType.isInScopePlaceholder)) {
2911
2896
  // If the TypeVar was not transformed, then it was unsolved,
2912
2897
  // and we'll eliminate it.
2913
2898
  if (preTransform === postTransform) {
@@ -3012,19 +2997,20 @@ class ApplySolvedTypeVarsTransformer extends TypeVarTransformer {
3012
2997
  }
3013
2998
  }
3014
2999
  class ExpectedTypeTransformer extends TypeVarTransformer {
3015
- constructor(_liveTypeVarScopes) {
3000
+ constructor(_liveTypeVarScopes, _usageOffset) {
3016
3001
  super();
3017
3002
  this._liveTypeVarScopes = _liveTypeVarScopes;
3003
+ this._usageOffset = _usageOffset;
3018
3004
  }
3019
3005
  transformTypeVar(typeVar) {
3020
3006
  if (!this._isTypeVarLive(typeVar)) {
3021
- return types_1.TypeVarType.cloneAsInScopePlaceholder(typeVar);
3007
+ return types_1.TypeVarType.cloneAsInScopePlaceholder(typeVar, this._usageOffset);
3022
3008
  }
3023
3009
  return typeVar;
3024
3010
  }
3025
3011
  transformParamSpec(paramSpec) {
3026
3012
  if (!this._isTypeVarLive(paramSpec)) {
3027
- return convertTypeToParamSpecValue(types_1.TypeVarType.cloneAsInScopePlaceholder(paramSpec));
3013
+ return convertTypeToParamSpecValue(types_1.TypeVarType.cloneAsInScopePlaceholder(paramSpec, this._usageOffset));
3028
3014
  }
3029
3015
  return undefined;
3030
3016
  }
@@ -3040,7 +3026,7 @@ class InScopePlaceholderTransformer extends TypeVarTransformer {
3040
3026
  transformTypeVar(typeVar) {
3041
3027
  var _a;
3042
3028
  if (typeVar.isInScopePlaceholder) {
3043
- return (_a = this._signatureContext.getTypeVarType(typeVar)) !== null && _a !== void 0 ? _a : types_1.UnknownType.create();
3029
+ return (_a = this._signatureContext.getTypeVarType(typeVar)) !== null && _a !== void 0 ? _a : typeVar;
3044
3030
  }
3045
3031
  return typeVar;
3046
3032
  }