@zzzen/pyright-internal 1.2.0-dev.20221225 → 1.2.0-dev.20230101

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 (47) hide show
  1. package/dist/analyzer/binder.js +5 -3
  2. package/dist/analyzer/binder.js.map +1 -1
  3. package/dist/analyzer/codeFlowEngine.d.ts +16 -1
  4. package/dist/analyzer/codeFlowEngine.js +17 -9
  5. package/dist/analyzer/codeFlowEngine.js.map +1 -1
  6. package/dist/analyzer/constraintSolver.js +30 -31
  7. package/dist/analyzer/constraintSolver.js.map +1 -1
  8. package/dist/analyzer/patternMatching.js +3 -2
  9. package/dist/analyzer/patternMatching.js.map +1 -1
  10. package/dist/analyzer/program.js +11 -5
  11. package/dist/analyzer/program.js.map +1 -1
  12. package/dist/analyzer/sourceFile.js +1 -0
  13. package/dist/analyzer/sourceFile.js.map +1 -1
  14. package/dist/analyzer/typeCacheUtils.d.ts +28 -0
  15. package/dist/analyzer/{typeCache.js → typeCacheUtils.js} +8 -14
  16. package/dist/analyzer/typeCacheUtils.js.map +1 -0
  17. package/dist/analyzer/typeEvaluator.js +447 -281
  18. package/dist/analyzer/typeEvaluator.js.map +1 -1
  19. package/dist/analyzer/typeGuards.js +1 -2
  20. package/dist/analyzer/typeGuards.js.map +1 -1
  21. package/dist/analyzer/typeUtils.d.ts +12 -3
  22. package/dist/analyzer/typeUtils.js +283 -134
  23. package/dist/analyzer/typeUtils.js.map +1 -1
  24. package/dist/analyzer/typeVarContext.d.ts +2 -10
  25. package/dist/analyzer/typeVarContext.js +12 -31
  26. package/dist/analyzer/typeVarContext.js.map +1 -1
  27. package/dist/analyzer/types.d.ts +4 -12
  28. package/dist/analyzer/types.js +22 -22
  29. package/dist/analyzer/types.js.map +1 -1
  30. package/dist/commands/dumpFileDebugInfoCommand.js +2 -2
  31. package/dist/commands/dumpFileDebugInfoCommand.js.map +1 -1
  32. package/dist/localization/localize.d.ts +18 -3
  33. package/dist/localization/localize.js +7 -1
  34. package/dist/localization/localize.js.map +1 -1
  35. package/dist/localization/package.nls.en-us.json +7 -1
  36. package/dist/parser/parser.js +3 -1
  37. package/dist/parser/parser.js.map +1 -1
  38. package/dist/tests/typeEvaluator2.test.js +1 -1
  39. package/dist/tests/typeEvaluator3.test.js +5 -1
  40. package/dist/tests/typeEvaluator3.test.js.map +1 -1
  41. package/dist/tests/typeEvaluator4.test.js +4 -0
  42. package/dist/tests/typeEvaluator4.test.js.map +1 -1
  43. package/dist/tests/typeEvaluator5.test.js +49 -7
  44. package/dist/tests/typeEvaluator5.test.js.map +1 -1
  45. package/package.json +1 -1
  46. package/dist/analyzer/typeCache.d.ts +0 -40
  47. package/dist/analyzer/typeCache.js.map +0 -1
@@ -8,8 +8,8 @@
8
8
  * Collection of functions that operate on Type objects.
9
9
  */
10
10
  Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.buildTypeVarContext = exports.buildTypeVarContextFromSpecializedClass = exports.setTypeArgumentsRecursive = exports.specializeClassType = exports.getTypeVarArgumentsRecursive = exports.addTypeVarsToListIfUnique = exports.getClassFieldsRecursive = exports.getClassIterator = exports.getClassMemberIterator = exports.lookUpClassMember = exports.lookUpObjectMember = exports.getProtocolSymbols = exports.transformExpectedTypeForConstructor = exports.applySolvedTypeVars = exports.populateTypeVarContextForSelfType = exports.partiallySpecializeType = exports.isUnboundedTupleClass = exports.isTupleClass = exports.isMaybeDescriptorInstance = exports.isDescriptorInstance = exports.isProperty = exports.isEllipsisType = exports.getUnionSubtypeCount = exports.getLiteralTypeClassName = exports.containsLiteralType = exports.containsType = exports.isLiteralTypeOrUnion = exports.isLiteralType = exports.getSpecializedTupleType = 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.isIncompleteUnknown = exports.isOptionalType = exports.getParameterListDetails = exports.ParameterSource = exports.AssignTypeFlags = exports.ClassIteratorFlags = exports.ClassMemberLookupFlags = void 0;
12
- exports.convertParamSpecValueToType = exports.getDeclaringModulesForType = exports.computeMroLinearization = exports.isVarianceOfTypeArgumentCompatible = exports.requiresSpecialization = exports.requiresTypeArguments = exports.getGeneratorTypeArgs = exports.removeParamSpecVariadicsFromFunction = exports.removeParamSpecVariadicsFromSignature = exports.specializeTupleClass = exports.combineSameSizedTuples = exports.explodeGenericClass = exports.isPartlyUnknown = exports.containsUnknown = exports.getMembersForModule = exports.getMembersForClass = exports.convertToInstantiable = exports.convertToInstance = exports.isEffectivelyInstantiable = exports.getGeneratorYieldType = exports.getDeclaredGeneratorReturnType = exports.synthesizeTypeVarForSelfCls = exports.derivesFromClassRecursive = exports.specializeForBaseClass = void 0;
11
+ exports.setTypeArgumentsRecursive = exports.specializeClassType = exports.isTypeVarLimitedToCallable = exports.getTypeVarArgumentsRecursive = exports.addTypeVarsToListIfUnique = exports.getClassFieldsRecursive = exports.getClassIterator = exports.getClassMemberIterator = exports.lookUpClassMember = exports.lookUpObjectMember = exports.getProtocolSymbols = exports.transformExpectedTypeForConstructor = exports.validateTypeVarDefault = exports.applySolvedTypeVars = exports.populateTypeVarContextForSelfType = exports.partiallySpecializeType = exports.isUnboundedTupleClass = exports.isTupleClass = exports.isMaybeDescriptorInstance = exports.isDescriptorInstance = exports.isProperty = exports.isEllipsisType = exports.getUnionSubtypeCount = exports.getLiteralTypeClassName = exports.containsLiteralType = exports.containsType = exports.isLiteralTypeOrUnion = exports.isLiteralType = exports.getSpecializedTupleType = 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.isIncompleteUnknown = exports.isOptionalType = exports.getParameterListDetails = exports.ParameterSource = exports.AssignTypeFlags = exports.ClassIteratorFlags = exports.ClassMemberLookupFlags = void 0;
12
+ exports.convertParamSpecValueToType = exports.convertTypeToParamSpecValue = exports.getDeclaringModulesForType = exports.computeMroLinearization = exports.isVarianceOfTypeArgumentCompatible = exports.requiresSpecialization = exports.requiresTypeArguments = exports.getGeneratorTypeArgs = exports.removeParamSpecVariadicsFromFunction = exports.removeParamSpecVariadicsFromSignature = exports.specializeTupleClass = exports.combineSameSizedTuples = exports.explodeGenericClass = exports.isPartlyUnknown = exports.containsUnknown = exports.getMembersForModule = exports.getMembersForClass = exports.convertToInstantiable = exports.convertToInstance = exports.isEffectivelyInstantiable = exports.getGeneratorYieldType = exports.getDeclaredGeneratorReturnType = exports.synthesizeTypeVarForSelfCls = exports.derivesFromClassRecursive = exports.specializeForBaseClass = exports.buildTypeVarContext = exports.buildTypeVarContextFromSpecializedClass = 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");
@@ -822,10 +822,7 @@ function partiallySpecializeType(type, contextClassType, selfClass, typeClassTyp
822
822
  if (selfClass) {
823
823
  populateTypeVarContextForSelfType(typeVarContext, contextClassType, selfClass);
824
824
  }
825
- return applySolvedTypeVars(type, typeVarContext,
826
- /* unknownIfNotFound */ undefined,
827
- /* useNarrowBoundOnly */ undefined,
828
- /* eliminateUnsolvedInUnions */ undefined, typeClassType);
825
+ return applySolvedTypeVars(type, typeVarContext, { typeClassType });
829
826
  }
830
827
  exports.partiallySpecializeType = partiallySpecializeType;
831
828
  function populateTypeVarContextForSelfType(typeVarContext, contextClassType, selfClass) {
@@ -835,15 +832,26 @@ function populateTypeVarContextForSelfType(typeVarContext, contextClassType, sel
835
832
  exports.populateTypeVarContextForSelfType = populateTypeVarContextForSelfType;
836
833
  // Specializes a (potentially generic) type by substituting
837
834
  // type variables from a type var map.
838
- function applySolvedTypeVars(type, typeVarContext, unknownIfNotFound = false, useNarrowBoundOnly = false, eliminateUnsolvedInUnions = false, typeClassType) {
835
+ function applySolvedTypeVars(type, typeVarContext, options = {}) {
839
836
  // Use a shortcut if the typeVarContext is empty and no transform is necessary.
840
- if (typeVarContext.isEmpty() && !unknownIfNotFound && !eliminateUnsolvedInUnions) {
837
+ if (typeVarContext.isEmpty() && !options.unknownIfNotFound && !options.eliminateUnsolvedInUnions) {
841
838
  return type;
842
839
  }
843
- const transformer = new ApplySolvedTypeVarsTransformer(typeVarContext, unknownIfNotFound, useNarrowBoundOnly, eliminateUnsolvedInUnions, typeClassType);
844
- return transformer.apply(type);
840
+ const transformer = new ApplySolvedTypeVarsTransformer(typeVarContext, options);
841
+ return transformer.apply(type, 0);
845
842
  }
846
843
  exports.applySolvedTypeVars = applySolvedTypeVars;
844
+ // Validates that a default type associated with a TypeVar does not refer to
845
+ // other TypeVars or ParamSpecs that are out of scope.
846
+ function validateTypeVarDefault(typeVar, liveTypeParams, invalidTypeVars) {
847
+ // If there is no default type or the default type is concrete, there's
848
+ // no need to do any more work here.
849
+ if (typeVar.details.defaultType && requiresSpecialization(typeVar.details.defaultType)) {
850
+ const validator = new TypeVarDefaultValidator(liveTypeParams, invalidTypeVars);
851
+ validator.apply(typeVar.details.defaultType, 0);
852
+ }
853
+ }
854
+ exports.validateTypeVarDefault = validateTypeVarDefault;
847
855
  // During bidirectional type inference for constructors, an "executed type"
848
856
  // is used to prepopulate the type var map. This is problematic when the
849
857
  // expected type uses TypeVars that are not part of the context of the
@@ -861,7 +869,7 @@ function transformExpectedTypeForConstructor(expectedType, liveTypeVarScopes) {
861
869
  return undefined;
862
870
  }
863
871
  const transformer = new ExpectedConstructorTypeTransformer(liveTypeVarScopes);
864
- return transformer.apply(expectedType);
872
+ return transformer.apply(expectedType, 0);
865
873
  }
866
874
  exports.transformExpectedTypeForConstructor = transformExpectedTypeForConstructor;
867
875
  // Given a protocol class, this function returns a set of all the
@@ -1125,15 +1133,6 @@ function getTypeVarArgumentsRecursive(type, recursionCount = 0) {
1125
1133
  return [];
1126
1134
  }
1127
1135
  recursionCount++;
1128
- const getTypeVarsFromClass = (classType) => {
1129
- const combinedList = [];
1130
- if (classType.typeArguments) {
1131
- classType.typeArguments.forEach((typeArg) => {
1132
- addTypeVarsToListIfUnique(combinedList, getTypeVarArgumentsRecursive(typeArg, recursionCount));
1133
- });
1134
- }
1135
- return combinedList;
1136
- };
1137
1136
  if ((_a = type.typeAliasInfo) === null || _a === void 0 ? void 0 : _a.typeArguments) {
1138
1137
  const combinedList = [];
1139
1138
  (_b = type.typeAliasInfo) === null || _b === void 0 ? void 0 : _b.typeArguments.forEach((typeArg) => {
@@ -1153,7 +1152,14 @@ function getTypeVarArgumentsRecursive(type, recursionCount = 0) {
1153
1152
  return [types_1.TypeBase.isInstantiable(type) ? types_1.TypeVarType.cloneAsInstance(type) : type];
1154
1153
  }
1155
1154
  if ((0, types_1.isClass)(type)) {
1156
- return getTypeVarsFromClass(type);
1155
+ const combinedList = [];
1156
+ const typeArgs = type.tupleTypeArguments ? type.tupleTypeArguments.map((e) => e.type) : type.typeArguments;
1157
+ if (typeArgs) {
1158
+ typeArgs.forEach((typeArg) => {
1159
+ addTypeVarsToListIfUnique(combinedList, getTypeVarArgumentsRecursive(typeArg, recursionCount));
1160
+ });
1161
+ }
1162
+ return combinedList;
1157
1163
  }
1158
1164
  if ((0, types_1.isUnion)(type)) {
1159
1165
  const combinedList = [];
@@ -1179,6 +1185,79 @@ function getTypeVarArgumentsRecursive(type, recursionCount = 0) {
1179
1185
  return [];
1180
1186
  }
1181
1187
  exports.getTypeVarArgumentsRecursive = getTypeVarArgumentsRecursive;
1188
+ // Determines if the type variable appears within the type and only within
1189
+ // a particular Callable within that type.
1190
+ function isTypeVarLimitedToCallable(type, typeVar) {
1191
+ const info = getTypeVarWithinTypeInfoRecursive(type, typeVar);
1192
+ return info.isTypeVarUsed && info.isUsedInCallable;
1193
+ }
1194
+ exports.isTypeVarLimitedToCallable = isTypeVarLimitedToCallable;
1195
+ function getTypeVarWithinTypeInfoRecursive(type, typeVar, recursionCount = 0) {
1196
+ if (recursionCount > types_1.maxTypeRecursionCount) {
1197
+ return { isTypeVarUsed: false, isUsedInCallable: false };
1198
+ }
1199
+ recursionCount++;
1200
+ let typeVarUsedCount = 0;
1201
+ let usedInCallableCount = 0;
1202
+ if ((0, types_1.isTypeVar)(type)) {
1203
+ // Ignore P.args or P.kwargs types.
1204
+ if (!(0, types_1.isParamSpec)(type) || !type.paramSpecAccess) {
1205
+ if ((0, types_1.isTypeSame)(typeVar, convertToInstance(type))) {
1206
+ typeVarUsedCount++;
1207
+ }
1208
+ }
1209
+ }
1210
+ else if ((0, types_1.isClass)(type)) {
1211
+ if (type.typeArguments) {
1212
+ type.typeArguments.forEach((typeArg) => {
1213
+ const subResult = getTypeVarWithinTypeInfoRecursive(typeArg, typeVar, recursionCount);
1214
+ if (subResult.isTypeVarUsed) {
1215
+ typeVarUsedCount++;
1216
+ }
1217
+ if (subResult.isUsedInCallable) {
1218
+ usedInCallableCount++;
1219
+ }
1220
+ });
1221
+ }
1222
+ }
1223
+ else if ((0, types_1.isUnion)(type)) {
1224
+ doForEachSubtype(type, (subtype) => {
1225
+ const subResult = getTypeVarWithinTypeInfoRecursive(subtype, typeVar, recursionCount);
1226
+ if (subResult.isTypeVarUsed) {
1227
+ typeVarUsedCount++;
1228
+ }
1229
+ if (subResult.isUsedInCallable) {
1230
+ usedInCallableCount++;
1231
+ }
1232
+ });
1233
+ }
1234
+ else if ((0, types_1.isFunction)(type)) {
1235
+ for (let i = 0; i < type.details.parameters.length; i++) {
1236
+ if (getTypeVarWithinTypeInfoRecursive(types_1.FunctionType.getEffectiveParameterType(type, i), typeVar, recursionCount).isTypeVarUsed) {
1237
+ typeVarUsedCount++;
1238
+ }
1239
+ }
1240
+ if (type.details.paramSpec) {
1241
+ if ((0, types_1.isTypeSame)(typeVar, convertToInstance(type.details.paramSpec))) {
1242
+ typeVarUsedCount++;
1243
+ }
1244
+ }
1245
+ const returnType = types_1.FunctionType.getSpecializedReturnType(type);
1246
+ if (returnType) {
1247
+ if (getTypeVarWithinTypeInfoRecursive(returnType, typeVar, recursionCount).isTypeVarUsed) {
1248
+ typeVarUsedCount++;
1249
+ }
1250
+ }
1251
+ if (typeVarUsedCount > 0) {
1252
+ typeVarUsedCount = 1;
1253
+ usedInCallableCount = 1;
1254
+ }
1255
+ }
1256
+ return {
1257
+ isTypeVarUsed: typeVarUsedCount > 0,
1258
+ isUsedInCallable: usedInCallableCount === 1 && typeVarUsedCount === 1,
1259
+ };
1260
+ }
1182
1261
  // Creates a specialized version of the class, filling in any unspecified
1183
1262
  // type arguments with Unknown.
1184
1263
  function specializeClassType(type) {
@@ -1237,13 +1316,9 @@ function setTypeArgumentsRecursive(destType, srcType, typeVarContext, recursionC
1237
1316
  if (destType.details.paramSpec) {
1238
1317
  // Fill in an empty signature for a ParamSpec if the source is Any or Unknown.
1239
1318
  if (!typeVarContext.hasTypeVar(destType.details.paramSpec) && (0, types_1.isAnyOrUnknown)(srcType)) {
1240
- typeVarContext.setParamSpec(destType.details.paramSpec, {
1241
- flags: 0 /* None */,
1242
- parameters: types_1.FunctionType.getDefaultParameters(),
1243
- typeVarScopeId: undefined,
1244
- docString: undefined,
1245
- paramSpec: undefined,
1246
- });
1319
+ const newFunction = types_1.FunctionType.createInstance('', '', '', 32768 /* SkipArgsKwargsCompatibilityCheck */ | 65536 /* ParamSpecValue */);
1320
+ types_1.FunctionType.addDefaultParameters(newFunction);
1321
+ typeVarContext.setTypeVarType(destType.details.paramSpec, newFunction);
1247
1322
  }
1248
1323
  }
1249
1324
  }
@@ -1290,10 +1365,10 @@ function buildTypeVarContext(typeParameters, typeArgs, typeVarScopeId) {
1290
1365
  if (index < typeArgs.length) {
1291
1366
  typeArgType = typeArgs[index];
1292
1367
  if ((0, types_1.isFunction)(typeArgType) && types_1.FunctionType.isParamSpecValue(typeArgType)) {
1293
- const paramSpecEntries = [];
1368
+ const parameters = [];
1294
1369
  const typeArgFunctionType = typeArgType;
1295
1370
  typeArgType.details.parameters.forEach((param, paramIndex) => {
1296
- paramSpecEntries.push({
1371
+ parameters.push({
1297
1372
  category: param.category,
1298
1373
  name: param.name,
1299
1374
  hasDefault: !!param.hasDefault,
@@ -1302,32 +1377,10 @@ function buildTypeVarContext(typeParameters, typeArgs, typeVarScopeId) {
1302
1377
  type: types_1.FunctionType.getEffectiveParameterType(typeArgFunctionType, paramIndex),
1303
1378
  });
1304
1379
  });
1305
- typeVarContext.setParamSpec(typeParam, {
1306
- parameters: paramSpecEntries,
1307
- typeVarScopeId: typeArgType.details.typeVarScopeId,
1308
- flags: typeArgType.details.flags,
1309
- docString: typeArgType.details.docString,
1310
- paramSpec: typeArgType.details.paramSpec,
1311
- });
1380
+ typeVarContext.setTypeVarType(typeParam, convertTypeToParamSpecValue(typeArgType));
1312
1381
  }
1313
- else if ((0, types_1.isParamSpec)(typeArgType)) {
1314
- typeVarContext.setParamSpec(typeParam, {
1315
- flags: 0 /* None */,
1316
- parameters: [],
1317
- typeVarScopeId: undefined,
1318
- docString: undefined,
1319
- paramSpec: typeArgType,
1320
- });
1321
- }
1322
- else if ((0, types_1.isAnyOrUnknown)(typeArgType)) {
1323
- // Fill in an empty signature if the arg type is Any or Unknown.
1324
- typeVarContext.setParamSpec(typeParam, {
1325
- flags: 32768 /* SkipArgsKwargsCompatibilityCheck */,
1326
- parameters: types_1.FunctionType.getDefaultParameters(),
1327
- typeVarScopeId: undefined,
1328
- docString: undefined,
1329
- paramSpec: undefined,
1330
- });
1382
+ else if ((0, types_1.isParamSpec)(typeArgType) || (0, types_1.isAnyOrUnknown)(typeArgType)) {
1383
+ typeVarContext.setTypeVarType(typeParam, convertTypeToParamSpecValue(typeArgType));
1331
1384
  }
1332
1385
  }
1333
1386
  }
@@ -2093,20 +2146,48 @@ function addDeclaringModuleNamesForType(type, moduleList, recursionCount = 0) {
2093
2146
  }
2094
2147
  }
2095
2148
  }
2096
- function convertParamSpecValueToType(paramSpecEntry, omitParamSpec = false) {
2097
- let hasParameters = paramSpecEntry.parameters.length > 0;
2098
- if (paramSpecEntry.parameters.length === 1) {
2149
+ function convertTypeToParamSpecValue(type) {
2150
+ if ((0, types_1.isParamSpec)(type)) {
2151
+ const newFunction = types_1.FunctionType.createInstance('', '', '', 65536 /* ParamSpecValue */);
2152
+ newFunction.details.paramSpec = type;
2153
+ newFunction.details.typeVarScopeId = getTypeVarScopeId(type);
2154
+ return newFunction;
2155
+ }
2156
+ if ((0, types_1.isFunction)(type)) {
2157
+ const newFunction = types_1.FunctionType.createInstance('', '', '', type.details.flags | 65536 /* ParamSpecValue */, type.details.docString);
2158
+ type.details.parameters.forEach((param, index) => {
2159
+ types_1.FunctionType.addParameter(newFunction, {
2160
+ category: param.category,
2161
+ name: param.name,
2162
+ hasDefault: param.hasDefault,
2163
+ defaultValueExpression: param.defaultValueExpression,
2164
+ isNameSynthesized: param.isNameSynthesized,
2165
+ type: types_1.FunctionType.getEffectiveParameterType(type, index),
2166
+ });
2167
+ });
2168
+ newFunction.details.typeVarScopeId = getTypeVarScopeId(type);
2169
+ newFunction.details.paramSpec = type.details.paramSpec;
2170
+ return newFunction;
2171
+ }
2172
+ const newFunction = types_1.FunctionType.createInstance('', '', '', 65536 /* ParamSpecValue */ | 32768 /* SkipArgsKwargsCompatibilityCheck */);
2173
+ types_1.FunctionType.addDefaultParameters(newFunction);
2174
+ return newFunction;
2175
+ }
2176
+ exports.convertTypeToParamSpecValue = convertTypeToParamSpecValue;
2177
+ function convertParamSpecValueToType(paramSpecValue, omitParamSpec = false) {
2178
+ let hasParameters = paramSpecValue.details.parameters.length > 0;
2179
+ if (paramSpecValue.details.parameters.length === 1) {
2099
2180
  // If the ParamSpec has a position-only separator as its only parameter,
2100
2181
  // treat it as though there are no parameters.
2101
- const onlyParam = paramSpecEntry.parameters[0];
2182
+ const onlyParam = paramSpecValue.details.parameters[0];
2102
2183
  if (onlyParam.category === 0 /* Simple */ && !onlyParam.name) {
2103
2184
  hasParameters = false;
2104
2185
  }
2105
2186
  }
2106
- if (hasParameters || !paramSpecEntry.paramSpec || omitParamSpec) {
2187
+ if (hasParameters || !paramSpecValue.details.paramSpec || omitParamSpec) {
2107
2188
  // Create a function type from the param spec entries.
2108
- const functionType = types_1.FunctionType.createInstance('', '', '', 65536 /* ParamSpecValue */ | paramSpecEntry.flags);
2109
- paramSpecEntry.parameters.forEach((entry) => {
2189
+ const functionType = types_1.FunctionType.createInstance('', '', '', 65536 /* ParamSpecValue */ | paramSpecValue.details.flags);
2190
+ paramSpecValue.details.parameters.forEach((entry) => {
2110
2191
  types_1.FunctionType.addParameter(functionType, {
2111
2192
  category: entry.category,
2112
2193
  name: entry.name,
@@ -2118,12 +2199,12 @@ function convertParamSpecValueToType(paramSpecEntry, omitParamSpec = false) {
2118
2199
  });
2119
2200
  });
2120
2201
  if (!omitParamSpec) {
2121
- functionType.details.paramSpec = paramSpecEntry.paramSpec;
2202
+ functionType.details.paramSpec = paramSpecValue.details.paramSpec;
2122
2203
  }
2123
- functionType.details.docString = paramSpecEntry.docString;
2204
+ functionType.details.docString = paramSpecValue.details.docString;
2124
2205
  return functionType;
2125
2206
  }
2126
- return paramSpecEntry.paramSpec;
2207
+ return paramSpecValue.details.paramSpec;
2127
2208
  }
2128
2209
  exports.convertParamSpecValueToType = convertParamSpecValueToType;
2129
2210
  // Recursively walks a type and calls a callback for each TypeVar, allowing
@@ -2133,7 +2214,7 @@ class TypeVarTransformer {
2133
2214
  this._isTransformingTypeArg = false;
2134
2215
  this._pendingTypeVarTransformations = new Set();
2135
2216
  }
2136
- apply(type, recursionCount = 0) {
2217
+ apply(type, recursionCount) {
2137
2218
  var _a, _b;
2138
2219
  if (recursionCount > types_1.maxTypeRecursionCount) {
2139
2220
  return type;
@@ -2177,16 +2258,26 @@ class TypeVarTransformer {
2177
2258
  // _pendingTypeVarTransformations set.
2178
2259
  const typeVarName = types_1.TypeVarType.getNameWithScope(type);
2179
2260
  if (!this._pendingTypeVarTransformations.has(typeVarName)) {
2180
- replacementType = (_b = this.transformTypeVar(type)) !== null && _b !== void 0 ? _b : type;
2181
- if (!this._isTransformingTypeArg) {
2182
- this._pendingTypeVarTransformations.add(typeVarName);
2183
- replacementType = this.apply(replacementType, recursionCount);
2184
- this._pendingTypeVarTransformations.delete(typeVarName);
2261
+ if (type.details.isParamSpec) {
2262
+ if (!type.paramSpecAccess) {
2263
+ const paramSpecValue = this.transformParamSpec(type, recursionCount);
2264
+ if (paramSpecValue) {
2265
+ replacementType = convertParamSpecValueToType(paramSpecValue);
2266
+ }
2267
+ }
2185
2268
  }
2186
- // If we're transforming a variadic type variable that was in a union,
2187
- // expand the union types.
2188
- if ((0, types_1.isVariadicTypeVar)(type) && type.isVariadicInUnion) {
2189
- replacementType = _expandVariadicUnpackedUnion(replacementType);
2269
+ else {
2270
+ replacementType = (_b = this.transformTypeVar(type, recursionCount)) !== null && _b !== void 0 ? _b : type;
2271
+ if (!this._isTransformingTypeArg) {
2272
+ this._pendingTypeVarTransformations.add(typeVarName);
2273
+ replacementType = this.apply(replacementType, recursionCount);
2274
+ this._pendingTypeVarTransformations.delete(typeVarName);
2275
+ }
2276
+ // If we're transforming a variadic type variable that was in a union,
2277
+ // expand the union types.
2278
+ if ((0, types_1.isVariadicTypeVar)(type) && type.isVariadicInUnion) {
2279
+ replacementType = _expandVariadicUnpackedUnion(replacementType);
2280
+ }
2190
2281
  }
2191
2282
  }
2192
2283
  return replacementType;
@@ -2204,7 +2295,7 @@ class TypeVarTransformer {
2204
2295
  transformedType = (0, types_1.combineTypes)(subtypesToCombine);
2205
2296
  }
2206
2297
  if (this.transformUnionSubtype) {
2207
- return this.transformUnionSubtype(subtype, transformedType);
2298
+ return this.transformUnionSubtype(subtype, transformedType, recursionCount);
2208
2299
  }
2209
2300
  return transformedType;
2210
2301
  });
@@ -2232,16 +2323,16 @@ class TypeVarTransformer {
2232
2323
  }
2233
2324
  return type;
2234
2325
  }
2235
- transformTypeVar(typeVar) {
2326
+ transformTypeVar(typeVar, recursionCount) {
2236
2327
  return typeVar;
2237
2328
  }
2238
- transformVariadicTypeVar(paramSpec) {
2329
+ transformTupleTypeVar(paramSpec, recursionCount) {
2239
2330
  return undefined;
2240
2331
  }
2241
- transformParamSpec(paramSpec) {
2332
+ transformParamSpec(paramSpec, recursionCount) {
2242
2333
  return undefined;
2243
2334
  }
2244
- transformUnionSubtype(preTransform, postTransform) {
2335
+ transformUnionSubtype(preTransform, postTransform, recursionCount) {
2245
2336
  return postTransform;
2246
2337
  }
2247
2338
  _transformGenericTypeAlias(type, recursionCount) {
@@ -2266,11 +2357,11 @@ class TypeVarTransformer {
2266
2357
  return classType;
2267
2358
  }
2268
2359
  let newTypeArgs = [];
2269
- let newVariadicTypeArgs;
2360
+ let newTupleTypeArgs;
2270
2361
  let specializationNeeded = false;
2271
2362
  const typeParams = types_1.ClassType.getTypeParameters(classType);
2272
2363
  const transformParamSpec = (paramSpec) => {
2273
- const paramSpecValue = this.transformParamSpec(paramSpec);
2364
+ const paramSpecValue = this.transformParamSpec(paramSpec, recursionCount);
2274
2365
  if (paramSpecValue) {
2275
2366
  specializationNeeded = true;
2276
2367
  return convertParamSpecValueToType(paramSpecValue);
@@ -2313,7 +2404,7 @@ class TypeVarTransformer {
2313
2404
  else {
2314
2405
  const typeParamName = types_1.TypeVarType.getNameWithScope(typeParam);
2315
2406
  if (!this._pendingTypeVarTransformations.has(typeParamName)) {
2316
- const transformedType = this.transformTypeVar(typeParam);
2407
+ const transformedType = this.transformTypeVar(typeParam, recursionCount);
2317
2408
  replacementType = transformedType !== null && transformedType !== void 0 ? transformedType : typeParam;
2318
2409
  if (replacementType !== typeParam) {
2319
2410
  specializationNeeded = true;
@@ -2328,7 +2419,7 @@ class TypeVarTransformer {
2328
2419
  }
2329
2420
  if (types_1.ClassType.isTupleClass(classType)) {
2330
2421
  if (classType.tupleTypeArguments) {
2331
- newVariadicTypeArgs = [];
2422
+ newTupleTypeArgs = [];
2332
2423
  classType.tupleTypeArguments.forEach((oldTypeArgType) => {
2333
2424
  const newTypeArgType = this.apply(oldTypeArgType.type, recursionCount);
2334
2425
  if (newTypeArgType !== oldTypeArgType.type) {
@@ -2338,16 +2429,16 @@ class TypeVarTransformer {
2338
2429
  (0, types_1.isClassInstance)(newTypeArgType) &&
2339
2430
  isTupleClass(newTypeArgType) &&
2340
2431
  newTypeArgType.tupleTypeArguments) {
2341
- (0, collectionUtils_1.appendArray)(newVariadicTypeArgs, newTypeArgType.tupleTypeArguments);
2432
+ (0, collectionUtils_1.appendArray)(newTupleTypeArgs, newTypeArgType.tupleTypeArguments);
2342
2433
  }
2343
2434
  else {
2344
- newVariadicTypeArgs.push({ type: newTypeArgType, isUnbounded: oldTypeArgType.isUnbounded });
2435
+ newTupleTypeArgs.push({ type: newTypeArgType, isUnbounded: oldTypeArgType.isUnbounded });
2345
2436
  }
2346
2437
  });
2347
2438
  }
2348
2439
  else if (typeParams.length > 0) {
2349
- newVariadicTypeArgs = this.transformVariadicTypeVar(typeParams[0]);
2350
- if (newVariadicTypeArgs) {
2440
+ newTupleTypeArgs = this.transformTupleTypeVar(typeParams[0], recursionCount);
2441
+ if (newTupleTypeArgs) {
2351
2442
  specializationNeeded = true;
2352
2443
  }
2353
2444
  }
@@ -2359,13 +2450,13 @@ class TypeVarTransformer {
2359
2450
  }
2360
2451
  return types_1.ClassType.cloneForSpecialization(classType, newTypeArgs,
2361
2452
  /* isTypeArgumentExplicit */ true,
2362
- /* includeSubclasses */ undefined, newVariadicTypeArgs);
2453
+ /* includeSubclasses */ undefined, newTupleTypeArgs);
2363
2454
  }
2364
2455
  _transformTypeVarsInFunctionType(sourceType, recursionCount) {
2365
2456
  let functionType = sourceType;
2366
2457
  // Handle functions with a parameter specification in a special manner.
2367
2458
  if (functionType.details.paramSpec) {
2368
- const paramSpec = this.transformParamSpec(functionType.details.paramSpec);
2459
+ const paramSpec = this.transformParamSpec(functionType.details.paramSpec, recursionCount);
2369
2460
  if (paramSpec) {
2370
2461
  functionType = types_1.FunctionType.cloneForParamSpec(functionType, paramSpec);
2371
2462
  }
@@ -2390,11 +2481,11 @@ class TypeVarTransformer {
2390
2481
  (0, types_1.isParamSpec)(argsParamType) &&
2391
2482
  (0, types_1.isParamSpec)(kwargsParamType) &&
2392
2483
  (0, types_1.isTypeSame)(argsParamType, kwargsParamType)) {
2393
- const paramSpecType = this.transformParamSpec(argsParamType);
2484
+ const paramSpecType = this.transformParamSpec(argsParamType, recursionCount);
2394
2485
  if (paramSpecType) {
2395
- if (paramSpecType.parameters.length > 0 ||
2396
- paramSpecType.paramSpec === undefined ||
2397
- !(0, types_1.isTypeSame)(argsParamType, paramSpecType.paramSpec)) {
2486
+ if (paramSpecType.details.parameters.length > 0 ||
2487
+ paramSpecType.details.paramSpec === undefined ||
2488
+ !(0, types_1.isTypeSame)(argsParamType, paramSpecType.details.paramSpec)) {
2398
2489
  functionType = types_1.FunctionType.cloneForParamSpecApplication(functionType, paramSpecType);
2399
2490
  }
2400
2491
  }
@@ -2506,26 +2597,47 @@ class TypeVarTransformer {
2506
2597
  return newFunctionType;
2507
2598
  }
2508
2599
  }
2600
+ // For a TypeVar with a default type, validates whether the default type is using
2601
+ // any other TypeVars that are not currently in scope.
2602
+ class TypeVarDefaultValidator extends TypeVarTransformer {
2603
+ constructor(_liveTypeParams, _invalidTypeVars) {
2604
+ super();
2605
+ this._liveTypeParams = _liveTypeParams;
2606
+ this._invalidTypeVars = _invalidTypeVars;
2607
+ }
2608
+ transformTypeVar(typeVar) {
2609
+ const replacementType = this._liveTypeParams.find((param) => param.details.name === typeVar.details.name);
2610
+ if (!replacementType || (0, types_1.isParamSpec)(replacementType)) {
2611
+ this._invalidTypeVars.add(typeVar.details.name);
2612
+ }
2613
+ return types_1.UnknownType.create();
2614
+ }
2615
+ transformParamSpec(paramSpec) {
2616
+ const replacementType = this._liveTypeParams.find((param) => param.details.name === paramSpec.details.name);
2617
+ if (!replacementType || !(0, types_1.isParamSpec)(replacementType)) {
2618
+ this._invalidTypeVars.add(paramSpec.details.name);
2619
+ }
2620
+ return undefined;
2621
+ }
2622
+ }
2509
2623
  // Specializes a (potentially generic) type by substituting
2510
2624
  // type variables from a type var map.
2511
2625
  class ApplySolvedTypeVarsTransformer extends TypeVarTransformer {
2512
- constructor(_typeVarContext, _unknownIfNotFound = false, _useNarrowBoundOnly = false, _eliminateUnsolvedInUnions = false, _typeClassType) {
2626
+ constructor(_typeVarContext, _options) {
2513
2627
  super();
2514
2628
  this._typeVarContext = _typeVarContext;
2515
- this._unknownIfNotFound = _unknownIfNotFound;
2516
- this._useNarrowBoundOnly = _useNarrowBoundOnly;
2517
- this._eliminateUnsolvedInUnions = _eliminateUnsolvedInUnions;
2518
- this._typeClassType = _typeClassType;
2629
+ this._options = _options;
2630
+ this._isSolvingDefaultType = false;
2519
2631
  }
2520
- transformTypeVar(typeVar) {
2632
+ transformTypeVar(typeVar, recursionCount) {
2521
2633
  // If the type variable is unrelated to the scopes we're solving,
2522
2634
  // don't transform that type variable.
2523
2635
  if (typeVar.scopeId && this._typeVarContext.hasSolveForScope(typeVar.scopeId)) {
2524
- let replacement = this._typeVarContext.getTypeVarType(typeVar, this._useNarrowBoundOnly);
2636
+ let replacement = this._typeVarContext.getTypeVarType(typeVar, !!this._options.useNarrowBoundOnly);
2525
2637
  // If there was no narrow bound but there is a wide bound that
2526
2638
  // contains literals, we'll use the wide bound even if "useNarrowBoundOnly"
2527
2639
  // is specified.
2528
- if (!replacement && this._useNarrowBoundOnly) {
2640
+ if (!replacement && !!this._options.useNarrowBoundOnly) {
2529
2641
  const wideType = this._typeVarContext.getTypeVarType(typeVar);
2530
2642
  if (wideType) {
2531
2643
  if (containsLiteralType(wideType, /* includeTypeArgs */ true)) {
@@ -2536,9 +2648,9 @@ class ApplySolvedTypeVarsTransformer extends TypeVarTransformer {
2536
2648
  if (replacement) {
2537
2649
  if (types_1.TypeBase.isInstantiable(typeVar)) {
2538
2650
  if ((0, types_1.isAnyOrUnknown)(replacement) &&
2539
- this._typeClassType &&
2540
- (0, types_1.isInstantiableClass)(this._typeClassType)) {
2541
- replacement = types_1.ClassType.cloneForSpecialization(types_1.ClassType.cloneAsInstance(this._typeClassType), [replacement],
2651
+ this._options.typeClassType &&
2652
+ (0, types_1.isInstantiableClass)(this._options.typeClassType)) {
2653
+ replacement = types_1.ClassType.cloneForSpecialization(types_1.ClassType.cloneAsInstance(this._options.typeClassType), [replacement],
2542
2654
  /* isTypeArgumentExplicit */ true);
2543
2655
  }
2544
2656
  else {
@@ -2550,13 +2662,40 @@ class ApplySolvedTypeVarsTransformer extends TypeVarTransformer {
2550
2662
  // If this typeVar is in scope for what we're solving but the type
2551
2663
  // var map doesn't contain any entry for it, replace with the
2552
2664
  // default or Unknown.
2553
- if (this._unknownIfNotFound && !this._typeVarContext.hasSolveForScope(types_1.WildcardTypeVarScopeId)) {
2665
+ if (this._options.unknownIfNotFound && !this._typeVarContext.hasSolveForScope(types_1.WildcardTypeVarScopeId)) {
2666
+ // Use the default value if there is one.
2554
2667
  if (typeVar.details.defaultType) {
2555
- return typeVar.details.defaultType;
2668
+ return this._solveDefaultType(typeVar.details.defaultType, recursionCount);
2556
2669
  }
2557
2670
  return types_1.UnknownType.create();
2558
2671
  }
2559
2672
  }
2673
+ // If we're solving a default type, handle type variables with no scope ID.
2674
+ if (this._isSolvingDefaultType && !typeVar.scopeId) {
2675
+ const replacementEntry = this._typeVarContext
2676
+ .getTypeVars()
2677
+ .find((entry) => entry.typeVar.details.name === typeVar.details.name);
2678
+ if (replacementEntry) {
2679
+ return this._typeVarContext.getTypeVarType(replacementEntry.typeVar);
2680
+ }
2681
+ if (typeVar.details.defaultType) {
2682
+ return this.apply(typeVar.details.defaultType, recursionCount);
2683
+ }
2684
+ return types_1.UnknownType.create();
2685
+ }
2686
+ // If we're solving a default type, handle type variables with no scope ID.
2687
+ if (this._isSolvingDefaultType && !typeVar.scopeId) {
2688
+ const replacementEntry = this._typeVarContext
2689
+ .getTypeVars()
2690
+ .find((entry) => entry.typeVar.details.name === typeVar.details.name);
2691
+ if (replacementEntry) {
2692
+ return this._typeVarContext.getTypeVarType(replacementEntry.typeVar);
2693
+ }
2694
+ if (typeVar.details.defaultType) {
2695
+ return this.apply(typeVar.details.defaultType, recursionCount);
2696
+ }
2697
+ return types_1.UnknownType.create();
2698
+ }
2560
2699
  return undefined;
2561
2700
  }
2562
2701
  transformUnionSubtype(preTransform, postTransform) {
@@ -2565,7 +2704,7 @@ class ApplySolvedTypeVarsTransformer extends TypeVarTransformer {
2565
2704
  // in cases where TypeVars can go unsolved due to unions in parameter
2566
2705
  // annotations, like this:
2567
2706
  // def test(x: Union[str, T]) -> Union[str, T]
2568
- if (this._eliminateUnsolvedInUnions) {
2707
+ if (this._options.eliminateUnsolvedInUnions) {
2569
2708
  if ((0, types_1.isTypeVar)(preTransform) &&
2570
2709
  preTransform.scopeId !== undefined &&
2571
2710
  this._typeVarContext.hasSolveForScope(preTransform.scopeId)) {
@@ -2576,56 +2715,66 @@ class ApplySolvedTypeVarsTransformer extends TypeVarTransformer {
2576
2715
  }
2577
2716
  // If _unknownIfNotFound is true, the postTransform type will
2578
2717
  // be Unknown, which we want to eliminate.
2579
- if ((0, types_1.isUnknown)(postTransform) && this._unknownIfNotFound) {
2718
+ if ((0, types_1.isUnknown)(postTransform) && this._options.unknownIfNotFound) {
2580
2719
  return undefined;
2581
2720
  }
2582
2721
  }
2583
2722
  }
2584
2723
  return postTransform;
2585
2724
  }
2586
- transformVariadicTypeVar(typeVar) {
2725
+ transformTupleTypeVar(typeVar) {
2587
2726
  if (!typeVar.scopeId || !this._typeVarContext.hasSolveForScope(typeVar.scopeId)) {
2588
- if (typeVar.details.defaultType &&
2589
- (0, types_1.isClassInstance)(typeVar.details.defaultType) &&
2590
- typeVar.details.defaultType.tupleTypeArguments) {
2591
- return typeVar.details.defaultType.tupleTypeArguments;
2727
+ const defaultType = typeVar.details.defaultType;
2728
+ if (defaultType && (0, types_1.isClassInstance)(defaultType) && defaultType.tupleTypeArguments) {
2729
+ return defaultType.tupleTypeArguments;
2592
2730
  }
2593
2731
  return undefined;
2594
2732
  }
2595
2733
  return this._typeVarContext.getTupleTypeVar(typeVar);
2596
2734
  }
2597
- transformParamSpec(paramSpec) {
2735
+ transformParamSpec(paramSpec, recursionCount) {
2736
+ // If we're solving a default type, handle param specs with no scope ID.
2737
+ if (this._isSolvingDefaultType && !paramSpec.scopeId) {
2738
+ const replacementEntry = this._typeVarContext
2739
+ .getTypeVars()
2740
+ .find((entry) => entry.typeVar.details.name === paramSpec.details.name);
2741
+ if (replacementEntry) {
2742
+ return this._typeVarContext.getParamSpecType(replacementEntry.typeVar);
2743
+ }
2744
+ if (paramSpec.details.defaultType) {
2745
+ return convertTypeToParamSpecValue(this.apply(paramSpec.details.defaultType, recursionCount));
2746
+ }
2747
+ return this._getUnknownParamSpec();
2748
+ }
2598
2749
  if (!paramSpec.scopeId || !this._typeVarContext.hasSolveForScope(paramSpec.scopeId)) {
2599
2750
  return undefined;
2600
2751
  }
2601
- const transformedParamSpec = this._typeVarContext.getParamSpec(paramSpec);
2752
+ const transformedParamSpec = this._typeVarContext.getParamSpecType(paramSpec);
2602
2753
  if (transformedParamSpec) {
2603
2754
  return transformedParamSpec;
2604
2755
  }
2605
- if (this._unknownIfNotFound && !this._typeVarContext.hasSolveForScope(types_1.WildcardTypeVarScopeId)) {
2756
+ if (this._options.unknownIfNotFound && !this._typeVarContext.hasSolveForScope(types_1.WildcardTypeVarScopeId)) {
2606
2757
  // Use the default value if there is one.
2607
- if (paramSpec.details.defaultType && (0, types_1.isFunction)(paramSpec.details.defaultType)) {
2608
- const funcType = paramSpec.details.defaultType;
2609
- return {
2610
- flags: funcType.details.flags,
2611
- parameters: funcType.details.parameters,
2612
- typeVarScopeId: funcType.details.typeVarScopeId,
2613
- docString: funcType.details.docString,
2614
- paramSpec: funcType.details.paramSpec,
2615
- };
2758
+ if (paramSpec.details.defaultType) {
2759
+ return convertTypeToParamSpecValue(this._solveDefaultType(paramSpec.details.defaultType, recursionCount));
2616
2760
  }
2617
2761
  // Convert to the ParamSpec equivalent of "Unknown".
2618
- const paramSpecValue = {
2619
- flags: 32768 /* SkipArgsKwargsCompatibilityCheck */,
2620
- parameters: types_1.FunctionType.getDefaultParameters(/* useUnknown */ true),
2621
- typeVarScopeId: undefined,
2622
- docString: undefined,
2623
- paramSpec: undefined,
2624
- };
2625
- return paramSpecValue;
2762
+ return this._getUnknownParamSpec();
2626
2763
  }
2627
2764
  return undefined;
2628
2765
  }
2766
+ _solveDefaultType(defaultType, recursionCount) {
2767
+ const wasSolvingDefaultType = this._isSolvingDefaultType;
2768
+ this._isSolvingDefaultType = true;
2769
+ const result = this.apply(defaultType, recursionCount);
2770
+ this._isSolvingDefaultType = wasSolvingDefaultType;
2771
+ return result;
2772
+ }
2773
+ _getUnknownParamSpec() {
2774
+ const paramSpecValue = types_1.FunctionType.createInstance('', '', '', 65536 /* ParamSpecValue */ | 32768 /* SkipArgsKwargsCompatibilityCheck */);
2775
+ types_1.FunctionType.addDefaultParameters(paramSpecValue);
2776
+ return paramSpecValue;
2777
+ }
2629
2778
  }
2630
2779
  class ExpectedConstructorTypeTransformer extends TypeVarTransformer {
2631
2780
  constructor(_liveTypeVarScopes) {