@zzzen/pyright-internal 1.2.0-dev.20240929 → 1.2.0-dev.20241006

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 (49) hide show
  1. package/dist/analyzer/checker.js +7 -7
  2. package/dist/analyzer/checker.js.map +1 -1
  3. package/dist/analyzer/codeFlowEngine.js +1 -1
  4. package/dist/analyzer/codeFlowEngine.js.map +1 -1
  5. package/dist/analyzer/constraintSolver.js +3 -12
  6. package/dist/analyzer/constraintSolver.js.map +1 -1
  7. package/dist/analyzer/constraintTracker.d.ts +0 -1
  8. package/dist/analyzer/constraintTracker.js +0 -6
  9. package/dist/analyzer/constraintTracker.js.map +1 -1
  10. package/dist/analyzer/dataClasses.js +3 -3
  11. package/dist/analyzer/dataClasses.js.map +1 -1
  12. package/dist/analyzer/patternMatching.js +12 -1
  13. package/dist/analyzer/patternMatching.js.map +1 -1
  14. package/dist/analyzer/protocols.js +9 -20
  15. package/dist/analyzer/protocols.js.map +1 -1
  16. package/dist/analyzer/tuples.d.ts +8 -3
  17. package/dist/analyzer/tuples.js +155 -0
  18. package/dist/analyzer/tuples.js.map +1 -1
  19. package/dist/analyzer/typeEvaluator.js +209 -308
  20. package/dist/analyzer/typeEvaluator.js.map +1 -1
  21. package/dist/analyzer/typeEvaluatorTypes.d.ts +9 -14
  22. package/dist/analyzer/typeEvaluatorTypes.js +7 -1
  23. package/dist/analyzer/typeEvaluatorTypes.js.map +1 -1
  24. package/dist/analyzer/typePrinter.d.ts +2 -1
  25. package/dist/analyzer/typePrinter.js +12 -10
  26. package/dist/analyzer/typePrinter.js.map +1 -1
  27. package/dist/analyzer/typeStubWriter.js +1 -1
  28. package/dist/analyzer/typeStubWriter.js.map +1 -1
  29. package/dist/analyzer/typeUtils.d.ts +10 -3
  30. package/dist/analyzer/typeUtils.js +4 -12
  31. package/dist/analyzer/typeUtils.js.map +1 -1
  32. package/dist/analyzer/types.d.ts +1 -1
  33. package/dist/analyzer/types.js +14 -14
  34. package/dist/analyzer/types.js.map +1 -1
  35. package/dist/common/collectionUtils.d.ts +2 -0
  36. package/dist/common/collectionUtils.js +15 -0
  37. package/dist/common/collectionUtils.js.map +1 -1
  38. package/dist/localization/package.nls.zh-cn.json +5 -5
  39. package/dist/localization/package.nls.zh-tw.json +5 -5
  40. package/dist/tests/typeEvaluator3.test.js +1 -1
  41. package/dist/tests/typeEvaluator4.test.js +1 -1
  42. package/dist/tests/typeEvaluator4.test.js.map +1 -1
  43. package/dist/tests/typeEvaluator5.test.js +3 -3
  44. package/dist/tests/typeEvaluator5.test.js.map +1 -1
  45. package/dist/tests/typeEvaluator6.test.js +6 -0
  46. package/dist/tests/typeEvaluator6.test.js.map +1 -1
  47. package/dist/tests/typeEvaluator7.test.js +0 -6
  48. package/dist/tests/typeEvaluator7.test.js.map +1 -1
  49. package/package.json +1 -1
@@ -152,15 +152,6 @@ const maxOverloadUnionExpansionCount = 64;
152
152
  // Maximum number of recursive function return type inference attempts
153
153
  // that can be concurrently pending before we give up.
154
154
  const maxInferFunctionReturnRecursionCount = 12;
155
- // In certain loops, it's possible to construct arbitrarily-deep containers
156
- // (tuples, lists, sets, or dicts) which can lead to infinite type analysis.
157
- // This limits the depth.
158
- const maxInferredContainerDepth = 8;
159
- // If a tuple expression with no declared type contains a large number
160
- // of elements, it can cause performance issues. This value limits the
161
- // number of elements that will be included in the tuple type before
162
- // we default to tuple[Unknown, ...].
163
- const maxInferredTupleEntryCount = 256;
164
155
  // Maximum recursion amount when comparing two recursive type aliases.
165
156
  // Increasing this can greatly increase the time required to evaluate
166
157
  // two recursive type aliases that have the same definition. Decreasing
@@ -639,7 +630,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
639
630
  break;
640
631
  }
641
632
  case 52 /* ParseNodeType.Tuple */: {
642
- typeResult = getTypeOfTuple(node, flags, inferenceContext);
633
+ typeResult = (0, tuples_1.getTypeOfTuple)(evaluatorInterface, node, flags, inferenceContext);
643
634
  break;
644
635
  }
645
636
  case 14 /* ParseNodeType.Constant */: {
@@ -863,8 +854,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
863
854
  typeResult = { type: types_1.AnyType.create(/* isEllipsis */ true) };
864
855
  }
865
856
  else {
866
- const ellipsisType = (_b = (_a = getBuiltInObject(node, 'EllipsisType')) !== null && _a !== void 0 ? _a : getBuiltInObject(node, 'ellipsis')) !== null && _b !== void 0 ? _b : types_1.AnyType.create();
867
- typeResult = { type: ellipsisType };
857
+ if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0 && (flags & 65536 /* EvalFlags.AllowEllipsis */) === 0) {
858
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.ellipsisContext(), node);
859
+ typeResult = { type: types_1.UnknownType.create() };
860
+ }
861
+ else {
862
+ const ellipsisType = (_b = (_a = getBuiltInObject(node, 'EllipsisType')) !== null && _a !== void 0 ? _a : getBuiltInObject(node, 'ellipsis')) !== null && _b !== void 0 ? _b : types_1.AnyType.create();
863
+ typeResult = { type: ellipsisType };
864
+ }
868
865
  }
869
866
  return typeResult;
870
867
  }
@@ -2645,7 +2642,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
2645
2642
  var _a;
2646
2643
  if ((0, types_1.isParamSpec)(subtype)) {
2647
2644
  if (subtype.priv.paramSpecAccess === 'args') {
2648
- return makeTupleObject([{ type: getObjectType(), isUnbounded: true }]);
2645
+ return (0, tuples_1.makeTupleObject)(evaluatorInterface, [{ type: getObjectType(), isUnbounded: true }]);
2649
2646
  }
2650
2647
  else if (subtype.priv.paramSpecAccess === 'kwargs') {
2651
2648
  if (dictClass && (0, types_1.isInstantiableClass)(dictClass) && strClass && (0, types_1.isInstantiableClass)(strClass)) {
@@ -2676,7 +2673,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
2676
2673
  return types_1.AnyType.create();
2677
2674
  }
2678
2675
  // Fall back to "*tuple[object, ...]".
2679
- return makeTupleObject([{ type: getObjectType(), isUnbounded: true }], /* isUnpacked */ true);
2676
+ return (0, tuples_1.makeTupleObject)(evaluatorInterface, [{ type: getObjectType(), isUnbounded: true }],
2677
+ /* isUnpacked */ true);
2680
2678
  }
2681
2679
  if ((0, types_1.isTypeVar)(subtype)) {
2682
2680
  // If this is a recursive type alias placeholder
@@ -4793,7 +4791,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
4793
4791
  }
4794
4792
  });
4795
4793
  }
4796
- const tupleObject = makeTupleObject(variadicTypes, /* isUnpacked */ true);
4794
+ const tupleObject = (0, tuples_1.makeTupleObject)(evaluatorInterface, variadicTypes, /* isUnpacked */ true);
4797
4795
  typeArgs = [
4798
4796
  ...typeArgs.slice(0, variadicIndex),
4799
4797
  { node: typeArgs[variadicIndex].node, type: tupleObject },
@@ -4805,7 +4803,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
4805
4803
  // Add an empty tuple that maps to the TypeVarTuple type parameter.
4806
4804
  typeArgs.push({
4807
4805
  node: errorNode,
4808
- type: makeTupleObject([], /* isUnpacked */ true),
4806
+ type: (0, tuples_1.makeTupleObject)(evaluatorInterface, [], /* isUnpacked */ true),
4809
4807
  });
4810
4808
  }
4811
4809
  }
@@ -4853,7 +4851,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
4853
4851
  });
4854
4852
  }
4855
4853
  else if ((0, types_1.isTypeVarTuple)(param) && tupleClass && (0, types_1.isInstantiableClass)(tupleClass)) {
4856
- defaultType = makeTupleObject([{ type: types_1.UnknownType.create(), isUnbounded: true }],
4854
+ defaultType = (0, tuples_1.makeTupleObject)(evaluatorInterface, [{ type: types_1.UnknownType.create(), isUnbounded: true }],
4857
4855
  /* isUnpacked */ true);
4858
4856
  }
4859
4857
  else {
@@ -4898,22 +4896,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
4898
4896
  }
4899
4897
  inferVarianceForTypeAlias(baseType);
4900
4898
  const typeParams = aliasInfo.shared.typeParams;
4901
- let typeArgs = adjustTypeArgsForTypeVarTuple(getTypeArgs(node, flags), typeParams, node);
4899
+ let typeArgs;
4900
+ typeArgs = adjustTypeArgsForTypeVarTuple(getTypeArgs(node, flags), typeParams, node);
4902
4901
  let reportedError = false;
4903
- // PEP 612 says that if the class has only one type parameter consisting
4904
- // of a ParamSpec, the list of arguments does not need to be enclosed in
4905
- // a list. We'll handle that case specially here. Presumably this applies to
4906
- // type aliases as well.
4907
- if (typeParams.length === 1 && (0, types_1.isParamSpec)(typeParams[0]) && typeArgs) {
4908
- if (typeArgs.every((typeArg) => !(0, typeUtils_1.isEllipsisType)(typeArg.type) && !typeArg.typeList && !(0, types_1.isParamSpec)(typeArg.type))) {
4909
- typeArgs = [
4910
- {
4911
- type: types_1.UnknownType.create(),
4912
- node: typeArgs.length > 0 ? typeArgs[0].node : node,
4913
- typeList: typeArgs,
4914
- },
4915
- ];
4916
- }
4902
+ typeArgs = transformTypeArgsForParamSpec(typeParams, typeArgs, node);
4903
+ if (!typeArgs) {
4904
+ typeArgs = [];
4905
+ reportedError = true;
4917
4906
  }
4918
4907
  let minTypeArgCount = typeParams.length;
4919
4908
  const firstDefaultParamIndex = typeParams.findIndex((param) => !!param.shared.isDefaultExplicit);
@@ -4950,8 +4939,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
4950
4939
  const typeList = typeArgs[index].typeList;
4951
4940
  if (typeList) {
4952
4941
  const functionType = types_1.FunctionType.createSynthesizedInstance('', 65536 /* FunctionTypeFlags.ParamSpecValue */);
4953
- typeList.forEach((paramType, paramIndex) => {
4954
- types_1.FunctionType.addParam(functionType, types_1.FunctionParam.create(0 /* ParamCategory.Simple */, (0, typeUtils_1.convertToInstance)(paramType.type), types_1.FunctionParamFlags.NameSynthesized | types_1.FunctionParamFlags.TypeDeclared, `__p${paramIndex}`));
4942
+ typeList.forEach((paramTypeResult, paramIndex) => {
4943
+ let paramType = paramTypeResult.type;
4944
+ if (!validateTypeArg(paramTypeResult)) {
4945
+ paramType = types_1.UnknownType.create();
4946
+ }
4947
+ types_1.FunctionType.addParam(functionType, types_1.FunctionParam.create(0 /* ParamCategory.Simple */, (0, typeUtils_1.convertToInstance)(paramType), types_1.FunctionParamFlags.NameSynthesized | types_1.FunctionParamFlags.TypeDeclared, `__p${paramIndex}`));
4955
4948
  });
4956
4949
  if (typeList.length > 0) {
4957
4950
  types_1.FunctionType.addPositionOnlyParamSeparator(functionType);
@@ -5283,14 +5276,17 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5283
5276
  // Looks at uses of the type parameters within the type and adjusts the
5284
5277
  // variances accordingly. For example, if the type is `Mapping[T1, T2]`,
5285
5278
  // then T1 will be set to invariant and T2 will be set to covariant.
5286
- function updateUsageVariancesRecursive(type, typeAliasTypeParams, usageVariances, varianceContext, recursionCount = 0) {
5279
+ function updateUsageVariancesRecursive(type, typeAliasTypeParams, usageVariances, varianceContext, pendingTypes = [], recursionCount = 0) {
5287
5280
  if (recursionCount > types_1.maxTypeRecursionCount) {
5288
5281
  return;
5289
5282
  }
5290
5283
  const transformedType = (0, typeUtils_1.transformPossibleRecursiveTypeAlias)(type);
5291
- // If this is a recursive type alias, use a lower recursion limit.
5284
+ // If this is a recursive type alias, see if we've already recursed
5285
+ // seen it once before in the recursion stack. If so, don't recurse
5286
+ // further.
5292
5287
  if (transformedType !== type) {
5293
- if (recursionCount > maxRecursiveTypeAliasRecursionCount) {
5288
+ const pendingOverlaps = pendingTypes.filter((pendingType) => (0, types_1.isTypeSame)(pendingType, type));
5289
+ if (pendingOverlaps.length > 1) {
5294
5290
  return;
5295
5291
  }
5296
5292
  }
@@ -5303,7 +5299,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5303
5299
  usageVariances[typeParamIndex] = (0, typeUtils_1.combineVariances)(usageVariances[typeParamIndex], variance);
5304
5300
  }
5305
5301
  else {
5306
- updateUsageVariancesRecursive(subtype, typeAliasTypeParams, usageVariances, variance, recursionCount);
5302
+ pendingTypes.push(type);
5303
+ updateUsageVariancesRecursive(subtype, typeAliasTypeParams, usageVariances, variance, pendingTypes, recursionCount);
5304
+ pendingTypes.pop();
5307
5305
  }
5308
5306
  });
5309
5307
  }
@@ -5349,12 +5347,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5349
5347
  }
5350
5348
  });
5351
5349
  }
5352
- function makeTupleObject(typeArgs, isUnpacked = false) {
5353
- if (tupleClass && (0, types_1.isInstantiableClass)(tupleClass)) {
5354
- return (0, typeUtils_1.convertToInstance)((0, typeUtils_1.specializeTupleClass)(tupleClass, typeArgs, /* isTypeArgExplicit */ true, isUnpacked));
5355
- }
5356
- return types_1.UnknownType.create();
5357
- }
5358
5350
  function getIndexAccessMagicMethodName(usage) {
5359
5351
  if (usage.method === 'get') {
5360
5352
  return '__getitem__';
@@ -5443,7 +5435,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5443
5435
  }
5444
5436
  else if (positionalArgs.length === 0 && unpackedListArgs.length === 0) {
5445
5437
  // Handle the case where there are no positionals provided but there are keywords.
5446
- positionalIndexType = makeTupleObject([]);
5438
+ positionalIndexType = (0, tuples_1.makeTupleObject)(evaluatorInterface, []);
5447
5439
  }
5448
5440
  else {
5449
5441
  // Package up all of the positionals into a tuple.
@@ -5464,7 +5456,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5464
5456
  const iterableType = (_b = (_a = getTypeOfIterator(typeResult, /* isAsync */ false, arg.d.valueExpr)) === null || _a === void 0 ? void 0 : _a.type) !== null && _b !== void 0 ? _b : types_1.UnknownType.create();
5465
5457
  tupleTypeArgs.push({ type: iterableType, isUnbounded: true });
5466
5458
  });
5467
- positionalIndexType = makeTupleObject(tupleTypeArgs);
5459
+ positionalIndexType = (0, tuples_1.makeTupleObject)(evaluatorInterface, tupleTypeArgs);
5468
5460
  }
5469
5461
  const argList = [
5470
5462
  {
@@ -5593,38 +5585,73 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5593
5585
  });
5594
5586
  // Set the node's type so it isn't reevaluated later.
5595
5587
  setTypeResultForNode(node.d.items[0].d.valueExpr, { type: types_1.UnknownType.create() });
5596
- }
5597
- else {
5598
- node.d.items.forEach((arg, index) => {
5599
- const typeResult = getTypeArgTypeResult(arg.d.valueExpr, index);
5600
- if (arg.d.argCategory !== 0 /* ArgCategory.Simple */) {
5601
- if (arg.d.argCategory === 1 /* ArgCategory.UnpackedList */) {
5602
- if (!(options === null || options === void 0 ? void 0 : options.isAnnotatedClass) || index === 0) {
5603
- if ((0, types_1.isTypeVarTuple)(typeResult.type) && !typeResult.type.priv.isUnpacked) {
5604
- typeResult.type = types_1.TypeVarType.cloneForUnpacked(typeResult.type);
5605
- }
5606
- else if ((0, types_1.isInstantiableClass)(typeResult.type) &&
5607
- !typeResult.type.priv.includeSubclasses &&
5608
- (0, typeUtils_1.isTupleClass)(typeResult.type)) {
5609
- typeResult.type = types_1.ClassType.cloneForUnpacked(typeResult.type);
5588
+ return typeArgs;
5589
+ }
5590
+ node.d.items.forEach((arg, index) => {
5591
+ const typeResult = getTypeArgTypeResult(arg.d.valueExpr, index);
5592
+ if (arg.d.argCategory !== 0 /* ArgCategory.Simple */) {
5593
+ if (arg.d.argCategory === 1 /* ArgCategory.UnpackedList */) {
5594
+ if (!(options === null || options === void 0 ? void 0 : options.isAnnotatedClass) || index === 0) {
5595
+ const unpackedType = applyUnpackToTupleLike(typeResult.type);
5596
+ if (unpackedType) {
5597
+ typeResult.type = unpackedType;
5598
+ }
5599
+ else {
5600
+ if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0) {
5601
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.unpackNotAllowed(), arg.d.valueExpr);
5602
+ typeResult.typeErrors = true;
5610
5603
  }
5611
5604
  else {
5612
- addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.unpackNotAllowed(), arg.d.valueExpr);
5605
+ typeResult.type = types_1.UnknownType.create();
5613
5606
  }
5614
5607
  }
5615
5608
  }
5616
5609
  }
5617
- if (arg.d.name) {
5610
+ }
5611
+ if (arg.d.name) {
5612
+ if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0) {
5618
5613
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.keywordArgInTypeArgument(), arg.d.valueExpr);
5614
+ typeResult.typeErrors = true;
5619
5615
  }
5620
- if (arg.d.valueExpr.nodeType !== 0 /* ParseNodeType.Error */ ||
5621
- arg.d.valueExpr.d.category !== 3 /* ErrorExpressionCategory.MissingIndexOrSlice */) {
5622
- typeArgs.push(typeResult);
5616
+ else {
5617
+ typeResult.type = types_1.UnknownType.create();
5623
5618
  }
5624
- });
5625
- }
5619
+ }
5620
+ if (arg.d.valueExpr.nodeType !== 0 /* ParseNodeType.Error */ ||
5621
+ arg.d.valueExpr.d.category !== 3 /* ErrorExpressionCategory.MissingIndexOrSlice */) {
5622
+ typeArgs.push(typeResult);
5623
+ }
5624
+ });
5626
5625
  return typeArgs;
5627
5626
  }
5627
+ function applyUnpackToTupleLike(type) {
5628
+ if ((0, types_1.isTypeVarTuple)(type)) {
5629
+ if (!type.priv.isUnpacked) {
5630
+ return types_1.TypeVarType.cloneForUnpacked(type);
5631
+ }
5632
+ return undefined;
5633
+ }
5634
+ if ((0, types_1.isParamSpec)(type)) {
5635
+ return undefined;
5636
+ }
5637
+ // Is this a TypeVar that has a tuple upper bound?
5638
+ if ((0, types_1.isTypeVar)(type)) {
5639
+ const upperBound = type.shared.boundType;
5640
+ if (upperBound && (0, types_1.isClassInstance)(upperBound) && (0, typeUtils_1.isTupleClass)(upperBound)) {
5641
+ const concrete = makeTopLevelTypeVarsConcrete(type);
5642
+ if ((0, types_1.isInstantiableClass)(concrete)) {
5643
+ return types_1.ClassType.cloneForUnpacked(concrete);
5644
+ }
5645
+ }
5646
+ return undefined;
5647
+ }
5648
+ if ((0, types_1.isInstantiableClass)(type) && !type.priv.includeSubclasses) {
5649
+ if ((0, typeUtils_1.isTupleClass)(type)) {
5650
+ return types_1.ClassType.cloneForUnpacked(type);
5651
+ }
5652
+ }
5653
+ return undefined;
5654
+ }
5628
5655
  function getTypeArg(node, flags) {
5629
5656
  let typeResult;
5630
5657
  let adjustedFlags = flags | 128 /* EvalFlags.InstantiableType */ | 1 /* EvalFlags.ConvertEllipsisToAny */ | 8 /* EvalFlags.StrLiteralAsType */;
@@ -5657,138 +5684,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5657
5684
  }
5658
5685
  return typeResult;
5659
5686
  }
5660
- function getTypeOfTuple(node, flags, inferenceContext) {
5661
- var _a;
5662
- if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0 && ((_a = node.parent) === null || _a === void 0 ? void 0 : _a.nodeType) !== 1 /* ParseNodeType.Argument */) {
5663
- // This is allowed inside of an index trailer, specifically
5664
- // to support Tuple[()], which is the documented way to annotate
5665
- // a zero-length tuple.
5666
- const diag = new diagnostic_1.DiagnosticAddendum();
5667
- diag.addMessage(localize_1.LocAddendum.useTupleInstead());
5668
- addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.tupleInAnnotation() + diag.getString(), node);
5669
- return { type: types_1.UnknownType.create() };
5670
- }
5671
- if ((flags & 128 /* EvalFlags.InstantiableType */) !== 0 && node.d.items.length === 0 && !inferenceContext) {
5672
- return { type: makeTupleObject([]), isEmptyTupleShorthand: true };
5673
- }
5674
- flags &= ~(256 /* EvalFlags.TypeExpression */ | 8 /* EvalFlags.StrLiteralAsType */ | 128 /* EvalFlags.InstantiableType */);
5675
- // If the expected type is a union, recursively call for each of the subtypes
5676
- // to find one that matches.
5677
- let expectedType = inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.expectedType;
5678
- let expectedTypeContainsAny = inferenceContext && (0, types_1.isAny)(inferenceContext.expectedType);
5679
- if (inferenceContext && (0, types_1.isUnion)(inferenceContext.expectedType)) {
5680
- let matchingSubtype;
5681
- (0, typeUtils_1.doForEachSubtype)(inferenceContext.expectedType, (subtype) => {
5682
- if ((0, types_1.isAny)(subtype)) {
5683
- expectedTypeContainsAny = true;
5684
- }
5685
- if (!matchingSubtype) {
5686
- const subtypeResult = useSpeculativeMode(node, () => {
5687
- return getTypeOfTupleWithContext(node, flags, (0, typeUtils_1.makeInferenceContext)(subtype));
5688
- });
5689
- if (subtypeResult && assignType(subtype, subtypeResult.type)) {
5690
- matchingSubtype = subtype;
5691
- }
5692
- }
5693
- },
5694
- /* sortSubtypes */ true);
5695
- expectedType = matchingSubtype;
5696
- }
5697
- let expectedTypeDiagAddendum;
5698
- if (expectedType) {
5699
- const result = getTypeOfTupleWithContext(node, flags, (0, typeUtils_1.makeInferenceContext)(expectedType));
5700
- if (result && !result.typeErrors) {
5701
- return result;
5702
- }
5703
- expectedTypeDiagAddendum = result === null || result === void 0 ? void 0 : result.expectedTypeDiagAddendum;
5704
- }
5705
- const typeResult = getTypeOfTupleInferred(node, flags);
5706
- // If there was an expected type of Any, replace the resulting type
5707
- // with Any rather than return a type with unknowns.
5708
- if (expectedTypeContainsAny) {
5709
- typeResult.type = types_1.AnyType.create();
5710
- }
5711
- return { ...typeResult, expectedTypeDiagAddendum };
5712
- }
5713
- function getTypeOfTupleWithContext(node, flags, inferenceContext) {
5714
- inferenceContext.expectedType = (0, typeUtils_1.transformPossibleRecursiveTypeAlias)(inferenceContext.expectedType);
5715
- if (!(0, types_1.isClassInstance)(inferenceContext.expectedType)) {
5716
- return undefined;
5717
- }
5718
- if (!tupleClass || !(0, types_1.isInstantiableClass)(tupleClass)) {
5719
- return undefined;
5720
- }
5721
- // Build an array of expected types.
5722
- let expectedTypes = [];
5723
- if ((0, typeUtils_1.isTupleClass)(inferenceContext.expectedType) && inferenceContext.expectedType.priv.tupleTypeArgs) {
5724
- expectedTypes = inferenceContext.expectedType.priv.tupleTypeArgs.map((t) => (0, typeUtils_1.transformPossibleRecursiveTypeAlias)(t.type));
5725
- const unboundedIndex = inferenceContext.expectedType.priv.tupleTypeArgs.findIndex((t) => t.isUnbounded);
5726
- if (unboundedIndex >= 0) {
5727
- if (expectedTypes.length > node.d.items.length) {
5728
- expectedTypes.splice(unboundedIndex, 1);
5729
- }
5730
- else {
5731
- while (expectedTypes.length < node.d.items.length) {
5732
- expectedTypes.splice(unboundedIndex, 0, expectedTypes[unboundedIndex]);
5733
- }
5734
- }
5735
- }
5736
- }
5737
- else {
5738
- const tupleConstraints = new constraintTracker_1.ConstraintTracker();
5739
- if (!(0, constraintSolver_1.addConstraintsForExpectedType)(evaluatorInterface, types_1.ClassType.cloneAsInstance(tupleClass), inferenceContext.expectedType, tupleConstraints, ParseTreeUtils.getTypeVarScopesForNode(node), node.start)) {
5740
- return undefined;
5741
- }
5742
- const specializedTuple = solveAndApplyConstraints(tupleClass, tupleConstraints);
5743
- if (!specializedTuple.priv.typeArgs || specializedTuple.priv.typeArgs.length !== 1) {
5744
- return undefined;
5745
- }
5746
- const homogenousType = (0, typeUtils_1.transformPossibleRecursiveTypeAlias)(specializedTuple.priv.typeArgs[0]);
5747
- for (let i = 0; i < node.d.items.length; i++) {
5748
- expectedTypes.push(homogenousType);
5749
- }
5750
- }
5751
- const entryTypeResults = node.d.items.map((expr, index) => getTypeOfExpression(expr, flags | 268435456 /* EvalFlags.StripTupleLiterals */, (0, typeUtils_1.makeInferenceContext)(index < expectedTypes.length ? expectedTypes[index] : undefined, inferenceContext.isTypeIncomplete)));
5752
- const isIncomplete = entryTypeResults.some((result) => result.isIncomplete);
5753
- // Copy any expected type diag addenda for precision error reporting.
5754
- let expectedTypeDiagAddendum;
5755
- if (entryTypeResults.some((result) => result.expectedTypeDiagAddendum)) {
5756
- expectedTypeDiagAddendum = new diagnostic_1.DiagnosticAddendum();
5757
- entryTypeResults.forEach((result) => {
5758
- if (result.expectedTypeDiagAddendum) {
5759
- expectedTypeDiagAddendum.addAddendum(result.expectedTypeDiagAddendum);
5760
- }
5761
- });
5762
- }
5763
- // If the tuple contains a very large number of entries, it's probably
5764
- // generated code. If we encounter type errors, don't bother building
5765
- // the full tuple type.
5766
- let type;
5767
- if (node.d.items.length > maxInferredTupleEntryCount && entryTypeResults.some((result) => result.typeErrors)) {
5768
- type = makeTupleObject([{ type: types_1.UnknownType.create(), isUnbounded: true }]);
5769
- }
5770
- else {
5771
- type = makeTupleObject(buildTupleTypesList(entryTypeResults, /* stripLiterals */ false));
5772
- }
5773
- return { type, expectedTypeDiagAddendum, isIncomplete };
5774
- }
5775
- function getTypeOfTupleInferred(node, flags) {
5776
- const entryTypeResults = node.d.items.map((expr) => getTypeOfExpression(expr, flags | 268435456 /* EvalFlags.StripTupleLiterals */));
5777
- const isIncomplete = entryTypeResults.some((result) => result.isIncomplete);
5778
- // If the tuple contains a very large number of entries, it's probably
5779
- // generated code. Rather than taking the time to evaluate every entry,
5780
- // simply return an unknown type in this case.
5781
- if (node.d.items.length > maxInferredTupleEntryCount) {
5782
- return { type: makeTupleObject([{ type: types_1.UnknownType.create(), isUnbounded: true }]) };
5783
- }
5784
- const type = makeTupleObject(buildTupleTypesList(entryTypeResults, (flags & 268435456 /* EvalFlags.StripTupleLiterals */) !== 0));
5785
- if (isIncomplete) {
5786
- if ((0, typeUtils_1.getContainerDepth)(type) > maxInferredContainerDepth) {
5787
- return { type: types_1.UnknownType.create() };
5788
- }
5789
- }
5790
- return { type, isIncomplete };
5791
- }
5792
5687
  function buildTupleTypesList(entryTypeResults, stripLiterals) {
5793
5688
  const entryTypes = [];
5794
5689
  for (const typeResult of entryTypeResults) {
@@ -5999,7 +5894,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5999
5894
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportCallIssue, localize_1.LocMessage.typeFormArgs(), node);
6000
5895
  return { type: types_1.UnknownType.create() };
6001
5896
  }
6002
- const typeFormResult = getTypeOfArgExpectingType((0, typeUtils_1.convertNodeToArg)(node.d.args[0]), {
5897
+ const typeFormResult = getTypeOfArgExpectingType(convertNodeToArg(node.d.args[0]), {
6003
5898
  typeFormArg: isTypeFormSupported(node),
6004
5899
  noNonTypeSpecialForms: true,
6005
5900
  typeExpression: true,
@@ -6022,7 +5917,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
6022
5917
  if (arg0TypeResult.isIncomplete) {
6023
5918
  return { type: types_1.UnknownType.create(/* isIncomplete */ true), isIncomplete: true };
6024
5919
  }
6025
- const assertedType = (0, typeUtils_1.convertToInstance)(getTypeOfArgExpectingType((0, typeUtils_1.convertNodeToArg)(node.d.args[1]), {
5920
+ const assertedType = (0, typeUtils_1.convertToInstance)(getTypeOfArgExpectingType(convertNodeToArg(node.d.args[1]), {
6026
5921
  typeExpression: true,
6027
5922
  }).type);
6028
5923
  // We'll replace TypeGuard and TypeIs with bool for purposes of assert_type testing.
@@ -6041,6 +5936,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
6041
5936
  }
6042
5937
  return { type: arg0TypeResult.type };
6043
5938
  }
5939
+ function convertNodeToArg(node) {
5940
+ return {
5941
+ argCategory: node.d.argCategory,
5942
+ name: node.d.name,
5943
+ valueExpression: node.d.valueExpr,
5944
+ };
5945
+ }
6044
5946
  function getTypeOfRevealType(node, inferenceContext) {
6045
5947
  let arg0Value;
6046
5948
  let expectedRevealTypeNode;
@@ -6071,7 +5973,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
6071
5973
  }
6072
5974
  else if (arg.d.name.d.value === 'expected_type') {
6073
5975
  expectedRevealTypeNode = arg.d.valueExpr;
6074
- expectedRevealType = (0, typeUtils_1.convertToInstance)(getTypeOfArgExpectingType((0, typeUtils_1.convertNodeToArg)(arg), {
5976
+ expectedRevealType = (0, typeUtils_1.convertToInstance)(getTypeOfArgExpectingType(convertNodeToArg(arg), {
6075
5977
  typeExpression: true,
6076
5978
  }).type);
6077
5979
  }
@@ -7351,7 +7253,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
7351
7253
  argCategory: 1 /* ArgCategory.UnpackedList */,
7352
7254
  valueExpression: undefined,
7353
7255
  typeResult: {
7354
- type: makeTupleObject([tupleTypeArg]),
7256
+ type: (0, tuples_1.makeTupleObject)(evaluatorInterface, [tupleTypeArg]),
7355
7257
  },
7356
7258
  });
7357
7259
  }
@@ -8191,7 +8093,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
8191
8093
  specializedTuple = tupleTypeArgs[0].type;
8192
8094
  }
8193
8095
  else {
8194
- specializedTuple = makeTupleObject(tupleTypeArgs, /* isUnpacked */ true);
8096
+ specializedTuple = (0, tuples_1.makeTupleObject)(evaluatorInterface, tupleTypeArgs, /* isUnpacked */ true);
8195
8097
  }
8196
8098
  const combinedArg = {
8197
8099
  paramCategory: 1 /* ParamCategory.ArgsList */,
@@ -9100,7 +9002,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
9100
9002
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typeVarFirstArg(), firstArg.valueExpression || errorNode);
9101
9003
  }
9102
9004
  const typeVar = types_1.TypeBase.cloneAsSpecialForm(types_1.TypeVarType.createInstantiable(typeVarName, types_1.TypeVarKind.TypeVarTuple), types_1.ClassType.cloneAsInstance(classType));
9103
- typeVar.shared.defaultType = makeTupleObject([{ type: types_1.UnknownType.create(), isUnbounded: true }]);
9005
+ typeVar.shared.defaultType = (0, tuples_1.makeTupleObject)(evaluatorInterface, [
9006
+ { type: types_1.UnknownType.create(), isUnbounded: true },
9007
+ ]);
9104
9008
  // Parse the remaining parameters.
9105
9009
  for (let i = 1; i < argList.length; i++) {
9106
9010
  const paramNameNode = argList[i].name;
@@ -9222,6 +9126,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
9222
9126
  const typeResult = getTypeOfExpressionExpectingType(node, {
9223
9127
  allowParamSpec: true,
9224
9128
  allowTypeVarsWithoutScopeId: true,
9129
+ allowEllipsis: true,
9225
9130
  typeExpression: true,
9226
9131
  });
9227
9132
  if (typeResult.typeErrors) {
@@ -9752,7 +9657,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
9752
9657
  /* tupleTypeArgs */ undefined, isEmptyContainer))
9753
9658
  : types_1.UnknownType.create();
9754
9659
  if (isIncomplete) {
9755
- if ((0, typeUtils_1.getContainerDepth)(type) > maxInferredContainerDepth) {
9660
+ if ((0, typeUtils_1.getContainerDepth)(type) > typeEvaluatorTypes_1.maxInferredContainerDepth) {
9756
9661
  return { type: types_1.UnknownType.create() };
9757
9662
  }
9758
9663
  }
@@ -10119,7 +10024,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10119
10024
  /* tupleTypeArgs */ undefined, isEmptyContainer))
10120
10025
  : types_1.UnknownType.create();
10121
10026
  if (isIncomplete) {
10122
- if ((0, typeUtils_1.getContainerDepth)(type) > maxInferredContainerDepth) {
10027
+ if ((0, typeUtils_1.getContainerDepth)(type) > typeEvaluatorTypes_1.maxInferredContainerDepth) {
10123
10028
  return { type: types_1.UnknownType.create() };
10124
10029
  }
10125
10030
  }
@@ -10519,7 +10424,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10519
10424
  if (!expectedValueOrElementType || !(0, typeUtils_1.containsLiteralType)(expectedValueOrElementType)) {
10520
10425
  valueType = stripLiteralValue(valueType);
10521
10426
  }
10522
- type = makeTupleObject([
10427
+ type = (0, tuples_1.makeTupleObject)(evaluatorInterface, [
10523
10428
  { type: keyType, isUnbounded: false },
10524
10429
  { type: valueType, isUnbounded: false },
10525
10430
  ]);
@@ -11087,16 +10992,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
11087
10992
  }
11088
10993
  return classType;
11089
10994
  }
11090
- let typeArgType = typeArgs[0].type;
11091
- if ((0, types_1.isUnion)(typeArgType) && typeArgType.priv.subtypes.length === 1) {
11092
- typeArgType = typeArgType.priv.subtypes[0];
11093
- }
10995
+ const typeArgType = typeArgs[0].type;
11094
10996
  if ((flags & 2097152 /* EvalFlags.AllowUnpackedTuple */) !== 0) {
11095
- if ((0, types_1.isInstantiableClass)(typeArgType) && !typeArgType.priv.includeSubclasses && (0, typeUtils_1.isTupleClass)(typeArgType)) {
11096
- return types_1.ClassType.cloneForUnpacked(typeArgType);
11097
- }
11098
- if ((0, types_1.isTypeVarTuple)(typeArgType) && !typeArgType.priv.isUnpacked) {
11099
- return types_1.TypeVarType.cloneForUnpacked(typeArgType);
10997
+ const unpackedType = applyUnpackToTupleLike(typeArgType);
10998
+ if (unpackedType) {
10999
+ return unpackedType;
11100
11000
  }
11101
11001
  if ((flags & 256 /* EvalFlags.TypeExpression */) === 0) {
11102
11002
  return classType;
@@ -11210,44 +11110,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
11210
11110
  }
11211
11111
  // Determines whether the metadata object is compatible with the base type.
11212
11112
  function validateTypeMetadata(errorNode, baseType, metaArg) {
11213
- var _a;
11214
- // This is an experimental feature because PEP 746 hasn't been accepted.
11215
- if (!AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.enableExperimentalFeatures) {
11216
- return true;
11217
- }
11218
- if (!(0, types_1.isClass)(metaArg.type)) {
11219
- return true;
11220
- }
11221
- const supportsTypeMethod = (_a = getTypeOfBoundMember(
11222
- /* errorNode */ undefined, metaArg.type, '__supports_type__')) === null || _a === void 0 ? void 0 : _a.type;
11223
- if (!supportsTypeMethod) {
11224
- return true;
11225
- }
11226
- // "Call" the __supports_type__ method to determine if the type is supported.
11227
- const callResult = useSpeculativeMode(errorNode, () => validateCallArgs(errorNode, [
11228
- {
11229
- argCategory: 0 /* ArgCategory.Simple */,
11230
- typeResult: { type: (0, typeUtils_1.convertToInstance)(baseType) },
11231
- },
11232
- ], { type: supportsTypeMethod },
11233
- /* constraints */ undefined,
11234
- /* skipUnknownArgCheck */ true,
11235
- /* inferenceContext */ undefined));
11236
- if (!callResult.returnType) {
11237
- return true;
11238
- }
11239
- // If there are no errors and the return type is potentially truthy,
11240
- // we know that the type is supported by this metadata object.
11241
- if (!callResult.argumentErrors && canBeTruthy(callResult.returnType)) {
11242
- return true;
11243
- }
11244
- if (!callResult.isTypeIncomplete) {
11245
- addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeArguments, localize_1.LocMessage.annotatedMetadataInconsistent().format({
11246
- metadataType: printType(metaArg.type),
11247
- type: printType((0, typeUtils_1.convertToInstance)(baseType)),
11248
- }), metaArg.node);
11249
- }
11250
- return false;
11113
+ // This function was added for draft PEP 746, but the functionality
11114
+ // has been removed for now while the PEP is being revised.
11115
+ return true;
11251
11116
  }
11252
11117
  // Creates one of several "special" types that are defined in typing.pyi
11253
11118
  // but not declared in their entirety. This includes the likes of "Tuple",
@@ -11409,7 +11274,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
11409
11274
  // is allowed if it's an unpacked TypeVarTuple or tuple. None is also allowed
11410
11275
  // since it is used to define NoReturn in typeshed stubs).
11411
11276
  if (types.length === 1 && !allowSingleTypeArg && !(0, typeUtils_1.isNoneInstance)(types[0])) {
11412
- addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeArguments, localize_1.LocMessage.unionTypeArgCount(), errorNode);
11277
+ if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0) {
11278
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeArguments, localize_1.LocMessage.unionTypeArgCount(), errorNode);
11279
+ }
11413
11280
  isValidTypeForm = false;
11414
11281
  }
11415
11282
  let unionType = (0, types_1.combineTypes)(types, { skipElideRedundantLiterals: true });
@@ -11462,9 +11329,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
11462
11329
  }
11463
11330
  function transformTypeForTypeAlias(type, errorNode, typeAliasPlaceholder, isPep695TypeVarType, typeParamNodes) {
11464
11331
  var _a;
11465
- if (!types_1.TypeBase.isInstantiable(type)) {
11466
- return type;
11467
- }
11468
11332
  // If this is a recursive type alias that hasn't yet been fully resolved
11469
11333
  // (i.e. there is no boundType associated with it), don't apply the transform.
11470
11334
  if ((0, typeUtils_1.isTypeAliasPlaceholder)(type)) {
@@ -11526,6 +11390,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
11526
11390
  }), errorNode);
11527
11391
  }
11528
11392
  }
11393
+ if (!types_1.TypeBase.isInstantiable(type)) {
11394
+ return type;
11395
+ }
11529
11396
  sharedInfo.typeParams = typeParams.length > 0 ? typeParams : undefined;
11530
11397
  let typeAlias = types_1.TypeBase.cloneForTypeAlias(type, {
11531
11398
  shared: sharedInfo,
@@ -13463,7 +13330,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
13463
13330
  if ((0, types_1.isUnpackedClass)(type)) {
13464
13331
  return types_1.ClassType.cloneForUnpacked(type, /* isUnpacked */ false);
13465
13332
  }
13466
- return makeTupleObject([{ type, isUnbounded: !(0, types_1.isTypeVarTuple)(type) }]);
13333
+ return (0, tuples_1.makeTupleObject)(evaluatorInterface, [{ type, isUnbounded: !(0, types_1.isTypeVarTuple)(type) }]);
13467
13334
  }
13468
13335
  case 2 /* ParamCategory.KwargsDict */: {
13469
13336
  // Leave a ParamSpec alone.
@@ -13493,7 +13360,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
13493
13360
  awaitableFunctionType.shared.declaredReturnType = createAwaitableReturnType(node, functionType.shared.declaredReturnType, types_1.FunctionType.isGenerator(functionType));
13494
13361
  }
13495
13362
  else {
13496
- awaitableFunctionType.priv.inferredReturnType = createAwaitableReturnType(node, getFunctionInferredReturnType(functionType), types_1.FunctionType.isGenerator(functionType));
13363
+ awaitableFunctionType.priv.inferredReturnType = createAwaitableReturnType(node, getInferredReturnType(functionType), types_1.FunctionType.isGenerator(functionType));
13497
13364
  }
13498
13365
  return awaitableFunctionType;
13499
13366
  }
@@ -14883,39 +14750,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14883
14750
  // Handle ParamSpec arguments and fill in any missing type arguments with Unknown.
14884
14751
  let typeArgTypes = [];
14885
14752
  const fullTypeParams = types_1.ClassType.getTypeParams(classType);
14886
- // PEP 612 says that if the class has only one type parameter consisting
14887
- // of a ParamSpec, the list of arguments does not need to be enclosed in
14888
- // a list. We'll handle that case specially here.
14889
- if (fullTypeParams.length === 1 && (0, types_1.isParamSpec)(fullTypeParams[0]) && typeArgs) {
14890
- if (typeArgs.every((typeArg) => !(0, typeUtils_1.isEllipsisType)(typeArg.type) && !typeArg.typeList && !(0, types_1.isParamSpec)(typeArg.type))) {
14891
- if (typeArgs.length !== 1 ||
14892
- !(0, types_1.isInstantiableClass)(typeArgs[0].type) ||
14893
- !types_1.ClassType.isBuiltIn(typeArgs[0].type, 'Concatenate')) {
14894
- // Package up the type arguments into a typeList.
14895
- typeArgs =
14896
- typeArgs.length > 0
14897
- ? [
14898
- {
14899
- type: types_1.UnknownType.create(),
14900
- node: typeArgs[0].node,
14901
- typeList: typeArgs,
14902
- },
14903
- ]
14904
- : [];
14905
- }
14906
- }
14907
- else if (typeArgs.length > 1) {
14908
- const paramSpecTypeArg = typeArgs.find((typeArg) => (0, types_1.isParamSpec)(typeArg.type));
14909
- if (paramSpecTypeArg) {
14910
- isValidTypeForm = false;
14911
- addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.paramSpecContext(), paramSpecTypeArg.node);
14912
- }
14913
- const listTypeArg = typeArgs.find((typeArg) => !!typeArg.typeList);
14914
- if (listTypeArg) {
14915
- isValidTypeForm = false;
14916
- addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgListNotAllowed(), listTypeArg.node);
14917
- }
14918
- }
14753
+ typeArgs = transformTypeArgsForParamSpec(fullTypeParams, typeArgs, errorNode);
14754
+ if (!typeArgs) {
14755
+ isValidTypeForm = false;
14919
14756
  }
14920
14757
  const constraints = new constraintTracker_1.ConstraintTracker();
14921
14758
  fullTypeParams.forEach((typeParam, index) => {
@@ -15022,6 +14859,57 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
15022
14859
  }
15023
14860
  return { type: specializedClass };
15024
14861
  }
14862
+ // PEP 612 says that if the class has only one type parameter consisting
14863
+ // of a ParamSpec, the list of arguments does not need to be enclosed in
14864
+ // a list. We'll handle that case specially here.
14865
+ function transformTypeArgsForParamSpec(typeParams, typeArgs, errorNode) {
14866
+ if (typeParams.length !== 1 || !(0, types_1.isParamSpec)(typeParams[0]) || !typeArgs) {
14867
+ return typeArgs;
14868
+ }
14869
+ if (typeArgs.length > 1) {
14870
+ for (const typeArg of typeArgs) {
14871
+ if ((0, types_1.isParamSpec)(typeArg.type)) {
14872
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.paramSpecContext(), typeArg.node);
14873
+ return undefined;
14874
+ }
14875
+ if ((0, typeUtils_1.isEllipsisType)(typeArg.type)) {
14876
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.ellipsisContext(), typeArg.node);
14877
+ return undefined;
14878
+ }
14879
+ if ((0, types_1.isInstantiableClass)(typeArg.type) && types_1.ClassType.isBuiltIn(typeArg.type, 'Concatenate')) {
14880
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.concatenateContext(), typeArg.node);
14881
+ return undefined;
14882
+ }
14883
+ if (typeArg.typeList) {
14884
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgListNotAllowed(), typeArg.node);
14885
+ return undefined;
14886
+ }
14887
+ }
14888
+ }
14889
+ if (typeArgs.length === 1) {
14890
+ // Don't transform a type list.
14891
+ if (typeArgs[0].typeList) {
14892
+ return typeArgs;
14893
+ }
14894
+ const typeArgType = typeArgs[0].type;
14895
+ // Don't transform a single ParamSpec or ellipsis.
14896
+ if ((0, types_1.isParamSpec)(typeArgType) || (0, typeUtils_1.isEllipsisType)(typeArgType)) {
14897
+ return typeArgs;
14898
+ }
14899
+ // Don't transform a Concatenate.
14900
+ if ((0, types_1.isInstantiableClass)(typeArgType) && types_1.ClassType.isBuiltIn(typeArgType, 'Concatenate')) {
14901
+ return typeArgs;
14902
+ }
14903
+ }
14904
+ // Package up the type arguments into a type list.
14905
+ return [
14906
+ {
14907
+ type: types_1.UnknownType.create(),
14908
+ node: typeArgs.length > 0 ? typeArgs[0].node : errorNode,
14909
+ typeList: typeArgs,
14910
+ },
14911
+ ];
14912
+ }
15025
14913
  function getTypeOfArg(arg, inferenceContext) {
15026
14914
  var _a, _b;
15027
14915
  if (arg.typeResult) {
@@ -15091,6 +14979,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
15091
14979
  if (options === null || options === void 0 ? void 0 : options.convertEllipsisToAny) {
15092
14980
  flags |= 1 /* EvalFlags.ConvertEllipsisToAny */;
15093
14981
  }
14982
+ if (options === null || options === void 0 ? void 0 : options.allowEllipsis) {
14983
+ flags |= 65536 /* EvalFlags.AllowEllipsis */;
14984
+ }
15094
14985
  if (options === null || options === void 0 ? void 0 : options.noNonTypeSpecialForms) {
15095
14986
  flags |= 67108864 /* EvalFlags.NoNonTypeSpecialForms */;
15096
14987
  }
@@ -15783,7 +15674,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
15783
15674
  typeVar.shared.isDefaultExplicit = true;
15784
15675
  }
15785
15676
  else {
15786
- typeVar.shared.defaultType = makeTupleObject([{ type: types_1.UnknownType.create(), isUnbounded: true }]);
15677
+ typeVar.shared.defaultType = (0, tuples_1.makeTupleObject)(evaluatorInterface, [
15678
+ { type: types_1.UnknownType.create(), isUnbounded: true },
15679
+ ]);
15787
15680
  }
15788
15681
  }
15789
15682
  else {
@@ -15813,7 +15706,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
15813
15706
  if (scopeNode.nodeType === 10 /* ParseNodeType.Class */) {
15814
15707
  scopeType = 0 /* TypeVarScopeType.Class */;
15815
15708
  // Set the variance to "auto" for class-scoped TypeVars.
15816
- typeVar.shared.declaredVariance = 0 /* Variance.Auto */;
15709
+ typeVar.shared.declaredVariance =
15710
+ (0, types_1.isParamSpec)(typeVar) || (0, types_1.isTypeVarTuple)(typeVar) ? 2 /* Variance.Invariant */ : 0 /* Variance.Auto */;
15817
15711
  }
15818
15712
  else if (scopeNode.nodeType === 31 /* ParseNodeType.Function */) {
15819
15713
  scopeType = 1 /* TypeVarScopeType.Function */;
@@ -15821,7 +15715,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
15821
15715
  else {
15822
15716
  (0, debug_1.assert)(scopeNode.nodeType === 77 /* ParseNodeType.TypeAlias */);
15823
15717
  scopeType = 2 /* TypeVarScopeType.TypeAlias */;
15824
- typeVar.shared.declaredVariance = 0 /* Variance.Auto */;
15718
+ typeVar.shared.declaredVariance =
15719
+ (0, types_1.isParamSpec)(typeVar) || (0, types_1.isTypeVarTuple)(typeVar) ? 2 /* Variance.Invariant */ : 0 /* Variance.Auto */;
15825
15720
  }
15826
15721
  typeVar = types_1.TypeVarType.cloneForScopeId(typeVar, ParseTreeUtils.getScopeIdForNode(scopeNode.nodeType === 77 /* ParseNodeType.TypeAlias */ ? scopeNode.d.name : scopeNode), scopeNode.d.name.d.value, scopeType);
15827
15722
  }
@@ -16426,7 +16321,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
16426
16321
  return specializedReturnType;
16427
16322
  }
16428
16323
  if (!(options === null || options === void 0 ? void 0 : options.skipInferReturnType)) {
16429
- return getFunctionInferredReturnType(type, options === null || options === void 0 ? void 0 : options.callSiteInfo);
16324
+ return getInferredReturnType(type, options === null || options === void 0 ? void 0 : options.callSiteInfo);
16430
16325
  }
16431
16326
  return types_1.UnknownType.create();
16432
16327
  }
@@ -17043,7 +16938,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
17043
16938
  const typeParam = destType.shared.typeParams[i];
17044
16939
  const variance = types_1.TypeVarType.getVariance(typeParam);
17045
16940
  if (curSrcType.priv.tupleTypeArgs) {
17046
- typeArgType = (0, typeUtils_1.convertToInstance)(makeTupleObject(curSrcType.priv.tupleTypeArgs, /* isUnpacked */ true));
16941
+ typeArgType = (0, typeUtils_1.convertToInstance)((0, tuples_1.makeTupleObject)(evaluatorInterface, curSrcType.priv.tupleTypeArgs, /* isUnpacked */ true));
17047
16942
  }
17048
16943
  else {
17049
16944
  typeArgType = i < srcTypeArgs.length ? srcTypeArgs[i] : types_1.UnknownType.create();
@@ -18360,7 +18255,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
18360
18255
  }
18361
18256
  });
18362
18257
  if (srcTupleTypes.length !== 1 || !(0, types_1.isTypeVarTuple)(srcTupleTypes[0].type)) {
18363
- const srcPositionalsType = makeTupleObject(srcTupleTypes, /* isUnpacked */ true);
18258
+ const srcPositionalsType = (0, tuples_1.makeTupleObject)(evaluatorInterface, srcTupleTypes, /* isUnpacked */ true);
18364
18259
  // Snip out the portion of the source positionals that map to the variadic
18365
18260
  // dest parameter and replace it with a single parameter that is typed as a
18366
18261
  // tuple containing the individual types of the replaced parameters.
@@ -18616,10 +18511,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
18616
18511
  let destArgsType = destParamDetails.params[destParamDetails.argsIndex].type;
18617
18512
  let srcArgsType = srcParamDetails.params[srcParamDetails.argsIndex].type;
18618
18513
  if (!(0, types_1.isUnpacked)(destArgsType)) {
18619
- destArgsType = makeTupleObject([{ type: destArgsType, isUnbounded: true }], /* isUnpacked */ true);
18514
+ destArgsType = (0, tuples_1.makeTupleObject)(evaluatorInterface, [{ type: destArgsType, isUnbounded: true }],
18515
+ /* isUnpacked */ true);
18620
18516
  }
18621
18517
  if (!(0, types_1.isUnpacked)(srcArgsType)) {
18622
- srcArgsType = makeTupleObject([{ type: srcArgsType, isUnbounded: true }], /* isUnpacked */ true);
18518
+ srcArgsType = (0, tuples_1.makeTupleObject)(evaluatorInterface, [{ type: srcArgsType, isUnbounded: true }],
18519
+ /* isUnpacked */ true);
18623
18520
  }
18624
18521
  if (!assignParam(destArgsType, srcArgsType, destParamDetails.params[destParamDetails.argsIndex].index, diag === null || diag === void 0 ? void 0 : diag.createAddendum(), constraints, flags, recursionCount)) {
18625
18522
  canAssign = false;
@@ -18973,7 +18870,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
18973
18870
  }
18974
18871
  return assignedSubtype;
18975
18872
  }
18976
- if (!(0, types_1.isTypeVar)(declaredSubtype) && (0, types_1.isTypeVar)(assignedSubtype)) {
18873
+ if (!(0, types_1.isTypeVar)(declaredSubtype) &&
18874
+ (0, types_1.isTypeVar)(assignedSubtype) &&
18875
+ !types_1.TypeVarType.isBound(assignedSubtype)) {
18977
18876
  // If the source is an unsolved TypeVar but the declared type is concrete,
18978
18877
  // use the concrete type.
18979
18878
  return declaredSubtype;
@@ -19868,7 +19767,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
19868
19767
  return codeFlowEngine.printControlFlowGraph(flowNode, reference, callName, logger);
19869
19768
  }
19870
19769
  // Track these apis internal usages when logging is on. otherwise, it should be noop.
19871
- const getFunctionInferredReturnType = wrapWithLogger(_getInferredReturnType);
19770
+ const getInferredReturnType = wrapWithLogger(_getInferredReturnType);
19872
19771
  const evaluatorInterface = {
19873
19772
  runWithCancellationToken,
19874
19773
  getType,
@@ -19886,7 +19785,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
19886
19785
  evaluateTypesForStatement,
19887
19786
  evaluateTypesForMatchStatement,
19888
19787
  evaluateTypesForCaseStatement,
19889
- evaluateTypeOfParam: evaluateTypeOfParam,
19788
+ evaluateTypeOfParam,
19890
19789
  canBeTruthy,
19891
19790
  canBeFalsy,
19892
19791
  stripLiteralValue,
@@ -19913,7 +19812,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
19913
19812
  getTypeOfIterable,
19914
19813
  getTypeOfIterator,
19915
19814
  getGetterTypeFromProperty,
19916
- getTypeOfArg: getTypeOfArg,
19815
+ getTypeOfArg,
19816
+ convertNodeToArg,
19817
+ buildTupleTypesList,
19917
19818
  markNamesAccessed,
19918
19819
  expandPromotionTypes,
19919
19820
  makeTopLevelTypeVarsConcrete,
@@ -19925,8 +19826,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
19925
19826
  getEffectiveTypeOfSymbolForUsage,
19926
19827
  getInferredTypeOfDeclaration,
19927
19828
  getDeclaredTypeForExpression,
19928
- getFunctionDeclaredReturnType: getDeclaredReturnType,
19929
- getFunctionInferredReturnType,
19829
+ getDeclaredReturnType,
19830
+ getInferredReturnType,
19930
19831
  getBestOverloadForArgs,
19931
19832
  getBuiltInType,
19932
19833
  getTypeOfMember,