@zzzen/pyright-internal 1.2.0-dev.20240714 → 1.2.0-dev.20240721

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 (91) hide show
  1. package/dist/analyzer/checker.js +23 -21
  2. package/dist/analyzer/checker.js.map +1 -1
  3. package/dist/analyzer/codeFlowEngine.js +26 -3
  4. package/dist/analyzer/codeFlowEngine.js.map +1 -1
  5. package/dist/analyzer/constraintSolver.js +2 -2
  6. package/dist/analyzer/constraintSolver.js.map +1 -1
  7. package/dist/analyzer/constructorTransform.d.ts +1 -2
  8. package/dist/analyzer/constructorTransform.js +4 -5
  9. package/dist/analyzer/constructorTransform.js.map +1 -1
  10. package/dist/analyzer/constructors.d.ts +2 -2
  11. package/dist/analyzer/constructors.js +49 -147
  12. package/dist/analyzer/constructors.js.map +1 -1
  13. package/dist/analyzer/dataClasses.js +1 -4
  14. package/dist/analyzer/dataClasses.js.map +1 -1
  15. package/dist/analyzer/decorators.js +1 -2
  16. package/dist/analyzer/decorators.js.map +1 -1
  17. package/dist/analyzer/operations.js +135 -86
  18. package/dist/analyzer/operations.js.map +1 -1
  19. package/dist/analyzer/parameterUtils.js +2 -1
  20. package/dist/analyzer/parameterUtils.js.map +1 -1
  21. package/dist/analyzer/parseTreeUtils.d.ts +1 -0
  22. package/dist/analyzer/parseTreeUtils.js +27 -4
  23. package/dist/analyzer/parseTreeUtils.js.map +1 -1
  24. package/dist/analyzer/patternMatching.js +2 -2
  25. package/dist/analyzer/patternMatching.js.map +1 -1
  26. package/dist/analyzer/properties.js +4 -0
  27. package/dist/analyzer/properties.js.map +1 -1
  28. package/dist/analyzer/protocols.js +9 -5
  29. package/dist/analyzer/protocols.js.map +1 -1
  30. package/dist/analyzer/testWalker.js +13 -1
  31. package/dist/analyzer/testWalker.js.map +1 -1
  32. package/dist/analyzer/typeEvaluator.js +710 -564
  33. package/dist/analyzer/typeEvaluator.js.map +1 -1
  34. package/dist/analyzer/typeEvaluatorTypes.d.ts +7 -4
  35. package/dist/analyzer/typeEvaluatorTypes.js +3 -0
  36. package/dist/analyzer/typeEvaluatorTypes.js.map +1 -1
  37. package/dist/analyzer/typeGuards.js +3 -6
  38. package/dist/analyzer/typeGuards.js.map +1 -1
  39. package/dist/analyzer/typeUtils.d.ts +16 -12
  40. package/dist/analyzer/typeUtils.js +114 -46
  41. package/dist/analyzer/typeUtils.js.map +1 -1
  42. package/dist/analyzer/typedDicts.js +1 -1
  43. package/dist/analyzer/typedDicts.js.map +1 -1
  44. package/dist/analyzer/types.d.ts +11 -7
  45. package/dist/analyzer/types.js +40 -9
  46. package/dist/analyzer/types.js.map +1 -1
  47. package/dist/commands/dumpFileDebugInfoCommand.js +1 -1
  48. package/dist/languageService/hoverProvider.js +11 -3
  49. package/dist/languageService/hoverProvider.js.map +1 -1
  50. package/dist/languageService/referencesProvider.d.ts +10 -4
  51. package/dist/languageService/referencesProvider.js +22 -10
  52. package/dist/languageService/referencesProvider.js.map +1 -1
  53. package/dist/languageService/renameProvider.js +21 -4
  54. package/dist/languageService/renameProvider.js.map +1 -1
  55. package/dist/localization/localize.d.ts +4 -3
  56. package/dist/localization/localize.js +2 -1
  57. package/dist/localization/localize.js.map +1 -1
  58. package/dist/localization/package.nls.cs.json +0 -1
  59. package/dist/localization/package.nls.de.json +0 -1
  60. package/dist/localization/package.nls.en-us.json +2 -1
  61. package/dist/localization/package.nls.es.json +0 -1
  62. package/dist/localization/package.nls.fr.json +0 -1
  63. package/dist/localization/package.nls.it.json +0 -1
  64. package/dist/localization/package.nls.ja.json +0 -1
  65. package/dist/localization/package.nls.ko.json +0 -1
  66. package/dist/localization/package.nls.pl.json +0 -1
  67. package/dist/localization/package.nls.pt-br.json +0 -1
  68. package/dist/localization/package.nls.qps-ploc.json +0 -1
  69. package/dist/localization/package.nls.ru.json +0 -1
  70. package/dist/localization/package.nls.tr.json +0 -1
  71. package/dist/localization/package.nls.zh-cn.json +0 -1
  72. package/dist/localization/package.nls.zh-tw.json +0 -1
  73. package/dist/parser/parseNodes.d.ts +3 -1
  74. package/dist/parser/parseNodes.js +3 -1
  75. package/dist/parser/parseNodes.js.map +1 -1
  76. package/dist/parser/parser.js +36 -13
  77. package/dist/parser/parser.js.map +1 -1
  78. package/dist/tests/fourslash/rename.args.fourslash.d.ts +1 -0
  79. package/dist/tests/fourslash/rename.args.fourslash.js +71 -0
  80. package/dist/tests/fourslash/rename.args.fourslash.js.map +1 -0
  81. package/dist/tests/typeEvaluator1.test.js +17 -0
  82. package/dist/tests/typeEvaluator1.test.js.map +1 -1
  83. package/dist/tests/typeEvaluator2.test.js +4 -0
  84. package/dist/tests/typeEvaluator2.test.js.map +1 -1
  85. package/dist/tests/typeEvaluator4.test.js +10 -6
  86. package/dist/tests/typeEvaluator4.test.js.map +1 -1
  87. package/dist/tests/typeEvaluator6.test.js +6 -2
  88. package/dist/tests/typeEvaluator6.test.js.map +1 -1
  89. package/dist/tests/typeEvaluator7.test.js +7 -3
  90. package/dist/tests/typeEvaluator7.test.js.map +1 -1
  91. package/package.json +1 -1
@@ -188,6 +188,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
188
188
  const returnTypeInferenceContextStack = [];
189
189
  let returnTypeInferenceTypeCache;
190
190
  let isPrefetchedTypesInitialized = false;
191
+ const signatureTrackerStack = [];
191
192
  // Various types prefetched from stdlib stubs
192
193
  let noneTypeClass;
193
194
  let objectClass;
@@ -500,7 +501,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
500
501
  }
501
502
  }
502
503
  }
503
- function getTypeOfExpression(node, flags = 0 /* EvalFlags.None */, inferenceContext, signatureTracker) {
504
+ function getTypeOfExpression(node, flags = 0 /* EvalFlags.None */, inferenceContext) {
504
505
  // Is this type already cached?
505
506
  const cacheEntry = readTypeCacheEntry(node);
506
507
  if (cacheEntry &&
@@ -538,7 +539,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
538
539
  // needed in assignType, but we don't have access to the parse tree
539
540
  // at that point.
540
541
  initializePrefetchedTypes(node);
541
- let typeResult = getTypeOfExpressionCore(node, flags, inferenceContext, signatureTracker);
542
+ let typeResult = getTypeOfExpressionCore(node, flags, inferenceContext);
542
543
  // Should we disable type promotions for bytes?
543
544
  if ((0, types_1.isInstantiableClass)(typeResult.type) &&
544
545
  typeResult.type.priv.includePromotions &&
@@ -555,16 +556,16 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
555
556
  // the target name node won't have a corresponding type cached speculatively.
556
557
  const allowSpeculativeCaching = node.nodeType !== 4 /* ParseNodeType.AssignmentExpression */;
557
558
  writeTypeCache(node, typeResult, flags, inferenceContext, allowSpeculativeCaching);
559
+ if (node.nodeType === 38 /* ParseNodeType.Name */ || node.nodeType === 35 /* ParseNodeType.MemberAccess */) {
560
+ // If this is a generic function and there is a signature tracker,
561
+ // make sure the signature is unique.
562
+ typeResult.type = ensureSignatureIsUnique(typeResult.type, node);
563
+ }
558
564
  // If there was an expected type, make sure that the result type is compatible.
559
565
  if (inferenceContext &&
560
566
  !(0, types_1.isAnyOrUnknown)(inferenceContext.expectedType) &&
561
567
  !(0, types_1.isNever)(inferenceContext.expectedType)) {
562
568
  expectedTypeCache.set(node.id, inferenceContext.expectedType);
563
- // If this is a generic function and there is a signature tracker,
564
- // make sure the signature is unique.
565
- if (signatureTracker && (0, types_1.isFunction)(typeResult.type)) {
566
- typeResult.type = (0, typeUtils_1.ensureFunctionSignaturesAreUnique)(typeResult.type, signatureTracker, node.start);
567
- }
568
569
  if (!typeResult.isIncomplete && !typeResult.expectedTypeDiagAddendum) {
569
570
  const diag = new diagnostic_1.DiagnosticAddendum();
570
571
  // Make sure the resulting type is assignable to the expected type.
@@ -584,7 +585,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
584
585
  return typeResult;
585
586
  }
586
587
  // This is a helper function that implements the core of getTypeOfExpression.
587
- function getTypeOfExpressionCore(node, flags = 0 /* EvalFlags.None */, inferenceContext, signatureTracker) {
588
+ function getTypeOfExpressionCore(node, flags = 0 /* EvalFlags.None */, inferenceContext) {
588
589
  var _a;
589
590
  let typeResult;
590
591
  let expectingInstantiable = (flags & 128 /* EvalFlags.InstantiableType */) !== 0;
@@ -602,11 +603,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
602
603
  break;
603
604
  }
604
605
  case 9 /* ParseNodeType.Call */: {
605
- typeResult = getTypeOfCall(node, flags, inferenceContext, signatureTracker);
606
+ typeResult = useSignatureTracker(node, () => getTypeOfCall(node, flags, inferenceContext));
606
607
  break;
607
608
  }
608
609
  case 52 /* ParseNodeType.Tuple */: {
609
- typeResult = getTypeOfTuple(node, flags, inferenceContext, signatureTracker);
610
+ typeResult = getTypeOfTuple(node, flags, inferenceContext);
610
611
  break;
611
612
  }
612
613
  case 14 /* ParseNodeType.Constant */: {
@@ -679,7 +680,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
679
680
  break;
680
681
  }
681
682
  case 3 /* ParseNodeType.Assignment */: {
682
- typeResult = getTypeOfExpression(node.d.rightExpr, flags, inferenceContext, signatureTracker);
683
+ typeResult = getTypeOfExpression(node.d.rightExpr, flags, inferenceContext);
683
684
  assignTypeToExpression(node.d.leftExpr, typeResult, node.d.rightExpr,
684
685
  /* ignoreEmptyContainers */ true,
685
686
  /* allowAssignmentToFinalVar */ true);
@@ -687,9 +688,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
687
688
  }
688
689
  case 4 /* ParseNodeType.AssignmentExpression */: {
689
690
  if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0) {
690
- addError(localize_1.LocMessage.walrusNotAllowed(), node);
691
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.walrusNotAllowed(), node);
691
692
  }
692
- typeResult = getTypeOfExpression(node.d.rightExpr, flags, inferenceContext, signatureTracker);
693
+ typeResult = getTypeOfExpression(node.d.rightExpr, flags, inferenceContext);
693
694
  assignTypeToExpression(node.d.name, typeResult, node.d.rightExpr, /* ignoreEmptyContainers */ true);
694
695
  break;
695
696
  }
@@ -778,7 +779,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
778
779
  }
779
780
  if ((flags & 64 /* EvalFlags.NoTypeVarTuple */) !== 0) {
780
781
  if ((0, types_1.isVariadicTypeVar)(typeResult.type) && !typeResult.type.priv.isVariadicInUnion) {
781
- addError(localize_1.LocMessage.typeVarTupleContext(), node);
782
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeVarTupleContext(), node);
782
783
  typeResult.type = types_1.UnknownType.create();
783
784
  }
784
785
  }
@@ -803,7 +804,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
803
804
  }
804
805
  function getTypeOfAwaitOperator(node, flags, inferenceContext) {
805
806
  if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0) {
806
- addError(localize_1.LocMessage.awaitNotAllowed(), node);
807
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.awaitNotAllowed(), node);
807
808
  return { type: types_1.UnknownType.create() };
808
809
  }
809
810
  const expectedType = inferenceContext
@@ -869,7 +870,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
869
870
  typeResult = { type: types_1.ClassType.cloneForUnpacked(iterType) };
870
871
  }
871
872
  else if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0) {
872
- addError(localize_1.LocMessage.unpackInAnnotation(), node, node.d.starToken);
873
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.unpackInAnnotation(), node, node.d.starToken);
873
874
  typeResult = { type: types_1.UnknownType.create() };
874
875
  }
875
876
  else {
@@ -1118,6 +1119,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
1118
1119
  if ((0, analyzerFileInfo_1.isAnnotationEvaluationPostponed)(fileInfo)) {
1119
1120
  evaluatorFlags |= 4 /* EvalFlags.ForwardRefs */;
1120
1121
  }
1122
+ if (options === null || options === void 0 ? void 0 : options.enforceClassTypeVarScope) {
1123
+ evaluatorFlags |= 1073741824 /* EvalFlags.EnforceClassTypeVarScope */;
1124
+ }
1121
1125
  // If the annotation is part of a comment, allow forward references
1122
1126
  // even if it's not enclosed in quotes.
1123
1127
  if (((_a = node === null || node === void 0 ? void 0 : node.parent) === null || _a === void 0 ? void 0 : _a.nodeType) === 3 /* ParseNodeType.Assignment */ && node.parent.d.annotationComment === node) {
@@ -1561,8 +1565,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
1561
1565
  useSpeculativeMode(callNode, () => {
1562
1566
  callResult = validateArgs(exprNode, argList, { type }, new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(type)),
1563
1567
  /* skipUnknownArgCheck */ true,
1564
- /* inferenceContext */ undefined,
1565
- /* signatureTracker */ undefined);
1568
+ /* inferenceContext */ undefined);
1566
1569
  });
1567
1570
  signatures.push({
1568
1571
  type: expandTypedKwargs(type),
@@ -1781,7 +1784,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
1781
1784
  }
1782
1785
  if (classOrObjectBase) {
1783
1786
  if (memberAccessClass && (0, types_1.isInstantiableClass)(memberAccessClass)) {
1784
- declaredType = (0, typeUtils_1.partiallySpecializeType)(declaredType, memberAccessClass);
1787
+ declaredType = (0, typeUtils_1.partiallySpecializeType)(declaredType, memberAccessClass, getTypeClassType());
1785
1788
  }
1786
1789
  if ((0, types_1.isFunction)(declaredType) || (0, types_1.isOverloadedFunction)(declaredType)) {
1787
1790
  if (bindFunction) {
@@ -2010,6 +2013,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
2010
2013
  function getUnionClassType() {
2011
2014
  return unionTypeClass !== null && unionTypeClass !== void 0 ? unionTypeClass : types_1.UnknownType.create();
2012
2015
  }
2016
+ function getTypeClassType() {
2017
+ if (typeClass && (0, types_1.isInstantiableClass)(typeClass)) {
2018
+ return typeClass;
2019
+ }
2020
+ return undefined;
2021
+ }
2013
2022
  function getTypingType(node, symbolName) {
2014
2023
  var _a;
2015
2024
  return ((_a = getTypeOfModule(node, symbolName, ['typing'])) !== null && _a !== void 0 ? _a : getTypeOfModule(node, symbolName, ['typing_extensions']));
@@ -2123,9 +2132,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
2123
2132
  function addInformation(message, node, range) {
2124
2133
  return addDiagnosticWithSuppressionCheck('information', message, node, range);
2125
2134
  }
2126
- function addError(message, node, range) {
2127
- return addDiagnosticWithSuppressionCheck('error', message, node, range);
2128
- }
2129
2135
  function addUnusedCode(node, textRange) {
2130
2136
  if (!isDiagnosticSuppressedForNode(node)) {
2131
2137
  const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
@@ -2307,6 +2313,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
2307
2313
  var _a;
2308
2314
  const baseTypeResult = getTypeOfExpression(target.d.leftExpr, 2 /* EvalFlags.MemberAccessBaseDefaults */);
2309
2315
  const baseType = makeTopLevelTypeVarsConcrete(baseTypeResult.type);
2316
+ let enclosingClass;
2310
2317
  // Handle member accesses (e.g. self.x or cls.y).
2311
2318
  if (target.d.leftExpr.nodeType === 38 /* ParseNodeType.Name */) {
2312
2319
  // Determine whether we're writing to a class or instance member.
@@ -2314,6 +2321,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
2314
2321
  if (enclosingClassNode) {
2315
2322
  const classTypeResults = getTypeOfClass(enclosingClassNode);
2316
2323
  if (classTypeResults && (0, types_1.isInstantiableClass)(classTypeResults.classType)) {
2324
+ enclosingClass = classTypeResults.classType;
2317
2325
  if ((0, types_1.isClassInstance)(baseType)) {
2318
2326
  if (types_1.ClassType.isSameGenericClass(baseType, classTypeResults.classType)) {
2319
2327
  assignTypeToMemberVariable(target, typeResult, /* isInstanceMember */ true, srcExpr);
@@ -2333,7 +2341,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
2333
2341
  return !ParseTreeUtils.getEnclosingFunction(decl.node);
2334
2342
  });
2335
2343
  if (classLevelDecls.length === 0) {
2336
- addError(localize_1.LocMessage.assignmentInProtocol(), target.d.member);
2344
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.assignmentInProtocol(), target.d.member);
2337
2345
  }
2338
2346
  }
2339
2347
  }
@@ -2354,8 +2362,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
2354
2362
  isIncomplete: typeResult.isIncomplete,
2355
2363
  memberAccessDeprecationInfo: setTypeResult.memberAccessDeprecationInfo,
2356
2364
  };
2357
- writeTypeCache(target.d.member, resultToCache, 0 /* EvalFlags.None */);
2358
2365
  writeTypeCache(target, resultToCache, 0 /* EvalFlags.None */);
2366
+ // If the target is an instance or class variable, update any class-scoped
2367
+ // type variables so the inferred type of the variable uses "external"
2368
+ // type variables.
2369
+ let memberResultToCache = resultToCache;
2370
+ if (enclosingClass === null || enclosingClass === void 0 ? void 0 : enclosingClass.shared.typeVarScopeId) {
2371
+ memberResultToCache = {
2372
+ ...resultToCache,
2373
+ type: (0, typeUtils_1.updateTypeWithExternalTypeVars)(resultToCache.type, [enclosingClass.shared.typeVarScopeId]),
2374
+ };
2375
+ }
2376
+ writeTypeCache(target.d.member, memberResultToCache, 0 /* EvalFlags.None */);
2359
2377
  }
2360
2378
  function assignTypeToMemberVariable(node, typeResult, isInstanceMember, srcExprNode) {
2361
2379
  var _a;
@@ -2636,6 +2654,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
2636
2654
  }
2637
2655
  // Fall back to a bound of "object" if no bound is provided.
2638
2656
  let boundType = (_a = subtype.shared.boundType) !== null && _a !== void 0 ? _a : getObjectType();
2657
+ // If this is a synthesized self/cls type var, self-specialize its type arguments.
2658
+ if (subtype.shared.isSynthesizedSelf && (0, types_1.isClass)(boundType)) {
2659
+ boundType = (0, typeUtils_1.selfSpecializeClass)(boundType, {
2660
+ useInternalTypeVars: types_1.TypeVarType.hasInternalScopeId(subtype),
2661
+ });
2662
+ }
2639
2663
  boundType = types_1.TypeBase.isInstantiable(subtype) ? (0, typeUtils_1.convertToInstantiable)(boundType) : boundType;
2640
2664
  return (0, typeUtils_1.addConditionToType)(boundType, [{ typeVar: subtype, constraintIndex: 0 }]);
2641
2665
  }
@@ -2767,7 +2791,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
2767
2791
  const typeVarTarget = target.nodeType === 54 /* ParseNodeType.TypeAnnotation */ ? target.d.valueExpr : target;
2768
2792
  if (typeVarTarget.nodeType !== 38 /* ParseNodeType.Name */ ||
2769
2793
  typeVarTarget.d.value !== typeResult.type.shared.name) {
2770
- addError(typeResult.type.shared.isParamSpec
2794
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, typeResult.type.shared.isParamSpec
2771
2795
  ? localize_1.LocMessage.paramSpecAssignedName().format({
2772
2796
  name: types_1.TypeVarType.getReadableName(typeResult.type),
2773
2797
  })
@@ -2854,7 +2878,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
2854
2878
  break;
2855
2879
  }
2856
2880
  default: {
2857
- addError(localize_1.LocMessage.assignmentTargetExpr(), target);
2881
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.assignmentTargetExpr(), target);
2858
2882
  break;
2859
2883
  }
2860
2884
  }
@@ -2883,8 +2907,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
2883
2907
  suppressDiagnostics(node.d.typeExpression, () => {
2884
2908
  callResult = (0, constructors_1.validateConstructorArguments)(evaluatorInterface, node.d.typeExpression, [], concreteSubtype,
2885
2909
  /* skipUnknownArgCheck */ false,
2886
- /* inferenceContext */ undefined,
2887
- /* signatureTracker */ undefined);
2910
+ /* inferenceContext */ undefined);
2888
2911
  });
2889
2912
  if (callResult && callResult.argumentErrors) {
2890
2913
  diagAddendum.addMessage(localize_1.LocMessage.exceptionTypeNotInstantiable().format({
@@ -3079,7 +3102,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
3079
3102
  }
3080
3103
  if ((0, types_1.isParamSpec)(type)) {
3081
3104
  if (flags & 32 /* EvalFlags.NoParamSpec */) {
3082
- addError(localize_1.LocMessage.paramSpecContext(), node);
3105
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.paramSpecContext(), node);
3083
3106
  type = types_1.UnknownType.create();
3084
3107
  }
3085
3108
  }
@@ -3221,89 +3244,137 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
3221
3244
  // cannot be used in this location.
3222
3245
  function validateTypeVarUsage(node, type, flags) {
3223
3246
  var _a;
3224
- if (types_1.TypeBase.isInstantiable(type) && !type.priv.scopeId && !(0, typeUtils_1.isTypeAliasPlaceholder)(type)) {
3225
- const scopedTypeVarInfo = findScopedTypeVar(node, type);
3226
- type = scopedTypeVarInfo.type;
3227
- if ((flags & 2048 /* EvalFlags.NoTypeVarWithScopeId */) !== 0 && type.priv.scopeId !== undefined) {
3228
- if (!type.shared.isSynthesized && !type.shared.isParamSpec) {
3229
- // This TypeVar already has a scope ID assigned to it. See if it
3230
- // originates from type parameter syntax. If so, allow it.
3231
- if (type.shared.isTypeParamSyntax) {
3232
- return type;
3233
- }
3234
- // If this type variable expression is used within a generic class,
3235
- // function, or type alias that uses type parameter syntax, there is
3236
- // no need to report an error here.
3237
- const typeVarScopeNode = ParseTreeUtils.getTypeVarScopeNode(node);
3238
- if (typeVarScopeNode &&
3239
- typeVarScopeNode.d.typeParams &&
3240
- !typeVarScopeNode.d.typeParams.d.params.some((t) => t.d.name === node)) {
3241
- return type;
3242
- }
3243
- addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typeVarUsedByOuterScope().format({ name: type.shared.name }), node);
3244
- }
3245
- }
3246
- else if ((flags & 8192 /* EvalFlags.TypeVarGetsCurScope */) !== 0) {
3247
- if (type.priv.scopeId === undefined) {
3248
- if (!scopedTypeVarInfo.foundInterveningClass) {
3249
- let enclosingScope = ParseTreeUtils.getEnclosingClassOrFunction(node);
3250
- // Handle P.args and P.kwargs as a special case for inner functions.
3251
- if (enclosingScope &&
3252
- ((_a = node.parent) === null || _a === void 0 ? void 0 : _a.nodeType) === 35 /* ParseNodeType.MemberAccess */ &&
3253
- node.parent.d.leftExpr === node) {
3254
- const memberName = node.parent.d.member.d.value;
3255
- if (memberName === 'args' || memberName === 'kwargs') {
3256
- const outerFunctionScope = ParseTreeUtils.getEnclosingClassOrFunction(enclosingScope);
3257
- if ((outerFunctionScope === null || outerFunctionScope === void 0 ? void 0 : outerFunctionScope.nodeType) === 31 /* ParseNodeType.Function */) {
3258
- enclosingScope = outerFunctionScope;
3259
- }
3260
- else if (!scopedTypeVarInfo.type.priv.scopeId) {
3261
- addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.paramSpecNotUsedByOuterScope().format({
3262
- name: type.shared.name,
3263
- }), node);
3264
- }
3265
- }
3266
- }
3267
- if (enclosingScope) {
3268
- // If the enclosing scope is using type parameter syntax, traditional
3269
- // type variables can't be used in this context.
3270
- if (enclosingScope.d.typeParams &&
3271
- !enclosingScope.d.typeParams.d.params.some((param) => param.d.name.d.value === type.shared.name)) {
3272
- addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typeParameterNotDeclared().format({
3273
- name: type.shared.name,
3274
- container: enclosingScope.d.name.d.value,
3275
- }), node);
3276
- }
3277
- type = types_1.TypeVarType.cloneForScopeId(type, ParseTreeUtils.getScopeIdForNode(enclosingScope), enclosingScope.d.name.d.value, enclosingScope.nodeType === 31 /* ParseNodeType.Function */
3278
- ? 1 /* TypeVarScopeType.Function */
3279
- : 0 /* TypeVarScopeType.Class */);
3280
- }
3281
- else {
3282
- (0, debug_1.fail)('AssociateTypeVarsWithCurrentScope flag was set but enclosing scope not found');
3283
- }
3284
- }
3285
- else {
3286
- addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typeVarUsedByOuterScope().format({ name: type.shared.name }), node);
3247
+ if (!types_1.TypeBase.isInstantiable(type) || (0, typeUtils_1.isTypeAliasPlaceholder)(type)) {
3248
+ return type;
3249
+ }
3250
+ // If the TypeVar doesn't have a scope ID, try to assign one.
3251
+ if (!type.priv.scopeId) {
3252
+ type = assignTypeVarScopeId(node, type, flags);
3253
+ }
3254
+ // If this TypeVar has an external scope ID, see if we need to
3255
+ // make it into an internal scope ID instead.
3256
+ if (type.priv.scopeId && !types_1.TypeVarType.hasInternalScopeId(type)) {
3257
+ // If this is a reference to a TypeVar defined in an outer scope,
3258
+ // change it to have an internal scope ID.
3259
+ const scopedNode = (_a = findScopedTypeVar(node, type)) === null || _a === void 0 ? void 0 : _a.scopeNode;
3260
+ if (scopedNode) {
3261
+ const enclosingSuite = ParseTreeUtils.getEnclosingClassOrFunctionSuite(node);
3262
+ if (enclosingSuite && ParseTreeUtils.isNodeContainedWithin(enclosingSuite, scopedNode)) {
3263
+ if (scopedNode.nodeType !== 10 /* ParseNodeType.Class */ || scopedNode.d.suite !== enclosingSuite) {
3264
+ type = types_1.TypeVarType.cloneWithInternalScopeId(type);
3287
3265
  }
3288
3266
  }
3289
3267
  }
3290
- else if ((flags & 4096 /* EvalFlags.AllowTypeVarWithoutScopeId */) === 0) {
3291
- if ((type.priv.scopeId === undefined || scopedTypeVarInfo.foundInterveningClass) &&
3292
- !type.shared.isSynthesized) {
3293
- const message = (0, types_1.isParamSpec)(type)
3294
- ? localize_1.LocMessage.paramSpecNotUsedByOuterScope()
3295
- : localize_1.LocMessage.typeVarNotUsedByOuterScope();
3296
- addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, message.format({ name: type.shared.name }), node);
3297
- }
3298
- }
3299
3268
  }
3300
3269
  // If this type var is variadic, the name refers to the packed form. It
3301
3270
  // must be unpacked in most contexts.
3302
3271
  if ((0, types_1.isUnpackedVariadicTypeVar)(type)) {
3303
3272
  type = types_1.TypeVarType.cloneForPacked(type);
3304
3273
  }
3274
+ if ((flags & 1073741824 /* EvalFlags.EnforceClassTypeVarScope */) !== 0 && !enforceClassTypeVarScope(node, type)) {
3275
+ return types_1.UnknownType.create();
3276
+ }
3305
3277
  return type;
3306
3278
  }
3279
+ function assignTypeVarScopeId(node, type, flags) {
3280
+ var _a;
3281
+ const scopedTypeVarInfo = findScopedTypeVar(node, type);
3282
+ type = scopedTypeVarInfo.type;
3283
+ if ((flags & 2048 /* EvalFlags.NoTypeVarWithScopeId */) !== 0 && !!type.priv.scopeId) {
3284
+ if (type.shared.isSynthesized || type.shared.isParamSpec) {
3285
+ return type;
3286
+ }
3287
+ // This TypeVar already has a scope ID assigned to it. See if it
3288
+ // originates from type parameter syntax. If so, allow it.
3289
+ if (type.shared.isTypeParamSyntax) {
3290
+ return type;
3291
+ }
3292
+ // If this type variable expression is used within a generic class,
3293
+ // function, or type alias that uses type parameter syntax, there is
3294
+ // no need to report an error here.
3295
+ const typeVarScopeNode = ParseTreeUtils.getTypeVarScopeNode(node);
3296
+ if (typeVarScopeNode &&
3297
+ typeVarScopeNode.d.typeParams &&
3298
+ !typeVarScopeNode.d.typeParams.d.params.some((t) => t.d.name === node)) {
3299
+ return type;
3300
+ }
3301
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typeVarUsedByOuterScope().format({ name: type.shared.name }), node);
3302
+ return type;
3303
+ }
3304
+ if ((flags & 8192 /* EvalFlags.TypeVarGetsCurScope */) !== 0) {
3305
+ if (type.priv.scopeId) {
3306
+ return type;
3307
+ }
3308
+ if (scopedTypeVarInfo.foundInterveningClass) {
3309
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typeVarUsedByOuterScope().format({ name: type.shared.name }), node);
3310
+ return type;
3311
+ }
3312
+ let enclosingScope = ParseTreeUtils.getEnclosingClassOrFunction(node);
3313
+ // Handle P.args and P.kwargs as a special case for inner functions.
3314
+ if (enclosingScope &&
3315
+ ((_a = node.parent) === null || _a === void 0 ? void 0 : _a.nodeType) === 35 /* ParseNodeType.MemberAccess */ &&
3316
+ node.parent.d.leftExpr === node) {
3317
+ const memberName = node.parent.d.member.d.value;
3318
+ if (memberName === 'args' || memberName === 'kwargs') {
3319
+ const outerFunctionScope = ParseTreeUtils.getEnclosingClassOrFunction(enclosingScope);
3320
+ if ((outerFunctionScope === null || outerFunctionScope === void 0 ? void 0 : outerFunctionScope.nodeType) === 31 /* ParseNodeType.Function */) {
3321
+ enclosingScope = outerFunctionScope;
3322
+ }
3323
+ else if (!scopedTypeVarInfo.type.priv.scopeId) {
3324
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.paramSpecNotUsedByOuterScope().format({
3325
+ name: type.shared.name,
3326
+ }), node);
3327
+ }
3328
+ }
3329
+ }
3330
+ if (!enclosingScope) {
3331
+ (0, debug_1.fail)('AssociateTypeVarsWithCurrentScope flag was set but enclosing scope not found');
3332
+ }
3333
+ // If the enclosing scope is using type parameter syntax, traditional
3334
+ // type variables can't be used in this context.
3335
+ if (enclosingScope.d.typeParams &&
3336
+ !enclosingScope.d.typeParams.d.params.some((param) => param.d.name.d.value === type.shared.name)) {
3337
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typeParameterNotDeclared().format({
3338
+ name: type.shared.name,
3339
+ container: enclosingScope.d.name.d.value,
3340
+ }), node);
3341
+ }
3342
+ const scopeIdToAssign = ParseTreeUtils.getScopeIdForNode(enclosingScope);
3343
+ return types_1.TypeVarType.cloneForScopeId(type, scopeIdToAssign, enclosingScope.d.name.d.value, enclosingScope.nodeType === 31 /* ParseNodeType.Function */ ? 1 /* TypeVarScopeType.Function */ : 0 /* TypeVarScopeType.Class */);
3344
+ }
3345
+ if ((flags & 4096 /* EvalFlags.AllowTypeVarWithoutScopeId */) === 0) {
3346
+ if (type.priv.scopeId && !scopedTypeVarInfo.foundInterveningClass) {
3347
+ return type;
3348
+ }
3349
+ if (!type.shared.isSynthesized) {
3350
+ const message = (0, types_1.isParamSpec)(type)
3351
+ ? localize_1.LocMessage.paramSpecNotUsedByOuterScope()
3352
+ : localize_1.LocMessage.typeVarNotUsedByOuterScope();
3353
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, message.format({ name: type.shared.name }), node);
3354
+ }
3355
+ }
3356
+ return type;
3357
+ }
3358
+ // Enforce that the type variable is scoped to the enclosing class or
3359
+ // an outer class that contains the class definition.
3360
+ function enforceClassTypeVarScope(node, type) {
3361
+ var _a, _b;
3362
+ const scopeId = (_b = (_a = type.priv.externalTypeVar) === null || _a === void 0 ? void 0 : _a.priv.scopeId) !== null && _b !== void 0 ? _b : type.priv.scopeId;
3363
+ if (!scopeId) {
3364
+ return true;
3365
+ }
3366
+ const enclosingClass = ParseTreeUtils.getEnclosingClass(node);
3367
+ if (enclosingClass) {
3368
+ const liveTypeVarScopeIds = ParseTreeUtils.getTypeVarScopesForNode(enclosingClass);
3369
+ if (!liveTypeVarScopeIds.includes(scopeId)) {
3370
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typeVarInvalidForMemberVariable().format({
3371
+ name: types_1.TypeVarType.getReadableName(type),
3372
+ }), node);
3373
+ return false;
3374
+ }
3375
+ }
3376
+ return true;
3377
+ }
3307
3378
  // Determines if the type is a generic class or type alias with missing
3308
3379
  // type arguments. If so, it fills in these type arguments with Unknown
3309
3380
  // and optionally reports an error.
@@ -3378,10 +3449,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
3378
3449
  let nestedClassCount = 0;
3379
3450
  (0, debug_1.assert)(types_1.TypeBase.isInstantiable(type));
3380
3451
  while (curNode) {
3381
- curNode = ParseTreeUtils.getTypeVarScopeNode(curNode);
3382
- if (!curNode) {
3452
+ const scopeNode = ParseTreeUtils.getTypeVarScopeNode(curNode);
3453
+ if (!scopeNode) {
3383
3454
  break;
3384
3455
  }
3456
+ curNode = scopeNode;
3385
3457
  let typeParametersForScope;
3386
3458
  let scopeUsesTypeParameterSyntax = false;
3387
3459
  if (curNode.nodeType === 10 /* ParseNodeType.Class */) {
@@ -3412,6 +3484,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
3412
3484
  type = types_1.TypeVarType.cloneForScopeId(type, match.priv.scopeId, match.priv.scopeName, match.priv.scopeType);
3413
3485
  return {
3414
3486
  type,
3487
+ scopeNode,
3415
3488
  foundInterveningClass: nestedClassCount > 1 && !scopeUsesTypeParameterSyntax,
3416
3489
  };
3417
3490
  }
@@ -3422,15 +3495,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
3422
3495
  curNode = node;
3423
3496
  while (curNode) {
3424
3497
  let leftType;
3425
- let typeAliasNode = undefined;
3498
+ let typeAliasNode;
3499
+ let scopeNode;
3426
3500
  if (curNode.nodeType === 77 /* ParseNodeType.TypeAlias */) {
3427
3501
  leftType = readTypeCache(curNode.d.name, 0 /* EvalFlags.None */);
3428
3502
  typeAliasNode = curNode;
3503
+ scopeNode = curNode;
3429
3504
  }
3430
3505
  else if (curNode.nodeType === 3 /* ParseNodeType.Assignment */) {
3431
3506
  leftType = readTypeCache(curNode.d.leftExpr, 0 /* EvalFlags.None */);
3507
+ scopeNode = curNode;
3432
3508
  }
3433
- if (leftType) {
3509
+ if (leftType && scopeNode) {
3434
3510
  // Is this a placeholder that was temporarily written to the cache for
3435
3511
  // purposes of resolving type aliases?
3436
3512
  if (leftType && (0, types_1.isTypeVar)(leftType) && leftType.shared.recursiveAlias) {
@@ -3449,12 +3525,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
3449
3525
  if (allowedTypeParams) {
3450
3526
  if (!allowedTypeParams.some((param) => param.shared.name === type.shared.name)) {
3451
3527
  // Return the original type.
3452
- return { type, foundInterveningClass: false };
3528
+ return { type, scopeNode, foundInterveningClass: false };
3453
3529
  }
3454
3530
  }
3455
3531
  }
3456
3532
  return {
3457
3533
  type: types_1.TypeVarType.cloneForScopeId(type, leftType.shared.recursiveAlias.scopeId, leftType.shared.recursiveAlias.name, 2 /* TypeVarScopeType.TypeAlias */),
3534
+ scopeNode,
3458
3535
  foundInterveningClass: false,
3459
3536
  };
3460
3537
  }
@@ -3462,7 +3539,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
3462
3539
  curNode = curNode.parent;
3463
3540
  }
3464
3541
  // Return the original type.
3465
- return { type, foundInterveningClass: false };
3542
+ return { type, scopeNode: undefined, foundInterveningClass: false };
3466
3543
  }
3467
3544
  function getTypeOfMemberAccess(node, flags) {
3468
3545
  var _a, _b;
@@ -3599,7 +3676,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
3599
3676
  const errorMessage = isArgs
3600
3677
  ? localize_1.LocMessage.paramSpecArgsUsage()
3601
3678
  : localize_1.LocMessage.paramSpecKwargsUsage();
3602
- addError(errorMessage, node);
3679
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, errorMessage, node);
3603
3680
  type = types_1.UnknownType.create(isIncomplete);
3604
3681
  break;
3605
3682
  }
@@ -3915,7 +3992,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
3915
3992
  types_1.ClassType.isSameGenericClass(containingClassType, classType)) {
3916
3993
  type = (_b = getDeclaredTypeOfSymbol(memberInfo.symbol)) === null || _b === void 0 ? void 0 : _b.type;
3917
3994
  if (type && (0, types_1.isInstantiableClass)(memberInfo.classType)) {
3918
- type = (0, typeUtils_1.partiallySpecializeType)(type, memberInfo.classType);
3995
+ type = (0, typeUtils_1.partiallySpecializeType)(type, memberInfo.classType,
3996
+ /* typeClassType */ undefined, selfType);
3919
3997
  }
3920
3998
  // If we're setting a class variable via a write through an object,
3921
3999
  // this is normally considered a type violation. But it is allowed
@@ -4118,14 +4196,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
4118
4196
  /* usage */ undefined, diag === null || diag === void 0 ? void 0 : diag.createAddendum(), 16 /* MemberAccessFlags.SkipInstanceMembers */ | 512 /* MemberAccessFlags.SkipAttributeAccessOverride */);
4119
4197
  if (!methodTypeResult || methodTypeResult.typeErrors) {
4120
4198
  // Provide special error messages for properties.
4121
- if (types_1.ClassType.isPropertyClass(concreteMemberType)) {
4122
- if (usage.method !== 'get') {
4123
- const message = usage.method === 'set'
4124
- ? localize_1.LocAddendum.propertyMissingSetter()
4125
- : localize_1.LocAddendum.propertyMissingDeleter();
4126
- diag === null || diag === void 0 ? void 0 : diag.addMessage(message.format({ name: memberName }));
4127
- return { type: types_1.AnyType.create(), typeErrors: true };
4128
- }
4199
+ if (types_1.ClassType.isPropertyClass(concreteMemberType) && usage.method !== 'get') {
4200
+ const message = usage.method === 'set' ? localize_1.LocAddendum.propertyMissingSetter() : localize_1.LocAddendum.propertyMissingDeleter();
4201
+ diag === null || diag === void 0 ? void 0 : diag.addMessage(message.format({ name: memberName }));
4202
+ return { type: types_1.AnyType.create(), typeErrors: true };
4203
+ }
4204
+ if (classType.shared.typeVarScopeId) {
4205
+ memberType = (0, typeUtils_1.updateTypeWithInternalTypeVars)(memberType, [classType.shared.typeVarScopeId]);
4129
4206
  }
4130
4207
  return { type: memberType };
4131
4208
  }
@@ -4177,7 +4254,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
4177
4254
  assignType(types_1.ClassType.cloneAsInstance(accessMethodClass), types_1.ClassType.cloneAsInstance(memberInfo.classType),
4178
4255
  /* diag */ undefined, typeVarContext);
4179
4256
  accessMethodClass = (0, typeUtils_1.applySolvedTypeVars)(accessMethodClass, typeVarContext);
4180
- const specializedType = (0, typeUtils_1.partiallySpecializeType)(methodType, accessMethodClass, selfType ? (0, typeUtils_1.convertToInstantiable)(selfType) : classType);
4257
+ const specializedType = (0, typeUtils_1.partiallySpecializeType)(methodType, accessMethodClass, getTypeClassType(), selfType ? (0, typeUtils_1.convertToInstantiable)(selfType) : classType);
4181
4258
  if ((0, types_1.isFunction)(specializedType) || (0, types_1.isOverloadedFunction)(specializedType)) {
4182
4259
  methodType = specializedType;
4183
4260
  }
@@ -4250,8 +4327,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
4250
4327
  return validateCallArguments(errorNode, argList, { type: methodType },
4251
4328
  /* typeVarContext */ undefined,
4252
4329
  /* skipUnknownArgCheck */ true,
4253
- /* inferenceContext */ undefined,
4254
- /* signatureTracker */ undefined);
4330
+ /* inferenceContext */ undefined);
4255
4331
  }, (suppressedDiags) => {
4256
4332
  // If diagnostics were recorded when suppressed, add them to the
4257
4333
  // diagnostic as messages.
@@ -4435,8 +4511,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
4435
4511
  const typeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(accessMemberType));
4436
4512
  const callResult = validateCallArguments(errorNode, argList, { type: accessMemberType }, typeVarContext,
4437
4513
  /* skipUnknownArgCheck */ true,
4438
- /* inferenceContext */ undefined,
4439
- /* signatureTracker */ undefined);
4514
+ /* inferenceContext */ undefined);
4440
4515
  let isAsymmetricAccessor = false;
4441
4516
  if (usage.method === 'set') {
4442
4517
  isAsymmetricAccessor = isClassWithAsymmetricAttributeAccessor(classType);
@@ -4454,7 +4529,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
4454
4529
  // runtime exception.
4455
4530
  if (flags & 128 /* EvalFlags.InstantiableType */) {
4456
4531
  if (node.d.leftExpr.nodeType === 48 /* ParseNodeType.StringList */) {
4457
- addError(localize_1.LocMessage.stringNotSubscriptable(), node.d.leftExpr);
4532
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportIndexIssue, localize_1.LocMessage.stringNotSubscriptable(), node.d.leftExpr);
4458
4533
  }
4459
4534
  }
4460
4535
  // Check for builtin classes that will generate runtime exceptions if subscripted.
@@ -4478,7 +4553,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
4478
4553
  if (minPythonVersion !== undefined &&
4479
4554
  fileInfo.executionEnvironment.pythonVersion.isLessThan(minPythonVersion) &&
4480
4555
  !fileInfo.isStubFile) {
4481
- addError(localize_1.LocMessage.classNotRuntimeSubscriptable().format({
4556
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportIndexIssue, localize_1.LocMessage.classNotRuntimeSubscriptable().format({
4482
4557
  name: baseTypeResult.type.priv.aliasName || baseTypeResult.type.shared.name,
4483
4558
  }), node.d.leftExpr);
4484
4559
  }
@@ -4634,7 +4709,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
4634
4709
  // If the variadic type variable is not unpacked, report an error.
4635
4710
  function validateVariadicTypeVarIsUnpacked(type, node) {
4636
4711
  if (!type.priv.isVariadicUnpacked) {
4637
- addError(localize_1.LocMessage.unpackedTypeVarTupleExpected().format({
4712
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.unpackedTypeVarTupleExpected().format({
4638
4713
  name1: type.shared.name,
4639
4714
  name2: type.shared.name,
4640
4715
  }), node);
@@ -4681,14 +4756,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
4681
4756
  minTypeArgCount = firstDefaultParamIndex;
4682
4757
  }
4683
4758
  if (typeArgs.length > typeParameters.length) {
4684
- addError(localize_1.LocMessage.typeArgsTooMany().format({
4759
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgsTooMany().format({
4685
4760
  name: printType(baseType),
4686
4761
  expected: typeParameters.length,
4687
4762
  received: typeArgs.length,
4688
4763
  }), typeArgs[typeParameters.length].node);
4689
4764
  }
4690
4765
  else if (typeArgs.length < minTypeArgCount) {
4691
- addError(localize_1.LocMessage.typeArgsTooFew().format({
4766
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgsTooFew().format({
4692
4767
  name: printType(baseType),
4693
4768
  expected: typeParameters.length,
4694
4769
  received: typeArgs.length,
@@ -4747,12 +4822,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
4747
4822
  (0, constraintSolver_1.assignTypeToTypeVar)(evaluatorInterface, param, functionType, diag, typeVarContext);
4748
4823
  }
4749
4824
  else {
4750
- addError(localize_1.LocMessage.typeArgListExpected(), typeArgs[index].node);
4825
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgListExpected(), typeArgs[index].node);
4751
4826
  }
4752
4827
  }
4753
4828
  else {
4754
4829
  if (index < typeArgs.length && typeArgs[index].typeList) {
4755
- addError(localize_1.LocMessage.typeArgListNotAllowed(), typeArgs[index].node);
4830
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgListNotAllowed(), typeArgs[index].node);
4756
4831
  }
4757
4832
  let typeArgType;
4758
4833
  if (index < typeArgs.length) {
@@ -4791,7 +4866,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
4791
4866
  }
4792
4867
  });
4793
4868
  if (!diag.isEmpty()) {
4794
- addError(localize_1.LocMessage.typeNotSpecializable().format({ type: printType(baseType) }) + diag.getString(), node, (_b = diag.getEffectiveTextRange()) !== null && _b !== void 0 ? _b : node);
4869
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeNotSpecializable().format({ type: printType(baseType) }) + diag.getString(), node, (_b = diag.getEffectiveTextRange()) !== null && _b !== void 0 ? _b : node);
4795
4870
  }
4796
4871
  const primarySignatureContext = typeVarContext.getPrimarySignature();
4797
4872
  const aliasTypeArgs = [];
@@ -4889,14 +4964,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
4889
4964
  const typeArgs = getTypeArgs(node, flags);
4890
4965
  if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0) {
4891
4966
  if ((flags & 32768 /* EvalFlags.VarTypeAnnotation */) === 0) {
4892
- addError(localize_1.LocMessage.initVarNotAllowed(), node.d.leftExpr);
4967
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.initVarNotAllowed(), node.d.leftExpr);
4893
4968
  }
4894
4969
  }
4895
4970
  if (typeArgs.length === 1) {
4896
4971
  return typeArgs[0].type;
4897
4972
  }
4898
4973
  else {
4899
- addError(localize_1.LocMessage.typeArgsMismatchOne().format({ received: typeArgs.length }), node.d.leftExpr);
4974
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgsMismatchOne().format({ received: typeArgs.length }), node.d.leftExpr);
4900
4975
  return types_1.UnknownType.create();
4901
4976
  }
4902
4977
  }
@@ -5225,8 +5300,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5225
5300
  const callResult = validateCallArguments(node, argList, { type: itemMethodType },
5226
5301
  /* typeVarContext */ undefined,
5227
5302
  /* skipUnknownArgCheck */ true,
5228
- /* inferenceContext */ undefined,
5229
- /* signatureTracker */ undefined);
5303
+ /* inferenceContext */ undefined);
5230
5304
  return {
5231
5305
  type: (_e = callResult.returnType) !== null && _e !== void 0 ? _e : types_1.UnknownType.create(),
5232
5306
  isIncomplete: !!callResult.isTypeIncomplete,
@@ -5279,7 +5353,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5279
5353
  }
5280
5354
  function getTypeArgs(node, flags, options) {
5281
5355
  const typeArgs = [];
5282
- let adjFlags = flags;
5356
+ let adjFlags = flags | 33554432 /* EvalFlags.NoConvertSpecialForm */;
5283
5357
  if (options === null || options === void 0 ? void 0 : options.isFinalAnnotation) {
5284
5358
  adjFlags |= 131072 /* EvalFlags.NoClassVar */ | 16 /* EvalFlags.NoFinal */;
5285
5359
  }
@@ -5368,13 +5442,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5368
5442
  typeResult.type = types_1.ClassType.cloneForUnpacked(typeResult.type);
5369
5443
  }
5370
5444
  else {
5371
- addError(localize_1.LocMessage.unpackNotAllowed(), arg.d.valueExpr);
5445
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.unpackNotAllowed(), arg.d.valueExpr);
5372
5446
  }
5373
5447
  }
5374
5448
  }
5375
5449
  }
5376
5450
  if (arg.d.name) {
5377
- addError(localize_1.LocMessage.keywordArgInTypeArgument(), arg.d.valueExpr);
5451
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.keywordArgInTypeArgument(), arg.d.valueExpr);
5378
5452
  }
5379
5453
  if (arg.d.valueExpr.nodeType !== 0 /* ParseNodeType.Error */ ||
5380
5454
  arg.d.valueExpr.d.category !== 3 /* ErrorExpressionCategory.MissingIndexOrSlice */) {
@@ -5405,18 +5479,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5405
5479
  else {
5406
5480
  typeResult = { ...getTypeOfExpression(node, adjustedFlags), node };
5407
5481
  if (node.nodeType === 18 /* ParseNodeType.Dictionary */) {
5408
- addError(localize_1.LocMessage.dictInAnnotation(), node);
5482
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.dictInAnnotation(), node);
5409
5483
  }
5410
5484
  if ((flags & 131072 /* EvalFlags.NoClassVar */) !== 0) {
5411
5485
  // "ClassVar" is not allowed as a type argument.
5412
5486
  if ((0, types_1.isClass)(typeResult.type) && types_1.ClassType.isBuiltIn(typeResult.type, 'ClassVar')) {
5413
- addError(localize_1.LocMessage.classVarNotAllowed(), node);
5487
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.classVarNotAllowed(), node);
5414
5488
  }
5415
5489
  }
5416
5490
  }
5417
5491
  return typeResult;
5418
5492
  }
5419
- function getTypeOfTuple(node, flags, inferenceContext, signatureTracker) {
5493
+ function getTypeOfTuple(node, flags, inferenceContext) {
5420
5494
  var _a;
5421
5495
  if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0 && ((_a = node.parent) === null || _a === void 0 ? void 0 : _a.nodeType) !== 1 /* ParseNodeType.Argument */) {
5422
5496
  // This is allowed inside of an index trailer, specifically
@@ -5443,8 +5517,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5443
5517
  }
5444
5518
  if (!matchingSubtype) {
5445
5519
  const subtypeResult = useSpeculativeMode(node, () => {
5446
- return getTypeOfTupleWithContext(node, flags, (0, typeUtils_1.makeInferenceContext)(subtype),
5447
- /* signatureTracker */ undefined);
5520
+ return getTypeOfTupleWithContext(node, flags, (0, typeUtils_1.makeInferenceContext)(subtype));
5448
5521
  });
5449
5522
  if (subtypeResult && assignType(subtype, subtypeResult.type)) {
5450
5523
  matchingSubtype = subtype;
@@ -5456,7 +5529,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5456
5529
  }
5457
5530
  let expectedTypeDiagAddendum;
5458
5531
  if (expectedType) {
5459
- const result = getTypeOfTupleWithContext(node, flags, (0, typeUtils_1.makeInferenceContext)(expectedType), signatureTracker);
5532
+ const result = getTypeOfTupleWithContext(node, flags, (0, typeUtils_1.makeInferenceContext)(expectedType));
5460
5533
  if (result && !result.typeErrors) {
5461
5534
  return result;
5462
5535
  }
@@ -5470,7 +5543,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5470
5543
  }
5471
5544
  return { ...typeResult, expectedTypeDiagAddendum };
5472
5545
  }
5473
- function getTypeOfTupleWithContext(node, flags, inferenceContext, signatureTracker) {
5546
+ function getTypeOfTupleWithContext(node, flags, inferenceContext) {
5474
5547
  inferenceContext.expectedType = (0, typeUtils_1.transformPossibleRecursiveTypeAlias)(inferenceContext.expectedType);
5475
5548
  if (!(0, types_1.isClassInstance)(inferenceContext.expectedType)) {
5476
5549
  return undefined;
@@ -5508,7 +5581,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5508
5581
  expectedTypes.push(homogenousType);
5509
5582
  }
5510
5583
  }
5511
- 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), signatureTracker));
5584
+ 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)));
5512
5585
  const isIncomplete = entryTypeResults.some((result) => result.isIncomplete);
5513
5586
  const type = makeTupleObject(buildTupleTypesList(entryTypeResults, /* stripLiterals */ false));
5514
5587
  // Copy any expected type diag addenda for precision error reporting.
@@ -5577,7 +5650,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5577
5650
  }
5578
5651
  return entryTypes;
5579
5652
  }
5580
- function getTypeOfCall(node, flags, inferenceContext, signatureTracker) {
5653
+ function getTypeOfCall(node, flags, inferenceContext) {
5581
5654
  var _a;
5582
5655
  let baseTypeResult;
5583
5656
  // Check for the use of `type(x)` within a type annotation. This isn't
@@ -5607,12 +5680,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5607
5680
  return functionArg;
5608
5681
  });
5609
5682
  let typeResult = { type: types_1.UnknownType.create() };
5610
- // If the inference context has an associated signature tracker, make sure
5611
- // the base type of this call is not the same as one of the tracked signatures.
5612
- // This is important for nested generic calls (e.g. "foo(foo(x))").
5613
- if (signatureTracker) {
5614
- baseTypeResult.type = (0, typeUtils_1.ensureFunctionSignaturesAreUnique)(baseTypeResult.type, signatureTracker, node.start);
5615
- }
5683
+ baseTypeResult.type = ensureSignatureIsUnique(baseTypeResult.type, node);
5616
5684
  if (!(0, typeUtils_1.isTypeAliasPlaceholder)(baseTypeResult.type)) {
5617
5685
  if (node.d.leftExpr.nodeType === 38 /* ParseNodeType.Name */ && node.d.leftExpr.d.value === 'super') {
5618
5686
  // Handle the built-in "super" call specially.
@@ -5622,15 +5690,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5622
5690
  node.d.leftExpr.nodeType === 38 /* ParseNodeType.Name */ &&
5623
5691
  node.d.leftExpr.d.value === 'reveal_type') {
5624
5692
  // Handle the implicit "reveal_type" call.
5625
- typeResult = getTypeOfRevealType(node, inferenceContext, signatureTracker);
5693
+ typeResult = getTypeOfRevealType(node, inferenceContext);
5626
5694
  }
5627
5695
  else if ((0, types_1.isFunction)(baseTypeResult.type) && types_1.FunctionType.isBuiltIn(baseTypeResult.type, 'reveal_type')) {
5628
5696
  // Handle the "typing.reveal_type" call.
5629
- typeResult = getTypeOfRevealType(node, inferenceContext, signatureTracker);
5697
+ typeResult = getTypeOfRevealType(node, inferenceContext);
5630
5698
  }
5631
5699
  else if ((0, types_1.isFunction)(baseTypeResult.type) && types_1.FunctionType.isBuiltIn(baseTypeResult.type, 'assert_type')) {
5632
5700
  // Handle the "typing.assert_type" call.
5633
- typeResult = getTypeOfAssertType(node, inferenceContext, signatureTracker);
5701
+ typeResult = getTypeOfAssertType(node, inferenceContext);
5634
5702
  }
5635
5703
  else if ((0, types_1.isAnyOrUnknown)(baseTypeResult.type) &&
5636
5704
  node.d.leftExpr.nodeType === 38 /* ParseNodeType.Name */ &&
@@ -5640,13 +5708,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5640
5708
  typeResult.type = getTypeOfRevealLocals(node);
5641
5709
  }
5642
5710
  else {
5643
- addError(localize_1.LocMessage.revealLocalsArgs(), node);
5711
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportCallIssue, localize_1.LocMessage.revealLocalsArgs(), node);
5644
5712
  }
5645
5713
  }
5646
5714
  else {
5647
5715
  const callResult = validateCallArguments(node, argList, baseTypeResult,
5648
5716
  /* typeVarContext */ undefined,
5649
- /* skipUnknownArgCheck */ false, inferenceContext, signatureTracker);
5717
+ /* skipUnknownArgCheck */ false, inferenceContext);
5650
5718
  typeResult.type = (_a = callResult.returnType) !== null && _a !== void 0 ? _a : types_1.UnknownType.create();
5651
5719
  if (callResult.argumentErrors) {
5652
5720
  typeResult.typeErrors = true;
@@ -5735,17 +5803,16 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5735
5803
  }
5736
5804
  return typeResult;
5737
5805
  }
5738
- function getTypeOfAssertType(node, inferenceContext, signatureTracker) {
5806
+ function getTypeOfAssertType(node, inferenceContext) {
5739
5807
  if (node.d.args.length !== 2 ||
5740
5808
  node.d.args[0].d.argCategory !== 0 /* ArgumentCategory.Simple */ ||
5741
5809
  node.d.args[0].d.name !== undefined ||
5742
5810
  node.d.args[0].d.argCategory !== 0 /* ArgumentCategory.Simple */ ||
5743
5811
  node.d.args[1].d.name !== undefined) {
5744
- addError(localize_1.LocMessage.assertTypeArgs(), node);
5812
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportCallIssue, localize_1.LocMessage.assertTypeArgs(), node);
5745
5813
  return { type: types_1.UnknownType.create() };
5746
5814
  }
5747
- const arg0TypeResult = getTypeOfExpression(node.d.args[0].d.valueExpr,
5748
- /* flags */ undefined, inferenceContext, signatureTracker);
5815
+ const arg0TypeResult = getTypeOfExpression(node.d.args[0].d.valueExpr, /* flags */ undefined, inferenceContext);
5749
5816
  if (arg0TypeResult.isIncomplete) {
5750
5817
  return { type: types_1.UnknownType.create(/* isIncomplete */ true), isIncomplete: true };
5751
5818
  }
@@ -5759,7 +5826,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5759
5826
  }
5760
5827
  return { type: arg0TypeResult.type };
5761
5828
  }
5762
- function getTypeOfRevealType(node, inferenceContext, signatureTracker) {
5829
+ function getTypeOfRevealType(node, inferenceContext) {
5763
5830
  let arg0Value;
5764
5831
  let expectedRevealTypeNode;
5765
5832
  let expectedRevealType;
@@ -5781,7 +5848,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5781
5848
  if (!(0, types_1.isClassInstance)(expectedTextType) ||
5782
5849
  !types_1.ClassType.isBuiltIn(expectedTextType, 'str') ||
5783
5850
  typeof expectedTextType.priv.literalValue !== 'string') {
5784
- addError(localize_1.LocMessage.revealTypeExpectedTextArg(), arg.d.valueExpr);
5851
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportCallIssue, localize_1.LocMessage.revealTypeExpectedTextArg(), arg.d.valueExpr);
5785
5852
  }
5786
5853
  else {
5787
5854
  expectedText = expectedTextType.priv.literalValue;
@@ -5793,17 +5860,17 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5793
5860
  }
5794
5861
  });
5795
5862
  if (!arg0Value) {
5796
- addError(localize_1.LocMessage.revealTypeArgs(), node);
5863
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportCallIssue, localize_1.LocMessage.revealTypeArgs(), node);
5797
5864
  return { type: types_1.UnknownType.create() };
5798
5865
  }
5799
- const typeResult = getTypeOfExpression(arg0Value, /* flags */ undefined, inferenceContext, signatureTracker);
5866
+ const typeResult = getTypeOfExpression(arg0Value, /* flags */ undefined, inferenceContext);
5800
5867
  const type = typeResult.type;
5801
5868
  const exprString = ParseTreeUtils.printExpression(arg0Value);
5802
5869
  const typeString = printType(type, { expandTypeAlias: true });
5803
5870
  if (!typeResult.isIncomplete) {
5804
5871
  if (expectedText !== undefined) {
5805
5872
  if (expectedText !== typeString) {
5806
- addError(localize_1.LocMessage.revealTypeExpectedTextMismatch().format({
5873
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.revealTypeExpectedTextMismatch().format({
5807
5874
  expected: expectedText,
5808
5875
  received: typeString,
5809
5876
  }), expectedTextNode !== null && expectedTextNode !== void 0 ? expectedTextNode : arg0Value);
@@ -5812,7 +5879,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5812
5879
  if (expectedRevealType) {
5813
5880
  if (!(0, types_1.isTypeSame)(expectedRevealType, type, { ignorePseudoGeneric: true })) {
5814
5881
  const expectedRevealTypeText = printType(expectedRevealType);
5815
- addError(localize_1.LocMessage.revealTypeExpectedTypeMismatch().format({
5882
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.revealTypeExpectedTypeMismatch().format({
5816
5883
  expected: expectedRevealTypeText,
5817
5884
  received: typeString,
5818
5885
  }), expectedRevealTypeNode !== null && expectedRevealTypeNode !== void 0 ? expectedRevealTypeNode : arg0Value);
@@ -5857,7 +5924,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5857
5924
  function getTypeOfSuperCall(node) {
5858
5925
  var _a, _b;
5859
5926
  if (node.d.args.length > 2) {
5860
- addError(localize_1.LocMessage.superCallArgCount(), node.d.args[2]);
5927
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportCallIssue, localize_1.LocMessage.superCallArgCount(), node.d.args[2]);
5861
5928
  }
5862
5929
  const enclosingFunction = ParseTreeUtils.getEnclosingFunctionEvaluationScope(node);
5863
5930
  const enclosingClass = enclosingFunction ? ParseTreeUtils.getEnclosingClass(enclosingFunction) : undefined;
@@ -5883,12 +5950,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5883
5950
  const functionInfo = (0, decorators_1.getFunctionInfoFromDecorators)(evaluatorInterface, enclosingFunction,
5884
5951
  /* isInClass */ true);
5885
5952
  if (((functionInfo === null || functionInfo === void 0 ? void 0 : functionInfo.flags) & 4 /* FunctionTypeFlags.StaticMethod */) !== 0) {
5886
- addError(localize_1.LocMessage.superCallZeroArgFormStaticMethod(), node.d.leftExpr);
5953
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.superCallZeroArgFormStaticMethod(), node.d.leftExpr);
5887
5954
  }
5888
5955
  }
5889
5956
  }
5890
5957
  else {
5891
- addError(localize_1.LocMessage.superCallZeroArgForm(), node.d.leftExpr);
5958
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.superCallZeroArgForm(), node.d.leftExpr);
5892
5959
  targetClassType = types_1.UnknownType.create();
5893
5960
  }
5894
5961
  }
@@ -5942,7 +6009,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5942
6009
  types_1.FunctionType.isInstanceMethod(methodType)) {
5943
6010
  if (methodType.shared.parameters.length > 0 &&
5944
6011
  types_1.FunctionParam.isTypeDeclared(methodType.shared.parameters[0])) {
5945
- implicitBindToType = makeTopLevelTypeVarsConcrete(methodType.shared.parameters[0].type);
6012
+ let paramType = methodType.shared.parameters[0].type;
6013
+ const liveScopeIds = ParseTreeUtils.getTypeVarScopesForNode(node);
6014
+ paramType = (0, typeUtils_1.updateTypeWithInternalTypeVars)(paramType, liveScopeIds);
6015
+ implicitBindToType = makeTopLevelTypeVarsConcrete(paramType);
5946
6016
  }
5947
6017
  }
5948
6018
  }
@@ -5991,6 +6061,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5991
6061
  isProtocolClass = true;
5992
6062
  effectiveTargetClass = undefined;
5993
6063
  }
6064
+ if (bindToType) {
6065
+ bindToType = (0, typeUtils_1.selfSpecializeClass)(bindToType, { useInternalTypeVars: true });
6066
+ }
5994
6067
  const lookupResults = bindToType
5995
6068
  ? (0, typeUtils_1.lookUpClassMember)(bindToType, memberName, 0 /* MemberAccessFlags.Default */, effectiveTargetClass)
5996
6069
  : undefined;
@@ -6011,13 +6084,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
6011
6084
  else {
6012
6085
  resultType = types_1.UnknownType.create();
6013
6086
  }
6014
- return {
6015
- type: resultIsInstance ? (0, typeUtils_1.convertToInstance)(resultType, /* includeSubclasses */ false) : resultType,
6016
- bindToSelfType: bindToType
6017
- ? types_1.TypeBase.cloneForCondition((0, typeUtils_1.synthesizeTypeVarForSelfCls)(types_1.ClassType.cloneIncludeSubclasses(bindToType, /* includeSubclasses */ false),
6018
- /* isClsParam */ false), (_b = bindToType.props) === null || _b === void 0 ? void 0 : _b.condition)
6019
- : undefined,
6020
- };
6087
+ let bindToSelfType;
6088
+ if (bindToType) {
6089
+ bindToSelfType = types_1.TypeBase.cloneForCondition(types_1.TypeVarType.cloneWithInternalScopeId((0, typeUtils_1.synthesizeTypeVarForSelfCls)(types_1.ClassType.cloneIncludeSubclasses(bindToType, /* includeSubclasses */ false),
6090
+ /* isClsParam */ false)), (_b = bindToType.props) === null || _b === void 0 ? void 0 : _b.condition);
6091
+ }
6092
+ const type = resultIsInstance ? (0, typeUtils_1.convertToInstance)(resultType, /* includeSubclasses */ false) : resultType;
6093
+ return { type, bindToSelfType };
6021
6094
  }
6022
6095
  // Handle the super() call when used outside of a member access expression.
6023
6096
  if ((0, types_1.isInstantiableClass)(concreteTargetClassType)) {
@@ -6073,6 +6146,27 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
6073
6146
  }
6074
6147
  return { type: types_1.UnknownType.create() };
6075
6148
  }
6149
+ // When evaluating a call, the errorNode is typically the call node, which
6150
+ // encompasses all of the argument expressions. This means we can normally
6151
+ // use the errorNode as the root for speculative evaluation. However, there
6152
+ // are some cases where we don't have a call node (e.g. in the case of an
6153
+ // __init_subclass__ validation). Here we need to find some other parent
6154
+ // node of the error node that encompasses all of the arguments.
6155
+ function getSpeculativeNodeForCall(errorNode) {
6156
+ var _a;
6157
+ // If the error node is within an arg, expand to include the parent of the arg list.
6158
+ const argParent = ParseTreeUtils.getParentNodeOfType(errorNode, 1 /* ParseNodeType.Argument */);
6159
+ if (argParent === null || argParent === void 0 ? void 0 : argParent.parent) {
6160
+ return argParent.parent;
6161
+ }
6162
+ // If the error node is the name in a class declaration, expand to include the class node.
6163
+ if (errorNode.nodeType === 38 /* ParseNodeType.Name */ &&
6164
+ ((_a = errorNode.parent) === null || _a === void 0 ? void 0 : _a.nodeType) === 10 /* ParseNodeType.Class */ &&
6165
+ errorNode.parent.d.name === errorNode) {
6166
+ return errorNode.parent;
6167
+ }
6168
+ return errorNode;
6169
+ }
6076
6170
  // Attempts to find an overloaded function for each set of argument
6077
6171
  // types in the expandedArgTypes list. If an argument type is undefined,
6078
6172
  // its type is evaluated from the argument's expression using the
@@ -6080,13 +6174,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
6080
6174
  // there will be only one argument list in expandedArgTypes, and all entries
6081
6175
  // (one for each argument) will be undefined. On subsequent calls, this
6082
6176
  // list will grow to include union expansions.
6083
- function validateOverloadsWithExpandedTypes(errorNode, expandedArgTypes, argParamMatches, typeVarContext, skipUnknownArgCheck, inferenceContext, signatureTracker) {
6177
+ function validateOverloadsWithExpandedTypes(errorNode, expandedArgTypes, argParamMatches, typeVarContext, skipUnknownArgCheck, inferenceContext) {
6084
6178
  var _a, _b;
6085
6179
  const returnTypes = [];
6086
6180
  const matchedOverloads = [];
6087
6181
  let isTypeIncomplete = false;
6088
6182
  let overloadsUsedForCall = [];
6089
6183
  let isDefinitiveMatchFound = false;
6184
+ const speculativeNode = getSpeculativeNodeForCall(errorNode);
6090
6185
  for (let expandedTypesIndex = 0; expandedTypesIndex < expandedArgTypes.length; expandedTypesIndex++) {
6091
6186
  let matchedOverload;
6092
6187
  const argTypeOverride = expandedArgTypes[expandedTypesIndex];
@@ -6114,9 +6209,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
6114
6209
  effectiveTypeVarContext.unlock();
6115
6210
  // Use speculative mode so we don't output any diagnostics or
6116
6211
  // record any final types in the type cache.
6117
- const callResult = useSpeculativeMode(errorNode, () => {
6212
+ const callResult = useSpeculativeMode(speculativeNode, () => {
6118
6213
  return validateArgTypesWithContext(errorNode, matchResults, effectiveTypeVarContext,
6119
- /* skipUnknownArgCheck */ true, inferenceContext, signatureTracker);
6214
+ /* skipUnknownArgCheck */ true, inferenceContext);
6120
6215
  });
6121
6216
  if (callResult.isTypeIncomplete) {
6122
6217
  isTypeIncomplete = true;
@@ -6225,7 +6320,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
6225
6320
  const finalTypeVarContext = typeVarContext !== null && typeVarContext !== void 0 ? typeVarContext : matchedOverloads[0].typeVarContext;
6226
6321
  finalTypeVarContext.unlock();
6227
6322
  finalTypeVarContext.addSolveForScope((0, typeUtils_1.getTypeVarScopeId)(matchedOverloads[0].overload));
6228
- const finalCallResult = validateArgTypesWithContext(errorNode, matchedOverloads[0].matchResults, finalTypeVarContext, skipUnknownArgCheck, inferenceContext, signatureTracker);
6323
+ const finalCallResult = validateArgTypesWithContext(errorNode, matchedOverloads[0].matchResults, finalTypeVarContext, skipUnknownArgCheck, inferenceContext);
6229
6324
  if (finalCallResult.isTypeIncomplete) {
6230
6325
  isTypeIncomplete = true;
6231
6326
  }
@@ -6287,24 +6382,25 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
6287
6382
  function getBestOverloadForArguments(errorNode, typeResult, argList) {
6288
6383
  let overloadIndex = 0;
6289
6384
  let matches = [];
6290
- const signatureTracker = new typeUtils_1.UniqueSignatureTracker();
6291
- // Create a list of potential overload matches based on arguments.
6292
- types_1.OverloadedFunctionType.getOverloads(typeResult.type).forEach((overload) => {
6293
- useSpeculativeMode(errorNode, () => {
6294
- const matchResults = matchArgsToParams(errorNode, argList, { type: overload, isIncomplete: typeResult.isIncomplete }, overloadIndex,
6295
- /* signatureTracker */ undefined);
6296
- if (!matchResults.argumentErrors) {
6297
- matches.push(matchResults);
6298
- }
6299
- overloadIndex++;
6385
+ const speculativeNode = getSpeculativeNodeForCall(errorNode);
6386
+ useSignatureTracker(errorNode, () => {
6387
+ // Create a list of potential overload matches based on arguments.
6388
+ types_1.OverloadedFunctionType.getOverloads(typeResult.type).forEach((overload) => {
6389
+ useSpeculativeMode(speculativeNode, () => {
6390
+ const matchResults = matchArgsToParams(errorNode, argList, { type: overload, isIncomplete: typeResult.isIncomplete }, overloadIndex);
6391
+ if (!matchResults.argumentErrors) {
6392
+ matches.push(matchResults);
6393
+ }
6394
+ overloadIndex++;
6395
+ });
6300
6396
  });
6301
6397
  });
6302
6398
  matches = sortOverloadsByBestMatch(matches);
6303
6399
  let winningOverloadIndex;
6304
6400
  matches.forEach((match, matchIndex) => {
6305
6401
  if (winningOverloadIndex === undefined) {
6306
- useSpeculativeMode(errorNode, () => {
6307
- const callResult = validateArgTypes(errorNode, match, new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(match.overload)), signatureTracker,
6402
+ useSpeculativeMode(speculativeNode, () => {
6403
+ const callResult = validateArgTypes(errorNode, match, new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(match.overload)),
6308
6404
  /* skipUnknownArgCheck */ true);
6309
6405
  if (callResult && !callResult.argumentErrors) {
6310
6406
  winningOverloadIndex = matchIndex;
@@ -6323,22 +6419,24 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
6323
6419
  return a.overloadIndex - b.overloadIndex;
6324
6420
  });
6325
6421
  }
6326
- function validateOverloadedFunctionArguments(errorNode, argList, typeResult, typeVarContext, skipUnknownArgCheck, inferenceContext, signatureTracker) {
6422
+ function validateOverloadedArgTypes(errorNode, argList, typeResult, typeVarContext, skipUnknownArgCheck, inferenceContext) {
6327
6423
  let filteredMatchResults = [];
6328
6424
  let contextFreeArgTypes;
6329
6425
  let isTypeIncomplete = !!typeResult.isIncomplete;
6426
+ const type = typeResult.type;
6427
+ const speculativeNode = getSpeculativeNodeForCall(errorNode);
6330
6428
  // Start by evaluating the types of the arguments without any expected
6331
6429
  // type. Also, filter the list of overloads based on the number of
6332
6430
  // positional and named arguments that are present. We do all of this
6333
6431
  // speculatively because we don't want to record any types in the type
6334
6432
  // cache or record any diagnostics at this stage.
6335
- useSpeculativeMode(errorNode, () => {
6433
+ useSpeculativeMode(speculativeNode, () => {
6336
6434
  let overloadIndex = 0;
6337
- types_1.OverloadedFunctionType.getOverloads(typeResult.type).forEach((overload) => {
6435
+ types_1.OverloadedFunctionType.getOverloads(type).forEach((overload) => {
6338
6436
  // Consider only the functions that have the @overload decorator,
6339
6437
  // not the final function that omits the overload. This is the
6340
6438
  // intended behavior according to PEP 484.
6341
- const matchResults = matchArgsToParams(errorNode, argList, { type: overload, isIncomplete: typeResult.isIncomplete }, overloadIndex, signatureTracker);
6439
+ const matchResults = matchArgsToParams(errorNode, argList, { type: overload, isIncomplete: typeResult.isIncomplete }, overloadIndex);
6342
6440
  if (!matchResults.argumentErrors) {
6343
6441
  filteredMatchResults.push(matchResults);
6344
6442
  }
@@ -6352,10 +6450,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
6352
6450
  // Skip the error message if we're in speculative mode because it's very
6353
6451
  // expensive, and we're going to suppress the diagnostic anyway.
6354
6452
  if (!canSkipDiagnosticForNode(errorNode)) {
6355
- const functionName = typeResult.type.priv.overloads[0].shared.name || '<anonymous function>';
6453
+ const functionName = type.priv.overloads[0].shared.name || '<anonymous function>';
6356
6454
  const diagAddendum = new diagnostic_1.DiagnosticAddendum();
6357
6455
  const argTypes = argList.map((t) => {
6358
- const typeString = printType(getTypeOfArgument(t, /* inferenceContext */ undefined, signatureTracker).type);
6456
+ const typeString = printType(getTypeOfArgument(t, /* inferenceContext */ undefined).type);
6359
6457
  if (t.argumentCategory === 1 /* ArgumentCategory.UnpackedList */) {
6360
6458
  return `*${typeString}`;
6361
6459
  }
@@ -6394,7 +6492,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
6394
6492
  const effectiveTypeVarContext = typeVarContext !== null && typeVarContext !== void 0 ? typeVarContext : new typeVarContext_1.TypeVarContext();
6395
6493
  effectiveTypeVarContext.addSolveForScope((0, typeUtils_1.getTypeVarScopeIds)(bestMatch.overload));
6396
6494
  effectiveTypeVarContext.unlock();
6397
- return validateArgTypesWithContext(errorNode, bestMatch, effectiveTypeVarContext, skipUnknownArgCheck, inferenceContext, signatureTracker);
6495
+ return validateArgTypesWithContext(errorNode, bestMatch, effectiveTypeVarContext, skipUnknownArgCheck, inferenceContext);
6398
6496
  }
6399
6497
  // If there is only one possible arg/param match among the overloads,
6400
6498
  // use the normal type matching mechanism because it is faster and
@@ -6406,7 +6504,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
6406
6504
  }
6407
6505
  let expandedArgTypes = [argList.map((arg) => undefined)];
6408
6506
  while (true) {
6409
- const callResult = validateOverloadsWithExpandedTypes(errorNode, expandedArgTypes, filteredMatchResults, typeVarContext, skipUnknownArgCheck, inferenceContext, signatureTracker);
6507
+ const callResult = validateOverloadsWithExpandedTypes(errorNode, expandedArgTypes, filteredMatchResults, typeVarContext, skipUnknownArgCheck, inferenceContext);
6410
6508
  if (callResult.isTypeIncomplete) {
6411
6509
  isTypeIncomplete = true;
6412
6510
  }
@@ -6416,7 +6514,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
6416
6514
  // We didn't find an overload match. Try to expand the next union
6417
6515
  // argument type into individual types and retry with the expanded types.
6418
6516
  if (!contextFreeArgTypes) {
6419
- useSpeculativeMode(errorNode, () => {
6517
+ useSpeculativeMode(getSpeculativeNodeForCall(errorNode), () => {
6420
6518
  // Evaluate the types of each argument expression without regard to
6421
6519
  // the context. We'll use this to determine whether we need to do
6422
6520
  // union expansion.
@@ -6504,7 +6602,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
6504
6602
  // list, specializes the call based on arg types, and returns the
6505
6603
  // specialized type of the return value. If it detects an error along
6506
6604
  // the way, it emits a diagnostic and sets argumentErrors to true.
6507
- function validateCallArguments(errorNode, argList, callTypeResult, typeVarContext, skipUnknownArgCheck, inferenceContext, signatureTracker, recursionCount = 0) {
6605
+ function validateCallArguments(errorNode, argList, callTypeResult, typeVarContext, skipUnknownArgCheck, inferenceContext, recursionCount = 0) {
6508
6606
  var _a;
6509
6607
  let argumentErrors = false;
6510
6608
  let isTypeIncomplete = false;
@@ -6523,8 +6621,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
6523
6621
  return { returnType: types_1.UnknownType.create(), argumentErrors: true, overloadsUsedForCall };
6524
6622
  }
6525
6623
  let returnType = mapSubtypesExpandTypeVars(callTypeResult.type, { sortSubtypes: true }, (expandedSubtype, unexpandedSubtype, isLastIteration) => {
6526
- return useSpeculativeMode(isLastIteration ? undefined : errorNode, () => {
6527
- const callResult = validateCallArgumentsForSubtype(errorNode, argList, expandedSubtype, unexpandedSubtype, !!callTypeResult.isIncomplete, typeVarContext, skipUnknownArgCheck, inferenceContext, signatureTracker, recursionCount);
6624
+ return useSpeculativeMode(isLastIteration ? undefined : getSpeculativeNodeForCall(errorNode), () => {
6625
+ const callResult = validateCallArgumentsForSubtype(errorNode, argList, expandedSubtype, unexpandedSubtype, !!callTypeResult.isIncomplete, typeVarContext, skipUnknownArgCheck, inferenceContext, recursionCount);
6528
6626
  if (callResult.argumentErrors) {
6529
6627
  argumentErrors = true;
6530
6628
  }
@@ -6554,7 +6652,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
6554
6652
  overloadsUsedForCall,
6555
6653
  };
6556
6654
  }
6557
- function validateCallArgumentsForSubtype(errorNode, argList, expandedCallType, unexpandedCallType, isCallTypeIncomplete, typeVarContext, skipUnknownArgCheck, inferenceContext, signatureTracker, recursionCount) {
6655
+ function validateCallArgumentsForSubtype(errorNode, argList, expandedCallType, unexpandedCallType, isCallTypeIncomplete, typeVarContext, skipUnknownArgCheck, inferenceContext, recursionCount) {
6558
6656
  switch (expandedCallType.category) {
6559
6657
  case 3 /* TypeCategory.Never */:
6560
6658
  case 1 /* TypeCategory.Unknown */:
@@ -6565,17 +6663,17 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
6565
6663
  if (!isCallTypeIncomplete) {
6566
6664
  argList.forEach((arg) => {
6567
6665
  if (arg.valueExpression && !isSpeculativeModeInUse(arg.valueExpression)) {
6568
- getTypeOfArgument(arg, /* inferenceContext */ undefined, signatureTracker);
6666
+ getTypeOfArgument(arg, /* inferenceContext */ undefined);
6569
6667
  }
6570
6668
  });
6571
6669
  }
6572
6670
  return { returnType: expandedCallType };
6573
6671
  }
6574
6672
  case 4 /* TypeCategory.Function */: {
6575
- return validateCallForFunction(errorNode, argList, expandedCallType, isCallTypeIncomplete, typeVarContext, skipUnknownArgCheck, inferenceContext, signatureTracker);
6673
+ return validateCallForFunction(errorNode, argList, expandedCallType, isCallTypeIncomplete, typeVarContext, skipUnknownArgCheck, inferenceContext);
6576
6674
  }
6577
6675
  case 5 /* TypeCategory.OverloadedFunction */: {
6578
- return validateCallForOverloadedFunction(errorNode, argList, expandedCallType, isCallTypeIncomplete, typeVarContext, skipUnknownArgCheck, inferenceContext, signatureTracker);
6676
+ return validateCallForOverloadedFunction(errorNode, argList, expandedCallType, isCallTypeIncomplete, typeVarContext, skipUnknownArgCheck, inferenceContext);
6579
6677
  }
6580
6678
  case 6 /* TypeCategory.Class */: {
6581
6679
  if ((0, typeUtils_1.isNoneInstance)(expandedCallType)) {
@@ -6583,16 +6681,16 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
6583
6681
  return { argumentErrors: true };
6584
6682
  }
6585
6683
  if (types_1.TypeBase.isInstantiable(expandedCallType)) {
6586
- return validateCallForInstantiableClass(errorNode, argList, expandedCallType, unexpandedCallType, skipUnknownArgCheck, inferenceContext, signatureTracker);
6684
+ return validateCallForInstantiableClass(errorNode, argList, expandedCallType, unexpandedCallType, skipUnknownArgCheck, inferenceContext);
6587
6685
  }
6588
- return validateCallForClassInstance(errorNode, argList, expandedCallType, unexpandedCallType, typeVarContext, skipUnknownArgCheck, inferenceContext, signatureTracker, recursionCount);
6686
+ return validateCallForClassInstance(errorNode, argList, expandedCallType, unexpandedCallType, typeVarContext, skipUnknownArgCheck, inferenceContext, recursionCount);
6589
6687
  }
6590
6688
  // TypeVars should have been expanded in most cases,
6591
6689
  // but we still need to handle the case of Type[T] where
6592
6690
  // T is a constrained type that contains a union. We also
6593
6691
  // need to handle recursive type aliases.
6594
6692
  case 9 /* TypeCategory.TypeVar */: {
6595
- return validateCallArguments(errorNode, argList, { type: (0, typeUtils_1.transformPossibleRecursiveTypeAlias)(expandedCallType), isIncomplete: isCallTypeIncomplete }, typeVarContext, skipUnknownArgCheck, inferenceContext, signatureTracker, recursionCount);
6693
+ return validateCallArguments(errorNode, argList, { type: (0, typeUtils_1.transformPossibleRecursiveTypeAlias)(expandedCallType), isIncomplete: isCallTypeIncomplete }, typeVarContext, skipUnknownArgCheck, inferenceContext, recursionCount);
6596
6694
  }
6597
6695
  case 7 /* TypeCategory.Module */: {
6598
6696
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportCallIssue, localize_1.LocMessage.moduleNotCallable(), errorNode);
@@ -6601,7 +6699,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
6601
6699
  }
6602
6700
  return { argumentErrors: true };
6603
6701
  }
6604
- function validateCallForFunction(errorNode, argList, type, isCallTypeIncomplete, typeVarContext, skipUnknownArgCheck, inferenceContext, signatureTracker) {
6702
+ function validateCallForFunction(errorNode, argList, type, isCallTypeIncomplete, typeVarContext, skipUnknownArgCheck, inferenceContext) {
6605
6703
  var _a;
6606
6704
  if (types_1.TypeBase.isInstantiable(type)) {
6607
6705
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportCallIssue, localize_1.LocMessage.callableNotInstantiable().format({
@@ -6609,11 +6707,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
6609
6707
  }), errorNode);
6610
6708
  return { returnType: undefined, argumentErrors: true };
6611
6709
  }
6612
- let effectiveTypeVarContext = typeVarContext;
6613
- if (!effectiveTypeVarContext) {
6614
- // If a typeVarContext wasn't provided by the caller, allocate one here.
6615
- effectiveTypeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeIds)(type));
6616
- }
6710
+ const effectiveTypeVarContext = typeVarContext !== null && typeVarContext !== void 0 ? typeVarContext : new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeIds)(type));
6617
6711
  // The stdlib collections/__init__.pyi stub file defines namedtuple
6618
6712
  // as a function rather than a class, so we need to check for it here.
6619
6713
  if (types_1.FunctionType.isBuiltIn(type, 'namedtuple')) {
@@ -6621,14 +6715,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
6621
6715
  const result = {
6622
6716
  returnType: (0, namedTuples_1.createNamedTupleType)(evaluatorInterface, errorNode, argList, /* includesTypes */ false),
6623
6717
  };
6624
- validateArgs(errorNode, argList, { type: type }, effectiveTypeVarContext, skipUnknownArgCheck, inferenceContext, signatureTracker);
6718
+ validateArgs(errorNode, argList, { type: type }, effectiveTypeVarContext, skipUnknownArgCheck, inferenceContext);
6625
6719
  return result;
6626
6720
  }
6627
6721
  // Handle the NewType specially, replacing the normal return type.
6628
6722
  if (types_1.FunctionType.isBuiltIn(type, 'NewType')) {
6629
6723
  return { returnType: createNewType(errorNode, argList) };
6630
6724
  }
6631
- const functionResult = validateArgs(errorNode, argList, { type, isIncomplete: isCallTypeIncomplete }, effectiveTypeVarContext, skipUnknownArgCheck, inferenceContext, signatureTracker);
6725
+ const functionResult = validateArgs(errorNode, argList, { type, isIncomplete: isCallTypeIncomplete }, effectiveTypeVarContext, skipUnknownArgCheck, inferenceContext);
6632
6726
  let isTypeIncomplete = !!functionResult.isTypeIncomplete;
6633
6727
  let returnType = functionResult.returnType;
6634
6728
  let argumentErrors = !!functionResult.argumentErrors;
@@ -6735,13 +6829,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
6735
6829
  }
6736
6830
  return { symbol, symbolName, classType, hasImplementation };
6737
6831
  }
6738
- function validateCallForOverloadedFunction(errorNode, argList, expandedCallType, isCallTypeIncomplete, typeVarContext, skipUnknownArgCheck, inferenceContext, signatureTracker) {
6832
+ function validateCallForOverloadedFunction(errorNode, argList, expandedCallType, isCallTypeIncomplete, typeVarContext, skipUnknownArgCheck, inferenceContext) {
6739
6833
  var _a, _b;
6740
6834
  // Handle the 'cast' call as a special case.
6741
6835
  if (types_1.FunctionType.isBuiltIn(expandedCallType.priv.overloads[0], 'cast') && argList.length === 2) {
6742
6836
  return { returnType: evaluateCastCall(argList, errorNode) };
6743
6837
  }
6744
- const callResult = validateOverloadedFunctionArguments(errorNode, argList, { type: expandedCallType, isIncomplete: isCallTypeIncomplete }, typeVarContext, skipUnknownArgCheck, inferenceContext, signatureTracker);
6838
+ const callResult = validateOverloadedArgTypes(errorNode, argList, { type: expandedCallType, isIncomplete: isCallTypeIncomplete }, typeVarContext, skipUnknownArgCheck, inferenceContext);
6745
6839
  let returnType = (_a = callResult.returnType) !== null && _a !== void 0 ? _a : types_1.UnknownType.create();
6746
6840
  let isTypeIncomplete = !!callResult.isTypeIncomplete;
6747
6841
  let argumentErrors = !!callResult.argumentErrors;
@@ -6768,7 +6862,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
6768
6862
  specializedInitSelfType: callResult.specializedInitSelfType,
6769
6863
  };
6770
6864
  }
6771
- function validateCallForInstantiableClass(errorNode, argList, expandedCallType, unexpandedCallType, skipUnknownArgCheck, inferenceContext, signatureTracker) {
6865
+ function validateCallForInstantiableClass(errorNode, argList, expandedCallType, unexpandedCallType, skipUnknownArgCheck, inferenceContext) {
6772
6866
  var _a, _b;
6773
6867
  if (expandedCallType.priv.literalValue !== undefined) {
6774
6868
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportCallIssue, localize_1.LocMessage.literalNotCallable(), errorNode);
@@ -6785,12 +6879,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
6785
6879
  return { returnType: types_1.UnknownType.create(), argumentErrors: true };
6786
6880
  }
6787
6881
  // Validate the constructor arguments.
6788
- (0, constructors_1.validateConstructorArguments)(evaluatorInterface, errorNode, argList, expandedCallType, skipUnknownArgCheck, inferenceContext, signatureTracker);
6882
+ (0, constructors_1.validateConstructorArguments)(evaluatorInterface, errorNode, argList, expandedCallType, skipUnknownArgCheck, inferenceContext);
6789
6883
  // The one-parameter form of "type" returns the class
6790
6884
  // for the specified object.
6791
6885
  if (expandedCallType.shared.name === 'type' && argList.length === 1) {
6792
- const argType = getTypeOfArgument(argList[0],
6793
- /* inferenceContext */ undefined, signatureTracker).type;
6886
+ const argType = getTypeOfArgument(argList[0], /* inferenceContext */ undefined).type;
6794
6887
  const returnType = (0, typeUtils_1.mapSubtypes)(argType, (subtype) => {
6795
6888
  if ((0, types_1.isInstantiableClass)(subtype) && subtype.shared.effectiveMetaclass) {
6796
6889
  return subtype.shared.effectiveMetaclass;
@@ -6850,9 +6943,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
6850
6943
  const initTypeResult = (0, constructors_1.getBoundInitMethod)(evaluatorInterface, errorNode, types_1.ClassType.cloneAsInstance(expandedCallType),
6851
6944
  /* diag */ undefined, 0 /* MemberAccessFlags.Default */);
6852
6945
  if (initTypeResult && (0, types_1.isOverloadedFunction)(initTypeResult.type)) {
6853
- validateOverloadedFunctionArguments(errorNode, argList, { type: initTypeResult.type },
6946
+ validateOverloadedArgTypes(errorNode, argList, { type: initTypeResult.type },
6854
6947
  /* typeVarContext */ undefined, skipUnknownArgCheck,
6855
- /* inferenceContext */ undefined, signatureTracker);
6948
+ /* inferenceContext */ undefined);
6856
6949
  }
6857
6950
  return result;
6858
6951
  }
@@ -6916,7 +7009,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
6916
7009
  }), errorNode);
6917
7010
  }
6918
7011
  // Assume this is a call to the constructor.
6919
- const constructorResult = (0, constructors_1.validateConstructorArguments)(evaluatorInterface, errorNode, argList, expandedCallType, skipUnknownArgCheck, inferenceContext, signatureTracker);
7012
+ const constructorResult = (0, constructors_1.validateConstructorArguments)(evaluatorInterface, errorNode, argList, expandedCallType, skipUnknownArgCheck, inferenceContext);
6920
7013
  const overloadsUsedForCall = constructorResult.overloadsUsedForCall;
6921
7014
  const argumentErrors = constructorResult.argumentErrors;
6922
7015
  const isTypeIncomplete = constructorResult.isTypeIncomplete;
@@ -6943,8 +7036,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
6943
7036
  returnType.shared.mro.some((baseClass) => (0, types_1.isInstantiableClass)(baseClass) && types_1.ClassType.isBuiltIn(baseClass, 'type'))) {
6944
7037
  let newClassName = '__class_' + returnType.shared.name;
6945
7038
  if (argList.length === 3) {
6946
- const firstArgType = getTypeOfArgument(argList[0],
6947
- /* inferenceContext */ undefined, signatureTracker).type;
7039
+ const firstArgType = getTypeOfArgument(argList[0], /* inferenceContext */ undefined).type;
6948
7040
  if ((0, types_1.isClassInstance)(firstArgType) &&
6949
7041
  types_1.ClassType.isBuiltIn(firstArgType, 'str') &&
6950
7042
  typeof firstArgType.priv.literalValue === 'string') {
@@ -6960,7 +7052,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
6960
7052
  }
6961
7053
  return { returnType, overloadsUsedForCall, argumentErrors, isTypeIncomplete };
6962
7054
  }
6963
- function validateCallForClassInstance(errorNode, argList, expandedCallType, unexpandedCallType, typeVarContext, skipUnknownArgCheck, inferenceContext, signatureTracker, recursionCount) {
7055
+ function validateCallForClassInstance(errorNode, argList, expandedCallType, unexpandedCallType, typeVarContext, skipUnknownArgCheck, inferenceContext, recursionCount) {
6964
7056
  var _a;
6965
7057
  const callDiag = new diagnostic_1.DiagnosticAddendum();
6966
7058
  const callMethodResult = getTypeOfBoundMember(errorNode, expandedCallType, '__call__',
@@ -6973,7 +7065,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
6973
7065
  }) + callDiag.getString(), errorNode);
6974
7066
  return { returnType: types_1.UnknownType.create(), argumentErrors: true };
6975
7067
  }
6976
- const callResult = validateCallArguments(errorNode, argList, { type: callMethodType }, typeVarContext, skipUnknownArgCheck, inferenceContext, signatureTracker, recursionCount);
7068
+ const callResult = validateCallArguments(errorNode, argList, { type: callMethodType }, typeVarContext, skipUnknownArgCheck, inferenceContext, recursionCount);
6977
7069
  let returnType = (_a = callResult.returnType) !== null && _a !== void 0 ? _a : types_1.UnknownType.create();
6978
7070
  if ((0, types_1.isTypeVar)(unexpandedCallType) &&
6979
7071
  types_1.TypeBase.isInstantiable(unexpandedCallType) &&
@@ -6994,9 +7086,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
6994
7086
  var _a;
6995
7087
  // Verify that the cast is necessary.
6996
7088
  const castToType = getTypeOfArgumentExpectingType(argList[0], { enforceTypeAnnotationRules: true }).type;
6997
- let castFromType = getTypeOfArgument(argList[1],
6998
- /* inferenceContext */ undefined,
6999
- /* signatureTracker */ undefined).type;
7089
+ let castFromType = getTypeOfArgument(argList[1], /* inferenceContext */ undefined).type;
7000
7090
  if ((_a = castFromType.props) === null || _a === void 0 ? void 0 : _a.specialForm) {
7001
7091
  castFromType = castFromType.props.specialForm;
7002
7092
  }
@@ -7012,12 +7102,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
7012
7102
  return (0, typeUtils_1.convertToInstance)(castToType);
7013
7103
  }
7014
7104
  // Expands any unpacked tuples within an argument list.
7015
- function expandArgList(argList, signatureTracker) {
7105
+ function expandArgList(argList) {
7016
7106
  var _a;
7017
7107
  const expandedArgList = [];
7018
7108
  for (const arg of argList) {
7019
7109
  if (arg.argumentCategory === 1 /* ArgumentCategory.UnpackedList */) {
7020
- const argType = getTypeOfArgument(arg, /* inferenceContext */ undefined, signatureTracker).type;
7110
+ const argType = getTypeOfArgument(arg, /* inferenceContext */ undefined).type;
7021
7111
  // If this is a tuple with specified element types, use those
7022
7112
  // specified types rather than using the more generic iterator
7023
7113
  // type which will be a union of all element types.
@@ -7059,7 +7149,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
7059
7149
  // function. This matching is done based on positions and keywords. Type evaluation and
7060
7150
  // validation is left to the caller.
7061
7151
  // This logic is based on PEP 3102: https://www.python.org/dev/peps/pep-3102/
7062
- function matchArgsToParams(errorNode, argList, typeResult, overloadIndex, signatureTracker) {
7152
+ function matchArgsToParams(errorNode, argList, typeResult, overloadIndex) {
7063
7153
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
7064
7154
  const overload = typeResult.type;
7065
7155
  const paramDetails = (0, parameterUtils_1.getParameterListDetails)(overload);
@@ -7070,7 +7160,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
7070
7160
  let isTypeIncomplete = !!typeResult.isIncomplete;
7071
7161
  let isVariadicTypeVarFullyMatched = false;
7072
7162
  // Expand any unpacked tuples in the arg list.
7073
- argList = expandArgList(argList, signatureTracker);
7163
+ argList = expandArgList(argList);
7074
7164
  // Build a map of parameters by name.
7075
7165
  const paramMap = new Map();
7076
7166
  paramDetails.params.forEach((paramInfo) => {
@@ -7194,8 +7284,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
7194
7284
  // If this is an unpacked iterable, we will conservatively assume that it
7195
7285
  // might have zero iterations unless we can tell from its type that it
7196
7286
  // definitely has at least one iterable value.
7197
- const argType = getTypeOfArgument(argList[argIndex],
7198
- /* inferenceContext */ undefined, signatureTracker).type;
7287
+ const argType = getTypeOfArgument(argList[argIndex], /* inferenceContext */ undefined).type;
7199
7288
  if ((0, types_1.isClassInstance)(argType) &&
7200
7289
  (0, typeUtils_1.isTupleClass)(argType) &&
7201
7290
  !(0, typeUtils_1.isUnboundedTupleClass)(argType) &&
@@ -7224,14 +7313,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
7224
7313
  break;
7225
7314
  }
7226
7315
  (0, debug_1.assert)(paramDetails.params[paramIndex], 'paramIndex params entry is undefined');
7227
- const paramType = paramDetails.params[paramIndex].type;
7228
- const paramName = paramDetails.params[paramIndex].param.name;
7229
- const isParamVariadic = paramDetails.params[paramIndex].param.category === 1 /* ParameterCategory.ArgsList */ &&
7230
- (0, types_1.isVariadicTypeVar)(paramType);
7316
+ const paramInfo = paramDetails.params[paramIndex];
7317
+ const paramType = paramInfo.type;
7318
+ const paramName = paramInfo.param.name;
7319
+ const isParamVariadic = paramInfo.param.category === 1 /* ParameterCategory.ArgsList */ && (0, types_1.isVariadicTypeVar)(paramType);
7231
7320
  if (argList[argIndex].argumentCategory === 1 /* ArgumentCategory.UnpackedList */) {
7232
7321
  let isArgCompatibleWithVariadic = false;
7233
- const argTypeResult = getTypeOfArgument(argList[argIndex],
7234
- /* inferenceContext */ undefined, signatureTracker);
7322
+ const argTypeResult = getTypeOfArgument(argList[argIndex], /* inferenceContext */ undefined);
7235
7323
  let listElementType;
7236
7324
  let advanceToNextArg = false;
7237
7325
  // Handle the case where *args is being passed to a function defined
@@ -7241,7 +7329,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
7241
7329
  if (paramIndex < positionParamLimitIndex) {
7242
7330
  if ((0, types_1.isTypeVar)(argTypeResult.type) &&
7243
7331
  argTypeResult.type.priv.paramSpecAccess === 'args' &&
7244
- paramDetails.params[paramIndex].param.category !== 1 /* ParameterCategory.ArgsList */) {
7332
+ paramInfo.param.category !== 1 /* ParameterCategory.ArgsList */) {
7245
7333
  if (!canSkipDiagnosticForNode(errorNode) && !isTypeIncomplete) {
7246
7334
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportCallIssue, positionParamLimitIndex === 1
7247
7335
  ? localize_1.LocMessage.argPositionalExpectedOne()
@@ -7292,7 +7380,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
7292
7380
  listElementType = (_f = getTypeOfIterator({ type: argType, isIncomplete: argTypeResult.isIncomplete },
7293
7381
  /* isAsync */ false, errorNode,
7294
7382
  /* emitNotIterableError */ false)) === null || _f === void 0 ? void 0 : _f.type;
7295
- if (paramDetails.params[paramIndex].param.category !== 1 /* ParameterCategory.ArgsList */) {
7383
+ if (paramInfo.param.category !== 1 /* ParameterCategory.ArgsList */) {
7296
7384
  matchedUnpackedListOfUnknownLength = true;
7297
7385
  }
7298
7386
  }
@@ -7314,18 +7402,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
7314
7402
  reportedArgError = true;
7315
7403
  }
7316
7404
  else {
7317
- if (paramSpecArgList) {
7405
+ if (paramSpecArgList && paramInfo.param.category !== 0 /* ParameterCategory.Simple */) {
7318
7406
  paramSpecArgList.push(argList[argIndex]);
7319
7407
  }
7320
7408
  if (funcArg) {
7321
7409
  validateArgTypeParams.push({
7322
- paramCategory: paramDetails.params[paramIndex].param.category,
7410
+ paramCategory: paramInfo.param.category,
7323
7411
  paramType,
7324
7412
  requiresTypeVarMatching: (0, typeUtils_1.requiresSpecialization)(paramType),
7325
7413
  argument: funcArg,
7326
7414
  errorNode: (_g = argList[argIndex].valueExpression) !== null && _g !== void 0 ? _g : errorNode,
7327
7415
  paramName,
7328
- isParamNameSynthesized: types_1.FunctionParam.isNameSynthesized(paramDetails.params[paramIndex].param),
7416
+ isParamNameSynthesized: types_1.FunctionParam.isNameSynthesized(paramInfo.param),
7329
7417
  mapsToVarArgList: isParamVariadic && remainingArgCount > remainingParamCount,
7330
7418
  });
7331
7419
  }
@@ -7466,7 +7554,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
7466
7554
  while (argIndex < argList.length) {
7467
7555
  if (argList[argIndex].argumentCategory === 2 /* ArgumentCategory.UnpackedDictionary */) {
7468
7556
  // Verify that the type used in this expression is a SupportsKeysAndGetItem[str, T].
7469
- const argTypeResult = getTypeOfArgument(argList[argIndex], (0, typeUtils_1.makeInferenceContext)(paramDetails.unpackedKwargsTypedDictType), signatureTracker);
7557
+ const argTypeResult = getTypeOfArgument(argList[argIndex], (0, typeUtils_1.makeInferenceContext)(paramDetails.unpackedKwargsTypedDictType));
7470
7558
  const argType = argTypeResult.type;
7471
7559
  if (argTypeResult.isIncomplete) {
7472
7560
  isTypeIncomplete = true;
@@ -7693,7 +7781,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
7693
7781
  // argument to a function that accepts a ParamSpec.
7694
7782
  if (paramSpec) {
7695
7783
  const argTypeResult = getTypeOfArgument(argList[argIndex],
7696
- /* inferenceContext */ undefined, signatureTracker);
7784
+ /* inferenceContext */ undefined);
7697
7785
  const argType = argTypeResult.type;
7698
7786
  if (argTypeResult.isIncomplete) {
7699
7787
  isTypeIncomplete = true;
@@ -7816,8 +7904,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
7816
7904
  if ((0, types_1.isVariadicTypeVar)(paramType) && !paramType.priv.isVariadicInUnion) {
7817
7905
  const tupleTypeArgs = variadicArgs.map((argParam) => {
7818
7906
  var _a;
7819
- const argType = getTypeOfArgument(argParam.argument,
7820
- /* inferenceContext */ undefined, signatureTracker).type;
7907
+ const argType = getTypeOfArgument(argParam.argument, /* inferenceContext */ undefined).type;
7821
7908
  const containsVariadicTypeVar = (0, types_1.isUnpackedVariadicTypeVar)(argType) ||
7822
7909
  ((0, types_1.isClassInstance)(argType) &&
7823
7910
  (0, typeUtils_1.isTupleClass)(argType) &&
@@ -7896,114 +7983,111 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
7896
7983
  // After having matched arguments with parameters, this function evaluates the
7897
7984
  // types of each argument expression and validates that the resulting type is
7898
7985
  // compatible with the declared type of the corresponding parameter.
7899
- function validateArgTypesWithContext(errorNode, matchResults, typeVarContext, skipUnknownArgCheck = false, inferenceContext, signatureTracker) {
7986
+ function validateArgTypesWithContext(errorNode, matchResults, typeVarContext, skipUnknownArgCheck = false, inferenceContext) {
7987
+ var _a;
7900
7988
  const type = matchResults.overload;
7901
- if (!signatureTracker) {
7902
- signatureTracker = new typeUtils_1.UniqueSignatureTracker();
7903
- }
7904
- matchResults.overload = (0, typeUtils_1.ensureFunctionSignaturesAreUnique)(matchResults.overload, signatureTracker, errorNode.start);
7905
- const returnType = getFunctionEffectiveReturnType(type);
7906
7989
  let expectedType = inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.expectedType;
7990
+ // Can we safely ignore the inference context, either because it's not provided
7991
+ // or will have no effect? If so, avoid the extra work.
7992
+ const returnType = (_a = inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.returnTypeOverride) !== null && _a !== void 0 ? _a : getFunctionEffectiveReturnType(type);
7993
+ if (!returnType || !(0, typeUtils_1.requiresSpecialization)(returnType)) {
7994
+ expectedType = undefined;
7995
+ }
7996
+ const tryExpectedType = (expectedSubtype) => {
7997
+ const clonedTypeVarContext = typeVarContext.clone();
7998
+ const callResult = validateArgTypesWithExpectedType(errorNode, matchResults, clonedTypeVarContext,
7999
+ /* skipUnknownArgCheck */ true, expectedSubtype, returnType);
8000
+ // Use a heuristic to pick a subtype that is most likely to be correct.
8001
+ // We'll look for a subtype that produces no argument errors and has
8002
+ // no Unknowns in the return type.
8003
+ if (!callResult.argumentErrors && callResult.returnType) {
8004
+ const returnType = (inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.returnTypeOverride)
8005
+ ? (0, typeUtils_1.applySolvedTypeVars)(inferenceContext.returnTypeOverride, clonedTypeVarContext)
8006
+ : callResult.returnType;
8007
+ if (assignType(expectedSubtype, returnType,
8008
+ /* diag */ undefined,
8009
+ /* destTypeVarContext */ undefined,
8010
+ /* srcTypeVarContext */ undefined, 1024 /* AssignTypeFlags.IgnoreTypeVarScope */)) {
8011
+ const anyOrUnknown = (0, typeUtils_1.containsAnyOrUnknown)(callResult.returnType, /* recurse */ true);
8012
+ // Prefer return types that have no unknown or Any.
8013
+ if (!anyOrUnknown) {
8014
+ return 3;
8015
+ }
8016
+ // Prefer Any over Unknown.
8017
+ return (0, types_1.isAny)(anyOrUnknown) ? 2 : 1;
8018
+ }
8019
+ }
8020
+ return 0;
8021
+ };
7907
8022
  // If the expected type is a union, we don't know which type is expected.
7908
8023
  // We may or may not be able to make use of the expected type. We'll evaluate
7909
- // speculatively to see if using the expected type works.
8024
+ // speculatively to see if using one of the expected subtypes works.
7910
8025
  if (expectedType && (0, types_1.isUnion)(expectedType)) {
7911
- let assignFlags = 2048 /* AssignTypeFlags.PopulatingExpectedType */;
7912
- if ((0, typeUtils_1.containsLiteralType)(expectedType, /* includeTypeArgs */ true)) {
7913
- assignFlags |= 256 /* AssignTypeFlags.RetainLiteralsForTypeVar */;
7914
- }
7915
- useSpeculativeMode(errorNode, () => {
7916
- const typeVarContextCopy = typeVarContext.clone();
7917
- assignType(returnType, expectedType,
7918
- /* diag */ undefined, typeVarContextCopy,
7919
- /* srcTypeVarContext */ undefined, assignFlags);
7920
- const speculativeResults = validateArgTypes(errorNode, matchResults, typeVarContextCopy, signatureTracker, skipUnknownArgCheck);
7921
- if (speculativeResults === null || speculativeResults === void 0 ? void 0 : speculativeResults.argumentErrors) {
7922
- expectedType = undefined;
8026
+ expectedType = useSpeculativeMode(getSpeculativeNodeForCall(errorNode), () => {
8027
+ let validExpectedSubtype;
8028
+ let bestSubtypeScore = -1;
8029
+ (0, typeUtils_1.doForEachSubtype)(expectedType, (expectedSubtype) => {
8030
+ if (bestSubtypeScore < 3) {
8031
+ const score = tryExpectedType(expectedSubtype);
8032
+ if (score > bestSubtypeScore) {
8033
+ validExpectedSubtype = expectedSubtype;
8034
+ bestSubtypeScore = score;
8035
+ }
8036
+ }
8037
+ },
8038
+ /* sortSubtypes */ true);
8039
+ if (bestSubtypeScore < 3) {
8040
+ const score = tryExpectedType(expectedType);
8041
+ if (score > bestSubtypeScore) {
8042
+ validExpectedSubtype = expectedType;
8043
+ }
7923
8044
  }
8045
+ return validExpectedSubtype;
7924
8046
  });
7925
8047
  }
7926
- if (expectedType) {
7927
- if ((0, types_1.isAnyOrUnknown)(expectedType) || (0, types_1.isNever)(expectedType)) {
7928
- expectedType = undefined;
7929
- }
7930
- }
7931
- // Can we safely ignore the inference context, either because it's not provided
7932
- // or will have no effect? If so, we can eliminate some extra work.
7933
- if (!expectedType || !returnType || !(0, typeUtils_1.requiresSpecialization)(returnType)) {
7934
- return validateArgTypes(errorNode, matchResults, typeVarContext, signatureTracker, skipUnknownArgCheck);
8048
+ // If there is no expected type, or the expected type is Any or Unknown,
8049
+ // there's nothing left to do here.
8050
+ if (!expectedType || (0, types_1.isAnyOrUnknown)(expectedType) || (0, types_1.isNever)(expectedType)) {
8051
+ return validateArgTypes(errorNode, matchResults, typeVarContext, skipUnknownArgCheck);
7935
8052
  }
7936
- return validateArgTypesWithExpectedType(errorNode, matchResults, typeVarContext, skipUnknownArgCheck, expectedType, returnType, signatureTracker);
8053
+ return validateArgTypesWithExpectedType(errorNode, matchResults, typeVarContext, skipUnknownArgCheck, expectedType, returnType);
7937
8054
  }
7938
- function validateArgTypesWithExpectedType(errorNode, matchResults, typeVarContext, skipUnknownArgCheck = false, expectedType, returnType, signatureTracker) {
8055
+ function validateArgTypesWithExpectedType(errorNode, matchResults, typeVarContext, skipUnknownArgCheck = false, expectedType, returnType) {
7939
8056
  const liveTypeVarScopes = ParseTreeUtils.getTypeVarScopesForNode(errorNode);
7940
8057
  let assignFlags = 2048 /* AssignTypeFlags.PopulatingExpectedType */;
7941
8058
  if ((0, typeUtils_1.containsLiteralType)(expectedType, /* includeTypeArgs */ true)) {
7942
8059
  assignFlags |= 256 /* AssignTypeFlags.RetainLiteralsForTypeVar */;
7943
8060
  }
7944
- // Prepopulate the typeVarContext based on the specialized expected type if the
7945
- // callee has a declared return type. This will allow us to more closely match
7946
- // the expected type if possible.
7947
- // Determine which type arguments are needed to match the expected type.
7948
- if ((0, types_1.isClassInstance)(returnType)) {
7949
- // If the return type is a class and the expected type is a union
7950
- // that is type compatible with that class, filter the subtypes in
7951
- // the union to see if we can find one that is potentially compatible.
7952
- if ((0, types_1.isUnion)(expectedType)) {
7953
- const filteredType = (0, typeUtils_1.mapSubtypes)(expectedType, (subtype) => {
7954
- if (!(0, types_1.isClassInstance)(subtype) || subtype.shared.typeParameters.length === 0) {
7955
- return undefined;
7956
- }
7957
- if (types_1.ClassType.isProtocolClass(subtype) ||
7958
- subtype.shared.mro.some((mroClass) => {
7959
- return ((0, types_1.isClassInstance)(mroClass) &&
7960
- mroClass.shared.typeParameters.length > 0 &&
7961
- types_1.ClassType.isSameGenericClass(returnType, mroClass));
7962
- })) {
7963
- return subtype;
7964
- }
7965
- return undefined;
7966
- }, { sortSubtypes: true });
7967
- if ((0, types_1.isClassInstance)(filteredType)) {
7968
- expectedType = filteredType;
7969
- }
7970
- }
7971
- if ((0, types_1.isClassInstance)(expectedType) && !(0, types_1.isTypeSame)(returnType, expectedType)) {
7972
- const tempTypeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(returnType));
7973
- if ((0, constraintSolver_1.addConstraintsForExpectedType)(evaluatorInterface, returnType, expectedType, tempTypeVarContext, liveTypeVarScopes, errorNode.start)) {
7974
- const genericReturnType = types_1.ClassType.cloneForSpecialization(returnType,
7975
- /* typeArguments */ undefined,
7976
- /* isTypeArgumentExplicit */ false);
7977
- expectedType = (0, typeUtils_1.applySolvedTypeVars)(genericReturnType, tempTypeVarContext, {
7978
- unknownIfNotFound: true,
7979
- tupleClassType: getTupleClassType(),
7980
- });
7981
- assignFlags |= 4096 /* AssignTypeFlags.SkipPopulateUnknownExpectedType */;
7982
- }
7983
- }
7984
- }
7985
- else if ((0, types_1.isFunction)(returnType)) {
7986
- // If the return type is a callable and the expected type is a union that
7987
- // includes one or more non-callables, filter those out.
7988
- if ((0, types_1.isUnion)(expectedType)) {
7989
- expectedType = (0, typeUtils_1.mapSubtypes)(expectedType, (subtype) => {
7990
- return (0, typeUtils_1.isCallableType)(subtype) ? subtype : undefined;
8061
+ // Prepopulate the typeVarContext based on the specialized expected type.
8062
+ // This will allow us to more closely match the expected type if possible.
8063
+ if ((0, types_1.isClassInstance)(returnType) && (0, types_1.isClassInstance)(expectedType) && !(0, types_1.isTypeSame)(returnType, expectedType)) {
8064
+ const tempTypeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(returnType));
8065
+ if ((0, constraintSolver_1.addConstraintsForExpectedType)(evaluatorInterface, returnType, expectedType, tempTypeVarContext, liveTypeVarScopes, errorNode.start)) {
8066
+ const genericReturnType = (0, typeUtils_1.selfSpecializeClass)(returnType, {
8067
+ overrideTypeArgs: true,
8068
+ });
8069
+ expectedType = (0, typeUtils_1.applySolvedTypeVars)(genericReturnType, tempTypeVarContext, {
8070
+ unknownIfNotFound: true,
8071
+ useUnknownOverDefault: true,
8072
+ tupleClassType: getTupleClassType(),
7991
8073
  });
8074
+ assignFlags |= 4096 /* AssignTypeFlags.SkipPopulateUnknownExpectedType */;
7992
8075
  }
7993
8076
  }
7994
8077
  expectedType = (0, typeUtils_1.transformExpectedType)(expectedType, liveTypeVarScopes, errorNode.start);
7995
8078
  assignType(returnType, expectedType,
7996
8079
  /* diag */ undefined, typeVarContext,
7997
8080
  /* srcTypeVarContext */ undefined, assignFlags);
7998
- return validateArgTypes(errorNode, matchResults, typeVarContext, signatureTracker, skipUnknownArgCheck);
8081
+ return validateArgTypes(errorNode, matchResults, typeVarContext, skipUnknownArgCheck);
7999
8082
  }
8000
- function validateArgTypes(errorNode, matchResults, typeVarContext, signatureTracker, skipUnknownArgCheck) {
8083
+ function validateArgTypes(errorNode, matchResults, typeVarContext, skipUnknownArgCheck) {
8001
8084
  const type = matchResults.overload;
8002
8085
  let isTypeIncomplete = matchResults.isTypeIncomplete;
8003
8086
  let argumentErrors = false;
8004
8087
  let argumentMatchScore = 0;
8005
8088
  let specializedInitSelfType;
8006
8089
  let anyOrUnknownArgument;
8090
+ const speculativeNode = getSpeculativeNodeForCall(errorNode);
8007
8091
  const typeCondition = (0, typeUtils_1.getTypeCondition)(type);
8008
8092
  const paramSpec = types_1.FunctionType.getParamSpecFromArgsKwargs(type);
8009
8093
  // Check for an attempt to invoke an unimplemented abstract method.
@@ -8056,7 +8140,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
8056
8140
  // where more than two passes are needed.
8057
8141
  let passCount = Math.min(typeVarMatchingCount, 2);
8058
8142
  for (let i = 0; i < passCount; i++) {
8059
- useSpeculativeMode(errorNode, () => {
8143
+ useSpeculativeMode(speculativeNode, () => {
8060
8144
  matchResults.argParams.forEach((argParam) => {
8061
8145
  if (!argParam.requiresTypeVarMatching) {
8062
8146
  return;
@@ -8073,7 +8157,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
8073
8157
  // expected type during the first pass. This causes problems for
8074
8158
  // cases where the the call expression result can influence the
8075
8159
  // type of the TypeVar, such as in the expression "min(1, max(2, 0.5))".
8076
- const argResult = validateArgType(argParam, typeVarContext, signatureTracker, { type, isIncomplete: matchResults.isTypeIncomplete }, {
8160
+ const argResult = validateArgType(argParam, typeVarContext, { type, isIncomplete: matchResults.isTypeIncomplete }, {
8077
8161
  skipUnknownArgCheck,
8078
8162
  skipOverloadArg: i === 0,
8079
8163
  skipBareTypeVarExpectedType: i === 0,
@@ -8101,7 +8185,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
8101
8185
  const argResults = [];
8102
8186
  matchResults.argParams.forEach((argParam, argParamIndex) => {
8103
8187
  var _a;
8104
- const argResult = validateArgType(argParam, typeVarContext, signatureTracker, { type, isIncomplete: matchResults.isTypeIncomplete }, {
8188
+ const argResult = validateArgType(argParam, typeVarContext, { type, isIncomplete: matchResults.isTypeIncomplete }, {
8105
8189
  skipUnknownArgCheck,
8106
8190
  conditionFilter: typeCondition,
8107
8191
  });
@@ -8140,7 +8224,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
8140
8224
  let paramSpecTypeVarContext = [];
8141
8225
  // Handle the assignment of additional arguments that map to a param spec.
8142
8226
  if (matchResults.paramSpecArgList && matchResults.paramSpecTarget) {
8143
- const paramSpecArgResult = validateArgTypesForParamSpec(errorNode, matchResults.paramSpecArgList, matchResults.paramSpecTarget, typeVarContext, signatureTracker);
8227
+ const paramSpecArgResult = validateArgTypesForParamSpec(errorNode, matchResults.paramSpecArgList, matchResults.paramSpecTarget, typeVarContext);
8144
8228
  if (paramSpecArgResult.argumentErrors) {
8145
8229
  argumentErrors = true;
8146
8230
  argumentMatchScore += 1;
@@ -8190,7 +8274,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
8190
8274
  // call to a generic function or if this isn't a callable
8191
8275
  // return with type parameters that are rescoped from the original
8192
8276
  // function to the returned callable.
8193
- const unknownIfNotFound = !ParseTreeUtils.getTypeVarScopesForNode(errorNode).some((typeVarScope) => typeVarContext.hasSolveForScope(typeVarScope));
8277
+ const liveTypeVarScopes = ParseTreeUtils.getTypeVarScopesForNode(errorNode);
8278
+ const unknownIfNotFound = !liveTypeVarScopes.some((typeVarScope) => typeVarContext.hasSolveForScope(typeVarScope) ||
8279
+ typeVarContext.hasSolveForScope(types_1.TypeVarType.makeInternalScopeId(typeVarScope)));
8194
8280
  let specializedReturnType = (0, typeUtils_1.applySolvedTypeVars)(returnType, typeVarContext, {
8195
8281
  unknownIfNotFound,
8196
8282
  tupleClassType: getTupleClassType(),
@@ -8240,8 +8326,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
8240
8326
  specializedReturnType = types_1.ClassType.cloneAsInstance(types_1.ClassType.cloneForTypeGuard(boolClass, typeGuardType, useTypeIsSemantics));
8241
8327
  }
8242
8328
  }
8243
- const liveTypeVarScopes = ParseTreeUtils.getTypeVarScopesForNode(errorNode);
8244
- specializedReturnType = adjustCallableReturnType(type, specializedReturnType, liveTypeVarScopes, signatureTracker.getTrackedSignatures());
8329
+ specializedReturnType = adjustCallableReturnType(type, specializedReturnType, liveTypeVarScopes);
8245
8330
  if (specializedInitSelfType) {
8246
8331
  specializedInitSelfType = (0, typeUtils_1.applySolvedTypeVars)(specializedInitSelfType, typeVarContext);
8247
8332
  }
@@ -8288,12 +8373,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
8288
8373
  // to allow these type vars to be solved. This won't work with overloads
8289
8374
  // or unions of callables. It's intended for a specific use case. We may
8290
8375
  // need to make this more sophisticated in the future.
8291
- // The trackedSignatures parameter supplies a list of function signatures
8292
- // that were used for the function and the arguments passed to it. This is
8293
- // important because the callable return value may be called again with
8294
- // one of these signatures, so we may need to "uniquify" the type parameters
8295
- // to avoid conflicts.
8296
- function adjustCallableReturnType(callableType, returnType, liveTypeVarScopes, trackedSignatures) {
8376
+ function adjustCallableReturnType(callableType, returnType, liveTypeVarScopes) {
8297
8377
  if ((0, types_1.isFunction)(returnType) && !returnType.shared.name && callableType.shared.typeVarScopeId) {
8298
8378
  // What type variables are referenced in the callable return type? Do not include any live type variables.
8299
8379
  const typeVarsInReturnType = (0, typeUtils_1.getTypeVarArgumentsRecursive)(returnType).filter((t) => !liveTypeVarScopes.some((scopeId) => t.priv.scopeId === scopeId));
@@ -8301,7 +8381,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
8301
8381
  // unsolved type variables, treat them as though they are rescoped
8302
8382
  // to the callable.
8303
8383
  if (typeVarsInReturnType.length > 0) {
8304
- return types_1.FunctionType.cloneWithNewTypeVarScopeId(returnType, callableType.shared.typeVarScopeId, callableType.priv.constructorTypeVarScopeId, typeVarsInReturnType, trackedSignatures);
8384
+ return types_1.FunctionType.cloneWithNewTypeVarScopeId(returnType, callableType.shared.typeVarScopeId, callableType.priv.constructorTypeVarScopeId, typeVarsInReturnType);
8305
8385
  }
8306
8386
  }
8307
8387
  return returnType;
@@ -8309,12 +8389,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
8309
8389
  // Tries to assign the call arguments to the function parameter
8310
8390
  // list and reports any mismatches in types or counts. Returns the
8311
8391
  // specialized return type of the call.
8312
- function validateArgs(errorNode, argList, typeResult, typeVarContext, skipUnknownArgCheck = false, inferenceContext, signatureTracker) {
8313
- if (!signatureTracker) {
8314
- signatureTracker = new typeUtils_1.UniqueSignatureTracker();
8315
- }
8316
- typeResult.type = (0, typeUtils_1.ensureFunctionSignaturesAreUnique)(typeResult.type, signatureTracker, errorNode.start);
8317
- const matchResults = matchArgsToParams(errorNode, argList, typeResult, 0, signatureTracker);
8392
+ function validateArgs(errorNode, argList, typeResult, typeVarContext, skipUnknownArgCheck = false, inferenceContext) {
8393
+ const matchResults = matchArgsToParams(errorNode, argList, typeResult, 0);
8318
8394
  if (matchResults.argumentErrors) {
8319
8395
  // Evaluate types of all args. This will ensure that referenced symbols are
8320
8396
  // not reported as unaccessed.
@@ -8329,22 +8405,23 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
8329
8405
  overloadsUsedForCall: [],
8330
8406
  };
8331
8407
  }
8332
- return validateArgTypesWithContext(errorNode, matchResults, typeVarContext, skipUnknownArgCheck, (0, typeUtils_1.makeInferenceContext)(inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.expectedType, inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.isTypeIncomplete), signatureTracker);
8408
+ return validateArgTypesWithContext(errorNode, matchResults, typeVarContext, skipUnknownArgCheck, (0, typeUtils_1.makeInferenceContext)(inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.expectedType, inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.isTypeIncomplete, inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.returnTypeOverride));
8333
8409
  }
8334
8410
  // Determines whether the specified argument list satisfies the function
8335
8411
  // signature bound to the specified ParamSpec. Return value indicates success.
8336
- function validateArgTypesForParamSpec(errorNode, argList, paramSpec, destTypeVarContext, signatureTracker) {
8412
+ function validateArgTypesForParamSpec(errorNode, argList, paramSpec, destTypeVarContext) {
8337
8413
  const signatureContexts = destTypeVarContext.getSignatureContexts();
8338
8414
  // Handle the common case where there is only one signature context.
8339
8415
  if (signatureContexts.length === 1) {
8340
- return validateArgTypesForParamSpecSignature(errorNode, argList, paramSpec, signatureContexts[0], signatureTracker);
8416
+ return validateArgTypesForParamSpecSignature(errorNode, argList, paramSpec, signatureContexts[0]);
8341
8417
  }
8342
8418
  const filteredSignatureContexts = [];
8343
8419
  const typeVarContexts = [];
8420
+ const speculativeNode = getSpeculativeNodeForCall(errorNode);
8344
8421
  signatureContexts.forEach((context) => {
8345
8422
  // Use speculative mode to avoid emitting errors or caching types.
8346
- useSpeculativeMode(errorNode, () => {
8347
- const paramSpecArgResult = validateArgTypesForParamSpecSignature(errorNode, argList, paramSpec, context, signatureTracker);
8423
+ useSpeculativeMode(speculativeNode, () => {
8424
+ const paramSpecArgResult = validateArgTypesForParamSpecSignature(errorNode, argList, paramSpec, context);
8348
8425
  if (!paramSpecArgResult.argumentErrors) {
8349
8426
  filteredSignatureContexts.push(context);
8350
8427
  }
@@ -8356,16 +8433,16 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
8356
8433
  destTypeVarContext.copySignatureContexts(filteredSignatureContexts);
8357
8434
  }
8358
8435
  // Evaluate non-speculatively to produce a final result and cache types.
8359
- const paramSpecArgResult = validateArgTypesForParamSpecSignature(errorNode, argList, paramSpec, filteredSignatureContexts.length > 0 ? filteredSignatureContexts[0] : signatureContexts[0], signatureTracker);
8436
+ const paramSpecArgResult = validateArgTypesForParamSpecSignature(errorNode, argList, paramSpec, filteredSignatureContexts.length > 0 ? filteredSignatureContexts[0] : signatureContexts[0]);
8360
8437
  return { argumentErrors: paramSpecArgResult.argumentErrors, typeVarContexts };
8361
8438
  }
8362
- function validateArgTypesForParamSpecSignature(errorNode, argList, paramSpec, typeVarContext, signatureTracker) {
8439
+ function validateArgTypesForParamSpecSignature(errorNode, argList, paramSpec, typeVarContext) {
8363
8440
  var _a;
8364
8441
  let paramSpecType = typeVarContext.getParamSpecType(paramSpec);
8365
8442
  if (!paramSpecType) {
8366
8443
  paramSpecType = (0, typeUtils_1.convertTypeToParamSpecValue)(paramSpec);
8367
8444
  }
8368
- const matchResults = matchArgsToParams(errorNode, argList, { type: paramSpecType }, 0, signatureTracker);
8445
+ const matchResults = matchArgsToParams(errorNode, argList, { type: paramSpecType }, 0);
8369
8446
  const functionType = matchResults.overload;
8370
8447
  const srcTypeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeIds)(paramSpecType));
8371
8448
  if (matchResults.argumentErrors) {
@@ -8392,7 +8469,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
8392
8469
  let argumentErrors = false;
8393
8470
  let argErrorNode;
8394
8471
  for (const arg of argList) {
8395
- const argType = (_a = getTypeOfArgument(arg, /* inferenceContext */ undefined, signatureTracker)) === null || _a === void 0 ? void 0 : _a.type;
8472
+ const argType = (_a = getTypeOfArgument(arg, /* inferenceContext */ undefined)) === null || _a === void 0 ? void 0 : _a.type;
8396
8473
  if (arg.argumentCategory === 1 /* ArgumentCategory.UnpackedList */) {
8397
8474
  if ((0, parameterUtils_1.isParamSpecArgsArgument)(paramSpec, argType)) {
8398
8475
  argsCount++;
@@ -8418,11 +8495,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
8418
8495
  }
8419
8496
  return { argumentErrors, typeVarContexts: [srcTypeVarContext] };
8420
8497
  }
8421
- const result = validateArgTypes(errorNode, matchResults, srcTypeVarContext, signatureTracker,
8498
+ const result = validateArgTypes(errorNode, matchResults, srcTypeVarContext,
8422
8499
  /* skipUnknownArgCheck */ undefined);
8423
8500
  return { argumentErrors: !!result.argumentErrors, typeVarContexts: [srcTypeVarContext] };
8424
8501
  }
8425
- function validateArgType(argParam, typeVarContext, signatureTracker, typeResult, options) {
8502
+ function validateArgType(argParam, typeVarContext, typeResult, options) {
8426
8503
  var _a, _b;
8427
8504
  let argType;
8428
8505
  let expectedTypeDiag;
@@ -8477,12 +8554,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
8477
8554
  const flags = argParam.isinstanceParam
8478
8555
  ? 536871546 /* EvalFlags.IsInstanceArgDefaults */
8479
8556
  : 16 /* EvalFlags.NoFinal */ | 2 /* EvalFlags.NoSpecialize */;
8480
- const exprTypeResult = getTypeOfExpression(argParam.argument.valueExpression, flags, (0, typeUtils_1.makeInferenceContext)(expectedType, !!(typeResult === null || typeResult === void 0 ? void 0 : typeResult.isIncomplete)), signatureTracker);
8557
+ const exprTypeResult = getTypeOfExpression(argParam.argument.valueExpression, flags, (0, typeUtils_1.makeInferenceContext)(expectedType, !!(typeResult === null || typeResult === void 0 ? void 0 : typeResult.isIncomplete)));
8481
8558
  argType = exprTypeResult.type;
8482
- // If the type includes multiple instances of a generic function
8483
- // signature, force the type arguments for the duplicates to have
8484
- // unique names.
8485
- argType = (0, typeUtils_1.ensureFunctionSignaturesAreUnique)(argType, signatureTracker, argParam.argument.valueExpression.start);
8486
8559
  if (exprTypeResult.isIncomplete) {
8487
8560
  isTypeIncomplete = true;
8488
8561
  }
@@ -8512,8 +8585,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
8512
8585
  argType = argParam.argType;
8513
8586
  }
8514
8587
  else {
8515
- const argTypeResult = getTypeOfArgument(argParam.argument,
8516
- /* inferenceContext */ undefined, signatureTracker);
8588
+ const argTypeResult = getTypeOfArgument(argParam.argument, /* inferenceContext */ undefined);
8517
8589
  argType = argTypeResult.type;
8518
8590
  if (argTypeResult.isIncomplete) {
8519
8591
  isTypeIncomplete = true;
@@ -8602,7 +8674,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
8602
8674
  }
8603
8675
  let assignTypeFlags = skipSolveTypeVars ? 8 /* AssignTypeFlags.SkipSolveTypeVars */ : 0 /* AssignTypeFlags.Default */;
8604
8676
  if (argParam.isinstanceParam) {
8605
- assignTypeFlags |= 32768 /* AssignTypeFlags.AllowIsinstanceSpecialForms */;
8677
+ assignTypeFlags |= 16384 /* AssignTypeFlags.AllowIsinstanceSpecialForms */;
8606
8678
  }
8607
8679
  if (!assignType(argParam.paramType, argType, diag.createAddendum(), typeVarContext,
8608
8680
  /* srcTypeVarContext */ undefined, assignTypeFlags)) {
@@ -8704,7 +8776,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
8704
8776
  let firstConstraintArg;
8705
8777
  let defaultValueNode;
8706
8778
  if (argList.length === 0) {
8707
- addError(localize_1.LocMessage.typeVarFirstArg(), errorNode);
8779
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typeVarFirstArg(), errorNode);
8708
8780
  return undefined;
8709
8781
  }
8710
8782
  const firstArg = argList[0];
@@ -8712,7 +8784,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
8712
8784
  typeVarName = firstArg.valueExpression.d.strings.map((s) => s.d.value).join('');
8713
8785
  }
8714
8786
  else {
8715
- addError(localize_1.LocMessage.typeVarFirstArg(), firstArg.valueExpression || errorNode);
8787
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typeVarFirstArg(), firstArg.valueExpression || errorNode);
8716
8788
  }
8717
8789
  const typeVar = types_1.TypeBase.cloneAsSpecialForm(types_1.TypeVarType.createInstantiable(typeVarName, /* isParamSpec */ false), types_1.ClassType.cloneAsInstance(classType));
8718
8790
  // Parse the remaining parameters.
@@ -8722,18 +8794,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
8722
8794
  const paramName = paramNameNode ? paramNameNode.d.value : undefined;
8723
8795
  if (paramName) {
8724
8796
  if (paramNameMap.get(paramName)) {
8725
- addError(localize_1.LocMessage.duplicateParam().format({ name: paramName }), argList[i].valueExpression || errorNode);
8797
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportCallIssue, localize_1.LocMessage.duplicateParam().format({ name: paramName }), argList[i].valueExpression || errorNode);
8726
8798
  }
8727
8799
  if (paramName === 'bound') {
8728
8800
  if (typeVar.shared.constraints.length > 0) {
8729
- addError(localize_1.LocMessage.typeVarBoundAndConstrained(), argList[i].valueExpression || errorNode);
8801
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typeVarBoundAndConstrained(), argList[i].valueExpression || errorNode);
8730
8802
  }
8731
8803
  else {
8732
8804
  const argType = (_b = (_a = argList[i].typeResult) === null || _a === void 0 ? void 0 : _a.type) !== null && _b !== void 0 ? _b : getTypeOfExpressionExpectingType(argList[i].valueExpression, {
8733
8805
  disallowProtocolAndTypedDict: true,
8734
8806
  }).type;
8735
8807
  if ((0, typeUtils_1.requiresSpecialization)(argType, { ignorePseudoGeneric: true, ignoreImplicitTypeArgs: true })) {
8736
- addError(localize_1.LocMessage.typeVarBoundGeneric(), argList[i].valueExpression || errorNode);
8808
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typeVarBoundGeneric(), argList[i].valueExpression || errorNode);
8737
8809
  }
8738
8810
  typeVar.shared.boundType = (0, typeUtils_1.convertToInstance)(argType);
8739
8811
  }
@@ -8742,7 +8814,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
8742
8814
  if (argList[i].valueExpression && getBooleanValue(argList[i].valueExpression)) {
8743
8815
  if (typeVar.shared.declaredVariance === 4 /* Variance.Contravariant */ ||
8744
8816
  typeVar.shared.declaredVariance === 0 /* Variance.Auto */) {
8745
- addError(localize_1.LocMessage.typeVarVariance(), argList[i].valueExpression);
8817
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typeVarVariance(), argList[i].valueExpression);
8746
8818
  }
8747
8819
  else {
8748
8820
  typeVar.shared.declaredVariance = 3 /* Variance.Covariant */;
@@ -8753,7 +8825,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
8753
8825
  if (argList[i].valueExpression && getBooleanValue(argList[i].valueExpression)) {
8754
8826
  if (typeVar.shared.declaredVariance === 3 /* Variance.Covariant */ ||
8755
8827
  typeVar.shared.declaredVariance === 0 /* Variance.Auto */) {
8756
- addError(localize_1.LocMessage.typeVarVariance(), argList[i].valueExpression);
8828
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typeVarVariance(), argList[i].valueExpression);
8757
8829
  }
8758
8830
  else {
8759
8831
  typeVar.shared.declaredVariance = 4 /* Variance.Contravariant */;
@@ -8764,7 +8836,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
8764
8836
  if (argList[i].valueExpression && getBooleanValue(argList[i].valueExpression)) {
8765
8837
  if (typeVar.shared.declaredVariance === 3 /* Variance.Covariant */ ||
8766
8838
  typeVar.shared.declaredVariance === 4 /* Variance.Contravariant */) {
8767
- addError(localize_1.LocMessage.typeVarVariance(), argList[i].valueExpression);
8839
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typeVarVariance(), argList[i].valueExpression);
8768
8840
  }
8769
8841
  else {
8770
8842
  typeVar.shared.declaredVariance = 0 /* Variance.Auto */;
@@ -8782,22 +8854,22 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
8782
8854
  if (!fileInfo.isStubFile &&
8783
8855
  fileInfo.executionEnvironment.pythonVersion.isLessThan(pythonVersion_1.pythonVersion3_13) &&
8784
8856
  classType.shared.moduleName !== 'typing_extensions') {
8785
- addError(localize_1.LocMessage.typeVarDefaultIllegal(), defaultValueNode);
8857
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typeVarDefaultIllegal(), defaultValueNode);
8786
8858
  }
8787
8859
  }
8788
8860
  else {
8789
- addError(localize_1.LocMessage.typeVarUnknownParam().format({ name: paramName }), ((_e = argList[i].node) === null || _e === void 0 ? void 0 : _e.d.name) || argList[i].valueExpression || errorNode);
8861
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportCallIssue, localize_1.LocMessage.typeVarUnknownParam().format({ name: paramName }), ((_e = argList[i].node) === null || _e === void 0 ? void 0 : _e.d.name) || argList[i].valueExpression || errorNode);
8790
8862
  }
8791
8863
  paramNameMap.set(paramName, paramName);
8792
8864
  }
8793
8865
  else {
8794
8866
  if (typeVar.shared.boundType) {
8795
- addError(localize_1.LocMessage.typeVarBoundAndConstrained(), argList[i].valueExpression || errorNode);
8867
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typeVarBoundAndConstrained(), argList[i].valueExpression || errorNode);
8796
8868
  }
8797
8869
  else {
8798
8870
  const argType = (_g = (_f = argList[i].typeResult) === null || _f === void 0 ? void 0 : _f.type) !== null && _g !== void 0 ? _g : getTypeOfExpressionExpectingType(argList[i].valueExpression).type;
8799
8871
  if ((0, typeUtils_1.requiresSpecialization)(argType, { ignorePseudoGeneric: true })) {
8800
- addError(localize_1.LocMessage.typeVarConstraintGeneric(), argList[i].valueExpression || errorNode);
8872
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typeVarConstraintGeneric(), argList[i].valueExpression || errorNode);
8801
8873
  }
8802
8874
  types_1.TypeVarType.addConstraint(typeVar, (0, typeUtils_1.convertToInstance)(argType));
8803
8875
  if (firstConstraintArg === undefined) {
@@ -8852,7 +8924,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
8852
8924
  var _a, _b;
8853
8925
  let typeVarName = '';
8854
8926
  if (argList.length === 0) {
8855
- addError(localize_1.LocMessage.typeVarFirstArg(), errorNode);
8927
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportCallIssue, localize_1.LocMessage.typeVarFirstArg(), errorNode);
8856
8928
  return undefined;
8857
8929
  }
8858
8930
  const firstArg = argList[0];
@@ -8860,7 +8932,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
8860
8932
  typeVarName = firstArg.valueExpression.d.strings.map((s) => s.d.value).join('');
8861
8933
  }
8862
8934
  else {
8863
- addError(localize_1.LocMessage.typeVarFirstArg(), firstArg.valueExpression || errorNode);
8935
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typeVarFirstArg(), firstArg.valueExpression || errorNode);
8864
8936
  }
8865
8937
  const typeVar = types_1.TypeBase.cloneAsSpecialForm(types_1.TypeVarType.createInstantiable(typeVarName, /* isParamSpec */ false), types_1.ClassType.cloneAsInstance(classType));
8866
8938
  typeVar.shared.isVariadic = true;
@@ -8883,15 +8955,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
8883
8955
  if (!fileInfo.isStubFile &&
8884
8956
  fileInfo.executionEnvironment.pythonVersion.isLessThan(pythonVersion_1.pythonVersion3_13) &&
8885
8957
  classType.shared.moduleName !== 'typing_extensions') {
8886
- addError(localize_1.LocMessage.typeVarDefaultIllegal(), expr);
8958
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typeVarDefaultIllegal(), expr);
8887
8959
  }
8888
8960
  }
8889
8961
  else {
8890
- addError(localize_1.LocMessage.typeVarTupleUnknownParam().format({ name: ((_a = argList[i].name) === null || _a === void 0 ? void 0 : _a.d.value) || '?' }), ((_b = argList[i].node) === null || _b === void 0 ? void 0 : _b.d.name) || argList[i].valueExpression || errorNode);
8962
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typeVarTupleUnknownParam().format({ name: ((_a = argList[i].name) === null || _a === void 0 ? void 0 : _a.d.value) || '?' }), ((_b = argList[i].node) === null || _b === void 0 ? void 0 : _b.d.name) || argList[i].valueExpression || errorNode);
8891
8963
  }
8892
8964
  }
8893
8965
  else {
8894
- addError(localize_1.LocMessage.typeVarTupleConstraints(), argList[i].valueExpression || errorNode);
8966
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typeVarTupleConstraints(), argList[i].valueExpression || errorNode);
8895
8967
  }
8896
8968
  }
8897
8969
  return typeVar;
@@ -8912,7 +8984,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
8912
8984
  }
8913
8985
  function createParamSpecType(errorNode, classType, argList) {
8914
8986
  if (argList.length === 0) {
8915
- addError(localize_1.LocMessage.paramSpecFirstArg(), errorNode);
8987
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportCallIssue, localize_1.LocMessage.paramSpecFirstArg(), errorNode);
8916
8988
  return undefined;
8917
8989
  }
8918
8990
  const firstArg = argList[0];
@@ -8921,7 +8993,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
8921
8993
  paramSpecName = firstArg.valueExpression.d.strings.map((s) => s.d.value).join('');
8922
8994
  }
8923
8995
  else {
8924
- addError(localize_1.LocMessage.paramSpecFirstArg(), firstArg.valueExpression || errorNode);
8996
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.paramSpecFirstArg(), firstArg.valueExpression || errorNode);
8925
8997
  }
8926
8998
  const paramSpec = types_1.TypeBase.cloneAsSpecialForm(types_1.TypeVarType.createInstantiable(paramSpecName, /* isParamSpec */ true), types_1.ClassType.cloneAsInstance(classType));
8927
8999
  paramSpec.shared.defaultType = (0, typeUtils_1.getUnknownTypeForParamSpec)();
@@ -8943,15 +9015,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
8943
9015
  if (!fileInfo.isStubFile &&
8944
9016
  fileInfo.executionEnvironment.pythonVersion.isLessThan(pythonVersion_1.pythonVersion3_13) &&
8945
9017
  classType.shared.moduleName !== 'typing_extensions') {
8946
- addError(localize_1.LocMessage.typeVarDefaultIllegal(), expr);
9018
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typeVarDefaultIllegal(), expr);
8947
9019
  }
8948
9020
  }
8949
9021
  else {
8950
- addError(localize_1.LocMessage.paramSpecUnknownParam().format({ name: paramName }), paramNameNode || argList[i].valueExpression || errorNode);
9022
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.paramSpecUnknownParam().format({ name: paramName }), paramNameNode || argList[i].valueExpression || errorNode);
8951
9023
  }
8952
9024
  }
8953
9025
  else {
8954
- addError(localize_1.LocMessage.paramSpecUnknownArg(), argList[i].valueExpression || errorNode);
9026
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportCallIssue, localize_1.LocMessage.paramSpecUnknownArg(), argList[i].valueExpression || errorNode);
8955
9027
  break;
8956
9028
  }
8957
9029
  }
@@ -9058,7 +9130,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
9058
9130
  let typeParameters;
9059
9131
  if (typeParamsExpr) {
9060
9132
  if (typeParamsExpr.nodeType !== 52 /* ParseNodeType.Tuple */) {
9061
- addError(localize_1.LocMessage.typeAliasTypeParamInvalid(), typeParamsExpr);
9133
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typeAliasTypeParamInvalid(), typeParamsExpr);
9062
9134
  return undefined;
9063
9135
  }
9064
9136
  typeParameters = [];
@@ -9187,16 +9259,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
9187
9259
  // Implements the semantics of the multi-parameter variant of the "type" call.
9188
9260
  function createClassFromMetaclass(errorNode, argList, metaclass) {
9189
9261
  const fileInfo = AnalyzerNodeInfo.getFileInfo(errorNode);
9190
- const arg0Type = getTypeOfArgument(argList[0],
9191
- /* inferenceContext */ undefined,
9192
- /* signatureTracker */ undefined).type;
9262
+ const arg0Type = getTypeOfArgument(argList[0], /* inferenceContext */ undefined).type;
9193
9263
  if (!(0, types_1.isClassInstance)(arg0Type) || !types_1.ClassType.isBuiltIn(arg0Type, 'str')) {
9194
9264
  return undefined;
9195
9265
  }
9196
9266
  const className = arg0Type.priv.literalValue || '_';
9197
- const arg1Type = getTypeOfArgument(argList[1],
9198
- /* inferenceContext */ undefined,
9199
- /* signatureTracker */ undefined).type;
9267
+ const arg1Type = getTypeOfArgument(argList[1], /* inferenceContext */ undefined).type;
9200
9268
  // TODO - properly handle case where tuple of base classes is provided.
9201
9269
  if (!(0, types_1.isClassInstance)(arg1Type) || !(0, typeUtils_1.isTupleClass)(arg1Type) || arg1Type.priv.tupleTypeArguments === undefined) {
9202
9270
  return undefined;
@@ -9212,7 +9280,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
9212
9280
  }
9213
9281
  });
9214
9282
  if (!(0, typeUtils_1.computeMroLinearization)(classType)) {
9215
- addError(localize_1.LocMessage.methodOrdering(), errorNode);
9283
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.methodOrdering(), errorNode);
9216
9284
  }
9217
9285
  return classType;
9218
9286
  }
@@ -9261,8 +9329,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
9261
9329
  (0, debug_1.assert)(magicMethodType !== undefined);
9262
9330
  return validateCallArguments(errorNode, functionArgs, { type: magicMethodType },
9263
9331
  /* typeVarContext */ undefined,
9264
- /* skipUnknownArgCheck */ true, inferenceContext,
9265
- /* signatureTracker */ undefined);
9332
+ /* skipUnknownArgCheck */ true, inferenceContext);
9266
9333
  });
9267
9334
  // If there were errors with the expected type, try
9268
9335
  // to evaluate without the expected type.
@@ -9272,8 +9339,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
9272
9339
  return validateCallArguments(errorNode, functionArgs, { type: magicMethodType },
9273
9340
  /* typeVarContext */ undefined,
9274
9341
  /* skipUnknownArgCheck */ true,
9275
- /* inferenceContext */ undefined,
9276
- /* signatureTracker */ undefined);
9342
+ /* inferenceContext */ undefined);
9277
9343
  });
9278
9344
  }
9279
9345
  if (callResult.argumentErrors) {
@@ -9921,7 +9987,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
9921
9987
  expectedYieldType = (0, typeUtils_1.getGeneratorYieldType)(returnType, !!enclosingFunction.d.isAsync);
9922
9988
  const generatorTypeArgs = (0, typeUtils_1.getGeneratorTypeArgs)(returnType);
9923
9989
  if (generatorTypeArgs && generatorTypeArgs.length >= 2) {
9924
- sentType = generatorTypeArgs[1];
9990
+ const liveScopeIds = ParseTreeUtils.getTypeVarScopesForNode(node);
9991
+ sentType = (0, typeUtils_1.updateTypeWithInternalTypeVars)(generatorTypeArgs[1], liveScopeIds);
9925
9992
  }
9926
9993
  }
9927
9994
  }
@@ -10289,7 +10356,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10289
10356
  function validateTypeArg(argResult, options) {
10290
10357
  if (argResult.typeList) {
10291
10358
  if (!(options === null || options === void 0 ? void 0 : options.allowTypeArgList)) {
10292
- addError(localize_1.LocMessage.typeArgListNotAllowed(), argResult.node);
10359
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgListNotAllowed(), argResult.node);
10293
10360
  return false;
10294
10361
  }
10295
10362
  else {
@@ -10300,23 +10367,23 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10300
10367
  }
10301
10368
  if ((0, typeUtils_1.isEllipsisType)(argResult.type)) {
10302
10369
  if (!(options === null || options === void 0 ? void 0 : options.allowTypeArgList)) {
10303
- addError(localize_1.LocMessage.ellipsisContext(), argResult.node);
10370
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.ellipsisContext(), argResult.node);
10304
10371
  return false;
10305
10372
  }
10306
10373
  }
10307
10374
  if ((0, types_1.isModule)(argResult.type)) {
10308
- addError(localize_1.LocMessage.moduleAsType(), argResult.node);
10375
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.moduleAsType(), argResult.node);
10309
10376
  return false;
10310
10377
  }
10311
10378
  if ((0, types_1.isParamSpec)(argResult.type)) {
10312
10379
  if (!(options === null || options === void 0 ? void 0 : options.allowParamSpec)) {
10313
- addError(localize_1.LocMessage.paramSpecContext(), argResult.node);
10380
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.paramSpecContext(), argResult.node);
10314
10381
  return false;
10315
10382
  }
10316
10383
  }
10317
10384
  if ((0, types_1.isVariadicTypeVar)(argResult.type) && !argResult.type.priv.isVariadicInUnion) {
10318
10385
  if (!(options === null || options === void 0 ? void 0 : options.allowVariadicTypeVar)) {
10319
- addError(localize_1.LocMessage.typeVarTupleContext(), argResult.node);
10386
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeVarTupleContext(), argResult.node);
10320
10387
  return false;
10321
10388
  }
10322
10389
  else {
@@ -10324,12 +10391,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10324
10391
  }
10325
10392
  }
10326
10393
  if (!(options === null || options === void 0 ? void 0 : options.allowEmptyTuple) && argResult.isEmptyTupleShorthand) {
10327
- addError(localize_1.LocMessage.zeroLengthTupleNotAllowed(), argResult.node);
10394
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.zeroLengthTupleNotAllowed(), argResult.node);
10328
10395
  return false;
10329
10396
  }
10330
10397
  if ((0, types_1.isUnpackedClass)(argResult.type)) {
10331
10398
  if (!(options === null || options === void 0 ? void 0 : options.allowUnpackedTuples)) {
10332
- addError(localize_1.LocMessage.unpackedArgInTypeArgument(), argResult.node);
10399
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.unpackedArgInTypeArgument(), argResult.node);
10333
10400
  return false;
10334
10401
  }
10335
10402
  }
@@ -10355,7 +10422,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10355
10422
  // Make sure we have at most one unpacked variadic type variable.
10356
10423
  if (sawUnpacked) {
10357
10424
  if (!reportedUnpackedError) {
10358
- addError(localize_1.LocMessage.variadicTypeArgsTooMany(), entry.node);
10425
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.variadicTypeArgsTooMany(), entry.node);
10359
10426
  reportedUnpackedError = true;
10360
10427
  }
10361
10428
  }
@@ -10417,7 +10484,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10417
10484
  }
10418
10485
  }
10419
10486
  else {
10420
- addError(localize_1.LocMessage.callableFirstArg(), typeArgs[0].node);
10487
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.callableFirstArg(), typeArgs[0].node);
10421
10488
  }
10422
10489
  }
10423
10490
  if (typeArgs.length > 1) {
@@ -10432,7 +10499,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10432
10499
  functionType.shared.declaredReturnType = types_1.UnknownType.create();
10433
10500
  }
10434
10501
  if (typeArgs.length > 2) {
10435
- addError(localize_1.LocMessage.callableExtraArgs(), typeArgs[2].node);
10502
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.callableExtraArgs(), typeArgs[2].node);
10436
10503
  }
10437
10504
  }
10438
10505
  else {
@@ -10451,13 +10518,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10451
10518
  // depends on whether we're evaluating a type annotation or
10452
10519
  // we're in some other context.
10453
10520
  if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0) {
10454
- addError(localize_1.LocMessage.optionalExtraArgs(), errorNode);
10521
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.optionalExtraArgs(), errorNode);
10455
10522
  return types_1.UnknownType.create();
10456
10523
  }
10457
10524
  return classType;
10458
10525
  }
10459
10526
  if (typeArgs.length > 1) {
10460
- addError(localize_1.LocMessage.optionalExtraArgs(), errorNode);
10527
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.optionalExtraArgs(), errorNode);
10461
10528
  return types_1.UnknownType.create();
10462
10529
  }
10463
10530
  let typeArg0Type = typeArgs[0].type;
@@ -10489,7 +10556,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10489
10556
  // Creates a type that represents a Literal.
10490
10557
  function createLiteralType(classType, node, flags) {
10491
10558
  if (node.d.items.length === 0) {
10492
- addError(localize_1.LocMessage.literalEmptyArgs(), node.d.leftExpr);
10559
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.literalEmptyArgs(), node.d.leftExpr);
10493
10560
  return types_1.UnknownType.create();
10494
10561
  }
10495
10562
  // As per the specification, we support None, int, bool, str, bytes literals
@@ -10499,11 +10566,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10499
10566
  let type;
10500
10567
  const itemExpr = item.d.valueExpr;
10501
10568
  if (item.d.argCategory !== 0 /* ArgumentCategory.Simple */) {
10502
- addError(localize_1.LocMessage.unpackedArgInTypeArgument(), itemExpr);
10569
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.unpackedArgInTypeArgument(), itemExpr);
10503
10570
  type = types_1.UnknownType.create();
10504
10571
  }
10505
10572
  else if (item.d.name) {
10506
- addError(localize_1.LocMessage.keywordArgInTypeArgument(), itemExpr);
10573
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.keywordArgInTypeArgument(), itemExpr);
10507
10574
  type = types_1.UnknownType.create();
10508
10575
  }
10509
10576
  else if (itemExpr.nodeType === 48 /* ParseNodeType.StringList */) {
@@ -10572,7 +10639,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10572
10639
  }
10573
10640
  }
10574
10641
  if (!type) {
10575
- addError(localize_1.LocMessage.literalUnsupportedType(), item);
10642
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.literalUnsupportedType(), item);
10576
10643
  type = types_1.UnknownType.create();
10577
10644
  }
10578
10645
  literalTypes.push(type);
@@ -10587,18 +10654,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10587
10654
  function createClassVarType(classType, errorNode, typeArgs, flags) {
10588
10655
  var _a;
10589
10656
  if (flags & 131072 /* EvalFlags.NoClassVar */) {
10590
- addError(localize_1.LocMessage.classVarNotAllowed(), errorNode);
10657
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.classVarNotAllowed(), errorNode);
10591
10658
  return types_1.AnyType.create();
10592
10659
  }
10593
10660
  if (!typeArgs) {
10594
10661
  return classType;
10595
10662
  }
10596
10663
  else if (typeArgs.length === 0) {
10597
- addError(localize_1.LocMessage.classVarFirstArgMissing(), errorNode);
10664
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.classVarFirstArgMissing(), errorNode);
10598
10665
  return types_1.UnknownType.create();
10599
10666
  }
10600
10667
  else if (typeArgs.length > 1) {
10601
- addError(localize_1.LocMessage.classVarTooManyArgs(), typeArgs[1].node);
10668
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.classVarTooManyArgs(), typeArgs[1].node);
10602
10669
  return types_1.UnknownType.create();
10603
10670
  }
10604
10671
  const type = typeArgs[0].type;
@@ -10619,12 +10686,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10619
10686
  // we're in some other context.
10620
10687
  if (!typeArgs) {
10621
10688
  if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0) {
10622
- addError(localize_1.LocMessage.typeGuardArgCount(), errorNode);
10689
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeGuardArgCount(), errorNode);
10623
10690
  }
10624
10691
  return classType;
10625
10692
  }
10626
10693
  else if (typeArgs.length !== 1) {
10627
- addError(localize_1.LocMessage.typeGuardArgCount(), errorNode);
10694
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeGuardArgCount(), errorNode);
10628
10695
  return types_1.UnknownType.create();
10629
10696
  }
10630
10697
  const convertedTypeArgs = typeArgs.map((typeArg) => {
@@ -10684,7 +10751,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10684
10751
  }
10685
10752
  }
10686
10753
  }
10687
- return (0, typeUtils_1.synthesizeTypeVarForSelfCls)(enclosingClassTypeResult.classType, /* isClsParam */ true);
10754
+ let result = (0, typeUtils_1.synthesizeTypeVarForSelfCls)(enclosingClassTypeResult.classType, /* isClsParam */ true);
10755
+ if (enclosingClass) {
10756
+ // If "Self" is used as a type expression within a function suite, it needs
10757
+ // to be marked as "internal".
10758
+ const enclosingSuite = ParseTreeUtils.getEnclosingClassOrFunctionSuite(errorNode);
10759
+ if (enclosingSuite && ParseTreeUtils.isNodeContainedWithin(enclosingSuite, enclosingClass)) {
10760
+ if (enclosingClass.d.suite !== enclosingSuite) {
10761
+ result = types_1.TypeVarType.cloneWithInternalScopeId(result);
10762
+ }
10763
+ }
10764
+ }
10765
+ return result;
10688
10766
  }
10689
10767
  function createRequiredOrReadOnlyType(classType, errorNode, typeArgs, flags) {
10690
10768
  // If no type arguments are provided, the resulting type
@@ -10694,7 +10772,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10694
10772
  return { type: classType };
10695
10773
  }
10696
10774
  if (!typeArgs || typeArgs.length !== 1) {
10697
- addError(classType.shared.name === 'ReadOnly'
10775
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, classType.shared.name === 'ReadOnly'
10698
10776
  ? localize_1.LocMessage.readOnlyArgCount()
10699
10777
  : classType.shared.name === 'Required'
10700
10778
  ? localize_1.LocMessage.requiredArgCount()
@@ -10736,7 +10814,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10736
10814
  isNotRequired = classType.shared.name === 'NotRequired';
10737
10815
  }
10738
10816
  if (!isUsageLegal) {
10739
- addError(classType.shared.name === 'ReadOnly'
10817
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, classType.shared.name === 'ReadOnly'
10740
10818
  ? localize_1.LocMessage.readOnlyNotInTypedDict()
10741
10819
  : classType.shared.name === 'Required'
10742
10820
  ? localize_1.LocMessage.requiredNotInTypedDict()
@@ -10753,7 +10831,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10753
10831
  return classType;
10754
10832
  }
10755
10833
  if (!typeArgs || typeArgs.length !== 1) {
10756
- addError(localize_1.LocMessage.unpackArgCount(), errorNode);
10834
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.unpackArgCount(), errorNode);
10757
10835
  return types_1.UnknownType.create();
10758
10836
  }
10759
10837
  let typeArgType = typeArgs[0].type;
@@ -10783,41 +10861,41 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10783
10861
  // Creates a "Final" type.
10784
10862
  function createFinalType(classType, errorNode, typeArgs, flags) {
10785
10863
  if (flags & 16 /* EvalFlags.NoFinal */) {
10786
- addError(localize_1.LocMessage.finalContext(), errorNode);
10864
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.finalContext(), errorNode);
10787
10865
  return classType;
10788
10866
  }
10789
10867
  if (!typeArgs || typeArgs.length === 0) {
10790
10868
  return classType;
10791
10869
  }
10792
10870
  if (typeArgs.length > 1) {
10793
- addError(localize_1.LocMessage.finalTooManyArgs(), errorNode);
10871
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.finalTooManyArgs(), errorNode);
10794
10872
  }
10795
10873
  return types_1.TypeBase.cloneAsSpecialForm(typeArgs[0].type, classType);
10796
10874
  }
10797
10875
  function createConcatenateType(classType, errorNode, typeArgs, flags) {
10798
10876
  if ((flags & 134217728 /* EvalFlags.AllowConcatenate */) === 0) {
10799
- addError(localize_1.LocMessage.concatenateContext(), errorNode);
10877
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.concatenateContext(), errorNode);
10800
10878
  return types_1.AnyType.create();
10801
10879
  }
10802
10880
  if (!typeArgs || typeArgs.length === 0) {
10803
- addError(localize_1.LocMessage.concatenateTypeArgsMissing(), errorNode);
10881
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.concatenateTypeArgsMissing(), errorNode);
10804
10882
  }
10805
10883
  else {
10806
10884
  typeArgs.forEach((typeArg, index) => {
10807
10885
  if (index === typeArgs.length - 1) {
10808
10886
  if (!(0, types_1.isParamSpec)(typeArg.type) && !(0, typeUtils_1.isEllipsisType)(typeArg.type)) {
10809
- addError(localize_1.LocMessage.concatenateParamSpecMissing(), typeArg.node);
10887
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.concatenateParamSpecMissing(), typeArg.node);
10810
10888
  }
10811
10889
  }
10812
10890
  else {
10813
10891
  if ((0, types_1.isParamSpec)(typeArg.type)) {
10814
- addError(localize_1.LocMessage.paramSpecContext(), typeArg.node);
10892
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.paramSpecContext(), typeArg.node);
10815
10893
  }
10816
10894
  else if ((0, types_1.isUnpackedVariadicTypeVar)(typeArg.type)) {
10817
- addError(localize_1.LocMessage.typeVarTupleContext(), typeArg.node);
10895
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeVarTupleContext(), typeArg.node);
10818
10896
  }
10819
10897
  else if ((0, types_1.isUnpackedClass)(typeArg.type)) {
10820
- addError(localize_1.LocMessage.unpackedArgInTypeArgument(), typeArg.node);
10898
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.unpackedArgInTypeArgument(), typeArg.node);
10821
10899
  }
10822
10900
  }
10823
10901
  });
@@ -10831,7 +10909,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10831
10909
  }
10832
10910
  if (typeArgs) {
10833
10911
  if (typeArgs.length < 2) {
10834
- addError(localize_1.LocMessage.annotatedTypeArgMissing(), errorNode);
10912
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.annotatedTypeArgMissing(), errorNode);
10835
10913
  }
10836
10914
  else {
10837
10915
  validateAnnotatedMetadata(errorNode, typeArgs[0].type, typeArgs.slice(1));
@@ -10841,7 +10919,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10841
10919
  return { type: types_1.AnyType.create() };
10842
10920
  }
10843
10921
  if (typeArgs[0].typeList) {
10844
- addError(localize_1.LocMessage.typeArgListNotAllowed(), typeArgs[0].node);
10922
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgListNotAllowed(), typeArgs[0].node);
10845
10923
  }
10846
10924
  return {
10847
10925
  type: types_1.TypeBase.cloneAsSpecialForm(typeArgs[0].type, classType),
@@ -10873,8 +10951,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10873
10951
  ], { type: supportsTypeMethod },
10874
10952
  /* typeVarContext */ undefined,
10875
10953
  /* skipUnknownArgCheck */ true,
10876
- /* inferenceContext */ undefined,
10877
- /* signatureTracker */ undefined));
10954
+ /* inferenceContext */ undefined));
10878
10955
  if (callResult.isTypeIncomplete || !callResult.returnType) {
10879
10956
  continue;
10880
10957
  }
@@ -10904,7 +10981,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10904
10981
  const noteSawUnpacked = (typeArg) => {
10905
10982
  if (sawUnpacked) {
10906
10983
  if (!reportedUnpackedError) {
10907
- addError(localize_1.LocMessage.variadicTypeArgsTooMany(), typeArg.node);
10984
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.variadicTypeArgsTooMany(), typeArg.node);
10908
10985
  reportedUnpackedError = true;
10909
10986
  }
10910
10987
  }
@@ -10918,18 +10995,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10918
10995
  if ((0, typeUtils_1.isEllipsisType)(typeArg.type)) {
10919
10996
  if (!isTupleTypeParam) {
10920
10997
  if (!allowParamSpec) {
10921
- addError(localize_1.LocMessage.ellipsisContext(), typeArg.node);
10998
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.ellipsisContext(), typeArg.node);
10922
10999
  }
10923
11000
  }
10924
11001
  else if (typeArgs.length !== 2 || index !== 1) {
10925
- addError(localize_1.LocMessage.ellipsisSecondArg(), typeArg.node);
11002
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.ellipsisSecondArg(), typeArg.node);
10926
11003
  }
10927
11004
  else {
10928
11005
  if ((0, types_1.isVariadicTypeVar)(typeArgs[0].type) && !typeArgs[0].type.priv.isVariadicInUnion) {
10929
- addError(localize_1.LocMessage.typeVarTupleContext(), typeArgs[0].node);
11006
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeVarTupleContext(), typeArgs[0].node);
10930
11007
  }
10931
11008
  else if ((0, types_1.isUnpackedClass)(typeArgs[0].type)) {
10932
- addError(localize_1.LocMessage.ellipsisAfterUnpacked(), typeArg.node);
11009
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.ellipsisAfterUnpacked(), typeArg.node);
10933
11010
  }
10934
11011
  }
10935
11012
  }
@@ -10958,7 +11035,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10958
11035
  // Make sure the argument list count is correct.
10959
11036
  if (paramLimit !== undefined) {
10960
11037
  if (typeArgs && typeArgTypes.length > paramLimit) {
10961
- addError(localize_1.LocMessage.typeArgsTooMany().format({
11038
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgsTooMany().format({
10962
11039
  name: classType.priv.aliasName || classType.shared.name,
10963
11040
  expected: paramLimit,
10964
11041
  received: typeArgTypes.length,
@@ -11015,7 +11092,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
11015
11092
  // depends on whether we're evaluating a type annotation or
11016
11093
  // we're in some other context.
11017
11094
  if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0) {
11018
- addError(localize_1.LocMessage.unionTypeArgCount(), errorNode);
11095
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.unionTypeArgCount(), errorNode);
11019
11096
  return types_1.NeverType.createNever();
11020
11097
  }
11021
11098
  return classType;
@@ -11079,7 +11156,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
11079
11156
  // depends on whether we're evaluating a type annotation or
11080
11157
  // we're in some other context.
11081
11158
  if ((flags & (256 /* EvalFlags.TypeExpression */ | 262144 /* EvalFlags.NoNakedGeneric */)) !== 0) {
11082
- addError(localize_1.LocMessage.genericTypeArgMissing(), errorNode);
11159
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.genericTypeArgMissing(), errorNode);
11083
11160
  }
11084
11161
  return classType;
11085
11162
  }
@@ -11087,16 +11164,16 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
11087
11164
  if (typeArgs) {
11088
11165
  // Make sure there's at least one type arg.
11089
11166
  if (typeArgs.length === 0) {
11090
- addError(localize_1.LocMessage.genericTypeArgMissing(), errorNode);
11167
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.genericTypeArgMissing(), errorNode);
11091
11168
  }
11092
11169
  // Make sure that all of the type args are typeVars and are unique.
11093
11170
  typeArgs.forEach((typeArg) => {
11094
11171
  if (!(0, types_1.isTypeVar)(typeArg.type)) {
11095
- addError(localize_1.LocMessage.genericTypeArgTypeVar(), typeArg.node);
11172
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.genericTypeArgTypeVar(), typeArg.node);
11096
11173
  }
11097
11174
  else {
11098
11175
  if (uniqueTypeVars.some((t) => (0, types_1.isTypeSame)(t, typeArg.type))) {
11099
- addError(localize_1.LocMessage.genericTypeArgUnique(), typeArg.node);
11176
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.genericTypeArgUnique(), typeArg.node);
11100
11177
  }
11101
11178
  uniqueTypeVars.push(typeArg.type);
11102
11179
  }
@@ -11154,14 +11231,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
11154
11231
  // Verify that we have at most one variadic type variable.
11155
11232
  const variadics = typeParameters.filter((param) => (0, types_1.isVariadicTypeVar)(param));
11156
11233
  if (variadics.length > 1) {
11157
- addError(localize_1.LocMessage.variadicTypeParamTooManyAlias().format({
11234
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.variadicTypeParamTooManyAlias().format({
11158
11235
  names: variadics.map((v) => `"${v.shared.name}"`).join(', '),
11159
11236
  }), errorNode);
11160
11237
  }
11161
11238
  if (!isPep695Syntax && !isPep695TypeVarType) {
11162
11239
  const boundTypeVars = typeParameters.filter((typeVar) => typeVar.priv.scopeId !== typeAliasScopeId && typeVar.priv.scopeType === 0 /* TypeVarScopeType.Class */);
11163
11240
  if (boundTypeVars.length > 0) {
11164
- addError(localize_1.LocMessage.genericTypeAliasBoundTypeVar().format({
11241
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.genericTypeAliasBoundTypeVar().format({
11165
11242
  names: boundTypeVars.map((t) => `${t.shared.name}`).join(', '),
11166
11243
  }), errorNode);
11167
11244
  }
@@ -11696,7 +11773,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
11696
11773
  if (!fileInfo.isStubFile &&
11697
11774
  !types_1.ClassType.isTypingExtensionClass(argType) &&
11698
11775
  fileInfo.executionEnvironment.pythonVersion.isLessThan(pythonVersion_1.pythonVersion3_7)) {
11699
- addError(localize_1.LocMessage.protocolIllegal(), arg.d.valueExpr);
11776
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.protocolIllegal(), arg.d.valueExpr);
11700
11777
  }
11701
11778
  classType.shared.flags |= 512 /* ClassTypeFlags.ProtocolClass */;
11702
11779
  }
@@ -11723,14 +11800,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
11723
11800
  // Validate that the class isn't deriving from itself, creating a
11724
11801
  // circular dependency.
11725
11802
  if ((0, typeUtils_1.derivesFromClassRecursive)(argType, classType, /* ignoreUnknown */ true)) {
11726
- addError(localize_1.LocMessage.baseClassCircular(), arg);
11803
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.baseClassCircular(), arg);
11727
11804
  argType = types_1.UnknownType.create();
11728
11805
  }
11729
11806
  // If the class is attempting to derive from a TypeAliasType,
11730
11807
  // generate an error.
11731
11808
  if (((_c = argType.props) === null || _c === void 0 ? void 0 : _c.specialForm) &&
11732
11809
  types_1.ClassType.isBuiltIn(argType.props.specialForm, 'TypeAliasType')) {
11733
- addError(localize_1.LocMessage.typeAliasTypeBaseClass(), arg);
11810
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typeAliasTypeBaseClass(), arg);
11734
11811
  argType = types_1.UnknownType.create();
11735
11812
  }
11736
11813
  }
@@ -11765,7 +11842,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
11765
11842
  }
11766
11843
  if (types_1.ClassType.isFinal(argType)) {
11767
11844
  const className = printObjectTypeForClass(argType);
11768
- addError(localize_1.LocMessage.baseClassFinal().format({ type: className }), arg.d.valueExpr);
11845
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.baseClassFinal().format({ type: className }), arg.d.valueExpr);
11769
11846
  }
11770
11847
  }
11771
11848
  (0, typeUtils_1.addTypeVarsToListIfUnique)(typeParameters, (0, typeUtils_1.getTypeVarArgumentsRecursive)(argType));
@@ -11808,7 +11885,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
11808
11885
  // PEP 589 specifies that the parameter must be either True or False.
11809
11886
  const constArgValue = (0, staticExpressions_1.evaluateStaticBoolExpression)(arg.d.valueExpr, fileInfo.executionEnvironment, fileInfo.definedConstants);
11810
11887
  if (constArgValue === undefined) {
11811
- addError(localize_1.LocMessage.typedDictBoolParam().format({ name: arg.d.name.d.value }), arg.d.valueExpr);
11888
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typedDictBoolParam().format({ name: arg.d.name.d.value }), arg.d.valueExpr);
11812
11889
  }
11813
11890
  else if (arg.d.name.d.value === 'total' && !constArgValue) {
11814
11891
  classType.shared.flags |= 32 /* ClassTypeFlags.CanOmitDictValues */;
@@ -11822,12 +11899,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
11822
11899
  }
11823
11900
  }
11824
11901
  else {
11825
- addError(localize_1.LocMessage.typedDictInitsubclassParameter().format({ name: arg.d.name.d.value }), arg);
11902
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typedDictInitsubclassParameter().format({ name: arg.d.name.d.value }), arg);
11826
11903
  }
11827
11904
  }
11828
11905
  else if (arg.d.name.d.value === 'metaclass') {
11829
11906
  if (metaclassNode) {
11830
- addError(localize_1.LocMessage.metaclassDuplicate(), arg);
11907
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.metaclassDuplicate(), arg);
11831
11908
  }
11832
11909
  else {
11833
11910
  metaclassNode = arg.d.valueExpr;
@@ -12053,7 +12130,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
12053
12130
  }
12054
12131
  });
12055
12132
  if (foundInvalidBaseClass) {
12056
- addError(localize_1.LocMessage.typedDictBaseClass() + diag.getString(), node.d.name);
12133
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typedDictBaseClass() + diag.getString(), node.d.name);
12057
12134
  }
12058
12135
  (0, typedDicts_1.synthesizeTypedDictClassMethods)(evaluatorInterface, node, classType);
12059
12136
  }
@@ -12348,13 +12425,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
12348
12425
  function completeClassTypeDeferred(type, errorNode) {
12349
12426
  // Recompute the MRO linearization.
12350
12427
  if (!(0, typeUtils_1.computeMroLinearization)(type)) {
12351
- addError(localize_1.LocMessage.methodOrdering(), errorNode);
12428
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.methodOrdering(), errorNode);
12352
12429
  }
12353
12430
  // Recompute the effective metaclass.
12354
12431
  computeEffectiveMetaclass(type, errorNode);
12355
12432
  }
12356
12433
  function validateInitSubclassArgs(node, classType) {
12357
- var _a, _b;
12358
12434
  // Collect arguments that will be passed to the `__init_subclass__`
12359
12435
  // method described in PEP 487 and validate it.
12360
12436
  const argList = [];
@@ -12368,7 +12444,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
12368
12444
  });
12369
12445
  }
12370
12446
  });
12371
- const errorNode = argList.length > 0 ? (_b = (_a = argList[0].node) === null || _a === void 0 ? void 0 : _a.d.valueExpr) !== null && _b !== void 0 ? _b : node.d.name : node.d.name;
12372
12447
  let newMethodMember;
12373
12448
  // See if the class has a metaclass that overrides `__new__`. If so, we
12374
12449
  // will validate the signature of the `__new__` method.
@@ -12399,7 +12474,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
12399
12474
  }
12400
12475
  argList.forEach((arg) => {
12401
12476
  var _a, _b, _c;
12402
- const signatureTracker = new typeUtils_1.UniqueSignatureTracker();
12403
12477
  if (arg.argumentCategory === 0 /* ArgumentCategory.Simple */ && arg.name) {
12404
12478
  const paramIndex = (_a = paramMap.get(arg.name.d.value)) !== null && _a !== void 0 ? _a : paramListDetails.kwargsIndex;
12405
12479
  if (paramIndex !== undefined) {
@@ -12409,13 +12483,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
12409
12483
  paramType: paramInfo.type,
12410
12484
  requiresTypeVarMatching: false,
12411
12485
  argument: arg,
12412
- errorNode: (_b = arg.valueExpression) !== null && _b !== void 0 ? _b : errorNode,
12486
+ errorNode: (_b = arg.valueExpression) !== null && _b !== void 0 ? _b : node.d.name,
12413
12487
  };
12414
- validateArgType(argParam, new typeVarContext_1.TypeVarContext(), signatureTracker, { type: newMethodType }, { skipUnknownArgCheck: true, skipOverloadArg: true });
12488
+ validateArgType(argParam, new typeVarContext_1.TypeVarContext(), { type: newMethodType }, { skipUnknownArgCheck: true, skipOverloadArg: true });
12415
12489
  paramMap.delete(arg.name.d.value);
12416
12490
  }
12417
12491
  else {
12418
- addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.paramNameMissing().format({ name: arg.name.d.value }), (_c = arg.name) !== null && _c !== void 0 ? _c : errorNode);
12492
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.paramNameMissing().format({ name: arg.name.d.value }), (_c = arg.name) !== null && _c !== void 0 ? _c : node.d.name);
12419
12493
  }
12420
12494
  }
12421
12495
  });
@@ -12432,7 +12506,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
12432
12506
  const missingParamNames = unassignedParams.map((p) => `"${p}"`).join(', ');
12433
12507
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, unassignedParams.length === 1
12434
12508
  ? localize_1.LocMessage.argMissingForParam().format({ name: missingParamNames })
12435
- : localize_1.LocMessage.argMissingForParams().format({ names: missingParamNames }), errorNode);
12509
+ : localize_1.LocMessage.argMissingForParams().format({ names: missingParamNames }), node.d.name);
12436
12510
  }
12437
12511
  }
12438
12512
  }
@@ -12440,7 +12514,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
12440
12514
  else {
12441
12515
  // If there was no custom metaclass __new__ method, see if there is an __init_subclass__
12442
12516
  // method present somewhere in the class hierarchy.
12443
- const initSubclassMethodInfo = getTypeOfBoundMember(errorNode, classType, '__init_subclass__',
12517
+ const initSubclassMethodInfo = getTypeOfBoundMember(node.d.name, classType, '__init_subclass__',
12444
12518
  /* usage */ undefined,
12445
12519
  /* diag */ undefined, 32 /* MemberAccessFlags.SkipClassMembers */ |
12446
12520
  1 /* MemberAccessFlags.SkipOriginalClass */ |
@@ -12448,10 +12522,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
12448
12522
  if (initSubclassMethodInfo) {
12449
12523
  const initSubclassMethodType = initSubclassMethodInfo.type;
12450
12524
  if (initSubclassMethodType && initSubclassMethodInfo.classType) {
12451
- const callResult = validateCallArguments(errorNode, argList, { type: initSubclassMethodType },
12525
+ const callResult = validateCallArguments(node.d.name, argList, { type: initSubclassMethodType },
12452
12526
  /* typeVarContext */ undefined,
12453
- /* skipUnknownArgCheck */ false, (0, typeUtils_1.makeInferenceContext)(getNoneType()),
12454
- /* signatureTracker */ undefined);
12527
+ /* skipUnknownArgCheck */ false, (0, typeUtils_1.makeInferenceContext)(getNoneType()));
12455
12528
  if (callResult.argumentErrors) {
12456
12529
  const diag = addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.initSubclassCallFailed(), node.d.name);
12457
12530
  const initSubclassFunction = (0, types_1.isOverloadedFunction)(initSubclassMethodType)
@@ -12620,7 +12693,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
12620
12693
  firstCommentAnnotationIndex = 0;
12621
12694
  }
12622
12695
  else if (received !== expected) {
12623
- addError(localize_1.LocMessage.annotatedParamCountMismatch().format({
12696
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.annotatedParamCountMismatch().format({
12624
12697
  expected,
12625
12698
  received,
12626
12699
  }), node.d.funcAnnotationComment);
@@ -12682,7 +12755,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
12682
12755
  (0, typeUtils_1.addTypeVarsToListIfUnique)(typeParametersSeen, (0, typeUtils_1.getTypeVarArgumentsRecursive)(annotatedType), functionType.shared.typeVarScopeId);
12683
12756
  }
12684
12757
  if ((0, types_1.isVariadicTypeVar)(annotatedType) && !annotatedType.priv.isVariadicUnpacked) {
12685
- addError(localize_1.LocMessage.unpackedTypeVarTupleExpected().format({
12758
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.unpackedTypeVarTupleExpected().format({
12686
12759
  name1: annotatedType.shared.name,
12687
12760
  name2: annotatedType.shared.name,
12688
12761
  }), paramTypeNode);
@@ -12794,12 +12867,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
12794
12867
  types_1.FunctionType.addPositionOnlyParameterSeparator(functionType);
12795
12868
  }
12796
12869
  // Update the types for the nodes associated with the parameters.
12870
+ const scopeIds = ParseTreeUtils.getTypeVarScopesForNode(node);
12797
12871
  paramTypes.forEach((paramType, index) => {
12798
12872
  const paramNameNode = node.d.params[index].d.name;
12799
12873
  if (paramNameNode) {
12800
12874
  if ((0, types_1.isUnknown)(paramType)) {
12801
12875
  functionType.shared.flags |= 16384 /* FunctionTypeFlags.UnannotatedParams */;
12802
12876
  }
12877
+ paramType = (0, typeUtils_1.updateTypeWithInternalTypeVars)(paramType, scopeIds);
12803
12878
  writeTypeCache(paramNameNode, { type: paramType }, 0 /* EvalFlags.None */);
12804
12879
  }
12805
12880
  });
@@ -13932,9 +14007,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
13932
14007
  // Return expressions must be evaluated in the context of the expected return type.
13933
14008
  if (parent.d.expr) {
13934
14009
  const enclosingFunctionNode = ParseTreeUtils.getEnclosingFunction(node);
13935
- const declaredReturnType = enclosingFunctionNode
14010
+ let declaredReturnType = enclosingFunctionNode
13936
14011
  ? getFunctionDeclaredReturnType(enclosingFunctionNode)
13937
14012
  : undefined;
14013
+ if (declaredReturnType) {
14014
+ const liveScopeIds = ParseTreeUtils.getTypeVarScopesForNode(node);
14015
+ declaredReturnType = (0, typeUtils_1.updateTypeWithInternalTypeVars)(declaredReturnType, liveScopeIds);
14016
+ }
13938
14017
  getTypeOfExpression(parent.d.expr, 0 /* EvalFlags.None */, (0, typeUtils_1.makeInferenceContext)(declaredReturnType));
13939
14018
  return;
13940
14019
  }
@@ -13956,6 +14035,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
13956
14035
  getTypeOfExpression(nodeToEvaluate, flags);
13957
14036
  }
13958
14037
  function evaluateTypeOfParameter(node) {
14038
+ var _a;
13959
14039
  // If this parameter has no name, we have nothing to do.
13960
14040
  if (!node.d.name) {
13961
14041
  return;
@@ -13988,7 +14068,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
13988
14068
  const typeParamName = getPseudoGenericTypeVarName(node.d.name.d.value);
13989
14069
  const paramType = classInfo.classType.shared.typeParameters.find((param) => param.shared.name === typeParamName);
13990
14070
  if (paramType) {
13991
- writeTypeCache(node.d.name, { type: paramType }, 0 /* EvalFlags.None */);
14071
+ writeTypeCache(node.d.name, { type: types_1.TypeVarType.cloneWithInternalScopeId(paramType) }, 0 /* EvalFlags.None */);
13992
14072
  return;
13993
14073
  }
13994
14074
  }
@@ -13997,8 +14077,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
13997
14077
  // a parent class if it has an annotated type.
13998
14078
  const functionFlags = (0, decorators_1.getFunctionInfoFromDecorators)(evaluatorInterface, functionNode,
13999
14079
  /* isInClass */ true).flags;
14000
- const inferredParamType = inferParameterType(functionNode, functionFlags, paramIndex, classInfo === null || classInfo === void 0 ? void 0 : classInfo.classType);
14001
- writeTypeCache(node.d.name, { type: transformVariadicParamType(node, node.d.category, inferredParamType !== null && inferredParamType !== void 0 ? inferredParamType : types_1.UnknownType.create()) }, 0 /* EvalFlags.None */);
14080
+ let inferredParamType = (_a = inferParameterType(functionNode, functionFlags, paramIndex, classInfo === null || classInfo === void 0 ? void 0 : classInfo.classType)) !== null && _a !== void 0 ? _a : types_1.UnknownType.create();
14081
+ const liveTypeVarScopes = ParseTreeUtils.getTypeVarScopesForNode(node);
14082
+ inferredParamType = (0, typeUtils_1.updateTypeWithInternalTypeVars)(inferredParamType, liveTypeVarScopes);
14083
+ writeTypeCache(node.d.name, { type: transformVariadicParamType(node, node.d.category, inferredParamType) }, 0 /* EvalFlags.None */);
14002
14084
  }
14003
14085
  // Evaluates the types that are assigned within the statement that contains
14004
14086
  // the specified parse node. In some cases, a broader statement may need to
@@ -14209,13 +14291,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14209
14291
  }
14210
14292
  case 'Never': {
14211
14293
  if (typeArgs && typeArgs.length > 0) {
14212
- addError(localize_1.LocMessage.typeArgsExpectingNone().format({ name: 'Never' }), typeArgs[0].node);
14294
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgsExpectingNone().format({ name: 'Never' }), typeArgs[0].node);
14213
14295
  }
14214
14296
  return { type: types_1.NeverType.createNever() };
14215
14297
  }
14216
14298
  case 'NoReturn': {
14217
14299
  if (typeArgs && typeArgs.length > 0) {
14218
- addError(localize_1.LocMessage.typeArgsExpectingNone().format({ name: 'NoReturn' }), typeArgs[0].node);
14300
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgsExpectingNone().format({ name: 'NoReturn' }), typeArgs[0].node);
14219
14301
  }
14220
14302
  return { type: types_1.NeverType.createNoReturn() };
14221
14303
  }
@@ -14236,11 +14318,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14236
14318
  }
14237
14319
  case 'Protocol': {
14238
14320
  if ((flags & (67108864 /* EvalFlags.NoNonTypeSpecialForms */ | 256 /* EvalFlags.TypeExpression */)) !== 0) {
14239
- addError(localize_1.LocMessage.protocolNotAllowed(), errorNode);
14321
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.protocolNotAllowed(), errorNode);
14240
14322
  }
14241
14323
  typeArgs === null || typeArgs === void 0 ? void 0 : typeArgs.forEach((typeArg) => {
14242
14324
  if (typeArg.typeList || !(0, types_1.isTypeVar)(typeArg.type)) {
14243
- addError(localize_1.LocMessage.protocolTypeArgMustBeTypeParam(), typeArg.node);
14325
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.protocolTypeArgMustBeTypeParam(), typeArg.node);
14244
14326
  }
14245
14327
  });
14246
14328
  return {
@@ -14251,13 +14333,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14251
14333
  }
14252
14334
  case 'TypedDict': {
14253
14335
  if ((flags & (67108864 /* EvalFlags.NoNonTypeSpecialForms */ | 256 /* EvalFlags.TypeExpression */)) !== 0) {
14254
- addError(localize_1.LocMessage.typedDictNotAllowed(), errorNode);
14336
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typedDictNotAllowed(), errorNode);
14255
14337
  }
14256
14338
  break;
14257
14339
  }
14258
14340
  case 'Literal': {
14259
14341
  if ((flags & (67108864 /* EvalFlags.NoNonTypeSpecialForms */ | 256 /* EvalFlags.TypeExpression */)) !== 0) {
14260
- addError(localize_1.LocMessage.literalNotAllowed(), errorNode);
14342
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.literalNotAllowed(), errorNode);
14261
14343
  }
14262
14344
  break;
14263
14345
  }
@@ -14426,11 +14508,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14426
14508
  else if (typeArgs.length > 1) {
14427
14509
  const paramSpecTypeArg = typeArgs.find((typeArg) => (0, types_1.isParamSpec)(typeArg.type));
14428
14510
  if (paramSpecTypeArg) {
14429
- addError(localize_1.LocMessage.paramSpecContext(), paramSpecTypeArg.node);
14511
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.paramSpecContext(), paramSpecTypeArg.node);
14430
14512
  }
14431
14513
  const listTypeArg = typeArgs.find((typeArg) => !!typeArg.typeList);
14432
14514
  if (listTypeArg) {
14433
- addError(localize_1.LocMessage.typeArgListNotAllowed(), listTypeArg.node);
14515
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgListNotAllowed(), listTypeArg.node);
14434
14516
  }
14435
14517
  }
14436
14518
  }
@@ -14538,7 +14620,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14538
14620
  const specializedClass = types_1.ClassType.cloneForSpecialization(classType, typeArgTypes, typeArgs !== undefined);
14539
14621
  return { type: specializedClass };
14540
14622
  }
14541
- function getTypeOfArgument(arg, inferenceContext, signatureTracker) {
14623
+ function getTypeOfArgument(arg, inferenceContext) {
14542
14624
  var _a, _b;
14543
14625
  if (arg.typeResult) {
14544
14626
  const type = arg.typeResult.type;
@@ -14550,12 +14632,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14550
14632
  }
14551
14633
  // If there was no defined type provided, there should always
14552
14634
  // be a value expression from which we can retrieve the type.
14553
- const typeResult = getTypeOfExpression(arg.valueExpression,
14554
- /* flags */ undefined, inferenceContext, signatureTracker);
14555
- if (signatureTracker) {
14556
- typeResult.type = (0, typeUtils_1.ensureFunctionSignaturesAreUnique)(typeResult.type, signatureTracker, arg.valueExpression.start);
14557
- }
14558
- return typeResult;
14635
+ return getTypeOfExpression(arg.valueExpression, /* flags */ undefined, inferenceContext);
14559
14636
  }
14560
14637
  // This function is like getTypeOfArgument except that it is
14561
14638
  // used in cases where the argument is expected to be a type
@@ -14746,6 +14823,50 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14746
14823
  throw e;
14747
14824
  }
14748
14825
  }
14826
+ function getSignatureTrackerForNode(node) {
14827
+ for (let i = signatureTrackerStack.length - 1; i >= 0; i--) {
14828
+ const rootNode = signatureTrackerStack[i].rootNode;
14829
+ if (ParseTreeUtils.isNodeContainedWithin(node, rootNode)) {
14830
+ return signatureTrackerStack[i].tracker;
14831
+ }
14832
+ }
14833
+ return undefined;
14834
+ }
14835
+ function useSignatureTracker(node, callback) {
14836
+ const tracker = getSignatureTrackerForNode(node);
14837
+ try {
14838
+ // If a signature tracker doesn't already exist, allocate one.
14839
+ if (!tracker) {
14840
+ signatureTrackerStack.push({
14841
+ tracker: new typeUtils_1.UniqueSignatureTracker(),
14842
+ rootNode: node,
14843
+ });
14844
+ }
14845
+ const result = callback();
14846
+ if (!tracker) {
14847
+ signatureTrackerStack.pop();
14848
+ }
14849
+ return result;
14850
+ }
14851
+ catch (e) {
14852
+ // We don't use finally here because the TypeScript debugger doesn't
14853
+ // handle finally well when single stepping.
14854
+ if (!tracker) {
14855
+ signatureTrackerStack.pop();
14856
+ }
14857
+ throw e;
14858
+ }
14859
+ }
14860
+ function ensureSignatureIsUnique(type, node) {
14861
+ const tracker = getSignatureTrackerForNode(node);
14862
+ if (!tracker) {
14863
+ return type;
14864
+ }
14865
+ if ((0, types_1.isFunction)(type) || (0, types_1.isOverloadedFunction)(type)) {
14866
+ return (0, typeUtils_1.ensureFunctionSignaturesAreUnique)(type, tracker, node.start);
14867
+ }
14868
+ return type;
14869
+ }
14749
14870
  // Disables recording of errors and warnings and disables any caching of
14750
14871
  // types, under the assumption that we're performing speculative evaluations.
14751
14872
  // If speculativeNode is undefined, speculative mode is not used. This is
@@ -14949,8 +15070,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14949
15070
  }
14950
15071
  }
14951
15072
  else if (node.parent && node.parent.nodeType === 1 /* ParseNodeType.Argument */ && node === node.parent.d.name) {
14952
- // The target node is the name in a named argument. We need to determine whether
14953
- // the corresponding named parameter can be determined from the context.
15073
+ // The target node is the name in a keyword argument. We need to determine whether
15074
+ // the corresponding keyword parameter can be determined from the context.
14954
15075
  const argNode = node.parent;
14955
15076
  const paramName = node.d.value;
14956
15077
  if (((_a = argNode.parent) === null || _a === void 0 ? void 0 : _a.nodeType) === 9 /* ParseNodeType.Call */) {
@@ -15089,7 +15210,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
15089
15210
  }
15090
15211
  }
15091
15212
  if (typeAnnotationNode) {
15092
- const declaredType = getTypeOfParameterAnnotation(typeAnnotationNode, declaration.node.d.category);
15213
+ let declaredType = getTypeOfParameterAnnotation(typeAnnotationNode, declaration.node.d.category);
15214
+ const liveTypeVarScopes = ParseTreeUtils.getTypeVarScopesForNode(declaration.node);
15215
+ declaredType = (0, typeUtils_1.updateTypeWithInternalTypeVars)(declaredType, liveTypeVarScopes);
15093
15216
  return {
15094
15217
  type: transformVariadicParamType(declaration.node, declaration.node.d.category, adjustParameterAnnotatedType(declaration.node, declaredType)),
15095
15218
  };
@@ -15119,9 +15242,21 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
15119
15242
  allowClassVar: ParseTreeUtils.isClassVarAllowedForAssignmentTarget(declNode),
15120
15243
  allowFinal: ParseTreeUtils.isFinalAllowedForAssignmentTarget(declNode),
15121
15244
  allowRequired: ParseTreeUtils.isRequiredAllowedForAssignmentTarget(declNode),
15245
+ enforceClassTypeVarScope: declaration.isDefinedByMemberAccess,
15122
15246
  });
15123
15247
  }
15124
15248
  if (declaredType) {
15249
+ // If this is a declaration for a member variable within a method,
15250
+ // we need to convert any "internal" TypeVars associated with the
15251
+ // class to their "external" counterparts.
15252
+ if (declaration.isDefinedByMemberAccess) {
15253
+ const enclosingClass = ParseTreeUtils.getEnclosingClass(declaration.node);
15254
+ if (enclosingClass) {
15255
+ declaredType = (0, typeUtils_1.updateTypeWithExternalTypeVars)(declaredType, [
15256
+ ParseTreeUtils.getScopeIdForNode(enclosingClass),
15257
+ ]);
15258
+ }
15259
+ }
15125
15260
  if ((0, types_1.isClassInstance)(declaredType) && types_1.ClassType.isBuiltIn(declaredType, 'TypeAlias')) {
15126
15261
  return { type: undefined, isTypeAlias: true };
15127
15262
  }
@@ -15173,7 +15308,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
15173
15308
  ignorePseudoGeneric: true,
15174
15309
  ignoreImplicitTypeArgs: true,
15175
15310
  })) {
15176
- addError(localize_1.LocMessage.typeVarBoundGeneric(), constraint);
15311
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typeVarBoundGeneric(), constraint);
15177
15312
  }
15178
15313
  return (0, typeUtils_1.convertToInstance)(constraintType);
15179
15314
  });
@@ -15190,7 +15325,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
15190
15325
  allowForwardReference: true,
15191
15326
  }).type;
15192
15327
  if ((0, typeUtils_1.requiresSpecialization)(boundType, { ignorePseudoGeneric: true })) {
15193
- addError(localize_1.LocMessage.typeVarConstraintGeneric(), node.d.boundExpr);
15328
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typeVarConstraintGeneric(), node.d.boundExpr);
15194
15329
  }
15195
15330
  if (node.d.typeParamKind === parseNodes_1.TypeParameterKind.TypeVar) {
15196
15331
  typeVar.shared.boundType = (0, typeUtils_1.convertToInstance)(boundType);
@@ -15856,7 +15991,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
15856
15991
  return types_1.UnknownType.create();
15857
15992
  }
15858
15993
  function _getFunctionInferredReturnType(type, callSiteInfo) {
15859
- var _a;
15994
+ var _a, _b, _c;
15860
15995
  let returnType;
15861
15996
  let isIncomplete = false;
15862
15997
  const analyzeUnannotatedFunctions = true;
@@ -15909,6 +16044,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
15909
16044
  if (!returnType) {
15910
16045
  returnType = types_1.UnknownType.create();
15911
16046
  }
16047
+ // Externalize any TypeVars that appear in the type.
16048
+ const typeVarScopes = [];
16049
+ if (type.shared.typeVarScopeId) {
16050
+ typeVarScopes.push(type.shared.typeVarScopeId);
16051
+ }
16052
+ if ((_a = type.shared.methodClass) === null || _a === void 0 ? void 0 : _a.shared.typeVarScopeId) {
16053
+ typeVarScopes.push(type.shared.methodClass.shared.typeVarScopeId);
16054
+ }
16055
+ returnType = (0, typeUtils_1.updateTypeWithExternalTypeVars)(returnType, typeVarScopes);
15912
16056
  // Cache the type for next time.
15913
16057
  if (!isIncomplete) {
15914
16058
  type.priv.inferredReturnType = returnType;
@@ -15926,7 +16070,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
15926
16070
  callSiteInfo) {
15927
16071
  let hasDecorators = false;
15928
16072
  let isAsync = false;
15929
- const declNode = (_a = type.shared.declaration) === null || _a === void 0 ? void 0 : _a.node;
16073
+ const declNode = (_b = type.shared.declaration) === null || _b === void 0 ? void 0 : _b.node;
15930
16074
  if (declNode) {
15931
16075
  if (declNode.d.decorators.length > 0) {
15932
16076
  hasDecorators = true;
@@ -15941,6 +16085,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
15941
16085
  const contextualReturnType = getFunctionInferredReturnTypeUsingArguments(type, callSiteInfo);
15942
16086
  if (contextualReturnType) {
15943
16087
  returnType = contextualReturnType;
16088
+ if ((_c = type.shared.declaration) === null || _c === void 0 ? void 0 : _c.node) {
16089
+ // Externalize any TypeVars that appear in the type.
16090
+ const liveScopeIds = ParseTreeUtils.getTypeVarScopesForNode(type.shared.declaration.node);
16091
+ returnType = (0, typeUtils_1.updateTypeWithExternalTypeVars)(returnType, liveScopeIds);
16092
+ }
15944
16093
  }
15945
16094
  }
15946
16095
  }
@@ -16083,26 +16232,24 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
16083
16232
  }
16084
16233
  return undefined;
16085
16234
  }
16235
+ // If the function has an explicitly-declared return type, it is returned
16236
+ // unaltered unless the function is a generator, in which case it is
16237
+ // modified to return only the return type for the generator.
16086
16238
  function getFunctionDeclaredReturnType(node) {
16087
16239
  const functionTypeInfo = getTypeOfFunction(node);
16088
- if (!functionTypeInfo) {
16089
- // We hit a recursive dependency.
16090
- return types_1.AnyType.create();
16091
- }
16092
- // Ignore this check for abstract methods, which often
16093
- // don't actually return any value.
16094
- if (types_1.FunctionType.isAbstractMethod(functionTypeInfo.functionType)) {
16095
- return types_1.AnyType.create();
16240
+ const returnType = functionTypeInfo === null || functionTypeInfo === void 0 ? void 0 : functionTypeInfo.functionType.shared.declaredReturnType;
16241
+ if (!returnType) {
16242
+ return undefined;
16096
16243
  }
16097
16244
  if (types_1.FunctionType.isGenerator(functionTypeInfo.functionType)) {
16098
16245
  return (0, typeUtils_1.getDeclaredGeneratorReturnType)(functionTypeInfo.functionType);
16099
16246
  }
16100
- return functionTypeInfo.functionType.shared.declaredReturnType;
16247
+ return returnType;
16101
16248
  }
16102
16249
  function getTypeOfMember(member) {
16103
16250
  if ((0, types_1.isInstantiableClass)(member.classType)) {
16104
- return (0, typeUtils_1.partiallySpecializeType)(getEffectiveTypeOfSymbol(member.symbol), member.classType,
16105
- /* selfClass */ undefined, typeClass && (0, types_1.isInstantiableClass)(typeClass) ? typeClass : undefined);
16251
+ return (0, typeUtils_1.partiallySpecializeType)(getEffectiveTypeOfSymbol(member.symbol), member.classType, getTypeClassType(),
16252
+ /* selfClass */ undefined);
16106
16253
  }
16107
16254
  return types_1.UnknownType.create();
16108
16255
  }
@@ -16136,7 +16283,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
16136
16283
  (0, types_1.isClass)(member.unspecializedClassType) &&
16137
16284
  (flags & 1024 /* MemberAccessFlags.DisallowGenericInstanceVariableAccess */) !== 0 &&
16138
16285
  (0, typeUtils_1.requiresSpecialization)(typeResult.type, { ignoreSelf: true, ignoreImplicitTypeArgs: true })) {
16139
- const specializedType = (0, typeUtils_1.partiallySpecializeType)(typeResult.type, member.unspecializedClassType, (0, typeUtils_1.selfSpecializeClass)(selfClass, { overrideTypeArgs: true }));
16286
+ const specializedType = (0, typeUtils_1.partiallySpecializeType)(typeResult.type, member.unspecializedClassType, getTypeClassType(), (0, typeUtils_1.selfSpecializeClass)(selfClass, { overrideTypeArgs: true }));
16140
16287
  if ((0, types_1.findSubtype)(specializedType, (subtype) => !(0, types_1.isFunction)(subtype) &&
16141
16288
  !(0, types_1.isOverloadedFunction)(subtype) &&
16142
16289
  (0, typeUtils_1.requiresSpecialization)(subtype, { ignoreSelf: true, ignoreImplicitTypeArgs: true }))) {
@@ -16144,7 +16291,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
16144
16291
  }
16145
16292
  }
16146
16293
  return {
16147
- type: (0, typeUtils_1.partiallySpecializeType)(typeResult.type, member.classType, selfClass),
16294
+ type: (0, typeUtils_1.partiallySpecializeType)(typeResult.type, member.classType, getTypeClassType(), selfClass),
16148
16295
  isIncomplete: !!typeResult.isIncomplete,
16149
16296
  };
16150
16297
  }
@@ -16295,7 +16442,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
16295
16442
  (0, debug_1.assert)(memberInfo !== undefined);
16296
16443
  let destMemberType = getEffectiveTypeOfSymbol(symbol);
16297
16444
  const srcMemberType = getTypeOfMember(memberInfo);
16298
- destMemberType = (0, typeUtils_1.partiallySpecializeType)(destMemberType, destType);
16445
+ destMemberType = (0, typeUtils_1.partiallySpecializeType)(destMemberType, destType, getTypeClassType());
16299
16446
  // Properties require special processing.
16300
16447
  if ((0, types_1.isClassInstance)(destMemberType) &&
16301
16448
  types_1.ClassType.isPropertyClass(destMemberType) &&
@@ -16323,7 +16470,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
16323
16470
  }
16324
16471
  if (!assignType(destMemberType, srcMemberType,
16325
16472
  /* diag */ undefined, typeVarContext,
16326
- /* srcTypeVarContext */ undefined, flags | 65536 /* AssignTypeFlags.IgnoreSelfClsParamCompatibility */, recursionCount)) {
16473
+ /* srcTypeVarContext */ undefined, flags | 32768 /* AssignTypeFlags.SkipSelfClsParamCheck */, recursionCount)) {
16327
16474
  isAssignable = false;
16328
16475
  }
16329
16476
  }
@@ -16771,7 +16918,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
16771
16918
  let isSpecialFormExempt = false;
16772
16919
  // A few special forms that are normally not compatible with type[T]
16773
16920
  // are compatible specifically in the context of isinstance and issubclass.
16774
- if ((flags & 32768 /* AssignTypeFlags.AllowIsinstanceSpecialForms */) !== 0) {
16921
+ if ((flags & 16384 /* AssignTypeFlags.AllowIsinstanceSpecialForms */) !== 0) {
16775
16922
  if (types_1.ClassType.isBuiltIn(specialForm, ['Callable', 'UnionType', 'Generic'])) {
16776
16923
  isSpecialFormExempt = true;
16777
16924
  }
@@ -16841,11 +16988,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
16841
16988
  if ((0, types_1.isUnbound)(destType) || (0, types_1.isUnbound)(srcType)) {
16842
16989
  return true;
16843
16990
  }
16844
- // If we're in "overload overlap" mode, convert top-level type variables
16845
- // to their concrete forms in the source.
16846
- if ((flags & 16 /* AssignTypeFlags.OverloadOverlapCheck */) !== 0) {
16847
- srcType = makeTopLevelTypeVarsConcrete(srcType);
16848
- }
16849
16991
  // Strip flags we don't want to propagate to other calls.
16850
16992
  const originalFlags = flags;
16851
16993
  flags &= ~128 /* AssignTypeFlags.AllowBoolTypeGuard */;
@@ -16891,6 +17033,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
16891
17033
  srcType.shared.boundType &&
16892
17034
  destType.shared.isSynthesizedSelf &&
16893
17035
  destType.shared.boundType &&
17036
+ types_1.TypeVarType.hasInternalScopeId(destType) === types_1.TypeVarType.hasInternalScopeId(srcType) &&
16894
17037
  types_1.TypeBase.isInstance(srcType) === types_1.TypeBase.isInstance(destType)) {
16895
17038
  if ((flags & 2 /* AssignTypeFlags.ReverseTypeVarMatching */) === 0 && destTypeVarContext) {
16896
17039
  (0, constraintSolver_1.assignTypeToTypeVar)(evaluatorInterface, destType, srcType, diag, destTypeVarContext, originalFlags, recursionCount);
@@ -16913,7 +17056,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
16913
17056
  if (!(0, constraintSolver_1.assignTypeToTypeVar)(evaluatorInterface, destType, srcType, diag, targetTypeVarContext !== null && targetTypeVarContext !== void 0 ? targetTypeVarContext : new typeVarContext_1.TypeVarContext(), originalFlags, recursionCount)) {
16914
17057
  return false;
16915
17058
  }
16916
- if ((0, types_1.isAnyOrUnknown)(srcType) && (flags & 16 /* AssignTypeFlags.OverloadOverlapCheck */) !== 0) {
17059
+ if ((0, types_1.isAnyOrUnknown)(srcType) && (flags & 16 /* AssignTypeFlags.OverloadOverlap */) !== 0) {
16917
17060
  return false;
16918
17061
  }
16919
17062
  return true;
@@ -16989,7 +17132,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
16989
17132
  const typeVarSubstitution = (0, typeUtils_1.isEllipsisType)(srcType) ? types_1.AnyType.create() : srcType;
16990
17133
  (0, typeUtils_1.setTypeArgumentsRecursive)(destType, typeVarSubstitution, targetTypeVarContext, recursionCount);
16991
17134
  }
16992
- if ((flags & 16 /* AssignTypeFlags.OverloadOverlapCheck */) === 0) {
17135
+ if ((flags & 16 /* AssignTypeFlags.OverloadOverlap */) === 0) {
16993
17136
  return true;
16994
17137
  }
16995
17138
  }
@@ -17077,8 +17220,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
17077
17220
  if ((0, types_1.isInstantiableClass)(expandedSrcType)) {
17078
17221
  // PEP 544 says that if the dest type is a type[Proto] class,
17079
17222
  // the source must be a "concrete" (non-protocol) class.
17080
- if (types_1.ClassType.isProtocolClass(destType) &&
17081
- (flags & 16384 /* AssignTypeFlags.IgnoreProtocolAssignmentCheck */) === 0) {
17223
+ if (types_1.ClassType.isProtocolClass(destType)) {
17082
17224
  if (types_1.ClassType.isProtocolClass(expandedSrcType) &&
17083
17225
  (0, types_1.isInstantiableClass)(srcType) &&
17084
17226
  !srcType.priv.includeSubclasses) {
@@ -17123,7 +17265,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
17123
17265
  if ((0, types_1.isInstantiableClass)(srcType) && isSpecialFormClass(srcType, flags)) {
17124
17266
  return false;
17125
17267
  }
17126
- if ((0, types_1.isAnyOrUnknown)(srcType) && (flags & 16 /* AssignTypeFlags.OverloadOverlapCheck */) !== 0) {
17268
+ if ((0, types_1.isAnyOrUnknown)(srcType) && (flags & 16 /* AssignTypeFlags.OverloadOverlap */) !== 0) {
17127
17269
  return false;
17128
17270
  }
17129
17271
  const destTypeArgs = destType.priv.typeArguments;
@@ -17185,6 +17327,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
17185
17327
  // Is the destination a callback protocol (defined in PEP 544)?
17186
17328
  const destCallbackType = getCallbackProtocolType(destType, recursionCount);
17187
17329
  if (destCallbackType) {
17330
+ if (destTypeVarContext) {
17331
+ destTypeVarContext.addSolveForScope((0, typeUtils_1.getTypeVarScopeId)(destCallbackType));
17332
+ }
17188
17333
  return assignType(destCallbackType, concreteSrcType, diag, destTypeVarContext, srcTypeVarContext, flags, recursionCount);
17189
17334
  }
17190
17335
  // All functions are considered instances of "builtins.function".
@@ -17226,7 +17371,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
17226
17371
  }
17227
17372
  }
17228
17373
  else if ((0, types_1.isAnyOrUnknown)(concreteSrcType) && !((_j = concreteSrcType.props) === null || _j === void 0 ? void 0 : _j.specialForm)) {
17229
- return (flags & 16 /* AssignTypeFlags.OverloadOverlapCheck */) === 0;
17374
+ return (flags & 16 /* AssignTypeFlags.OverloadOverlap */) === 0;
17230
17375
  }
17231
17376
  else if ((0, types_1.isUnion)(concreteSrcType)) {
17232
17377
  return assignType(destType, concreteSrcType, diag, destTypeVarContext, srcTypeVarContext, flags, recursionCount);
@@ -17255,7 +17400,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
17255
17400
  }
17256
17401
  }
17257
17402
  if ((0, types_1.isAnyOrUnknown)(concreteSrcType)) {
17258
- return (flags & 16 /* AssignTypeFlags.OverloadOverlapCheck */) === 0;
17403
+ return (flags & 16 /* AssignTypeFlags.OverloadOverlap */) === 0;
17259
17404
  }
17260
17405
  if ((0, types_1.isOverloadedFunction)(concreteSrcType)) {
17261
17406
  // Find all of the overloaded functions that match the parameters.
@@ -17507,7 +17652,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
17507
17652
  }
17508
17653
  // If we're looking for type overlaps and at least one type was matched,
17509
17654
  // consider it as assignable.
17510
- if ((flags & 32 /* AssignTypeFlags.PartialOverloadOverlapCheck */) !== 0 && matchedSomeSubtypes) {
17655
+ if ((flags & 32 /* AssignTypeFlags.PartialOverloadOverlap */) !== 0 && matchedSomeSubtypes) {
17511
17656
  return true;
17512
17657
  }
17513
17658
  }
@@ -17535,7 +17680,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
17535
17680
  if (isIncompatible) {
17536
17681
  // If we're looking for type overlaps and at least one type was matched,
17537
17682
  // consider it as assignable.
17538
- if ((flags & 32 /* AssignTypeFlags.PartialOverloadOverlapCheck */) !== 0 && matchedSomeSubtypes) {
17683
+ if ((flags & 32 /* AssignTypeFlags.PartialOverloadOverlap */) !== 0 && matchedSomeSubtypes) {
17539
17684
  return true;
17540
17685
  }
17541
17686
  diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.LocAddendum.typeAssignmentMismatch().format(printSrcDestTypes(srcType, destType)));
@@ -17544,7 +17689,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
17544
17689
  return true;
17545
17690
  }
17546
17691
  function isSpecialFormClass(classType, flags) {
17547
- if ((flags & 32768 /* AssignTypeFlags.AllowIsinstanceSpecialForms */) !== 0) {
17692
+ if ((flags & 16384 /* AssignTypeFlags.AllowIsinstanceSpecialForms */) !== 0) {
17548
17693
  return false;
17549
17694
  }
17550
17695
  return types_1.ClassType.isSpecialFormClass(classType);
@@ -17802,13 +17947,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
17802
17947
  let specializedSrcType = srcType;
17803
17948
  let specializedDestType = destType;
17804
17949
  let doSpecializationStep = false;
17805
- if ((flags & 2 /* AssignTypeFlags.ReverseTypeVarMatching */) === 0) {
17806
- specializedDestType = (0, typeUtils_1.applySolvedTypeVars)(destType, destTypeVarContext, { useNarrowBoundOnly: true });
17807
- doSpecializationStep = (0, typeUtils_1.requiresSpecialization)(specializedDestType);
17808
- }
17809
- else {
17810
- specializedSrcType = (0, typeUtils_1.applySolvedTypeVars)(srcType, srcTypeVarContext, { useNarrowBoundOnly: true });
17811
- doSpecializationStep = (0, typeUtils_1.requiresSpecialization)(specializedSrcType);
17950
+ if ((flags & 16 /* AssignTypeFlags.OverloadOverlap */) === 0) {
17951
+ if ((flags & 2 /* AssignTypeFlags.ReverseTypeVarMatching */) === 0) {
17952
+ specializedDestType = (0, typeUtils_1.applySolvedTypeVars)(destType, destTypeVarContext, { useNarrowBoundOnly: true });
17953
+ doSpecializationStep = (0, typeUtils_1.requiresSpecialization)(specializedDestType);
17954
+ }
17955
+ else {
17956
+ specializedSrcType = (0, typeUtils_1.applySolvedTypeVars)(srcType, srcTypeVarContext, { useNarrowBoundOnly: true });
17957
+ doSpecializationStep = (0, typeUtils_1.requiresSpecialization)(specializedSrcType);
17958
+ }
17812
17959
  }
17813
17960
  // Is an additional specialization step required?
17814
17961
  if (doSpecializationStep) {
@@ -17913,9 +18060,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
17913
18060
  function assignFunction(destType, srcType, diag, destTypeVarContext, srcTypeVarContext, flags, recursionCount) {
17914
18061
  var _a, _b, _c, _d, _e, _f, _g, _h;
17915
18062
  let canAssign = true;
17916
- const checkReturnType = (flags & 64 /* AssignTypeFlags.SkipFunctionReturnTypeCheck */) === 0;
18063
+ const checkReturnType = (flags & 64 /* AssignTypeFlags.SkipReturnTypeCheck */) === 0;
17917
18064
  const reverseMatching = (flags & 2 /* AssignTypeFlags.ReverseTypeVarMatching */) !== 0;
17918
- flags &= ~64 /* AssignTypeFlags.SkipFunctionReturnTypeCheck */;
18065
+ flags &= ~64 /* AssignTypeFlags.SkipReturnTypeCheck */;
17919
18066
  const destParamSpec = types_1.FunctionType.getParamSpecFromArgsKwargs(destType);
17920
18067
  if (destParamSpec) {
17921
18068
  destType = types_1.FunctionType.cloneRemoveParamSpecArgsKwargs(destType);
@@ -17935,7 +18082,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
17935
18082
  for (let paramIndex = 0; paramIndex < positionalsToMatch; paramIndex++) {
17936
18083
  if (paramIndex === 0 &&
17937
18084
  destType.shared.methodClass &&
17938
- (flags & 65536 /* AssignTypeFlags.IgnoreSelfClsParamCompatibility */) !== 0) {
18085
+ (flags & 32768 /* AssignTypeFlags.SkipSelfClsParamCheck */) !== 0) {
17939
18086
  if (types_1.FunctionType.isInstanceMethod(destType) || types_1.FunctionType.isClassMethod(destType)) {
17940
18087
  continue;
17941
18088
  }
@@ -18461,10 +18608,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
18461
18608
  // consistent and probably not sound from a type theory perspective. It
18462
18609
  // should be completely reworked once there has been a public discussion
18463
18610
  // about the correct behavior.
18464
- // If the result is incomplete, do not attempt to narrow the type.
18465
- if (assignedTypeResult.isIncomplete) {
18466
- return assignedTypeResult;
18467
- }
18468
18611
  const narrowedType = (0, typeUtils_1.mapSubtypes)(assignedTypeResult.type, (assignedSubtype) => {
18469
18612
  // Handle the special case where the assigned type is a literal type.
18470
18613
  // Some types include very large unions of literal types, and we don't
@@ -18997,10 +19140,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
18997
19140
  let bestConstraintSoFar;
18998
19141
  // Try to find the best (narrowest) match among the constraints.
18999
19142
  for (const constraint of constraints) {
19000
- // Don't allow Never as a type argument.
19001
- if (assignType(constraint, effectiveSrcType) && !(0, types_1.isNever)(effectiveSrcType)) {
19002
- if (!bestConstraintSoFar || assignType(bestConstraintSoFar, constraint)) {
19003
- bestConstraintSoFar = constraint;
19143
+ if (assignType(constraint, effectiveSrcType)) {
19144
+ // Don't allow Never to match unless the constraint is also explicitly Never.
19145
+ if (!(0, types_1.isNever)(effectiveSrcType) || (0, types_1.isNever)(constraint)) {
19146
+ if (!bestConstraintSoFar || assignType(bestConstraintSoFar, constraint)) {
19147
+ bestConstraintSoFar = constraint;
19148
+ }
19004
19149
  }
19005
19150
  }
19006
19151
  }
@@ -19069,18 +19214,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
19069
19214
  else if (memberClass && (0, typeUtils_1.isInstantiableMetaclass)(memberClass)) {
19070
19215
  stripFirstParam = true;
19071
19216
  }
19072
- return partiallySpecializeFunctionForBoundClassOrObject(baseType, functionType, memberClass !== null && memberClass !== void 0 ? memberClass : types_1.ClassType.cloneAsInstantiable(baseObj), diag, recursionCount, selfType !== null && selfType !== void 0 ? selfType : baseObj, stripFirstParam);
19217
+ return partiallySpecializeBoundMethod(baseType, functionType, memberClass !== null && memberClass !== void 0 ? memberClass : types_1.ClassType.cloneAsInstantiable(baseObj), diag, recursionCount, selfType !== null && selfType !== void 0 ? selfType : baseObj, stripFirstParam);
19073
19218
  }
19074
19219
  if (types_1.FunctionType.isClassMethod(functionType) ||
19075
19220
  (treatConstructorAsClassMethod && types_1.FunctionType.isConstructorMethod(functionType))) {
19076
19221
  const baseClass = (0, types_1.isInstantiableClass)(baseType) ? baseType : types_1.ClassType.cloneAsInstantiable(baseType);
19077
19222
  const clsType = selfType ? (0, typeUtils_1.convertToInstantiable)(selfType) : undefined;
19078
- return partiallySpecializeFunctionForBoundClassOrObject(baseClass, functionType, memberClass !== null && memberClass !== void 0 ? memberClass : baseClass, diag, recursionCount, clsType !== null && clsType !== void 0 ? clsType : baseClass,
19223
+ return partiallySpecializeBoundMethod(baseClass, functionType, memberClass !== null && memberClass !== void 0 ? memberClass : baseClass, diag, recursionCount, clsType !== null && clsType !== void 0 ? clsType : baseClass,
19079
19224
  /* stripFirstParam */ true);
19080
19225
  }
19081
19226
  if (types_1.FunctionType.isStaticMethod(functionType)) {
19082
19227
  const baseClass = (0, types_1.isInstantiableClass)(baseType) ? baseType : types_1.ClassType.cloneAsInstantiable(baseType);
19083
- return partiallySpecializeFunctionForBoundClassOrObject(baseClass, functionType, memberClass !== null && memberClass !== void 0 ? memberClass : baseClass, diag, recursionCount,
19228
+ return partiallySpecializeBoundMethod(baseClass, functionType, memberClass !== null && memberClass !== void 0 ? memberClass : baseClass, diag, recursionCount,
19084
19229
  /* firstParamType */ undefined,
19085
19230
  /* stripFirstParam */ false);
19086
19231
  }
@@ -19093,7 +19238,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
19093
19238
  // is the type used to reference the member, and the memberClass
19094
19239
  // is the class that provided the member (could be an ancestor of
19095
19240
  // the baseType's class).
19096
- function partiallySpecializeFunctionForBoundClassOrObject(baseType, memberType, memberClass, diag, recursionCount, firstParamType, stripFirstParam = true) {
19241
+ function partiallySpecializeBoundMethod(baseType, memberType, memberClass, diag, recursionCount, firstParamType, stripFirstParam = true) {
19097
19242
  const typeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(memberClass));
19098
19243
  if (firstParamType && memberType.shared.parameters.length > 0) {
19099
19244
  const memberTypeFirstParam = memberType.shared.parameters[0];
@@ -19337,7 +19482,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
19337
19482
  /* initialParenDepth */ undefined, fileInfo.typingSymbolAliases);
19338
19483
  if (parseResults.parseTree && parseResults.parseTree.nodeType !== 62 /* ParseNodeType.FunctionAnnotation */) {
19339
19484
  parseResults.diagnostics.forEach((diag) => {
19340
- addError(diag.message, node);
19485
+ addDiagnosticWithSuppressionCheck('error', diag.message, node);
19341
19486
  });
19342
19487
  parseResults.parseTree.parent = node;
19343
19488
  return parseResults.parseTree;
@@ -19403,7 +19548,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
19403
19548
  removeFalsinessFromType,
19404
19549
  verifyRaiseExceptionType,
19405
19550
  verifyDeleteExpression,
19406
- validateOverloadedFunctionArguments,
19551
+ validateOverloadedArgTypes,
19407
19552
  validateInitSubclassArgs,
19408
19553
  isAfterNodeReachable,
19409
19554
  isNodeReachable,
@@ -19453,6 +19598,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
19453
19598
  getObjectType,
19454
19599
  getNoneType,
19455
19600
  getUnionClassType,
19601
+ getTypeClassType,
19456
19602
  getBuiltInObject,
19457
19603
  getTypingType,
19458
19604
  assignTypeArguments,