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

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 (109) hide show
  1. package/dist/analyzer/checker.js +53 -32
  2. package/dist/analyzer/checker.js.map +1 -1
  3. package/dist/analyzer/constraintSolver.js +17 -11
  4. package/dist/analyzer/constraintSolver.js.map +1 -1
  5. package/dist/analyzer/constructors.js +3 -2
  6. package/dist/analyzer/constructors.js.map +1 -1
  7. package/dist/analyzer/dataClasses.js +1 -1
  8. package/dist/analyzer/dataClasses.js.map +1 -1
  9. package/dist/analyzer/importResolver.d.ts +2 -0
  10. package/dist/analyzer/importResolver.js +57 -35
  11. package/dist/analyzer/importResolver.js.map +1 -1
  12. package/dist/analyzer/operations.js +2 -0
  13. package/dist/analyzer/operations.js.map +1 -1
  14. package/dist/analyzer/program.d.ts +2 -2
  15. package/dist/analyzer/program.js +12 -33
  16. package/dist/analyzer/program.js.map +1 -1
  17. package/dist/analyzer/protocols.d.ts +1 -1
  18. package/dist/analyzer/protocols.js +225 -205
  19. package/dist/analyzer/protocols.js.map +1 -1
  20. package/dist/analyzer/service.d.ts +2 -2
  21. package/dist/analyzer/service.js +15 -7
  22. package/dist/analyzer/service.js.map +1 -1
  23. package/dist/analyzer/typeEvaluator.js +272 -137
  24. package/dist/analyzer/typeEvaluator.js.map +1 -1
  25. package/dist/analyzer/typeEvaluatorTypes.d.ts +5 -2
  26. package/dist/analyzer/typeEvaluatorTypes.js +2 -0
  27. package/dist/analyzer/typeEvaluatorTypes.js.map +1 -1
  28. package/dist/analyzer/typeGuards.js +1 -1
  29. package/dist/analyzer/typeGuards.js.map +1 -1
  30. package/dist/analyzer/typeUtils.d.ts +20 -21
  31. package/dist/analyzer/typeUtils.js +162 -107
  32. package/dist/analyzer/typeUtils.js.map +1 -1
  33. package/dist/analyzer/types.d.ts +2 -0
  34. package/dist/analyzer/types.js +7 -3
  35. package/dist/analyzer/types.js.map +1 -1
  36. package/dist/backgroundAnalysisBase.d.ts +1 -1
  37. package/dist/backgroundAnalysisBase.js +2 -2
  38. package/dist/backgroundAnalysisBase.js.map +1 -1
  39. package/dist/backgroundThreadBase.d.ts +12 -9
  40. package/dist/backgroundThreadBase.js +28 -20
  41. package/dist/backgroundThreadBase.js.map +1 -1
  42. package/dist/common/charCodes.d.ts +147 -0
  43. package/dist/common/charCodes.js +164 -0
  44. package/dist/common/charCodes.js.map +1 -0
  45. package/dist/common/extensibility.d.ts +6 -2
  46. package/dist/common/extensibility.js.map +1 -1
  47. package/dist/common/fileSystem.d.ts +1 -0
  48. package/dist/common/fileSystem.js.map +1 -1
  49. package/dist/common/pathUtils.js.map +1 -1
  50. package/dist/common/realFileSystem.js +3 -0
  51. package/dist/common/realFileSystem.js.map +1 -1
  52. package/dist/common/workspaceEditUtils.d.ts +3 -3
  53. package/dist/common/workspaceEditUtils.js +23 -13
  54. package/dist/common/workspaceEditUtils.js.map +1 -1
  55. package/dist/languageServerBase.d.ts +51 -3
  56. package/dist/languageServerBase.js +5 -5
  57. package/dist/languageServerBase.js.map +1 -1
  58. package/dist/languageService/completionProvider.js +2 -0
  59. package/dist/languageService/completionProvider.js.map +1 -1
  60. package/dist/languageService/definitionProvider.d.ts +2 -0
  61. package/dist/languageService/definitionProvider.js +82 -74
  62. package/dist/languageService/definitionProvider.js.map +1 -1
  63. package/dist/languageService/hoverProvider.d.ts +16 -8
  64. package/dist/languageService/hoverProvider.js +53 -53
  65. package/dist/languageService/hoverProvider.js.map +1 -1
  66. package/dist/localization/localize.d.ts +7 -6
  67. package/dist/localization/localize.js +3 -3
  68. package/dist/localization/localize.js.map +1 -1
  69. package/dist/localization/package.nls.en-us.json +3 -3
  70. package/dist/parser/characterStream.js.map +1 -1
  71. package/dist/parser/characters.js.map +1 -1
  72. package/dist/parser/parser.js +5 -2
  73. package/dist/parser/parser.js.map +1 -1
  74. package/dist/parser/stringTokenUtils.js.map +1 -1
  75. package/dist/parser/tokenizer.js +10 -1
  76. package/dist/parser/tokenizer.js.map +1 -1
  77. package/dist/pyright.js +1 -1
  78. package/dist/pyright.js.map +1 -1
  79. package/dist/pyrightFileSystem.d.ts +1 -0
  80. package/dist/pyrightFileSystem.js +3 -0
  81. package/dist/pyrightFileSystem.js.map +1 -1
  82. package/dist/readonlyAugmentedFileSystem.d.ts +1 -0
  83. package/dist/readonlyAugmentedFileSystem.js +3 -0
  84. package/dist/readonlyAugmentedFileSystem.js.map +1 -1
  85. package/dist/tests/checker.test.js +1 -1
  86. package/dist/tests/harness/fourslash/testLanguageService.d.ts +1 -0
  87. package/dist/tests/harness/fourslash/testLanguageService.js +3 -0
  88. package/dist/tests/harness/fourslash/testLanguageService.js.map +1 -1
  89. package/dist/tests/harness/fourslash/testState.js +2 -2
  90. package/dist/tests/harness/fourslash/testState.js.map +1 -1
  91. package/dist/tests/harness/vfs/filesystem.d.ts +12 -1
  92. package/dist/tests/harness/vfs/filesystem.js +33 -6
  93. package/dist/tests/harness/vfs/filesystem.js.map +1 -1
  94. package/dist/tests/tokenizer.test.js +13 -2
  95. package/dist/tests/tokenizer.test.js.map +1 -1
  96. package/dist/tests/typeEvaluator1.test.js +5 -1
  97. package/dist/tests/typeEvaluator1.test.js.map +1 -1
  98. package/dist/tests/typeEvaluator2.test.js +17 -1
  99. package/dist/tests/typeEvaluator2.test.js.map +1 -1
  100. package/dist/tests/typeEvaluator3.test.js +10 -2
  101. package/dist/tests/typeEvaluator3.test.js.map +1 -1
  102. package/dist/tests/typeEvaluator4.test.js +1 -1
  103. package/dist/tests/typeEvaluator5.test.js +1 -1
  104. package/dist/tests/workspaceEditUtils.test.js +2 -3
  105. package/dist/tests/workspaceEditUtils.test.js.map +1 -1
  106. package/dist/workspaceFactory.d.ts +2 -0
  107. package/dist/workspaceFactory.js +11 -2
  108. package/dist/workspaceFactory.js.map +1 -1
  109. package/package.json +1 -2
@@ -9,7 +9,7 @@
9
9
  */
10
10
  Object.defineProperty(exports, "__esModule", { value: true });
11
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;
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.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");
@@ -43,18 +43,14 @@ var ClassMemberLookupFlags;
43
43
  var ClassIteratorFlags;
44
44
  (function (ClassIteratorFlags) {
45
45
  ClassIteratorFlags[ClassIteratorFlags["Default"] = 0] = "Default";
46
- // By default, the original (derived) class is searched along
47
- // with its base classes. If this flag is set, the original
48
- // class is skipped and only the base classes are searched.
49
- ClassIteratorFlags[ClassIteratorFlags["SkipOriginalClass"] = 1] = "SkipOriginalClass";
50
46
  // By default, base classes are searched as well as the
51
47
  // original (derived) class. If this flag is set, no recursion
52
48
  // is performed.
53
- ClassIteratorFlags[ClassIteratorFlags["SkipBaseClasses"] = 2] = "SkipBaseClasses";
49
+ ClassIteratorFlags[ClassIteratorFlags["SkipBaseClasses"] = 1] = "SkipBaseClasses";
54
50
  // Skip the 'object' base class in particular.
55
- ClassIteratorFlags[ClassIteratorFlags["SkipObjectBaseClass"] = 4] = "SkipObjectBaseClass";
51
+ ClassIteratorFlags[ClassIteratorFlags["SkipObjectBaseClass"] = 2] = "SkipObjectBaseClass";
56
52
  // Skip the 'type' base class in particular.
57
- ClassIteratorFlags[ClassIteratorFlags["SkipTypeBaseClass"] = 8] = "SkipTypeBaseClass";
53
+ ClassIteratorFlags[ClassIteratorFlags["SkipTypeBaseClass"] = 4] = "SkipTypeBaseClass";
58
54
  })(ClassIteratorFlags = exports.ClassIteratorFlags || (exports.ClassIteratorFlags = {}));
59
55
  var AssignTypeFlags;
60
56
  (function (AssignTypeFlags) {
@@ -100,22 +96,38 @@ var AssignTypeFlags;
100
96
  // employing narrowing or widening, and don't strip literals.
101
97
  AssignTypeFlags[AssignTypeFlags["PopulatingExpectedType"] = 1024] = "PopulatingExpectedType";
102
98
  })(AssignTypeFlags = exports.AssignTypeFlags || (exports.AssignTypeFlags = {}));
99
+ // Tracks whether a function signature has been seen before within
100
+ // an expression. For example, in the expression "foo(foo, foo)", the
101
+ // signature for "foo" will be seen three times at three different
102
+ // file offsets. If the signature is generic, we need to create unique
103
+ // type variables for each instance because they are independent of
104
+ // each other.
103
105
  class UniqueSignatureTracker {
104
106
  constructor() {
105
- this.signaturesSeen = [];
107
+ this._signaturesSeen = [];
106
108
  }
107
109
  findSignature(signature) {
108
- return this.signaturesSeen.find((s) => {
109
- return (0, types_1.isTypeSame)(signature, s.type);
110
+ // Use the associated overload type if this is a function associated with an overload.
111
+ let effectiveSignature = signature;
112
+ if ((0, types_1.isFunction)(signature) && signature.overloaded) {
113
+ effectiveSignature = signature.overloaded;
114
+ }
115
+ return this._signaturesSeen.find((s) => {
116
+ return (0, types_1.isTypeSame)(effectiveSignature, s.type);
110
117
  });
111
118
  }
112
- addSignature(signature) {
113
- const existingSignature = this.findSignature(signature);
119
+ addSignature(signature, offset) {
120
+ var _a;
121
+ // If this function is part of a broader overload, use the overload instead.
122
+ const effectiveSignature = (_a = signature.overloaded) !== null && _a !== void 0 ? _a : signature;
123
+ const existingSignature = this.findSignature(effectiveSignature);
114
124
  if (existingSignature) {
115
- existingSignature.count++;
125
+ if (!existingSignature.expressionOffsets.some((o) => o === offset)) {
126
+ existingSignature.expressionOffsets.push(offset);
127
+ }
116
128
  }
117
129
  else {
118
- this.signaturesSeen.push({ type: signature, count: 1 });
130
+ this._signaturesSeen.push({ type: effectiveSignature, expressionOffsets: [offset] });
119
131
  }
120
132
  }
121
133
  }
@@ -161,11 +173,11 @@ function isTypeVarSame(type1, type2) {
161
173
  return isCompatible;
162
174
  }
163
175
  exports.isTypeVarSame = isTypeVarSame;
164
- function makeInferenceContext(expectedType, isTypeIncomplete) {
176
+ function makeInferenceContext(expectedType, isTypeIncomplete, signatureTracker) {
165
177
  if (!expectedType) {
166
178
  return undefined;
167
179
  }
168
- return { expectedType, isTypeIncomplete };
180
+ return { expectedType, isTypeIncomplete, signatureTracker };
169
181
  }
170
182
  exports.makeInferenceContext = makeInferenceContext;
171
183
  // Calls a callback for each subtype and combines the results
@@ -712,7 +724,7 @@ function partiallySpecializeType(type, contextClassType, selfClass, typeClassTyp
712
724
  return type;
713
725
  }
714
726
  // Partially specialize the type using the specialized class type vars.
715
- const typeVarContext = buildTypeVarContextFromSpecializedClass(contextClassType, /* makeConcrete */ undefined);
727
+ const typeVarContext = buildTypeVarContextFromSpecializedClass(contextClassType);
716
728
  if (selfClass) {
717
729
  populateTypeVarContextForSelfType(typeVarContext, contextClassType, selfClass);
718
730
  }
@@ -726,8 +738,8 @@ function populateTypeVarContextForSelfType(typeVarContext, contextClassType, sel
726
738
  exports.populateTypeVarContextForSelfType = populateTypeVarContextForSelfType;
727
739
  // Looks for duplicate function types within the type and ensures that
728
740
  // if they are generic, they have unique type variables.
729
- function ensureFunctionSignaturesAreUnique(type, signatureTracker) {
730
- const transformer = new UniqueFunctionSignatureTransformer(signatureTracker);
741
+ function ensureFunctionSignaturesAreUnique(type, signatureTracker, expressionOffset) {
742
+ const transformer = new UniqueFunctionSignatureTransformer(signatureTracker, expressionOffset);
731
743
  return transformer.apply(type, 0);
732
744
  }
733
745
  exports.ensureFunctionSignaturesAreUnique = ensureFunctionSignaturesAreUnique;
@@ -895,18 +907,18 @@ function getContainerDepth(type, recursionCount = 0) {
895
907
  return 1 + maxChildDepth;
896
908
  }
897
909
  exports.getContainerDepth = getContainerDepth;
898
- function lookUpObjectMember(objectType, memberName, flags = 0 /* Default */) {
910
+ function lookUpObjectMember(objectType, memberName, flags = 0 /* Default */, skipMroClass) {
899
911
  if ((0, types_1.isClassInstance)(objectType)) {
900
- return lookUpClassMember(objectType, memberName, flags);
912
+ return lookUpClassMember(objectType, memberName, flags, skipMroClass);
901
913
  }
902
914
  return undefined;
903
915
  }
904
916
  exports.lookUpObjectMember = lookUpObjectMember;
905
917
  // Looks up a member in a class using the multiple-inheritance rules
906
918
  // defined by Python.
907
- function lookUpClassMember(classType, memberName, flags = 0 /* Default */) {
919
+ function lookUpClassMember(classType, memberName, flags = 0 /* Default */, skipMroClass) {
908
920
  var _a;
909
- const memberItr = getClassMemberIterator(classType, memberName, flags);
921
+ const memberItr = getClassMemberIterator(classType, memberName, flags, skipMroClass);
910
922
  return (_a = memberItr.next()) === null || _a === void 0 ? void 0 : _a.value;
911
923
  }
912
924
  exports.lookUpClassMember = lookUpClassMember;
@@ -918,24 +930,28 @@ exports.lookUpClassMember = lookUpClassMember;
918
930
  // ClassB[str] which inherits from Dict[_T1, int], a search for '__iter__'
919
931
  // would return a class type of Dict[str, int] and a symbolType of
920
932
  // (self) -> Iterator[str].
921
- function* getClassMemberIterator(classType, memberName, flags = 0 /* Default */) {
933
+ // If skipMroClass is defined, all MRO classes up to and including that class
934
+ // are skipped.
935
+ function* getClassMemberIterator(classType, memberName, flags = 0 /* Default */, skipMroClass) {
922
936
  const declaredTypesOnly = (flags & 16 /* DeclaredTypesOnly */) !== 0;
923
937
  let skippedUndeclaredType = false;
924
938
  if ((0, types_1.isClass)(classType)) {
925
939
  let classFlags = 0 /* Default */;
926
940
  if (flags & 1 /* SkipOriginalClass */) {
927
- classFlags = classFlags | 1 /* SkipOriginalClass */;
941
+ if ((0, types_1.isClass)(classType)) {
942
+ skipMroClass = classType;
943
+ }
928
944
  }
929
945
  if (flags & 2 /* SkipBaseClasses */) {
930
- classFlags = classFlags | 2 /* SkipBaseClasses */;
946
+ classFlags = classFlags | 1 /* SkipBaseClasses */;
931
947
  }
932
948
  if (flags & 4 /* SkipObjectBaseClass */) {
933
- classFlags = classFlags | 4 /* SkipObjectBaseClass */;
949
+ classFlags = classFlags | 2 /* SkipObjectBaseClass */;
934
950
  }
935
951
  if (flags & 32 /* SkipTypeBaseClass */) {
936
- classFlags = classFlags | 8 /* SkipTypeBaseClass */;
952
+ classFlags = classFlags | 4 /* SkipTypeBaseClass */;
937
953
  }
938
- const classItr = getClassIterator(classType, classFlags);
954
+ const classItr = getClassIterator(classType, classFlags, skipMroClass);
939
955
  for (const [mroClass, specializedMroClass] of classItr) {
940
956
  if (!(0, types_1.isInstantiableClass)(mroClass)) {
941
957
  if (!declaredTypesOnly) {
@@ -1035,19 +1051,28 @@ function* getClassMemberIterator(classType, memberName, flags = 0 /* Default */)
1035
1051
  return undefined;
1036
1052
  }
1037
1053
  exports.getClassMemberIterator = getClassMemberIterator;
1038
- function* getClassIterator(classType, flags = 0 /* Default */) {
1054
+ function* getClassIterator(classType, flags = 0 /* Default */, skipMroClass) {
1039
1055
  if ((0, types_1.isClass)(classType)) {
1040
- let skipMroEntry = (flags & 1 /* SkipOriginalClass */) !== 0;
1056
+ let foundSkipMroClass = skipMroClass === undefined;
1041
1057
  for (const mroClass of classType.details.mro) {
1042
- if (skipMroEntry) {
1043
- skipMroEntry = false;
1044
- continue;
1058
+ // Are we still searching fro teh skipMroClass?
1059
+ if (!foundSkipMroClass && skipMroClass) {
1060
+ if (!(0, types_1.isClass)(mroClass)) {
1061
+ foundSkipMroClass = true;
1062
+ }
1063
+ else if (types_1.ClassType.isSameGenericClass(mroClass, skipMroClass)) {
1064
+ foundSkipMroClass = true;
1065
+ continue;
1066
+ }
1067
+ else {
1068
+ continue;
1069
+ }
1045
1070
  }
1046
1071
  // If mroClass is an ancestor of classType, partially specialize
1047
1072
  // it in the context of classType.
1048
1073
  const specializedMroClass = partiallySpecializeType(mroClass, classType);
1049
1074
  // Should we ignore members on the 'object' base class?
1050
- if (flags & 4 /* SkipObjectBaseClass */) {
1075
+ if (flags & 2 /* SkipObjectBaseClass */) {
1051
1076
  if ((0, types_1.isInstantiableClass)(specializedMroClass)) {
1052
1077
  if (types_1.ClassType.isBuiltIn(specializedMroClass, 'object')) {
1053
1078
  break;
@@ -1055,7 +1080,7 @@ function* getClassIterator(classType, flags = 0 /* Default */) {
1055
1080
  }
1056
1081
  }
1057
1082
  // Should we ignore members on the 'type' base class?
1058
- if (flags & 8 /* SkipTypeBaseClass */) {
1083
+ if (flags & 4 /* SkipTypeBaseClass */) {
1059
1084
  if ((0, types_1.isInstantiableClass)(specializedMroClass)) {
1060
1085
  if (types_1.ClassType.isBuiltIn(specializedMroClass, 'type')) {
1061
1086
  break;
@@ -1063,7 +1088,7 @@ function* getClassIterator(classType, flags = 0 /* Default */) {
1063
1088
  }
1064
1089
  }
1065
1090
  yield [mroClass, specializedMroClass];
1066
- if ((flags & 2 /* SkipBaseClasses */) !== 0) {
1091
+ if ((flags & 1 /* SkipBaseClasses */) !== 0) {
1067
1092
  break;
1068
1093
  }
1069
1094
  }
@@ -1328,16 +1353,9 @@ exports.setTypeArgumentsRecursive = setTypeArgumentsRecursive;
1328
1353
  // types. For example, if the generic type is Dict[_T1, _T2] and the
1329
1354
  // specialized type is Dict[str, int], it returns a map that associates
1330
1355
  // _T1 with str and _T2 with int.
1331
- function buildTypeVarContextFromSpecializedClass(classType, makeConcrete = true) {
1356
+ function buildTypeVarContextFromSpecializedClass(classType) {
1332
1357
  const typeParameters = types_1.ClassType.getTypeParameters(classType);
1333
- let typeArguments = classType.typeArguments;
1334
- // If there are no type arguments, we can either use the type variables
1335
- // from the type parameters (keeping the type arguments generic) or
1336
- // fill in concrete types.
1337
- if (!typeArguments && !makeConcrete) {
1338
- typeArguments = typeParameters;
1339
- }
1340
- const typeVarContext = buildTypeVarContext(typeParameters, typeArguments, getTypeVarScopeId(classType));
1358
+ const typeVarContext = buildTypeVarContext(typeParameters, classType.typeArguments, getTypeVarScopeId(classType));
1341
1359
  if (types_1.ClassType.isTupleClass(classType) && classType.tupleTypeArguments && typeParameters.length >= 1) {
1342
1360
  typeVarContext.setTupleTypeVar(typeParameters[0], classType.tupleTypeArguments);
1343
1361
  }
@@ -1349,7 +1367,7 @@ function buildTypeVarContext(typeParameters, typeArgs, typeVarScopeId) {
1349
1367
  typeParameters.forEach((typeParam, index) => {
1350
1368
  let typeArgType;
1351
1369
  if (typeArgs) {
1352
- if ((0, types_1.isParamSpec)(typeParam)) {
1370
+ if (typeParam.details.isParamSpec) {
1353
1371
  if (index < typeArgs.length) {
1354
1372
  typeArgType = typeArgs[index];
1355
1373
  if ((0, types_1.isFunction)(typeArgType) && types_1.FunctionType.isParamSpecValue(typeArgType)) {
@@ -1692,27 +1710,71 @@ function containsAnyOrUnknown(type, recurse) {
1692
1710
  }
1693
1711
  exports.containsAnyOrUnknown = containsAnyOrUnknown;
1694
1712
  // 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;
1713
+ // This function does not use the TypeWalker because it is called very frequently,
1714
+ // and allocating a memory walker object for every call significantly increases
1715
+ // peak memory usage.
1716
+ function isPartlyUnknown(type, allowUnknownTypeArgsForClasses = false, recursionCount = 0) {
1717
+ var _a, _b;
1718
+ if (recursionCount > types_1.maxTypeRecursionCount) {
1719
+ return false;
1720
+ }
1721
+ recursionCount++;
1722
+ if ((0, types_1.isUnknown)(type)) {
1723
+ return true;
1724
+ }
1725
+ // If this is a generic type alias, see if any of its type arguments
1726
+ // are either unspecified or are partially known.
1727
+ if ((_a = type.typeAliasInfo) === null || _a === void 0 ? void 0 : _a.typeArguments) {
1728
+ if (type.typeAliasInfo.typeArguments.some((typeArg) => isPartlyUnknown(typeArg, allowUnknownTypeArgsForClasses, recursionCount))) {
1729
+ return true;
1701
1730
  }
1702
- visitUnknown(type) {
1703
- this.isPartlyUnknown = true;
1704
- // No need to keep walking.
1705
- this.cancelWalk();
1731
+ }
1732
+ // See if a union contains an unknown type.
1733
+ if ((0, types_1.isUnion)(type)) {
1734
+ return ((0, types_1.findSubtype)(type, (subtype) => isPartlyUnknown(subtype, allowUnknownTypeArgsForClasses, recursionCount)) !==
1735
+ undefined);
1736
+ }
1737
+ // See if an object or class has an unknown type argument.
1738
+ if ((0, types_1.isClass)(type)) {
1739
+ if (types_1.TypeBase.isInstance(type)) {
1740
+ allowUnknownTypeArgsForClasses = false;
1741
+ }
1742
+ if (!allowUnknownTypeArgsForClasses && !types_1.ClassType.isPseudoGenericClass(type)) {
1743
+ const typeArgs = ((_b = type.tupleTypeArguments) === null || _b === void 0 ? void 0 : _b.map((t) => t.type)) || type.typeArguments;
1744
+ if (typeArgs) {
1745
+ for (const argType of typeArgs) {
1746
+ if (isPartlyUnknown(argType, allowUnknownTypeArgsForClasses, recursionCount)) {
1747
+ return true;
1748
+ }
1749
+ }
1750
+ }
1706
1751
  }
1707
- visitClass(type) {
1708
- if (!this._allowUnknownTypeArgsForClasses || !types_1.TypeBase.isInstantiable(type)) {
1709
- super.visitClass(type);
1752
+ return false;
1753
+ }
1754
+ // See if a function has an unknown type.
1755
+ if ((0, types_1.isOverloadedFunction)(type)) {
1756
+ return types_1.OverloadedFunctionType.getOverloads(type).some((overload) => {
1757
+ return isPartlyUnknown(overload, /* allowUnknownTypeArgsForClasses */ false, recursionCount);
1758
+ });
1759
+ }
1760
+ if ((0, types_1.isFunction)(type)) {
1761
+ for (let i = 0; i < type.details.parameters.length; i++) {
1762
+ // Ignore parameters such as "*" that have no name.
1763
+ if (type.details.parameters[i].name) {
1764
+ const paramType = types_1.FunctionType.getEffectiveParameterType(type, i);
1765
+ if (isPartlyUnknown(paramType, /* allowUnknownTypeArgsForClasses */ false, recursionCount)) {
1766
+ return true;
1767
+ }
1710
1768
  }
1711
1769
  }
1770
+ if (type.details.declaredReturnType &&
1771
+ !types_1.FunctionType.isParamSpecValue(type) &&
1772
+ isPartlyUnknown(type.details.declaredReturnType, /* allowUnknownTypeArgsForClasses */ false, recursionCount)) {
1773
+ return true;
1774
+ }
1775
+ return false;
1712
1776
  }
1713
- const walker = new PartlyUnknownWalker(allowUnknownTypeArgsForClasses);
1714
- walker.walk(type);
1715
- return walker.isPartlyUnknown;
1777
+ return false;
1716
1778
  }
1717
1779
  exports.isPartlyUnknown = isPartlyUnknown;
1718
1780
  // If the specified type is a generic class with a single type argument
@@ -1800,31 +1862,6 @@ function specializeTupleClass(classType, typeArgs, isTypeArgumentExplicit = true
1800
1862
  return clonedClassType;
1801
1863
  }
1802
1864
  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;
1828
1865
  // If the type is a function or overloaded function that has a paramSpec
1829
1866
  // associated with it and P.args and P.kwargs at the end of the signature,
1830
1867
  // it removes these parameters from the function.
@@ -2101,7 +2138,7 @@ function computeMroLinearization(classType) {
2101
2138
  const classListsToMerge = [];
2102
2139
  filteredBaseClasses.forEach((baseClass) => {
2103
2140
  if ((0, types_1.isInstantiableClass)(baseClass)) {
2104
- const typeVarContext = buildTypeVarContextFromSpecializedClass(baseClass, /* makeConcrete */ false);
2141
+ const typeVarContext = buildTypeVarContextFromSpecializedClass(baseClass);
2105
2142
  classListsToMerge.push(baseClass.details.mro.map((mroClass) => {
2106
2143
  return applySolvedTypeVars(mroClass, typeVarContext);
2107
2144
  }));
@@ -2111,11 +2148,11 @@ function computeMroLinearization(classType) {
2111
2148
  }
2112
2149
  });
2113
2150
  classListsToMerge.push(filteredBaseClasses.map((baseClass) => {
2114
- const typeVarContext = buildTypeVarContextFromSpecializedClass(classType, /* makeConcrete */ false);
2151
+ const typeVarContext = buildTypeVarContextFromSpecializedClass(classType);
2115
2152
  return applySolvedTypeVars(baseClass, typeVarContext);
2116
2153
  }));
2117
2154
  // The first class in the MRO is the class itself.
2118
- const typeVarContext = buildTypeVarContextFromSpecializedClass(classType, /* makeConcrete */ false);
2155
+ const typeVarContext = buildTypeVarContextFromSpecializedClass(classType);
2119
2156
  classType.details.mro.push(applySolvedTypeVars(classType, typeVarContext));
2120
2157
  // Helper function that returns true if the specified searchClass
2121
2158
  // is found in the "tail" (i.e. in elements 1 through n) of any
@@ -2758,9 +2795,18 @@ class TypeVarDefaultValidator extends TypeVarTransformer {
2758
2795
  }
2759
2796
  }
2760
2797
  class UniqueFunctionSignatureTransformer extends TypeVarTransformer {
2761
- constructor(_signatureTracker) {
2798
+ constructor(_signatureTracker, _expressionOffset) {
2762
2799
  super();
2763
2800
  this._signatureTracker = _signatureTracker;
2801
+ this._expressionOffset = _expressionOffset;
2802
+ }
2803
+ transformGenericTypeAlias(type, recursionCount) {
2804
+ // Don't transform type aliases.
2805
+ return type;
2806
+ }
2807
+ transformTypeVarsInClassType(classType, recursionCount) {
2808
+ // Don't transform classes.
2809
+ return classType;
2764
2810
  }
2765
2811
  transformTypeVarsInFunctionType(sourceType, recursionCount) {
2766
2812
  // If this function is not generic, there's no need to check for uniqueness.
@@ -2771,19 +2817,25 @@ class UniqueFunctionSignatureTransformer extends TypeVarTransformer {
2771
2817
  const existingSignature = this._signatureTracker.findSignature(sourceType);
2772
2818
  if (existingSignature) {
2773
2819
  const typeVarContext = new typeVarContext_1.TypeVarContext(getTypeVarScopeId(sourceType));
2774
- // Create new type variables with the same scope but with
2775
- // different (unique) names.
2776
- sourceType.details.typeParameters.forEach((typeParam) => {
2777
- let replacement = types_1.TypeVarType.cloneForNewName(typeParam, `${typeParam.details.name}(${existingSignature.count})`);
2778
- if (replacement.details.isParamSpec) {
2779
- replacement = convertTypeToParamSpecValue(replacement);
2780
- }
2781
- typeVarContext.setTypeVarType(typeParam, replacement);
2782
- });
2783
- updatedSourceType = applySolvedTypeVars(sourceType, typeVarContext);
2784
- (0, debug_1.assert)((0, types_1.isFunction)(updatedSourceType) || (0, types_1.isOverloadedFunction)(updatedSourceType));
2820
+ let offsetIndex = existingSignature.expressionOffsets.findIndex((offset) => offset === this._expressionOffset);
2821
+ if (offsetIndex < 0) {
2822
+ offsetIndex = existingSignature.expressionOffsets.length;
2823
+ }
2824
+ if (offsetIndex > 0) {
2825
+ // Create new type variables with the same scope but with
2826
+ // different (unique) names.
2827
+ sourceType.details.typeParameters.forEach((typeParam) => {
2828
+ let replacement = types_1.TypeVarType.cloneForNewName(typeParam, `${typeParam.details.name}(${offsetIndex})`);
2829
+ if (replacement.details.isParamSpec) {
2830
+ replacement = convertTypeToParamSpecValue(replacement);
2831
+ }
2832
+ typeVarContext.setTypeVarType(typeParam, replacement);
2833
+ });
2834
+ updatedSourceType = applySolvedTypeVars(sourceType, typeVarContext);
2835
+ (0, debug_1.assert)((0, types_1.isFunction)(updatedSourceType) || (0, types_1.isOverloadedFunction)(updatedSourceType));
2836
+ }
2785
2837
  }
2786
- this._signatureTracker.addSignature(sourceType);
2838
+ this._signatureTracker.addSignature(sourceType, this._expressionOffset);
2787
2839
  return updatedSourceType;
2788
2840
  }
2789
2841
  }
@@ -2921,7 +2973,7 @@ class ApplySolvedTypeVarsTransformer extends TypeVarTransformer {
2921
2973
  return signatureContext.getTupleTypeVar(typeVar);
2922
2974
  }
2923
2975
  transformParamSpec(paramSpec, recursionCount) {
2924
- var _a;
2976
+ var _a, _b;
2925
2977
  const signatureContext = this._typeVarContext.getSignatureContext((_a = this._activeTypeVarSignatureContextIndex) !== null && _a !== void 0 ? _a : 0);
2926
2978
  // If we're solving a default type, handle param specs with no scope ID.
2927
2979
  if (this._isSolvingDefaultType && !paramSpec.scopeId) {
@@ -2945,7 +2997,10 @@ class ApplySolvedTypeVarsTransformer extends TypeVarTransformer {
2945
2997
  }
2946
2998
  let useDefaultOrUnknown = false;
2947
2999
  if (this._options.unknownIfNotFound && !this._typeVarContext.hasSolveForScope(types_1.WildcardTypeVarScopeId)) {
2948
- useDefaultOrUnknown = true;
3000
+ const exemptTypeVars = (_b = this._options.unknownExemptTypeVars) !== null && _b !== void 0 ? _b : [];
3001
+ if (!exemptTypeVars.some((t) => (0, types_1.isTypeSame)(t, paramSpec, { ignoreTypeFlags: true }))) {
3002
+ useDefaultOrUnknown = true;
3003
+ }
2949
3004
  }
2950
3005
  else if (this._options.applyInScopePlaceholders && paramSpec.isInScopePlaceholder) {
2951
3006
  useDefaultOrUnknown = true;