@zzzen/pyright-internal 1.2.0-dev.20230108 → 1.2.0-dev.20230115

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 (93) hide show
  1. package/dist/analyzer/analyzerFileInfo.js +14 -1
  2. package/dist/analyzer/analyzerFileInfo.js.map +1 -1
  3. package/dist/analyzer/backgroundAnalysisProgram.d.ts +3 -3
  4. package/dist/analyzer/backgroundAnalysisProgram.js +4 -4
  5. package/dist/analyzer/backgroundAnalysisProgram.js.map +1 -1
  6. package/dist/analyzer/checker.d.ts +1 -1
  7. package/dist/analyzer/checker.js +128 -42
  8. package/dist/analyzer/checker.js.map +1 -1
  9. package/dist/analyzer/codeFlowEngine.js +7 -7
  10. package/dist/analyzer/codeFlowEngine.js.map +1 -1
  11. package/dist/analyzer/constraintSolver.js +4 -2
  12. package/dist/analyzer/constraintSolver.js.map +1 -1
  13. package/dist/analyzer/dataClasses.js +6 -1
  14. package/dist/analyzer/dataClasses.js.map +1 -1
  15. package/dist/analyzer/program.js +13 -12
  16. package/dist/analyzer/program.js.map +1 -1
  17. package/dist/analyzer/service.d.ts +3 -2
  18. package/dist/analyzer/service.js +14 -12
  19. package/dist/analyzer/service.js.map +1 -1
  20. package/dist/analyzer/sourceFile.d.ts +3 -2
  21. package/dist/analyzer/sourceFile.js +4 -4
  22. package/dist/analyzer/sourceFile.js.map +1 -1
  23. package/dist/analyzer/typeCacheUtils.d.ts +2 -1
  24. package/dist/analyzer/typeCacheUtils.js +5 -1
  25. package/dist/analyzer/typeCacheUtils.js.map +1 -1
  26. package/dist/analyzer/typeEvaluator.js +395 -330
  27. package/dist/analyzer/typeEvaluator.js.map +1 -1
  28. package/dist/analyzer/typeEvaluatorTypes.d.ts +18 -12
  29. package/dist/analyzer/typeEvaluatorTypes.js +15 -15
  30. package/dist/analyzer/typeEvaluatorTypes.js.map +1 -1
  31. package/dist/analyzer/typeGuards.js +25 -2
  32. package/dist/analyzer/typeGuards.js.map +1 -1
  33. package/dist/analyzer/typeUtils.d.ts +7 -0
  34. package/dist/analyzer/typeUtils.js +10 -3
  35. package/dist/analyzer/typeUtils.js.map +1 -1
  36. package/dist/analyzer/types.d.ts +3 -0
  37. package/dist/analyzer/types.js +10 -2
  38. package/dist/analyzer/types.js.map +1 -1
  39. package/dist/backgroundAnalysisBase.d.ts +4 -1
  40. package/dist/backgroundAnalysisBase.js +1 -1
  41. package/dist/backgroundAnalysisBase.js.map +1 -1
  42. package/dist/backgroundThreadBase.d.ts +5 -2
  43. package/dist/backgroundThreadBase.js.map +1 -1
  44. package/dist/commands/createTypeStub.js +3 -0
  45. package/dist/commands/createTypeStub.js.map +1 -1
  46. package/dist/common/configOptions.d.ts +1 -0
  47. package/dist/common/configOptions.js +4 -0
  48. package/dist/common/configOptions.js.map +1 -1
  49. package/dist/common/diagnosticRules.d.ts +1 -0
  50. package/dist/common/diagnosticRules.js +1 -0
  51. package/dist/common/diagnosticRules.js.map +1 -1
  52. package/dist/common/fileSystem.d.ts +1 -0
  53. package/dist/common/fileSystem.js.map +1 -1
  54. package/dist/languageServerBase.js +2 -2
  55. package/dist/languageServerBase.js.map +1 -1
  56. package/dist/languageService/documentSymbolCollector.d.ts +12 -4
  57. package/dist/languageService/documentSymbolCollector.js +103 -22
  58. package/dist/languageService/documentSymbolCollector.js.map +1 -1
  59. package/dist/languageService/documentSymbolProvider.js +6 -0
  60. package/dist/languageService/documentSymbolProvider.js.map +1 -1
  61. package/dist/languageService/indentationUtils.d.ts +1 -1
  62. package/dist/languageService/indentationUtils.js +73 -28
  63. package/dist/languageService/indentationUtils.js.map +1 -1
  64. package/dist/languageService/referencesProvider.d.ts +5 -4
  65. package/dist/languageService/referencesProvider.js +15 -10
  66. package/dist/languageService/referencesProvider.js.map +1 -1
  67. package/dist/languageService/renameModuleProvider.js +11 -11
  68. package/dist/languageService/renameModuleProvider.js.map +1 -1
  69. package/dist/localization/localize.d.ts +3 -0
  70. package/dist/localization/localize.js +3 -0
  71. package/dist/localization/localize.js.map +1 -1
  72. package/dist/localization/package.nls.en-us.json +3 -0
  73. package/dist/parser/tokenizer.js +6 -2
  74. package/dist/parser/tokenizer.js.map +1 -1
  75. package/dist/tests/checker.test.js +16 -0
  76. package/dist/tests/checker.test.js.map +1 -1
  77. package/dist/tests/documentSymbolCollector.test.js +86 -5
  78. package/dist/tests/documentSymbolCollector.test.js.map +1 -1
  79. package/dist/tests/fourslash/rename.init.fourslash.d.ts +1 -0
  80. package/dist/tests/fourslash/rename.init.fourslash.js +23 -0
  81. package/dist/tests/fourslash/rename.init.fourslash.js.map +1 -0
  82. package/dist/tests/harness/fourslash/testState.d.ts +2 -2
  83. package/dist/tests/harness/fourslash/testState.js +9 -6
  84. package/dist/tests/harness/fourslash/testState.js.map +1 -1
  85. package/dist/tests/indentationUtils.ptvs.test.js +12 -12
  86. package/dist/tests/indentationUtils.ptvs.test.js.map +1 -1
  87. package/dist/tests/indentationUtils.test.js +65 -7
  88. package/dist/tests/indentationUtils.test.js.map +1 -1
  89. package/dist/tests/typeEvaluator1.test.js +8 -0
  90. package/dist/tests/typeEvaluator1.test.js.map +1 -1
  91. package/dist/tests/typeEvaluator3.test.js +6 -2
  92. package/dist/tests/typeEvaluator3.test.js.map +1 -1
  93. package/package.json +3 -3
@@ -323,7 +323,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
323
323
  }
324
324
  return cacheEntry.typeResult.type;
325
325
  }
326
- function writeTypeCache(node, typeResult, flags, expectedType, allowSpeculativeCaching = false) {
326
+ function writeTypeCache(node, typeResult, flags, inferenceContext, allowSpeculativeCaching = false) {
327
327
  // Should we use a temporary cache associated with a contextual
328
328
  // analysis of a function, contextualized based on call-site argument types?
329
329
  const typeCacheToUse = returnTypeInferenceTypeCache && isNodeInReturnTypeInferenceContext(node)
@@ -336,16 +336,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
336
336
  if (speculativeTypeTracker.isSpeculative(node)) {
337
337
  speculativeTypeTracker.trackEntry(typeCacheToUse, node.id);
338
338
  if (allowSpeculativeCaching && !typeResult.isIncomplete) {
339
- speculativeTypeTracker.addSpeculativeType(node, typeResult, expectedType);
339
+ speculativeTypeTracker.addSpeculativeType(node, typeResult, inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.expectedType);
340
340
  }
341
341
  }
342
342
  }
343
- function deleteTypeCacheEntry(node) {
344
- const typeCacheToUse = returnTypeInferenceTypeCache && isNodeInReturnTypeInferenceContext(node)
345
- ? returnTypeInferenceTypeCache
346
- : typeCache;
347
- typeCacheToUse.delete(node.id);
348
- }
349
343
  function setTypeForNode(node, type = types_1.UnknownType.create(), flags = 0 /* None */) {
350
344
  writeTypeCache(node, { type }, flags);
351
345
  }
@@ -492,8 +486,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
492
486
  typedDictClassType = getTypingType(node, '_TypedDict');
493
487
  }
494
488
  }
495
- function getTypeOfExpression(node, flags = 0 /* None */, expectedType) {
496
- var _a;
489
+ function getTypeOfExpression(node, flags = 0 /* None */, inferenceContext) {
490
+ var _a, _b;
497
491
  // Is this type already cached?
498
492
  const cacheEntry = readTypeCacheEntry(node);
499
493
  if (cacheEntry && !cacheEntry.typeResult.isIncomplete) {
@@ -504,7 +498,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
504
498
  }
505
499
  else {
506
500
  // Is it cached in the speculative type cache?
507
- const cachedTypeResult = speculativeTypeTracker.getSpeculativeType(node, expectedType);
501
+ const cachedTypeResult = speculativeTypeTracker.getSpeculativeType(node, inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.expectedType);
508
502
  if (cachedTypeResult) {
509
503
  if (printExpressionTypes) {
510
504
  console.log(`${getPrintExpressionTypesSpaces()}${ParseTreeUtils.printExpression(node)} (${getLineNum(node)}): Speculative ${printType(cachedTypeResult.type)}`);
@@ -520,14 +514,16 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
520
514
  // the cancellation check. If the operation is canceled, an exception
521
515
  // will be thrown at this point.
522
516
  checkForCancellation();
523
- expectedType = (0, typeUtils_1.transformPossibleRecursiveTypeAlias)(expectedType);
517
+ if (inferenceContext) {
518
+ inferenceContext.expectedType = (0, typeUtils_1.transformPossibleRecursiveTypeAlias)(inferenceContext.expectedType);
519
+ }
524
520
  // If we haven't already fetched some core type definitions from the
525
521
  // typeshed stubs, do so here. It would be better to fetch this when it's
526
522
  // needed in assignType, but we don't have access to the parse tree
527
523
  // at that point.
528
524
  initializedBasicTypes(node);
529
525
  let typeResult;
530
- let reportExpectingTypeErrors = (flags & 64 /* ExpectingType */) !== 0;
526
+ let reportExpectingTypeErrors = (flags & 128 /* ExpectingType */) !== 0;
531
527
  switch (node.nodeType) {
532
528
  case 38 /* Name */: {
533
529
  typeResult = getTypeOfName(node, flags);
@@ -542,11 +538,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
542
538
  break;
543
539
  }
544
540
  case 9 /* Call */: {
545
- typeResult = getTypeOfCall(node, expectedType, flags);
541
+ typeResult = getTypeOfCall(node, inferenceContext, flags);
546
542
  break;
547
543
  }
548
544
  case 52 /* Tuple */: {
549
- typeResult = getTypeOfTuple(node, expectedType, flags);
545
+ typeResult = getTypeOfTuple(node, inferenceContext, flags);
550
546
  break;
551
547
  }
552
548
  case 11 /* Constant */: {
@@ -572,20 +568,20 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
572
568
  break;
573
569
  }
574
570
  case 55 /* UnaryOperation */: {
575
- typeResult = getTypeOfUnaryOperation(node, expectedType);
571
+ typeResult = getTypeOfUnaryOperation(node, inferenceContext);
576
572
  break;
577
573
  }
578
574
  case 7 /* BinaryOperation */: {
579
- typeResult = getTypeOfBinaryOperation(node, expectedType, flags);
575
+ typeResult = getTypeOfBinaryOperation(node, inferenceContext, flags);
580
576
  break;
581
577
  }
582
578
  case 5 /* AugmentedAssignment */: {
583
- typeResult = getTypeOfAugmentedAssignment(node, expectedType);
579
+ typeResult = getTypeOfAugmentedAssignment(node, inferenceContext);
584
580
  break;
585
581
  }
586
582
  case 31 /* List */:
587
583
  case 45 /* Set */: {
588
- typeResult = getTypeOfListOrSet(node, expectedType);
584
+ typeResult = getTypeOfListOrSet(node, inferenceContext);
589
585
  break;
590
586
  }
591
587
  case 46 /* Slice */: {
@@ -593,23 +589,23 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
593
589
  break;
594
590
  }
595
591
  case 6 /* Await */: {
596
- typeResult = getTypeOfAwaitOperator(node, flags, expectedType);
592
+ typeResult = getTypeOfAwaitOperator(node, flags, inferenceContext);
597
593
  break;
598
594
  }
599
595
  case 51 /* Ternary */: {
600
- typeResult = getTypeOfTernary(node, flags, expectedType);
596
+ typeResult = getTypeOfTernary(node, flags, inferenceContext);
601
597
  break;
602
598
  }
603
599
  case 32 /* ListComprehension */: {
604
- typeResult = getTypeOfListComprehension(node, expectedType);
600
+ typeResult = getTypeOfListComprehension(node, inferenceContext);
605
601
  break;
606
602
  }
607
603
  case 15 /* Dictionary */: {
608
- typeResult = getTypeOfDictionary(node, expectedType);
604
+ typeResult = getTypeOfDictionary(node, inferenceContext);
609
605
  break;
610
606
  }
611
607
  case 30 /* Lambda */: {
612
- typeResult = getTypeOfLambda(node, expectedType);
608
+ typeResult = getTypeOfLambda(node, inferenceContext);
613
609
  break;
614
610
  }
615
611
  case 3 /* Assignment */: {
@@ -636,15 +632,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
636
632
  break;
637
633
  }
638
634
  case 56 /* Unpack */: {
639
- typeResult = getTypeOfUnpackOperator(node, flags, expectedType);
635
+ typeResult = getTypeOfUnpackOperator(node, flags, inferenceContext);
640
636
  break;
641
637
  }
642
638
  case 54 /* TypeAnnotation */: {
643
- typeResult = getTypeOfExpression(node.typeAnnotation, 64 /* ExpectingType */ |
644
- 1024 /* ExpectingTypeAnnotation */ |
639
+ typeResult = getTypeOfExpression(node.typeAnnotation, 128 /* ExpectingType */ |
640
+ 256 /* ExpectingTypeAnnotation */ |
645
641
  8 /* EvaluateStringLiteralAsType */ |
646
- 32 /* ParamSpecDisallowed */ |
647
- 128 /* TypeVarTupleDisallowed */ |
642
+ 32 /* DisallowParamSpec */ |
643
+ 64 /* DisallowTypeVarTuple */ |
648
644
  32768 /* VariableTypeAnnotation */);
649
645
  break;
650
646
  }
@@ -665,14 +661,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
665
661
  break;
666
662
  }
667
663
  default:
668
- (0, debug_1.assertNever)(node);
664
+ (0, debug_1.assertNever)(node, `Illegal node type: ${node.nodeType}`);
669
665
  }
670
666
  if (!typeResult) {
671
667
  // We shouldn't get here. If we do, report an error.
672
668
  (0, debug_1.fail)(`Unhandled expression type '${ParseTreeUtils.printExpression(node)}'`);
673
669
  }
674
670
  if (reportExpectingTypeErrors && !typeResult.isIncomplete) {
675
- if (flags & 128 /* TypeVarTupleDisallowed */) {
671
+ if (flags & 64 /* DisallowTypeVarTuple */) {
676
672
  if ((0, types_1.isVariadicTypeVar)(typeResult.type) && !typeResult.type.isVariadicInUnion) {
677
673
  addError(localize_1.Localizer.Diagnostic.typeVarTupleContext(), node);
678
674
  typeResult.type = types_1.UnknownType.create();
@@ -694,12 +690,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
694
690
  typeResult.type.details.illegalRecursionDetected = true;
695
691
  }
696
692
  }
697
- writeTypeCache(node, typeResult, flags, expectedType, /* allowSpeculativeCaching */ true);
698
- if (expectedType && !(0, types_1.isAnyOrUnknown)(expectedType) && !(0, types_1.isNever)(expectedType)) {
699
- expectedTypeCache.set(node.id, expectedType);
693
+ writeTypeCache(node, typeResult, flags, inferenceContext, /* allowSpeculativeCaching */ true);
694
+ if (inferenceContext &&
695
+ !(0, types_1.isAnyOrUnknown)(inferenceContext.expectedType) &&
696
+ !(0, types_1.isNever)(inferenceContext.expectedType)) {
697
+ expectedTypeCache.set(node.id, inferenceContext.expectedType);
700
698
  if (!typeResult.isIncomplete && !typeResult.expectedTypeDiagAddendum) {
701
699
  const diag = new diagnostic_1.DiagnosticAddendum();
702
- if (!assignType(expectedType, typeResult.type, diag)) {
700
+ // Make sure the resulting type is assignable to the expected type.
701
+ // Use the "solve for scopes" of the associated typeVarContext if
702
+ // it is provided.
703
+ if (!assignType(inferenceContext.expectedType, typeResult.type, diag, new typeVarContext_1.TypeVarContext((_b = inferenceContext.typeVarContext) === null || _b === void 0 ? void 0 : _b.getSolveForScopes()))) {
704
+ typeResult.typeErrors = true;
703
705
  typeResult.expectedTypeDiagAddendum = diag;
704
706
  diag.addTextRange(node);
705
707
  }
@@ -711,11 +713,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
711
713
  }
712
714
  return typeResult;
713
715
  }
714
- function getTypeOfAwaitOperator(node, flags, expectedType) {
715
- const effectiveExpectedType = expectedType
716
- ? createAwaitableReturnType(node, expectedType, /* isGenerator */ false)
716
+ function getTypeOfAwaitOperator(node, flags, inferenceContext) {
717
+ const effectiveExpectedType = inferenceContext
718
+ ? createAwaitableReturnType(node, inferenceContext.expectedType, /* isGenerator */ false)
717
719
  : undefined;
718
- const exprTypeResult = getTypeOfExpression(node.expression, flags, effectiveExpectedType);
720
+ const exprTypeResult = getTypeOfExpression(node.expression, flags, (0, typeUtils_1.makeInferenceContext)(effectiveExpectedType));
719
721
  const typeResult = {
720
722
  type: getTypeOfAwaitable(exprTypeResult.type, node.expression),
721
723
  };
@@ -728,7 +730,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
728
730
  if ((flags & 1 /* ConvertEllipsisToAny */) !== 0) {
729
731
  typeResult = { type: types_1.AnyType.create(/* isEllipsis */ true) };
730
732
  }
731
- else if ((flags & 256 /* ConvertEllipsisToUnknown */) !== 0) {
733
+ else if ((flags & 512 /* ConvertEllipsisToUnknown */) !== 0) {
732
734
  typeResult = { type: types_1.UnknownType.create() };
733
735
  }
734
736
  else {
@@ -749,19 +751,20 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
749
751
  }
750
752
  return typeResult;
751
753
  }
752
- function getTypeOfUnpackOperator(node, flags, expectedType) {
754
+ function getTypeOfUnpackOperator(node, flags, inferenceContext) {
753
755
  var _a;
754
756
  let typeResult;
755
757
  let iterExpectedType;
756
- if (expectedType) {
758
+ if (inferenceContext) {
757
759
  const iterableType = getBuiltInType(node, 'Iterable');
758
760
  if (iterableType && (0, types_1.isInstantiableClass)(iterableType)) {
759
- iterExpectedType = types_1.ClassType.cloneAsInstance(types_1.ClassType.cloneForSpecialization(iterableType, [expectedType], /* isTypeArgumentExplicit */ true));
761
+ iterExpectedType = types_1.ClassType.cloneAsInstance(types_1.ClassType.cloneForSpecialization(iterableType, [inferenceContext.expectedType],
762
+ /* isTypeArgumentExplicit */ true));
760
763
  }
761
764
  }
762
- const iterTypeResult = getTypeOfExpression(node.expression, flags, iterExpectedType);
765
+ const iterTypeResult = getTypeOfExpression(node.expression, flags, (0, typeUtils_1.makeInferenceContext)(iterExpectedType));
763
766
  const iterType = iterTypeResult.type;
764
- if ((flags & 128 /* TypeVarTupleDisallowed */) === 0 &&
767
+ if ((flags & 64 /* DisallowTypeVarTuple */) === 0 &&
765
768
  (0, types_1.isVariadicTypeVar)(iterType) &&
766
769
  !iterType.isVariadicUnpacked) {
767
770
  typeResult = { type: types_1.TypeVarType.cloneForUnpacked(iterType) };
@@ -789,7 +792,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
789
792
  function getTypeOfStringList(node, flags, isExpectingType) {
790
793
  let typeResult;
791
794
  if (isExpectingType) {
792
- let updatedFlags = flags | 4 /* AllowForwardReferences */ | 64 /* ExpectingType */;
795
+ let updatedFlags = flags | 4 /* AllowForwardReferences */ | 128 /* ExpectingType */;
793
796
  // In most cases, annotations within a string are not parsed by the interpreter.
794
797
  // There are a few exceptions (e.g. the "bound" value for a TypeVar constructor).
795
798
  if ((flags & 4194304 /* InterpreterParsesStringLiteral */) === 0) {
@@ -939,27 +942,27 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
939
942
  return specialType;
940
943
  }
941
944
  }
942
- let evaluatorFlags = 64 /* ExpectingType */ |
943
- 1024 /* ExpectingTypeAnnotation */ |
945
+ let evaluatorFlags = 128 /* ExpectingType */ |
946
+ 256 /* ExpectingTypeAnnotation */ |
944
947
  1 /* ConvertEllipsisToAny */ |
945
948
  8 /* EvaluateStringLiteralAsType */;
946
949
  if (options === null || options === void 0 ? void 0 : options.isVariableAnnotation) {
947
950
  evaluatorFlags |= 32768 /* VariableTypeAnnotation */;
948
951
  }
949
952
  if (!(options === null || options === void 0 ? void 0 : options.allowFinal)) {
950
- evaluatorFlags |= 16 /* FinalDisallowed */;
953
+ evaluatorFlags |= 16 /* DisallowFinal */;
951
954
  }
952
955
  if (!(options === null || options === void 0 ? void 0 : options.allowClassVar)) {
953
- evaluatorFlags |= 131072 /* ClassVarDisallowed */;
956
+ evaluatorFlags |= 131072 /* DisallowClassVar */;
954
957
  }
955
958
  if (!(options === null || options === void 0 ? void 0 : options.allowTypeVarTuple)) {
956
- evaluatorFlags |= 128 /* TypeVarTupleDisallowed */;
959
+ evaluatorFlags |= 64 /* DisallowTypeVarTuple */;
957
960
  }
958
961
  else {
959
962
  evaluatorFlags |= 2097152 /* AllowUnpackedTupleOrTypeVarTuple */;
960
963
  }
961
964
  if (!(options === null || options === void 0 ? void 0 : options.allowParamSpec)) {
962
- evaluatorFlags |= 32 /* ParamSpecDisallowed */;
965
+ evaluatorFlags |= 32 /* DisallowParamSpec */;
963
966
  }
964
967
  if (options === null || options === void 0 ? void 0 : options.associateTypeVarsWithScope) {
965
968
  evaluatorFlags |= 8192 /* AssociateTypeVarsWithCurrentScope */;
@@ -2613,7 +2616,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
2613
2616
  suppressDiagnostics(node.typeExpression, () => {
2614
2617
  callResult = validateConstructorArguments(node.typeExpression, [], concreteSubtype,
2615
2618
  /* skipUnknownArgCheck */ false,
2616
- /* expectedType */ undefined);
2619
+ /* inferenceContext */ undefined);
2617
2620
  });
2618
2621
  if (callResult && callResult.argumentErrors) {
2619
2622
  diagAddendum.addMessage(localize_1.Localizer.Diagnostic.exceptionTypeNotInstantiable().format({
@@ -2764,14 +2767,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
2764
2767
  else {
2765
2768
  // Look for the scope that contains the value definition and
2766
2769
  // see if it has a declared type.
2767
- let symbolWithScope = lookUpSymbolRecursive(node, name, !allowForwardReferences, allowForwardReferences && (flags & 1024 /* ExpectingTypeAnnotation */) !== 0);
2770
+ let symbolWithScope = lookUpSymbolRecursive(node, name, !allowForwardReferences, allowForwardReferences && (flags & 256 /* ExpectingTypeAnnotation */) !== 0);
2768
2771
  if (!symbolWithScope) {
2769
2772
  // If the node is part of a "from X import Y as Z" statement and the node
2770
2773
  // is the "Y" (non-aliased) name, we need to look up the alias symbol
2771
2774
  // since the non-aliased name is not in the symbol table.
2772
2775
  const alias = getAliasFromImport(node);
2773
2776
  if (alias) {
2774
- symbolWithScope = lookUpSymbolRecursive(alias, alias.value, !allowForwardReferences, allowForwardReferences && (flags & 1024 /* ExpectingTypeAnnotation */) !== 0);
2777
+ symbolWithScope = lookUpSymbolRecursive(alias, alias.value, !allowForwardReferences, allowForwardReferences && (flags & 256 /* ExpectingTypeAnnotation */) !== 0);
2775
2778
  }
2776
2779
  }
2777
2780
  if (symbolWithScope) {
@@ -2830,7 +2833,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
2830
2833
  }
2831
2834
  const codeFlowTypeResult = getFlowTypeOfReference(node, symbol.id, typeAtStart,
2832
2835
  /* startNode */ undefined, {
2833
- skipConditionalNarrowing: (flags & 1024 /* ExpectingTypeAnnotation */) !== 0,
2836
+ skipConditionalNarrowing: (flags & 256 /* ExpectingTypeAnnotation */) !== 0,
2834
2837
  });
2835
2838
  if (codeFlowTypeResult.type) {
2836
2839
  type = codeFlowTypeResult.type;
@@ -2850,7 +2853,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
2850
2853
  }
2851
2854
  // Detect, report, and fill in missing type arguments if appropriate.
2852
2855
  type = reportMissingTypeArguments(node, type, flags);
2853
- if ((flags & 1024 /* ExpectingTypeAnnotation */) !== 0) {
2856
+ if ((flags & 256 /* ExpectingTypeAnnotation */) !== 0) {
2854
2857
  // Verify that the name does not refer to a (non type alias) variable.
2855
2858
  if (effectiveTypeInfo.includesVariableDecl && !type.typeAliasInfo) {
2856
2859
  let isAllowedTypeForVariable = (0, types_1.isTypeVar)(type) || (0, typeUtils_1.isTypeAliasPlaceholder)(type);
@@ -2887,7 +2890,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
2887
2890
  }
2888
2891
  }
2889
2892
  if ((0, types_1.isParamSpec)(type)) {
2890
- if (flags & 32 /* ParamSpecDisallowed */) {
2893
+ if (flags & 32 /* DisallowParamSpec */) {
2891
2894
  addError(localize_1.Localizer.Diagnostic.paramSpecContext(), node);
2892
2895
  type = types_1.UnknownType.create();
2893
2896
  }
@@ -2895,7 +2898,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
2895
2898
  if ((0, types_1.isTypeVar)(type) &&
2896
2899
  !type.details.isParamSpec &&
2897
2900
  !type.isVariadicInUnion &&
2898
- (flags & 64 /* ExpectingType */) === 0 &&
2901
+ (flags & 128 /* ExpectingType */) === 0 &&
2899
2902
  type.details.name === name) {
2900
2903
  // Handle the special case of a PEP 604 union. These can appear within
2901
2904
  // an implied type alias where we are not expecting a type.
@@ -2915,8 +2918,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
2915
2918
  }
2916
2919
  }
2917
2920
  }
2918
- if ((flags & 64 /* ExpectingType */) !== 0) {
2919
- if ((flags & 512 /* AllowGenericClassType */) === 0) {
2921
+ if ((flags & 128 /* ExpectingType */) !== 0) {
2922
+ if ((flags & 1024 /* AllowGenericClassType */) === 0) {
2920
2923
  if ((0, types_1.isInstantiableClass)(type) && types_1.ClassType.isBuiltIn(type, 'Generic')) {
2921
2924
  addDiagnostic(AnalyzerNodeInfo.getFileInfo(node).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.genericNotAllowed(), node);
2922
2925
  }
@@ -3089,7 +3092,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3089
3092
  }
3090
3093
  // Is this a generic class that needs to be specialized?
3091
3094
  if ((0, types_1.isInstantiableClass)(type)) {
3092
- if ((flags & 64 /* ExpectingType */) !== 0) {
3095
+ if ((flags & 128 /* ExpectingType */) !== 0) {
3093
3096
  if ((0, typeUtils_1.requiresTypeArguments)(type) && !type.typeArguments) {
3094
3097
  addDiagnostic(AnalyzerNodeInfo.getFileInfo(node).diagnosticRuleSet.reportMissingTypeArgument, diagnosticRules_1.DiagnosticRule.reportMissingTypeArgument, localize_1.Localizer.Diagnostic.typeArgsMissingForClass().format({
3095
3098
  name: type.aliasName || type.details.name,
@@ -3101,7 +3104,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3101
3104
  }
3102
3105
  }
3103
3106
  // Is this a generic type alias that needs to be specialized?
3104
- if ((flags & 64 /* ExpectingType */) !== 0 &&
3107
+ if ((flags & 128 /* ExpectingType */) !== 0 &&
3105
3108
  type.typeAliasInfo &&
3106
3109
  type.typeAliasInfo.typeParameters &&
3107
3110
  type.typeAliasInfo.typeParameters.length > 0 &&
@@ -3255,7 +3258,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3255
3258
  function getTypeOfMemberAccess(node, flags) {
3256
3259
  const baseTypeFlags = 2 /* DoNotSpecialize */ |
3257
3260
  (flags &
3258
- (1024 /* ExpectingTypeAnnotation */ |
3261
+ (256 /* ExpectingTypeAnnotation */ |
3259
3262
  32768 /* VariableTypeAnnotation */ |
3260
3263
  4 /* AllowForwardReferences */ |
3261
3264
  524288 /* NotParsedByInterpreter */ |
@@ -3296,7 +3299,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3296
3299
  const codeFlowTypeResult = getFlowTypeOfReference(node, symbol_1.indeterminateSymbolId, typeAtStart,
3297
3300
  /* startNode */ undefined, {
3298
3301
  isTypeAtStartIncomplete,
3299
- skipConditionalNarrowing: (flags & 1024 /* ExpectingTypeAnnotation */) !== 0,
3302
+ skipConditionalNarrowing: (flags & 256 /* ExpectingTypeAnnotation */) !== 0,
3300
3303
  });
3301
3304
  if (codeFlowTypeResult.type) {
3302
3305
  typeResult.type = codeFlowTypeResult.type;
@@ -3306,8 +3309,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3306
3309
  }
3307
3310
  // Detect, report, and fill in missing type arguments if appropriate.
3308
3311
  typeResult.type = reportMissingTypeArguments(node, typeResult.type, flags);
3309
- deleteTypeCacheEntry(node);
3310
- deleteTypeCacheEntry(node.memberName);
3311
3312
  }
3312
3313
  if (baseTypeResult.isIncomplete) {
3313
3314
  typeResult.isIncomplete = true;
@@ -3381,7 +3382,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3381
3382
  }
3382
3383
  return { type: types_1.UnknownType.create(isIncomplete), isIncomplete };
3383
3384
  }
3384
- if (flags & 64 /* ExpectingType */) {
3385
+ if (flags & 128 /* ExpectingType */) {
3385
3386
  if (!isIncomplete) {
3386
3387
  addDiagnostic(AnalyzerNodeInfo.getFileInfo(node).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeVarNoMember().format({
3387
3388
  type: printType(baseType),
@@ -4141,7 +4142,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4141
4142
  // If this is meant to be a type and the base expression is a string expression,
4142
4143
  // emit an error because this will generate a runtime exception in Python versions
4143
4144
  // less than 3.10.
4144
- if (flags & 64 /* ExpectingType */) {
4145
+ if (flags & 128 /* ExpectingType */) {
4145
4146
  if (node.baseExpression.nodeType === 48 /* StringList */) {
4146
4147
  const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
4147
4148
  if (!fileInfo.isStubFile && fileInfo.executionEnvironment.pythonVersion < pythonVersion_1.PythonVersion.V3_10) {
@@ -4197,7 +4198,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4197
4198
  const codeFlowTypeResult = getFlowTypeOfReference(node, symbol_1.indeterminateSymbolId, indexTypeResult.type,
4198
4199
  /* startNode */ undefined, {
4199
4200
  isTypeAtStartIncomplete: !!baseTypeResult.isIncomplete || !!indexTypeResult.isIncomplete,
4200
- skipConditionalNarrowing: (flags & 1024 /* ExpectingTypeAnnotation */) !== 0,
4201
+ skipConditionalNarrowing: (flags & 256 /* ExpectingTypeAnnotation */) !== 0,
4201
4202
  });
4202
4203
  if (codeFlowTypeResult.type) {
4203
4204
  indexTypeResult.type = codeFlowTypeResult.type;
@@ -4205,14 +4206,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4205
4206
  if (codeFlowTypeResult.isIncomplete) {
4206
4207
  indexTypeResult.isIncomplete = true;
4207
4208
  }
4208
- deleteTypeCacheEntry(node);
4209
4209
  }
4210
4210
  }
4211
4211
  if (baseTypeResult.isIncomplete) {
4212
4212
  indexTypeResult.isIncomplete = true;
4213
4213
  }
4214
4214
  // Handle "Required" and "NotRequired" specially.
4215
- if ((flags & 1048576 /* RequiredAllowed */) !== 0) {
4215
+ if ((flags & 1048576 /* AllowRequired */) !== 0) {
4216
4216
  if ((0, types_1.isInstantiableClass)(baseTypeResult.type)) {
4217
4217
  if (types_1.ClassType.isBuiltIn(baseTypeResult.type, 'Required')) {
4218
4218
  indexTypeResult.isRequired = true;
@@ -4454,7 +4454,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4454
4454
  if ((0, types_1.isAnyOrUnknown)(concreteSubtype)) {
4455
4455
  return concreteSubtype;
4456
4456
  }
4457
- if (flags & 64 /* ExpectingType */) {
4457
+ if (flags & 128 /* ExpectingType */) {
4458
4458
  if ((0, types_1.isTypeVar)(unexpandedSubtype)) {
4459
4459
  addDiagnostic(AnalyzerNodeInfo.getFileInfo(node).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeVarNotSubscriptable().format({
4460
4460
  type: printType(unexpandedSubtype),
@@ -4790,16 +4790,16 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4790
4790
  const typeArgs = [];
4791
4791
  let adjFlags = flags;
4792
4792
  if (isFinalAnnotation || isClassVarAnnotation) {
4793
- adjFlags |= 131072 /* ClassVarDisallowed */ | 16 /* FinalDisallowed */;
4793
+ adjFlags |= 131072 /* DisallowClassVar */ | 16 /* DisallowFinal */;
4794
4794
  }
4795
4795
  else {
4796
4796
  adjFlags &= ~(2 /* DoNotSpecialize */ |
4797
- 32 /* ParamSpecDisallowed */ |
4798
- 128 /* TypeVarTupleDisallowed */ |
4799
- 1048576 /* RequiredAllowed */ |
4797
+ 32 /* DisallowParamSpec */ |
4798
+ 64 /* DisallowTypeVarTuple */ |
4799
+ 1048576 /* AllowRequired */ |
4800
4800
  16384 /* EnforceTypeVarVarianceConsistency */);
4801
4801
  if (!isAnnotatedClass) {
4802
- adjFlags |= 131072 /* ClassVarDisallowed */ | 16 /* FinalDisallowed */;
4802
+ adjFlags |= 131072 /* DisallowClassVar */ | 16 /* DisallowFinal */;
4803
4803
  }
4804
4804
  adjFlags |= 2097152 /* AllowUnpackedTupleOrTypeVarTuple */;
4805
4805
  }
@@ -4811,10 +4811,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4811
4811
  // should be treated as a type. The others can be regular (non-type) objects.
4812
4812
  if (hasCustomClassGetItem || (isAnnotatedClass && argIndex > 0)) {
4813
4813
  typeResult = {
4814
- ...getTypeOfExpression(expr, 32 /* ParamSpecDisallowed */ |
4815
- 128 /* TypeVarTupleDisallowed */ |
4814
+ ...getTypeOfExpression(expr, 32 /* DisallowParamSpec */ |
4815
+ 64 /* DisallowTypeVarTuple */ |
4816
4816
  2 /* DoNotSpecialize */ |
4817
- 131072 /* ClassVarDisallowed */),
4817
+ 131072 /* DisallowClassVar */),
4818
4818
  node: expr,
4819
4819
  };
4820
4820
  }
@@ -4861,7 +4861,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4861
4861
  function getTypeArg(node, flags) {
4862
4862
  let typeResult;
4863
4863
  let adjustedFlags = flags |
4864
- 64 /* ExpectingType */ |
4864
+ 128 /* ExpectingType */ |
4865
4865
  1 /* ConvertEllipsisToAny */ |
4866
4866
  8 /* EvaluateStringLiteralAsType */;
4867
4867
  const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
@@ -4885,7 +4885,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4885
4885
  if ((0, types_1.isClass)(typeResult.type) && types_1.ClassType.isBuiltIn(typeResult.type, 'Protocol')) {
4886
4886
  addError(localize_1.Localizer.Diagnostic.protocolNotAllowedInTypeArgument(), node);
4887
4887
  }
4888
- if ((flags & 131072 /* ClassVarDisallowed */) !== 0) {
4888
+ if ((flags & 131072 /* DisallowClassVar */) !== 0) {
4889
4889
  // "ClassVar" is not allowed as a type argument.
4890
4890
  if ((0, types_1.isClass)(typeResult.type) && types_1.ClassType.isBuiltIn(typeResult.type, 'ClassVar')) {
4891
4891
  addError(localize_1.Localizer.Diagnostic.classVarNotAllowed(), node);
@@ -4894,23 +4894,23 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4894
4894
  }
4895
4895
  return typeResult;
4896
4896
  }
4897
- function getTypeOfTuple(node, expectedType, flags) {
4898
- if ((flags & 64 /* ExpectingType */) !== 0 && node.expressions.length === 0 && !expectedType) {
4897
+ function getTypeOfTuple(node, inferenceContext, flags) {
4898
+ if ((flags & 128 /* ExpectingType */) !== 0 && node.expressions.length === 0 && !inferenceContext) {
4899
4899
  return { type: makeTupleObject([]), isEmptyTupleShorthand: true };
4900
4900
  }
4901
4901
  // If the expected type is a union, recursively call for each of the subtypes
4902
4902
  // to find one that matches.
4903
- let effectiveExpectedType = expectedType;
4904
- let expectedTypeContainsAny = expectedType && (0, types_1.isAny)(expectedType);
4905
- if (expectedType && (0, types_1.isUnion)(expectedType)) {
4903
+ let effectiveExpectedType = inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.expectedType;
4904
+ let expectedTypeContainsAny = inferenceContext && (0, types_1.isAny)(inferenceContext.expectedType);
4905
+ if (inferenceContext && (0, types_1.isUnion)(inferenceContext.expectedType)) {
4906
4906
  let matchingSubtype;
4907
- (0, typeUtils_1.doForEachSubtype)(expectedType, (subtype) => {
4907
+ (0, typeUtils_1.doForEachSubtype)(inferenceContext.expectedType, (subtype) => {
4908
4908
  if ((0, types_1.isAny)(subtype)) {
4909
4909
  expectedTypeContainsAny = true;
4910
4910
  }
4911
4911
  if (!matchingSubtype) {
4912
4912
  const subtypeResult = useSpeculativeMode(node, () => {
4913
- return getTypeOfTupleExpected(node, subtype);
4913
+ return getTypeOfTupleWithContext(node, (0, typeUtils_1.makeInferenceContext)(subtype, inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.typeVarContext));
4914
4914
  });
4915
4915
  if (subtypeResult && assignType(subtype, subtypeResult.type)) {
4916
4916
  matchingSubtype = subtype;
@@ -4921,7 +4921,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4921
4921
  }
4922
4922
  let expectedTypeDiagAddendum;
4923
4923
  if (effectiveExpectedType) {
4924
- const result = getTypeOfTupleExpected(node, effectiveExpectedType);
4924
+ const result = getTypeOfTupleWithContext(node, (0, typeUtils_1.makeInferenceContext)(effectiveExpectedType, inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.typeVarContext));
4925
4925
  if (result && !result.typeErrors) {
4926
4926
  return result;
4927
4927
  }
@@ -4935,9 +4935,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4935
4935
  }
4936
4936
  return { ...typeResult, expectedTypeDiagAddendum };
4937
4937
  }
4938
- function getTypeOfTupleExpected(node, expectedType) {
4939
- expectedType = (0, typeUtils_1.transformPossibleRecursiveTypeAlias)(expectedType);
4940
- if (!(0, types_1.isClassInstance)(expectedType)) {
4938
+ function getTypeOfTupleWithContext(node, inferenceContext) {
4939
+ inferenceContext.expectedType = (0, typeUtils_1.transformPossibleRecursiveTypeAlias)(inferenceContext.expectedType);
4940
+ if (!(0, types_1.isClassInstance)(inferenceContext.expectedType)) {
4941
4941
  return undefined;
4942
4942
  }
4943
4943
  if (!tupleClassType || !(0, types_1.isInstantiableClass)(tupleClassType)) {
@@ -4945,9 +4945,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4945
4945
  }
4946
4946
  // Build an array of expected types.
4947
4947
  let expectedTypes = [];
4948
- if ((0, typeUtils_1.isTupleClass)(expectedType) && expectedType.tupleTypeArguments) {
4949
- expectedTypes = expectedType.tupleTypeArguments.map((t) => (0, typeUtils_1.transformPossibleRecursiveTypeAlias)(t.type));
4950
- const unboundedIndex = expectedType.tupleTypeArguments.findIndex((t) => t.isUnbounded);
4948
+ if ((0, typeUtils_1.isTupleClass)(inferenceContext.expectedType) && inferenceContext.expectedType.tupleTypeArguments) {
4949
+ expectedTypes = inferenceContext.expectedType.tupleTypeArguments.map((t) => (0, typeUtils_1.transformPossibleRecursiveTypeAlias)(t.type));
4950
+ const unboundedIndex = inferenceContext.expectedType.tupleTypeArguments.findIndex((t) => t.isUnbounded);
4951
4951
  if (unboundedIndex >= 0) {
4952
4952
  if (expectedTypes.length > node.expressions.length) {
4953
4953
  expectedTypes.splice(unboundedIndex, 1);
@@ -4961,7 +4961,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4961
4961
  }
4962
4962
  else {
4963
4963
  const tupleTypeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(tupleClassType));
4964
- if (!(0, constraintSolver_1.populateTypeVarContextBasedOnExpectedType)(evaluatorInterface, types_1.ClassType.cloneAsInstance(tupleClassType), expectedType, tupleTypeVarContext, getTypeVarScopesForNode(node))) {
4964
+ if (!(0, constraintSolver_1.populateTypeVarContextBasedOnExpectedType)(evaluatorInterface, types_1.ClassType.cloneAsInstance(tupleClassType), inferenceContext.expectedType, tupleTypeVarContext, getTypeVarScopesForNode(node))) {
4965
4965
  return undefined;
4966
4966
  }
4967
4967
  const specializedTuple = (0, typeUtils_1.applySolvedTypeVars)(tupleClassType, tupleTypeVarContext);
@@ -4974,7 +4974,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4974
4974
  }
4975
4975
  }
4976
4976
  const entryTypeResults = node.expressions.map((expr, index) => getTypeOfExpression(expr,
4977
- /* flags */ undefined, index < expectedTypes.length ? expectedTypes[index] : undefined));
4977
+ /* flags */ undefined, (0, typeUtils_1.makeInferenceContext)(index < expectedTypes.length ? expectedTypes[index] : undefined)));
4978
4978
  const isIncomplete = entryTypeResults.some((result) => result.isIncomplete);
4979
4979
  const type = (0, typeUtils_1.convertToInstance)((0, typeUtils_1.specializeTupleClass)(tupleClassType, buildTupleTypesList(entryTypeResults),
4980
4980
  /* isTypeArgumentExplicit */ true));
@@ -5041,7 +5041,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5041
5041
  }
5042
5042
  return entryTypes;
5043
5043
  }
5044
- function getTypeOfCall(node, expectedType, flags) {
5044
+ function getTypeOfCall(node, inferenceContext, flags) {
5045
5045
  var _a;
5046
5046
  const baseTypeResult = getTypeOfExpression(node.leftExpression, 2 /* DoNotSpecialize */);
5047
5047
  const argList = node.arguments.map((arg) => {
@@ -5063,15 +5063,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5063
5063
  node.leftExpression.nodeType === 38 /* Name */ &&
5064
5064
  node.leftExpression.value === 'reveal_type') {
5065
5065
  // Handle the implicit "reveal_type" call.
5066
- typeResult = getTypeOfRevealType(node, expectedType);
5066
+ typeResult = getTypeOfRevealType(node, inferenceContext);
5067
5067
  }
5068
5068
  else if ((0, types_1.isFunction)(baseTypeResult.type) && baseTypeResult.type.details.builtInName === 'reveal_type') {
5069
5069
  // Handle the "typing.reveal_type" call.
5070
- typeResult = getTypeOfRevealType(node, expectedType);
5070
+ typeResult = getTypeOfRevealType(node, inferenceContext);
5071
5071
  }
5072
5072
  else if ((0, types_1.isFunction)(baseTypeResult.type) && baseTypeResult.type.details.builtInName === 'assert_type') {
5073
5073
  // Handle the "typing.assert_type" call.
5074
- typeResult = getTypeOfAssertType(node, expectedType);
5074
+ typeResult = getTypeOfAssertType(node, inferenceContext);
5075
5075
  }
5076
5076
  else if ((0, types_1.isAnyOrUnknown)(baseTypeResult.type) &&
5077
5077
  node.leftExpression.nodeType === 38 /* Name */ &&
@@ -5087,11 +5087,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5087
5087
  else {
5088
5088
  const callResult = validateCallArguments(node, argList, baseTypeResult,
5089
5089
  /* typeVarContext */ undefined,
5090
- /* skipUnknownArgCheck */ false, expectedType);
5090
+ /* skipUnknownArgCheck */ false, inferenceContext);
5091
5091
  typeResult.type = (_a = callResult.returnType) !== null && _a !== void 0 ? _a : types_1.UnknownType.create();
5092
5092
  if (callResult.argumentErrors) {
5093
5093
  typeResult.typeErrors = true;
5094
5094
  }
5095
+ else {
5096
+ typeResult.overloadsUsedForCall = callResult.overloadsUsedForCall;
5097
+ }
5095
5098
  if (callResult.isTypeIncomplete) {
5096
5099
  typeResult.isIncomplete = true;
5097
5100
  }
@@ -5125,13 +5128,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5125
5128
  });
5126
5129
  }
5127
5130
  }
5128
- if ((flags & 1024 /* ExpectingTypeAnnotation */) !== 0) {
5131
+ if ((flags & 256 /* ExpectingTypeAnnotation */) !== 0) {
5129
5132
  addDiagnostic(AnalyzerNodeInfo.getFileInfo(node).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeAnnotationCall(), node);
5130
5133
  typeResult = { type: types_1.UnknownType.create() };
5131
5134
  }
5132
5135
  return typeResult;
5133
5136
  }
5134
- function getTypeOfAssertType(node, expectedType) {
5137
+ function getTypeOfAssertType(node, inferenceContext) {
5135
5138
  if (node.arguments.length !== 2 ||
5136
5139
  node.arguments[0].argumentCategory !== 0 /* Simple */ ||
5137
5140
  node.arguments[0].name !== undefined ||
@@ -5141,7 +5144,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5141
5144
  return { type: types_1.UnknownType.create() };
5142
5145
  }
5143
5146
  const arg0TypeResult = getTypeOfExpression(node.arguments[0].valueExpression,
5144
- /* flags */ undefined, expectedType);
5147
+ /* flags */ undefined, inferenceContext);
5145
5148
  if (arg0TypeResult.isIncomplete) {
5146
5149
  return { type: types_1.UnknownType.create(/* isIncomplete */ true), isIncomplete: true };
5147
5150
  }
@@ -5154,7 +5157,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5154
5157
  }
5155
5158
  return { type: arg0TypeResult.type };
5156
5159
  }
5157
- function getTypeOfRevealType(node, expectedType) {
5160
+ function getTypeOfRevealType(node, inferenceContext) {
5158
5161
  let arg0Value;
5159
5162
  let expectedRevealTypeNode;
5160
5163
  let expectedRevealType;
@@ -5191,7 +5194,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5191
5194
  addError(localize_1.Localizer.Diagnostic.revealTypeArgs(), node);
5192
5195
  return { type: types_1.UnknownType.create() };
5193
5196
  }
5194
- const typeResult = getTypeOfExpression(arg0Value, /* flags */ undefined, expectedType);
5197
+ const typeResult = getTypeOfExpression(arg0Value, /* flags */ undefined, inferenceContext);
5195
5198
  const type = typeResult.type;
5196
5199
  const exprString = ParseTreeUtils.printExpression(arg0Value);
5197
5200
  const typeString = printType(type, { expandTypeAlias: true });
@@ -5395,11 +5398,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5395
5398
  // there will be only one argument list in expandedArgTypes, and all entries
5396
5399
  // (one for each argument) will be undefined. On subsequent calls, this
5397
5400
  // list will grow to include union expansions.
5398
- function validateOverloadsWithExpandedTypes(errorNode, expandedArgTypes, argParamMatches, typeVarContext, skipUnknownArgCheck, expectedType) {
5401
+ function validateOverloadsWithExpandedTypes(errorNode, expandedArgTypes, argParamMatches, typeVarContext, skipUnknownArgCheck, inferenceContext) {
5399
5402
  var _a;
5400
5403
  const returnTypes = [];
5401
5404
  const matchedOverloads = [];
5402
5405
  let isTypeIncomplete = false;
5406
+ const overloadsUsedForCall = [];
5403
5407
  for (let expandedTypesIndex = 0; expandedTypesIndex < expandedArgTypes.length; expandedTypesIndex++) {
5404
5408
  let matchedOverload;
5405
5409
  const argTypeOverride = expandedArgTypes[expandedTypesIndex];
@@ -5433,12 +5437,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5433
5437
  // record any final types in the type cache.
5434
5438
  const callResult = useSpeculativeMode(errorNode, () => {
5435
5439
  return validateFunctionArgumentTypesWithExpectedType(errorNode, matchResults, effectiveTypeVarContext,
5436
- /* skipUnknownArgCheck */ true, expectedType);
5440
+ /* skipUnknownArgCheck */ true, inferenceContext);
5437
5441
  });
5438
5442
  if (callResult.isTypeIncomplete) {
5439
5443
  isTypeIncomplete = true;
5440
5444
  }
5441
5445
  if (!callResult.argumentErrors && callResult.returnType) {
5446
+ overloadsUsedForCall.push(overload);
5442
5447
  matchedOverload = overload;
5443
5448
  matchedOverloads.push({
5444
5449
  overload: matchedOverload,
@@ -5484,7 +5489,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5484
5489
  returnTypes.push(dedupedMatchResults.length > 1 ? types_1.UnknownType.createPossibleType(combinedTypes) : combinedTypes);
5485
5490
  }
5486
5491
  if (!matchedOverload) {
5487
- return { argumentErrors: true, isTypeIncomplete };
5492
+ return { argumentErrors: true, isTypeIncomplete, overloadsUsedForCall };
5488
5493
  }
5489
5494
  }
5490
5495
  // We found a match for all of the expanded argument lists. Copy the
@@ -5499,7 +5504,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5499
5504
  const finalTypeVarContext = typeVarContext !== null && typeVarContext !== void 0 ? typeVarContext : matchedOverloads[0].typeVarContext;
5500
5505
  finalTypeVarContext.unlock();
5501
5506
  finalTypeVarContext.addSolveForScope((0, typeUtils_1.getTypeVarScopeId)(matchedOverloads[0].overload));
5502
- const finalCallResult = validateFunctionArgumentTypesWithExpectedType(errorNode, matchedOverloads[0].matchResults, finalTypeVarContext, skipUnknownArgCheck, expectedType);
5507
+ const finalCallResult = validateFunctionArgumentTypesWithExpectedType(errorNode, matchedOverloads[0].matchResults, finalTypeVarContext, skipUnknownArgCheck, inferenceContext);
5503
5508
  if (finalCallResult.isTypeIncomplete) {
5504
5509
  isTypeIncomplete = true;
5505
5510
  }
@@ -5509,6 +5514,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5509
5514
  returnType: (0, types_1.combineTypes)(returnTypes),
5510
5515
  isTypeIncomplete,
5511
5516
  specializedInitSelfType: finalCallResult.specializedInitSelfType,
5517
+ overloadsUsedForCall,
5512
5518
  };
5513
5519
  }
5514
5520
  function getBestOverloadForArguments(errorNode, type, argList) {
@@ -5548,7 +5554,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5548
5554
  return a.overloadIndex - b.overloadIndex;
5549
5555
  });
5550
5556
  }
5551
- function validateOverloadedFunctionArguments(errorNode, argList, type, typeVarContext, skipUnknownArgCheck, expectedType) {
5557
+ function validateOverloadedFunctionArguments(errorNode, argList, type, typeVarContext, skipUnknownArgCheck, inferenceContext) {
5552
5558
  let filteredMatchResults = [];
5553
5559
  let contextFreeArgTypes;
5554
5560
  // Start by evaluating the types of the arguments without any expected
@@ -5582,7 +5588,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5582
5588
  diagAddendum.addMessage(localize_1.Localizer.DiagnosticAddendum.argumentTypes().format({ types: argTypes.join(', ') }));
5583
5589
  addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.noOverload().format({ name: functionName }) + diagAddendum.getString(), errorNode);
5584
5590
  }
5585
- return { argumentErrors: true, isTypeIncomplete: false };
5591
+ return { argumentErrors: true, isTypeIncomplete: false, overloadsUsedForCall: [] };
5586
5592
  }
5587
5593
  // Create a helper lambda that evaluates the overload that matches
5588
5594
  // the arg/param lists.
@@ -5595,7 +5601,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5595
5601
  const effectiveTypeVarContext = typeVarContext !== null && typeVarContext !== void 0 ? typeVarContext : new typeVarContext_1.TypeVarContext();
5596
5602
  effectiveTypeVarContext.addSolveForScope((0, typeUtils_1.getTypeVarScopeId)(lastMatch.overload));
5597
5603
  effectiveTypeVarContext.unlock();
5598
- return validateFunctionArgumentTypesWithExpectedType(errorNode, lastMatch, effectiveTypeVarContext, skipUnknownArgCheck, expectedType);
5604
+ return validateFunctionArgumentTypesWithExpectedType(errorNode, lastMatch, effectiveTypeVarContext, skipUnknownArgCheck, inferenceContext);
5599
5605
  };
5600
5606
  // If there is only one possible arg/param match among the overloads,
5601
5607
  // use the normal type matching mechanism because it is faster and
@@ -5606,7 +5612,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5606
5612
  let expandedArgTypes = [argList.map((arg) => undefined)];
5607
5613
  let isTypeIncomplete = false;
5608
5614
  while (true) {
5609
- const callResult = validateOverloadsWithExpandedTypes(errorNode, expandedArgTypes, filteredMatchResults, typeVarContext, skipUnknownArgCheck, expectedType);
5615
+ const callResult = validateOverloadsWithExpandedTypes(errorNode, expandedArgTypes, filteredMatchResults, typeVarContext, skipUnknownArgCheck, inferenceContext);
5610
5616
  if (callResult.isTypeIncomplete) {
5611
5617
  isTypeIncomplete = true;
5612
5618
  }
@@ -5618,7 +5624,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5618
5624
  if (!contextFreeArgTypes) {
5619
5625
  useSpeculativeMode(errorNode, () => {
5620
5626
  // Evaluate the types of each argument expression without regard to
5621
- // the expectedType. We'll use this to determine whether we need to do
5627
+ // the context. We'll use this to determine whether we need to do
5622
5628
  // union expansion.
5623
5629
  contextFreeArgTypes = argList.map((arg) => {
5624
5630
  if (arg.typeResult) {
@@ -5650,7 +5656,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5650
5656
  result.returnType = types_1.UnknownType.create();
5651
5657
  return { ...result, argumentErrors: true };
5652
5658
  }
5653
- return { argumentErrors: true, isTypeIncomplete: false };
5659
+ return { argumentErrors: true, isTypeIncomplete: false, overloadsUsedForCall: [] };
5654
5660
  }
5655
5661
  // Replaces each item in the expandedArgTypes with n items where n is
5656
5662
  // the number of subtypes in a union. The contextFreeArgTypes parameter
@@ -5702,13 +5708,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5702
5708
  // If successful, it returns the resulting (specialized) object type that
5703
5709
  // is allocated by the constructor. If unsuccessful, it records diagnostic
5704
5710
  // information and returns undefined.
5705
- function validateConstructorArguments(errorNode, argList, type, skipUnknownArgCheck, expectedType) {
5711
+ function validateConstructorArguments(errorNode, argList, type, skipUnknownArgCheck, inferenceContext) {
5706
5712
  var _a;
5707
5713
  let validatedTypes = false;
5708
5714
  let returnType;
5709
5715
  let reportedErrors = false;
5710
5716
  let isTypeIncomplete = false;
5711
5717
  let usedMetaclassCallMethod = false;
5718
+ const overloadsUsedForCall = [];
5712
5719
  // Create a helper function that determines whether we should skip argument
5713
5720
  // validation for either __init__ or __new__. This is required for certain
5714
5721
  // synthesized constructor types, namely NamedTuples.
@@ -5726,13 +5733,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5726
5733
  // If there is an expected type, analyze the constructor call
5727
5734
  // for each of the subtypes that comprise the expected type. If
5728
5735
  // one or more analyzes with no errors, use those results.
5729
- if (expectedType) {
5730
- const expectedCallResult = validateConstructorMethodWithExpectedType(errorNode, argList, type, skipUnknownArgCheck, expectedType, initMethodType);
5736
+ if (inferenceContext) {
5737
+ const expectedCallResult = validateConstructorMethodWithExpectedType(errorNode, argList, type, skipUnknownArgCheck, inferenceContext, initMethodType);
5731
5738
  if (expectedCallResult) {
5732
5739
  returnType = expectedCallResult.returnType;
5733
5740
  if (expectedCallResult.isTypeIncomplete) {
5734
5741
  isTypeIncomplete = true;
5735
5742
  }
5743
+ overloadsUsedForCall.push(...expectedCallResult.overloadsUsedForCall);
5736
5744
  }
5737
5745
  }
5738
5746
  if (!returnType) {
@@ -5749,10 +5757,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5749
5757
  adjustedClassType = types_1.ClassType.cloneAsInstantiable(callResult.specializedInitSelfType);
5750
5758
  }
5751
5759
  returnType = applyExpectedTypeForConstructor(adjustedClassType,
5752
- /* expectedType */ undefined, typeVarContext);
5760
+ /* inferenceContext */ undefined, typeVarContext);
5753
5761
  if (callResult.isTypeIncomplete) {
5754
5762
  isTypeIncomplete = true;
5755
5763
  }
5764
+ overloadsUsedForCall.push(...callResult.overloadsUsedForCall);
5756
5765
  }
5757
5766
  else {
5758
5767
  reportedErrors = true;
@@ -5790,8 +5799,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5790
5799
  const constructorMethodType = constructorMethodInfo.type;
5791
5800
  // If there is an expected type that was not applied above when
5792
5801
  // handling the __init__ method, try to apply it with the __new__ method.
5793
- if (expectedType && !returnType) {
5794
- const expectedCallResult = validateConstructorMethodWithExpectedType(errorNode, argList, type, skipUnknownArgCheck, expectedType, constructorMethodType);
5802
+ if (inferenceContext && !returnType) {
5803
+ const expectedCallResult = validateConstructorMethodWithExpectedType(errorNode, argList, type, skipUnknownArgCheck, inferenceContext, constructorMethodType);
5795
5804
  if (expectedCallResult) {
5796
5805
  returnType = expectedCallResult.returnType;
5797
5806
  if (expectedCallResult.isTypeIncomplete) {
@@ -5806,14 +5815,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5806
5815
  typeVarContext.addSolveForScope((0, typeUtils_1.getTypeVarScopeId)(constructorMethodType));
5807
5816
  // Skip the unknown argument check if we've already checked for __init__.
5808
5817
  const callResult = validateCallArguments(errorNode, argList, constructorMethodInfo, typeVarContext, skipUnknownArgCheck);
5818
+ if (callResult.isTypeIncomplete) {
5819
+ isTypeIncomplete = true;
5820
+ }
5809
5821
  if (callResult.argumentErrors) {
5810
5822
  reportedErrors = true;
5811
5823
  }
5812
5824
  else {
5813
5825
  let newReturnType = callResult.returnType;
5814
- if (callResult.isTypeIncomplete) {
5815
- isTypeIncomplete = true;
5816
- }
5817
5826
  // If the constructor returned an object whose type matches the class of
5818
5827
  // the original type being constructed, use the return type in case it was
5819
5828
  // specialized. If it doesn't match, we'll fall back on the assumption that
@@ -5848,10 +5857,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5848
5857
  }
5849
5858
  }
5850
5859
  if (!returnType) {
5851
- returnType = applyExpectedTypeForConstructor(type, expectedType, typeVarContext);
5860
+ returnType = applyExpectedTypeForConstructor(type, inferenceContext, typeVarContext);
5852
5861
  }
5853
5862
  else if ((0, types_1.isClassInstance)(returnType) && (0, typeUtils_1.isTupleClass)(returnType) && !returnType.tupleTypeArguments) {
5854
- returnType = applyExpectedTypeForTupleConstructor(returnType, expectedType);
5863
+ returnType = applyExpectedTypeForTupleConstructor(returnType, inferenceContext);
5855
5864
  }
5856
5865
  validatedTypes = true;
5857
5866
  }
@@ -5880,7 +5889,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5880
5889
  if (!returnType) {
5881
5890
  // There was no __init__ or __new__ method or we couldn't match the provided
5882
5891
  // arguments to them.
5883
- if (!expectedType && type.typeArguments) {
5892
+ if (!inferenceContext && type.typeArguments) {
5884
5893
  // If there was no expected type but the type was already specialized,
5885
5894
  // assume that we're constructing an instance of the specialized type.
5886
5895
  returnType = (0, typeUtils_1.convertToInstance)(type);
@@ -5889,10 +5898,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5889
5898
  // Do our best to specialize the instantiated class based on the expected
5890
5899
  // type if provided.
5891
5900
  const typeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(type));
5892
- if (expectedType) {
5893
- (0, constraintSolver_1.populateTypeVarContextBasedOnExpectedType)(evaluatorInterface, types_1.ClassType.cloneAsInstance(type), expectedType, typeVarContext, getTypeVarScopesForNode(errorNode));
5901
+ if (inferenceContext) {
5902
+ (0, constraintSolver_1.populateTypeVarContextBasedOnExpectedType)(evaluatorInterface, types_1.ClassType.cloneAsInstance(type), inferenceContext.expectedType, typeVarContext, getTypeVarScopesForNode(errorNode));
5894
5903
  }
5895
- returnType = applyExpectedTypeForConstructor(type, expectedType, typeVarContext);
5904
+ returnType = applyExpectedTypeForConstructor(type, inferenceContext, typeVarContext);
5896
5905
  }
5897
5906
  }
5898
5907
  if (!reportedErrors) {
@@ -5909,16 +5918,22 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5909
5918
  reportedErrors = true;
5910
5919
  }
5911
5920
  }
5912
- const result = { argumentErrors: reportedErrors, returnType, isTypeIncomplete };
5921
+ const result = {
5922
+ argumentErrors: reportedErrors,
5923
+ returnType,
5924
+ isTypeIncomplete,
5925
+ overloadsUsedForCall,
5926
+ };
5913
5927
  return result;
5914
5928
  }
5915
5929
  // For a constructor call that targets a generic class and an "expected type"
5916
5930
  // (i.e. bidirectional inference), this function attempts to infer the correct
5917
5931
  // specialized return type for the constructor.
5918
- function validateConstructorMethodWithExpectedType(errorNode, argList, type, skipUnknownArgCheck, expectedType, constructorMethodType) {
5932
+ function validateConstructorMethodWithExpectedType(errorNode, argList, type, skipUnknownArgCheck, inferenceContext, constructorMethodType) {
5919
5933
  let isTypeIncomplete = false;
5920
5934
  let argumentErrors = false;
5921
- const returnType = (0, typeUtils_1.mapSubtypes)(expectedType, (expectedSubType) => {
5935
+ const overloadsUsedForCall = [];
5936
+ const returnType = (0, typeUtils_1.mapSubtypes)(inferenceContext.expectedType, (expectedSubType) => {
5922
5937
  expectedSubType = (0, typeUtils_1.transformPossibleRecursiveTypeAlias)(expectedSubType);
5923
5938
  const typeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(type));
5924
5939
  if ((0, constraintSolver_1.populateTypeVarContextBasedOnExpectedType)(evaluatorInterface, types_1.ClassType.cloneAsInstance(type), expectedSubType, typeVarContext, getTypeVarScopesForNode(errorNode))) {
@@ -5936,6 +5951,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5936
5951
  if (callResult.argumentErrors) {
5937
5952
  argumentErrors = true;
5938
5953
  }
5954
+ overloadsUsedForCall.push(...callResult.overloadsUsedForCall);
5939
5955
  return applyExpectedSubtypeForConstructor(type, expectedSubType, typeVarContext);
5940
5956
  }
5941
5957
  }
@@ -5944,7 +5960,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5944
5960
  if ((0, types_1.isNever)(returnType)) {
5945
5961
  return undefined;
5946
5962
  }
5947
- return { returnType, isTypeIncomplete, argumentErrors };
5963
+ return { returnType, isTypeIncomplete, argumentErrors, overloadsUsedForCall };
5948
5964
  }
5949
5965
  function applyExpectedSubtypeForConstructor(type, expectedSubtype, typeVarContext) {
5950
5966
  const specializedType = (0, typeUtils_1.applySolvedTypeVars)(types_1.ClassType.cloneAsInstance(type), typeVarContext);
@@ -5959,10 +5975,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5959
5975
  }
5960
5976
  // Handles the case where a constructor is a generic type and the type
5961
5977
  // arguments are not specified but can be provided by the expected type.
5962
- function applyExpectedTypeForConstructor(type, expectedType, typeVarContext) {
5978
+ function applyExpectedTypeForConstructor(type, inferenceContext, typeVarContext) {
5963
5979
  let unsolvedTypeVarsAreUnknown = true;
5964
- if (expectedType) {
5965
- const specializedExpectedType = (0, typeUtils_1.mapSubtypes)(expectedType, (expectedSubtype) => {
5980
+ if (inferenceContext) {
5981
+ const specializedExpectedType = (0, typeUtils_1.mapSubtypes)(inferenceContext.expectedType, (expectedSubtype) => {
5966
5982
  return applyExpectedSubtypeForConstructor(type, expectedSubtype, typeVarContext);
5967
5983
  });
5968
5984
  if (!(0, types_1.isNever)(specializedExpectedType)) {
@@ -5982,13 +5998,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5982
5998
  }
5983
5999
  // Similar to applyExpectedTypeForConstructor, this function handles the
5984
6000
  // special case of the tuple class.
5985
- function applyExpectedTypeForTupleConstructor(type, expectedType) {
6001
+ function applyExpectedTypeForTupleConstructor(type, inferenceContext) {
5986
6002
  let specializedType = type;
5987
- if (expectedType &&
5988
- (0, types_1.isClassInstance)(expectedType) &&
5989
- (0, typeUtils_1.isTupleClass)(expectedType) &&
5990
- expectedType.tupleTypeArguments) {
5991
- specializedType = (0, typeUtils_1.specializeTupleClass)(type, expectedType.tupleTypeArguments);
6003
+ if (inferenceContext &&
6004
+ (0, types_1.isClassInstance)(inferenceContext.expectedType) &&
6005
+ (0, typeUtils_1.isTupleClass)(inferenceContext.expectedType) &&
6006
+ inferenceContext.expectedType.tupleTypeArguments) {
6007
+ specializedType = (0, typeUtils_1.specializeTupleClass)(type, inferenceContext.expectedType.tupleTypeArguments);
5992
6008
  }
5993
6009
  return specializedType;
5994
6010
  }
@@ -5996,12 +6012,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5996
6012
  // list, specializes the call based on arg types, and returns the
5997
6013
  // specialized type of the return value. If it detects an error along
5998
6014
  // the way, it emits a diagnostic and sets argumentErrors to true.
5999
- function validateCallArguments(errorNode, argList, callTypeResult, typeVarContext, skipUnknownArgCheck = false, expectedType, recursionCount = 0) {
6015
+ function validateCallArguments(errorNode, argList, callTypeResult, typeVarContext, skipUnknownArgCheck = false, inferenceContext, recursionCount = 0) {
6000
6016
  let argumentErrors = false;
6001
6017
  let isTypeIncomplete = false;
6002
6018
  let specializedInitSelfType;
6019
+ const overloadsUsedForCall = [];
6003
6020
  if (recursionCount > types_1.maxTypeRecursionCount) {
6004
- return { returnType: types_1.UnknownType.create(), argumentErrors: true };
6021
+ return { returnType: types_1.UnknownType.create(), argumentErrors: true, overloadsUsedForCall };
6005
6022
  }
6006
6023
  recursionCount++;
6007
6024
  if (types_1.TypeBase.isSpecialForm(callTypeResult.type)) {
@@ -6010,7 +6027,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6010
6027
  expression: ParseTreeUtils.printExpression(exprNode),
6011
6028
  type: printType(callTypeResult.type, { expandTypeAlias: true }),
6012
6029
  }), exprNode);
6013
- return { returnType: types_1.UnknownType.create(), argumentErrors: true };
6030
+ return { returnType: types_1.UnknownType.create(), argumentErrors: true, overloadsUsedForCall };
6014
6031
  }
6015
6032
  const returnType = mapSubtypesExpandTypeVars(callTypeResult.type,
6016
6033
  /* conditionFilter */ undefined, (expandedSubtype, unexpandedSubtype) => {
@@ -6054,10 +6071,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6054
6071
  effectiveTypeVarContext.addSolveForScope(types_1.WildcardTypeVarScopeId);
6055
6072
  }
6056
6073
  }
6057
- const functionResult = validateFunctionArguments(errorNode, argList, expandedSubtype, effectiveTypeVarContext, skipUnknownArgCheck, expectedType);
6074
+ const functionResult = validateFunctionArguments(errorNode, argList, expandedSubtype, effectiveTypeVarContext, skipUnknownArgCheck, inferenceContext);
6058
6075
  if (functionResult.isTypeIncomplete) {
6059
6076
  isTypeIncomplete = true;
6060
6077
  }
6078
+ overloadsUsedForCall.push(...functionResult.overloadsUsedForCall);
6061
6079
  if (functionResult.argumentErrors) {
6062
6080
  argumentErrors = true;
6063
6081
  }
@@ -6102,7 +6120,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6102
6120
  }
6103
6121
  return (0, typeUtils_1.convertToInstance)(castToType);
6104
6122
  }
6105
- const functionResult = validateOverloadedFunctionArguments(errorNode, argList, expandedSubtype, typeVarContext, skipUnknownArgCheck, expectedType);
6123
+ const functionResult = validateOverloadedFunctionArguments(errorNode, argList, expandedSubtype, typeVarContext, skipUnknownArgCheck, inferenceContext);
6124
+ overloadsUsedForCall.push(...functionResult.overloadsUsedForCall);
6106
6125
  if (functionResult.isTypeIncomplete) {
6107
6126
  isTypeIncomplete = true;
6108
6127
  }
@@ -6138,7 +6157,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6138
6157
  const className = expandedSubtype.aliasName || expandedSubtype.details.name;
6139
6158
  if (className === 'type') {
6140
6159
  // Validate the constructor arguments.
6141
- validateConstructorArguments(errorNode, argList, expandedSubtype, skipUnknownArgCheck, expectedType);
6160
+ validateConstructorArguments(errorNode, argList, expandedSubtype, skipUnknownArgCheck, inferenceContext);
6142
6161
  // Handle the 'type' call specially.
6143
6162
  if (argList.length === 1) {
6144
6163
  // The one-parameter form of "type" returns the class
@@ -6234,7 +6253,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6234
6253
  }), errorNode);
6235
6254
  }
6236
6255
  // Assume this is a call to the constructor.
6237
- const constructorResult = validateConstructorArguments(errorNode, argList, expandedSubtype, skipUnknownArgCheck, expectedType);
6256
+ const constructorResult = validateConstructorArguments(errorNode, argList, expandedSubtype, skipUnknownArgCheck, inferenceContext);
6257
+ overloadsUsedForCall.push(...constructorResult.overloadsUsedForCall);
6238
6258
  if (constructorResult.argumentErrors) {
6239
6259
  argumentErrors = true;
6240
6260
  }
@@ -6274,7 +6294,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6274
6294
  else {
6275
6295
  const memberType = (_d = getTypeOfObjectMember(errorNode, expandedSubtype, '__call__')) === null || _d === void 0 ? void 0 : _d.type;
6276
6296
  if (memberType) {
6277
- const functionResult = validateCallArguments(errorNode, argList, { type: memberType }, typeVarContext, skipUnknownArgCheck, expectedType, recursionCount);
6297
+ const functionResult = validateCallArguments(errorNode, argList, { type: memberType }, typeVarContext, skipUnknownArgCheck, inferenceContext, recursionCount);
6298
+ overloadsUsedForCall.push(...functionResult.overloadsUsedForCall);
6278
6299
  if (functionResult.argumentErrors) {
6279
6300
  argumentErrors = true;
6280
6301
  }
@@ -6306,7 +6327,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6306
6327
  // need to handle recursive type aliases.
6307
6328
  case 10 /* TypeVar */: {
6308
6329
  expandedSubtype = (0, typeUtils_1.transformPossibleRecursiveTypeAlias)(expandedSubtype);
6309
- const callResult = validateCallArguments(errorNode, argList, { type: expandedSubtype }, typeVarContext, skipUnknownArgCheck, expectedType, recursionCount);
6330
+ const callResult = validateCallArguments(errorNode, argList, { type: expandedSubtype }, typeVarContext, skipUnknownArgCheck, inferenceContext, recursionCount);
6331
+ overloadsUsedForCall.push(...callResult.overloadsUsedForCall);
6310
6332
  if (callResult.argumentErrors) {
6311
6333
  argumentErrors = true;
6312
6334
  }
@@ -6324,6 +6346,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6324
6346
  returnType: (0, types_1.isNever)(returnType) && !returnType.isNoReturn ? undefined : returnType,
6325
6347
  isTypeIncomplete,
6326
6348
  specializedInitSelfType,
6349
+ overloadsUsedForCall,
6327
6350
  };
6328
6351
  }
6329
6352
  // Expands any unpacked tuples within an argument list.
@@ -7129,19 +7152,19 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
7129
7152
  // After having matched arguments with parameters, this function evaluates the
7130
7153
  // types of each argument expression and validates that the resulting type is
7131
7154
  // compatible with the declared type of the corresponding parameter.
7132
- function validateFunctionArgumentTypesWithExpectedType(errorNode, matchResults, typeVarContext, skipUnknownArgCheck = false, expectedType) {
7155
+ function validateFunctionArgumentTypesWithExpectedType(errorNode, matchResults, typeVarContext, skipUnknownArgCheck = false, inferenceContext) {
7133
7156
  var _a;
7134
7157
  const type = matchResults.overload;
7135
- if (!expectedType ||
7136
- (0, types_1.isAnyOrUnknown)(expectedType) ||
7137
- (0, types_1.isNever)(expectedType) ||
7138
- (0, typeUtils_1.requiresSpecialization)(expectedType) ||
7158
+ if (!inferenceContext ||
7159
+ (0, types_1.isAnyOrUnknown)(inferenceContext.expectedType) ||
7160
+ (0, types_1.isNever)(inferenceContext.expectedType) ||
7161
+ (0, typeUtils_1.requiresSpecialization)(inferenceContext.expectedType) ||
7139
7162
  !type.details.declaredReturnType ||
7140
7163
  !(0, typeUtils_1.requiresSpecialization)((_a = types_1.FunctionType.getSpecializedReturnType(type)) !== null && _a !== void 0 ? _a : types_1.UnknownType.create())) {
7141
7164
  return validateFunctionArgumentTypes(errorNode, matchResults, typeVarContext, skipUnknownArgCheck);
7142
7165
  }
7143
7166
  const effectiveReturnType = getFunctionEffectiveReturnType(type);
7144
- let effectiveExpectedType = expectedType;
7167
+ let effectiveExpectedType = inferenceContext.expectedType;
7145
7168
  let effectiveFlags = 0 /* Default */;
7146
7169
  if ((0, typeUtils_1.containsLiteralType)(effectiveExpectedType, /* includeTypeArgs */ true)) {
7147
7170
  effectiveFlags |= 128 /* RetainLiteralsForTypeVar */;
@@ -7149,7 +7172,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
7149
7172
  // If the expected type is a union, we don't know which type is expected.
7150
7173
  // We may or may not be able to make use of the expected type. We'll evaluate
7151
7174
  // speculatively to see if using the expected type works.
7152
- if ((0, types_1.isUnion)(expectedType)) {
7175
+ if ((0, types_1.isUnion)(inferenceContext.expectedType)) {
7153
7176
  let speculativeResults;
7154
7177
  useSpeculativeMode(errorNode, () => {
7155
7178
  const typeVarContextCopy = typeVarContext.clone();
@@ -7400,6 +7423,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
7400
7423
  isTypeIncomplete,
7401
7424
  activeParam: matchResults.activeParam,
7402
7425
  specializedInitSelfType,
7426
+ overloadsUsedForCall: argumentErrors ? [] : [type],
7403
7427
  };
7404
7428
  }
7405
7429
  function adjustCallableReturnType(type) {
@@ -7419,7 +7443,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
7419
7443
  // Tries to assign the call arguments to the function parameter
7420
7444
  // list and reports any mismatches in types or counts. Returns the
7421
7445
  // specialized return type of the call.
7422
- function validateFunctionArguments(errorNode, argList, type, typeVarContext, skipUnknownArgCheck = false, expectedType) {
7446
+ function validateFunctionArguments(errorNode, argList, type, typeVarContext, skipUnknownArgCheck = false, inferenceContext) {
7423
7447
  const matchResults = matchFunctionArgumentsToParameters(errorNode, argList, type, 0);
7424
7448
  if (matchResults.argumentErrors) {
7425
7449
  // Evaluate types of all args. This will ensure that referenced symbols are
@@ -7432,9 +7456,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
7432
7456
  return {
7433
7457
  argumentErrors: true,
7434
7458
  activeParam: matchResults.activeParam,
7459
+ overloadsUsedForCall: [],
7435
7460
  };
7436
7461
  }
7437
- return validateFunctionArgumentTypesWithExpectedType(errorNode, matchResults, typeVarContext, skipUnknownArgCheck, expectedType);
7462
+ return validateFunctionArgumentTypesWithExpectedType(errorNode, matchResults, typeVarContext, skipUnknownArgCheck, inferenceContext);
7438
7463
  }
7439
7464
  // Determines whether the specified argument list satisfies the function
7440
7465
  // signature bound to the specified ParamSpec. Return value indicates success.
@@ -7558,11 +7583,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
7558
7583
  // to true if this is the first pass through the parameter list because
7559
7584
  // a wide bound on a TypeVar (if a narrow bound has not yet been established)
7560
7585
  // will unnecessarily constrain the expected type.
7561
- let expectedType = (0, types_1.isTypeVar)(argParam.paramType) &&
7562
- functionType !== undefined &&
7563
- argParam.paramType.scopeId === functionType.details.typeVarScopeId
7564
- ? undefined
7565
- : (0, typeUtils_1.applySolvedTypeVars)(argParam.paramType, typeVarContext, { useNarrowBoundOnly });
7586
+ let expectedType;
7587
+ if (!(0, types_1.isTypeVar)(argParam.paramType) || argParam.paramType.scopeId !== (functionType === null || functionType === void 0 ? void 0 : functionType.details.typeVarScopeId)) {
7588
+ expectedType = (0, typeUtils_1.applySolvedTypeVars)(argParam.paramType, typeVarContext, { useNarrowBoundOnly });
7589
+ }
7566
7590
  // If the expected type is unknown, don't use an expected type. Instead,
7567
7591
  // use default rules for evaluating the expression type.
7568
7592
  if (expectedType && (0, types_1.isUnknown)(expectedType)) {
@@ -7575,10 +7599,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
7575
7599
  else {
7576
7600
  const flags = argParam.expectingType
7577
7601
  ? 8 /* EvaluateStringLiteralAsType */ |
7578
- 32 /* ParamSpecDisallowed */ |
7579
- 128 /* TypeVarTupleDisallowed */
7602
+ 32 /* DisallowParamSpec */ |
7603
+ 64 /* DisallowTypeVarTuple */
7580
7604
  : 0 /* None */;
7581
- const exprTypeResult = getTypeOfExpression(argParam.argument.valueExpression, flags, expectedType);
7605
+ const exprTypeResult = getTypeOfExpression(argParam.argument.valueExpression, flags, (0, typeUtils_1.makeInferenceContext)(expectedType, typeVarContext));
7582
7606
  argType = exprTypeResult.type;
7583
7607
  if (exprTypeResult.isIncomplete) {
7584
7608
  isTypeIncomplete = true;
@@ -7591,7 +7615,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
7591
7615
  if (argParam.argument &&
7592
7616
  argParam.argument.name &&
7593
7617
  !speculativeTypeTracker.isSpeculative(argParam.errorNode)) {
7594
- writeTypeCache(argParam.argument.name, { type: expectedType || argType, isIncomplete: isTypeIncomplete }, 0 /* None */);
7618
+ writeTypeCache(argParam.argument.name, { type: expectedType !== null && expectedType !== void 0 ? expectedType : argType, isIncomplete: isTypeIncomplete }, 0 /* None */);
7595
7619
  }
7596
7620
  }
7597
7621
  else {
@@ -7601,8 +7625,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
7601
7625
  }
7602
7626
  else if (argParam.expectingType && !argParam.argument.typeResult && argParam.argument.valueExpression) {
7603
7627
  const argTypeResult = getTypeOfExpression(argParam.argument.valueExpression, 8 /* EvaluateStringLiteralAsType */ |
7604
- 32 /* ParamSpecDisallowed */ |
7605
- 128 /* TypeVarTupleDisallowed */);
7628
+ 32 /* DisallowParamSpec */ |
7629
+ 64 /* DisallowTypeVarTuple */);
7606
7630
  argType = argTypeResult.type;
7607
7631
  if (argTypeResult.isIncomplete) {
7608
7632
  isTypeIncomplete = true;
@@ -8139,7 +8163,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8139
8163
  function getTypeOfConstant(node, flags) {
8140
8164
  let type;
8141
8165
  if (node.constType === 26 /* None */) {
8142
- type = (flags & 64 /* ExpectingType */) !== 0 ? types_1.NoneType.createType() : types_1.NoneType.createInstance();
8166
+ type = (flags & 128 /* ExpectingType */) !== 0 ? types_1.NoneType.createType() : types_1.NoneType.createInstance();
8143
8167
  }
8144
8168
  else if (node.constType === 33 /* True */ ||
8145
8169
  node.constType === 15 /* False */ ||
@@ -8161,7 +8185,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8161
8185
  }
8162
8186
  return { type };
8163
8187
  }
8164
- function getTypeOfUnaryOperation(node, expectedType) {
8188
+ function getTypeOfUnaryOperation(node, inferenceContext) {
8165
8189
  const exprTypeResult = getTypeOfExpression(node.expression);
8166
8190
  let exprType = makeTopLevelTypeVarsConcrete(exprTypeResult.type);
8167
8191
  const isIncomplete = exprTypeResult.isIncomplete;
@@ -8224,15 +8248,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8224
8248
  }
8225
8249
  else {
8226
8250
  const magicMethodName = unaryOperatorMap[node.operator];
8227
- type = getTypeOfMagicMethodReturn(exprType, [], magicMethodName, node, expectedType);
8251
+ type = getTypeOfMagicMethodReturn(exprType, [], magicMethodName, node, inferenceContext);
8228
8252
  }
8229
8253
  if (!type) {
8230
8254
  const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
8231
- if (expectedType) {
8255
+ if (inferenceContext) {
8232
8256
  addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeNotSupportUnaryOperatorBidirectional().format({
8233
8257
  operator: ParseTreeUtils.printOperator(node.operator),
8234
8258
  type: printType(exprType),
8235
- expectedType: printType(expectedType),
8259
+ expectedType: printType(inferenceContext.expectedType),
8236
8260
  }), node);
8237
8261
  }
8238
8262
  else {
@@ -8247,7 +8271,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8247
8271
  }
8248
8272
  return { type, isIncomplete };
8249
8273
  }
8250
- function getTypeOfBinaryOperation(node, expectedType, flags) {
8274
+ function getTypeOfBinaryOperation(node, inferenceContext, flags) {
8251
8275
  const leftExpression = node.leftExpression;
8252
8276
  let rightExpression = node.rightExpression;
8253
8277
  let isIncomplete = false;
@@ -8259,7 +8283,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8259
8283
  !rightExpression.parenthesized &&
8260
8284
  ParseTreeUtils.operatorSupportsChaining(rightExpression.operator)) {
8261
8285
  // Evaluate the right expression so it is type checked.
8262
- getTypeOfBinaryOperation(rightExpression, expectedType, flags);
8286
+ getTypeOfBinaryOperation(rightExpression, inferenceContext, flags);
8263
8287
  // Use the left side of the right expression for comparison purposes.
8264
8288
  rightExpression = rightExpression.leftExpression;
8265
8289
  }
@@ -8267,21 +8291,24 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8267
8291
  // For most binary operations, the "expected type" is applied to the output
8268
8292
  // of the magic method for that operation. However, the "or" and "and" operators
8269
8293
  // have no magic method, so we apply the expected type directly to both operands.
8270
- let expectedOperandType = node.operator === 37 /* Or */ || node.operator === 36 /* And */ ? expectedType : undefined;
8294
+ let expectedOperandType = node.operator === 37 /* Or */ || node.operator === 36 /* And */
8295
+ ? inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.expectedType
8296
+ : undefined;
8271
8297
  // Handle the very special case where the expected type is a list
8272
8298
  // and the operator is a multiply. This comes up in the common case
8273
8299
  // of "x: List[Optional[X]] = [None] * y" where y is an integer literal.
8274
8300
  let expectedLeftOperandType;
8275
8301
  if (node.operator === 26 /* Multiply */ &&
8276
- expectedType &&
8277
- (0, types_1.isClassInstance)(expectedType) &&
8278
- types_1.ClassType.isBuiltIn(expectedType, 'list') &&
8279
- expectedType.typeArguments &&
8280
- expectedType.typeArguments.length >= 1 &&
8302
+ inferenceContext &&
8303
+ (0, types_1.isClassInstance)(inferenceContext.expectedType) &&
8304
+ types_1.ClassType.isBuiltIn(inferenceContext.expectedType, 'list') &&
8305
+ inferenceContext.expectedType.typeArguments &&
8306
+ inferenceContext.expectedType.typeArguments.length >= 1 &&
8281
8307
  node.leftExpression.nodeType === 31 /* List */) {
8282
- expectedLeftOperandType = expectedType;
8308
+ expectedLeftOperandType = inferenceContext.expectedType;
8283
8309
  }
8284
- const leftTypeResult = getTypeOfExpression(leftExpression, flags, expectedOperandType || expectedLeftOperandType);
8310
+ const effectiveExpectedType = expectedOperandType !== null && expectedOperandType !== void 0 ? expectedOperandType : expectedLeftOperandType;
8311
+ const leftTypeResult = getTypeOfExpression(leftExpression, flags, (0, typeUtils_1.makeInferenceContext)(effectiveExpectedType));
8285
8312
  let leftType = leftTypeResult.type;
8286
8313
  if (!expectedOperandType) {
8287
8314
  if (node.operator === 37 /* Or */ || node.operator === 36 /* And */) {
@@ -8302,7 +8329,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8302
8329
  }
8303
8330
  }
8304
8331
  }
8305
- const rightTypeResult = getTypeOfExpression(rightExpression, flags, expectedOperandType);
8332
+ const rightTypeResult = getTypeOfExpression(rightExpression, flags, (0, typeUtils_1.makeInferenceContext)(expectedOperandType));
8306
8333
  let rightType = rightTypeResult.type;
8307
8334
  if (leftTypeResult.isIncomplete || rightTypeResult.isIncomplete) {
8308
8335
  isIncomplete = true;
@@ -8396,7 +8423,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8396
8423
  // incomplete because we may be evaluating types within a loop,
8397
8424
  // so the literal values may change each time.
8398
8425
  const isLiteralMathAllowed = !ParseTreeUtils.isWithinLoop(node);
8399
- let type = validateBinaryOperation(node.operator, { type: leftType, isIncomplete: leftTypeResult.isIncomplete }, { type: rightType, isIncomplete: rightTypeResult.isIncomplete }, node, expectedType, diag, isLiteralMathAllowed);
8426
+ // Don't special-case tuple __add__ if the left type is a union. This
8427
+ // can result in an infinite loop if we keep creating new tuple types
8428
+ // within a loop construct using __add__.
8429
+ const isTupleAddAllowed = !(0, types_1.isUnion)(leftType);
8430
+ let type = validateBinaryOperation(node.operator, { type: leftType, isIncomplete: leftTypeResult.isIncomplete }, { type: rightType, isIncomplete: rightTypeResult.isIncomplete }, node, inferenceContext, diag, { isLiteralMathAllowed, isTupleAddAllowed });
8400
8431
  if (!diag.isEmpty() || !type) {
8401
8432
  if (!isIncomplete) {
8402
8433
  const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
@@ -8441,7 +8472,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8441
8472
  }
8442
8473
  return true;
8443
8474
  }
8444
- function getTypeOfAugmentedAssignment(node, expectedType) {
8475
+ function getTypeOfAugmentedAssignment(node, inferenceContext) {
8445
8476
  const operatorMap = {
8446
8477
  [1 /* AddEqual */]: ['__iadd__', 0 /* Add */],
8447
8478
  [34 /* SubtractEqual */]: ['__isub__', 33 /* Subtract */],
@@ -8468,7 +8499,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8468
8499
  // us to support the case where a TypedDict is being updated with a dict expression.
8469
8500
  expectedOperandType = leftType;
8470
8501
  }
8471
- const rightTypeResult = getTypeOfExpression(node.rightExpression, /* flags */ undefined, expectedOperandType);
8502
+ const rightTypeResult = getTypeOfExpression(node.rightExpression,
8503
+ /* flags */ undefined, (0, typeUtils_1.makeInferenceContext)(expectedOperandType));
8472
8504
  const rightType = rightTypeResult.type;
8473
8505
  const isIncomplete = !!rightTypeResult.isIncomplete || !!leftTypeResult.isIncomplete;
8474
8506
  if ((0, types_1.isNever)(leftType) || (0, types_1.isNever)(rightType)) {
@@ -8482,14 +8514,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8482
8514
  return (0, typeUtils_1.preserveUnknown)(leftSubtypeUnexpanded, rightSubtypeUnexpanded);
8483
8515
  }
8484
8516
  const magicMethodName = operatorMap[node.operator][0];
8485
- let returnType = getTypeOfMagicMethodReturn(leftSubtypeUnexpanded, [{ type: rightSubtypeUnexpanded, isIncomplete: rightTypeResult.isIncomplete }], magicMethodName, node, expectedType);
8517
+ let returnType = getTypeOfMagicMethodReturn(leftSubtypeUnexpanded, [{ type: rightSubtypeUnexpanded, isIncomplete: rightTypeResult.isIncomplete }], magicMethodName, node, inferenceContext);
8486
8518
  if (!returnType && leftSubtypeUnexpanded !== leftSubtypeExpanded) {
8487
8519
  // Try with the expanded left type.
8488
- returnType = getTypeOfMagicMethodReturn(leftSubtypeExpanded, [{ type: rightSubtypeUnexpanded, isIncomplete: rightTypeResult.isIncomplete }], magicMethodName, node, expectedType);
8520
+ returnType = getTypeOfMagicMethodReturn(leftSubtypeExpanded, [{ type: rightSubtypeUnexpanded, isIncomplete: rightTypeResult.isIncomplete }], magicMethodName, node, inferenceContext);
8489
8521
  }
8490
8522
  if (!returnType && rightSubtypeUnexpanded !== rightSubtypeExpanded) {
8491
8523
  // Try with the expanded left and right type.
8492
- returnType = getTypeOfMagicMethodReturn(leftSubtypeExpanded, [{ type: rightSubtypeExpanded, isIncomplete: rightTypeResult.isIncomplete }], magicMethodName, node, expectedType);
8524
+ returnType = getTypeOfMagicMethodReturn(leftSubtypeExpanded, [{ type: rightSubtypeExpanded, isIncomplete: rightTypeResult.isIncomplete }], magicMethodName, node, inferenceContext);
8493
8525
  }
8494
8526
  if (!returnType) {
8495
8527
  // If the LHS class didn't support the magic method for augmented
@@ -8502,7 +8534,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8502
8534
  !rightTypeResult.isIncomplete &&
8503
8535
  (0, typeUtils_1.getUnionSubtypeCount)(leftType) * (0, typeUtils_1.getUnionSubtypeCount)(rightType) <
8504
8536
  maxLiteralMathSubtypeCount;
8505
- returnType = validateBinaryOperation(binaryOperator, { type: leftSubtypeUnexpanded, isIncomplete: leftTypeResult.isIncomplete }, { type: rightSubtypeUnexpanded, isIncomplete: rightTypeResult.isIncomplete }, node, expectedType, diag, isLiteralMathAllowed);
8537
+ // Don't special-case tuple __add__ if the left type is a union. This
8538
+ // can result in an infinite loop if we keep creating new tuple types
8539
+ // within a loop construct using __add__.
8540
+ const isTupleAddAllowed = !(0, types_1.isUnion)(leftType);
8541
+ returnType = validateBinaryOperation(binaryOperator, { type: leftSubtypeUnexpanded, isIncomplete: leftTypeResult.isIncomplete }, { type: rightSubtypeUnexpanded, isIncomplete: rightTypeResult.isIncomplete }, node, inferenceContext, diag, { isLiteralMathAllowed, isTupleAddAllowed });
8506
8542
  }
8507
8543
  return returnType;
8508
8544
  });
@@ -8525,7 +8561,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8525
8561
  assignTypeToExpression(node.destExpression, typeResult.type, !!typeResult.isIncomplete, node.rightExpression);
8526
8562
  return typeResult;
8527
8563
  }
8528
- function validateBinaryOperation(operator, leftTypeResult, rightTypeResult, errorNode, expectedType, diag, isLiteralMathAllowed) {
8564
+ function validateBinaryOperation(operator, leftTypeResult, rightTypeResult, errorNode, inferenceContext, diag, options) {
8529
8565
  const leftType = leftTypeResult.type;
8530
8566
  const rightType = rightTypeResult.type;
8531
8567
  let type;
@@ -8579,7 +8615,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8579
8615
  return (0, typeUtils_1.preserveUnknown)(leftSubtype, rightSubtypeExpanded);
8580
8616
  }
8581
8617
  let returnType = getTypeOfMagicMethodReturn(rightSubtypeExpanded, [{ type: leftSubtype, isIncomplete: leftTypeResult.isIncomplete }], '__contains__', errorNode,
8582
- /* expectedType */ undefined);
8618
+ /* inferenceContext */ undefined);
8583
8619
  if (!returnType) {
8584
8620
  // If __contains__ was not supported, fall back
8585
8621
  // on an iterable.
@@ -8626,7 +8662,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8626
8662
  // Handle certain operations on certain homogenous literal types
8627
8663
  // using special-case math. For example, Literal[1, 2] + Literal[3, 4]
8628
8664
  // should result in Literal[4, 5, 6].
8629
- if (isLiteralMathAllowed) {
8665
+ if (options.isLiteralMathAllowed) {
8630
8666
  const leftLiteralClassName = (0, typeUtils_1.getLiteralTypeClassName)(leftType);
8631
8667
  if (leftLiteralClassName && !(0, typeUtils_1.getTypeCondition)(leftType)) {
8632
8668
  const rightLiteralClassName = (0, typeUtils_1.getLiteralTypeClassName)(rightType);
@@ -8690,7 +8726,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8690
8726
  else {
8691
8727
  // Convert back to a simple number if it fits. Leave as a bigint
8692
8728
  // if it doesn't.
8693
- if (newValue === BigInt(Number(newValue))) {
8729
+ if (newValue >= Number.MIN_SAFE_INTEGER &&
8730
+ newValue <= Number.MAX_SAFE_INTEGER) {
8694
8731
  newValue = Number(newValue);
8695
8732
  }
8696
8733
  return types_1.ClassType.cloneWithLiteral(leftClassSubtype, newValue);
@@ -8718,7 +8755,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8718
8755
  return (0, typeUtils_1.preserveUnknown)(leftSubtypeUnexpanded, rightSubtypeUnexpanded);
8719
8756
  }
8720
8757
  // Special-case __add__ for tuples when the types for both tuples are known.
8721
- if (operator === 0 /* Add */ &&
8758
+ if (options.isTupleAddAllowed &&
8759
+ operator === 0 /* Add */ &&
8722
8760
  (0, types_1.isClassInstance)(leftSubtypeExpanded) &&
8723
8761
  (0, typeUtils_1.isTupleClass)(leftSubtypeExpanded) &&
8724
8762
  leftSubtypeExpanded.tupleTypeArguments &&
@@ -8735,19 +8773,19 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8735
8773
  ]));
8736
8774
  }
8737
8775
  const magicMethodName = binaryOperatorMap[operator][0];
8738
- let resultType = getTypeOfMagicMethodReturn(convertFunctionToObject(leftSubtypeUnexpanded), [{ type: rightSubtypeUnexpanded, isIncomplete: rightTypeResult.isIncomplete }], magicMethodName, errorNode, expectedType);
8776
+ let resultType = getTypeOfMagicMethodReturn(convertFunctionToObject(leftSubtypeUnexpanded), [{ type: rightSubtypeUnexpanded, isIncomplete: rightTypeResult.isIncomplete }], magicMethodName, errorNode, inferenceContext);
8739
8777
  if (!resultType && leftSubtypeUnexpanded !== leftSubtypeExpanded) {
8740
8778
  // Try the expanded left type.
8741
- resultType = getTypeOfMagicMethodReturn(convertFunctionToObject(leftSubtypeExpanded), [{ type: rightSubtypeUnexpanded, isIncomplete: rightTypeResult.isIncomplete }], magicMethodName, errorNode, expectedType);
8779
+ resultType = getTypeOfMagicMethodReturn(convertFunctionToObject(leftSubtypeExpanded), [{ type: rightSubtypeUnexpanded, isIncomplete: rightTypeResult.isIncomplete }], magicMethodName, errorNode, inferenceContext);
8742
8780
  }
8743
8781
  if (!resultType && rightSubtypeUnexpanded !== rightSubtypeExpanded) {
8744
8782
  // Try the expanded left and right type.
8745
- resultType = getTypeOfMagicMethodReturn(convertFunctionToObject(leftSubtypeExpanded), [{ type: rightSubtypeExpanded, isIncomplete: rightTypeResult.isIncomplete }], magicMethodName, errorNode, expectedType);
8783
+ resultType = getTypeOfMagicMethodReturn(convertFunctionToObject(leftSubtypeExpanded), [{ type: rightSubtypeExpanded, isIncomplete: rightTypeResult.isIncomplete }], magicMethodName, errorNode, inferenceContext);
8746
8784
  }
8747
8785
  if (!resultType) {
8748
8786
  // Try the alternate form (swapping right and left).
8749
8787
  const altMagicMethodName = binaryOperatorMap[operator][1];
8750
- resultType = getTypeOfMagicMethodReturn(convertFunctionToObject(rightSubtypeUnexpanded), [{ type: leftSubtypeUnexpanded, isIncomplete: leftTypeResult.isIncomplete }], altMagicMethodName, errorNode, expectedType);
8788
+ resultType = getTypeOfMagicMethodReturn(convertFunctionToObject(rightSubtypeUnexpanded), [{ type: leftSubtypeUnexpanded, isIncomplete: leftTypeResult.isIncomplete }], altMagicMethodName, errorNode, inferenceContext);
8751
8789
  if (!resultType && rightSubtypeUnexpanded !== rightSubtypeExpanded) {
8752
8790
  // Try the expanded right type.
8753
8791
  resultType = getTypeOfMagicMethodReturn(convertFunctionToObject(rightSubtypeExpanded), [
@@ -8755,20 +8793,20 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8755
8793
  type: leftSubtypeUnexpanded,
8756
8794
  isIncomplete: leftTypeResult.isIncomplete,
8757
8795
  },
8758
- ], altMagicMethodName, errorNode, expectedType);
8796
+ ], altMagicMethodName, errorNode, inferenceContext);
8759
8797
  }
8760
8798
  if (!resultType && leftSubtypeUnexpanded !== leftSubtypeExpanded) {
8761
8799
  // Try the expanded right and left type.
8762
- resultType = getTypeOfMagicMethodReturn(convertFunctionToObject(rightSubtypeExpanded), [{ type: leftSubtypeExpanded, isIncomplete: leftTypeResult.isIncomplete }], altMagicMethodName, errorNode, expectedType);
8800
+ resultType = getTypeOfMagicMethodReturn(convertFunctionToObject(rightSubtypeExpanded), [{ type: leftSubtypeExpanded, isIncomplete: leftTypeResult.isIncomplete }], altMagicMethodName, errorNode, inferenceContext);
8763
8801
  }
8764
8802
  }
8765
8803
  if (!resultType) {
8766
- if (expectedType) {
8804
+ if (inferenceContext) {
8767
8805
  diag.addMessage(localize_1.Localizer.Diagnostic.typeNotSupportBinaryOperatorBidirectional().format({
8768
8806
  operator: ParseTreeUtils.printOperator(operator),
8769
8807
  leftType: printType(leftSubtypeExpanded),
8770
8808
  rightType: printType(rightSubtypeExpanded),
8771
- expectedType: printType(expectedType),
8809
+ expectedType: printType(inferenceContext.expectedType),
8772
8810
  }));
8773
8811
  }
8774
8812
  else {
@@ -8786,7 +8824,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8786
8824
  }
8787
8825
  return type && (0, types_1.isNever)(type) ? undefined : type;
8788
8826
  }
8789
- function getTypeOfMagicMethodReturn(objType, args, magicMethodName, errorNode, expectedType) {
8827
+ function getTypeOfMagicMethodReturn(objType, args, magicMethodName, errorNode, inferenceContext) {
8790
8828
  let magicMethodSupported = true;
8791
8829
  // Create a helper lambda for object subtypes.
8792
8830
  const handleSubtype = (subtype) => {
@@ -8814,7 +8852,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8814
8852
  useSpeculativeMode(errorNode, () => {
8815
8853
  callResult = validateCallArguments(errorNode, functionArgs, { type: magicMethodType },
8816
8854
  /* typeVarContext */ undefined,
8817
- /* skipUnknownArgCheck */ true, expectedType);
8855
+ /* skipUnknownArgCheck */ true, inferenceContext);
8818
8856
  });
8819
8857
  if (callResult.argumentErrors) {
8820
8858
  magicMethodSupported = false;
@@ -8862,20 +8900,20 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8862
8900
  }
8863
8901
  return type;
8864
8902
  }
8865
- function getTypeOfDictionary(node, expectedType) {
8903
+ function getTypeOfDictionary(node, inferenceContext) {
8866
8904
  // If the expected type is a union, analyze for each of the subtypes
8867
8905
  // to find one that matches.
8868
- let effectiveExpectedType = expectedType;
8869
- if (expectedType && (0, types_1.isUnion)(expectedType)) {
8906
+ let effectiveExpectedType = inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.expectedType;
8907
+ if (inferenceContext && (0, types_1.isUnion)(inferenceContext.expectedType)) {
8870
8908
  let matchingSubtype;
8871
8909
  let matchingSubtypeResult;
8872
- (0, typeUtils_1.doForEachSubtype)(expectedType, (subtype) => {
8910
+ (0, typeUtils_1.doForEachSubtype)(inferenceContext.expectedType, (subtype) => {
8873
8911
  // Use shortcut if we've already found a match.
8874
8912
  if (matchingSubtypeResult && !matchingSubtypeResult.typeErrors) {
8875
8913
  return;
8876
8914
  }
8877
8915
  const subtypeResult = useSpeculativeMode(node, () => {
8878
- return getTypeOfDictionaryExpected(node, subtype);
8916
+ return getTypeOfDictionaryWithContext(node, (0, typeUtils_1.makeInferenceContext)(subtype, inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.typeVarContext));
8879
8917
  });
8880
8918
  if (subtypeResult && assignType(subtype, subtypeResult.type)) {
8881
8919
  // If this is the first result we're seeing or it's the first result
@@ -8891,28 +8929,26 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8891
8929
  let expectedTypeDiagAddendum = undefined;
8892
8930
  if (effectiveExpectedType) {
8893
8931
  expectedTypeDiagAddendum = new diagnostic_1.DiagnosticAddendum();
8894
- const result = getTypeOfDictionaryExpected(node, effectiveExpectedType, expectedTypeDiagAddendum);
8932
+ const result = getTypeOfDictionaryWithContext(node, (0, typeUtils_1.makeInferenceContext)(effectiveExpectedType, inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.typeVarContext), expectedTypeDiagAddendum);
8895
8933
  if (result) {
8896
8934
  return result;
8897
8935
  }
8898
8936
  }
8899
- const result = getTypeOfDictionaryInferred(node, /* hasExpectedType */ !!expectedType);
8937
+ const result = getTypeOfDictionaryInferred(node, /* hasExpectedType */ !!inferenceContext);
8900
8938
  return { ...result, expectedTypeDiagAddendum };
8901
8939
  }
8902
- // Attempts to infer the type of a dictionary statement. If an expectedType
8903
- // is provided, the resulting type must be compatible with the expected type.
8904
- // If this isn't possible, undefined is returned.
8905
- function getTypeOfDictionaryExpected(node, expectedType, expectedDiagAddendum) {
8906
- expectedType = (0, typeUtils_1.transformPossibleRecursiveTypeAlias)(expectedType);
8907
- if (!(0, types_1.isClassInstance)(expectedType)) {
8940
+ function getTypeOfDictionaryWithContext(node, inferenceContext, expectedDiagAddendum) {
8941
+ inferenceContext.expectedType = (0, typeUtils_1.transformPossibleRecursiveTypeAlias)(inferenceContext.expectedType);
8942
+ const concreteExpectedType = makeTopLevelTypeVarsConcrete(inferenceContext.expectedType);
8943
+ if (!(0, types_1.isClassInstance)(concreteExpectedType)) {
8908
8944
  return undefined;
8909
8945
  }
8910
8946
  const keyTypes = [];
8911
8947
  const valueTypes = [];
8912
8948
  let isIncomplete = false;
8913
8949
  // Handle TypedDict's as a special case.
8914
- if (types_1.ClassType.isTypedDictClass(expectedType)) {
8915
- const expectedTypedDictEntries = (0, typedDicts_1.getTypedDictMembersForClass)(evaluatorInterface, expectedType);
8950
+ if (types_1.ClassType.isTypedDictClass(concreteExpectedType)) {
8951
+ const expectedTypedDictEntries = (0, typedDicts_1.getTypedDictMembersForClass)(evaluatorInterface, concreteExpectedType);
8916
8952
  // Infer the key and value types if possible.
8917
8953
  if (getKeyAndValueTypesFromDictionary(node, keyTypes, valueTypes,
8918
8954
  /* forceStrictInference */ true,
@@ -8920,8 +8956,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8920
8956
  /* expectedValueType */ undefined, expectedTypedDictEntries, expectedDiagAddendum)) {
8921
8957
  isIncomplete = true;
8922
8958
  }
8923
- if (types_1.ClassType.isTypedDictClass(expectedType)) {
8924
- const resultTypedDict = (0, typedDicts_1.assignToTypedDict)(evaluatorInterface, expectedType, keyTypes, valueTypes,
8959
+ if (types_1.ClassType.isTypedDictClass(concreteExpectedType)) {
8960
+ const resultTypedDict = (0, typedDicts_1.assignToTypedDict)(evaluatorInterface, concreteExpectedType, keyTypes, valueTypes,
8925
8961
  // Don't overwrite existing expectedDiagAddendum messages if they were
8926
8962
  // already provided by getKeyValueTypesFromDictionary.
8927
8963
  (expectedDiagAddendum === null || expectedDiagAddendum === void 0 ? void 0 : expectedDiagAddendum.isEmpty()) ? expectedDiagAddendum : undefined);
@@ -8939,7 +8975,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8939
8975
  return undefined;
8940
8976
  }
8941
8977
  const dictTypeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(builtInDict));
8942
- if (!(0, constraintSolver_1.populateTypeVarContextBasedOnExpectedType)(evaluatorInterface, builtInDict, expectedType, dictTypeVarContext, getTypeVarScopesForNode(node))) {
8978
+ if (!(0, constraintSolver_1.populateTypeVarContextBasedOnExpectedType)(evaluatorInterface, builtInDict, inferenceContext.expectedType, dictTypeVarContext, getTypeVarScopesForNode(node))) {
8943
8979
  return undefined;
8944
8980
  }
8945
8981
  const specializedDict = (0, typeUtils_1.applySolvedTypeVars)(types_1.ClassType.cloneAsInstantiable(builtInDict), dictTypeVarContext);
@@ -8956,8 +8992,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8956
8992
  // Dict and MutableMapping types have invariant value types, so they
8957
8993
  // cannot be narrowed further. Other super-types like Mapping, Collection,
8958
8994
  // and Iterable use covariant value types, so they can be narrowed.
8959
- const isValueTypeInvariant = (0, types_1.isClassInstance)(expectedType) &&
8960
- (types_1.ClassType.isBuiltIn(expectedType, 'dict') || types_1.ClassType.isBuiltIn(expectedType, 'MutableMapping'));
8995
+ const isValueTypeInvariant = (0, types_1.isClassInstance)(inferenceContext.expectedType) &&
8996
+ (types_1.ClassType.isBuiltIn(inferenceContext.expectedType, 'dict') ||
8997
+ types_1.ClassType.isBuiltIn(inferenceContext.expectedType, 'MutableMapping'));
8961
8998
  const specializedKeyType = inferTypeArgFromExpectedType(expectedKeyType, keyTypes.map((result) => result.type),
8962
8999
  /* isNarrowable */ false);
8963
9000
  const specializedValueType = inferTypeArgFromExpectedType(expectedValueType, valueTypes.map((result) => result.type),
@@ -9021,7 +9058,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9021
9058
  let addUnknown = true;
9022
9059
  if (entryNode.nodeType === 17 /* DictionaryKeyEntry */) {
9023
9060
  const keyTypeResult = getTypeOfExpression(entryNode.keyExpression,
9024
- /* flags */ undefined, expectedKeyType !== null && expectedKeyType !== void 0 ? expectedKeyType : (forceStrictInference ? types_1.NeverType.createNever() : undefined));
9061
+ /* flags */ undefined, (0, typeUtils_1.makeInferenceContext)(expectedKeyType !== null && expectedKeyType !== void 0 ? expectedKeyType : (forceStrictInference ? types_1.NeverType.createNever() : undefined)));
9025
9062
  if (keyTypeResult.isIncomplete) {
9026
9063
  isIncomplete = true;
9027
9064
  }
@@ -9035,12 +9072,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9035
9072
  types_1.ClassType.isBuiltIn(keyType, 'str') &&
9036
9073
  (0, typeUtils_1.isLiteralType)(keyType) &&
9037
9074
  expectedTypedDictEntries.has(keyType.literalValue)) {
9075
+ const effectiveValueType = expectedTypedDictEntries.get(keyType.literalValue).valueType;
9038
9076
  valueTypeResult = getTypeOfExpression(entryNode.valueExpression,
9039
- /* flags */ undefined, expectedTypedDictEntries.get(keyType.literalValue).valueType);
9077
+ /* flags */ undefined, (0, typeUtils_1.makeInferenceContext)(effectiveValueType));
9040
9078
  }
9041
9079
  else {
9080
+ const effectiveValueType = expectedValueType !== null && expectedValueType !== void 0 ? expectedValueType : (forceStrictInference ? types_1.NeverType.createNever() : undefined);
9042
9081
  valueTypeResult = getTypeOfExpression(entryNode.valueExpression,
9043
- /* flags */ undefined, expectedValueType !== null && expectedValueType !== void 0 ? expectedValueType : (forceStrictInference ? types_1.NeverType.createNever() : undefined));
9082
+ /* flags */ undefined, (0, typeUtils_1.makeInferenceContext)(effectiveValueType));
9044
9083
  }
9045
9084
  if (expectedDiagAddendum && valueTypeResult.expectedTypeDiagAddendum) {
9046
9085
  expectedDiagAddendum.addAddendum(valueTypeResult.expectedTypeDiagAddendum);
@@ -9138,20 +9177,20 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9138
9177
  });
9139
9178
  return isIncomplete;
9140
9179
  }
9141
- function getTypeOfListOrSet(node, expectedType) {
9180
+ function getTypeOfListOrSet(node, inferenceContext) {
9142
9181
  // If the expected type is a union, recursively call for each of the subtypes
9143
9182
  // to find one that matches.
9144
- let effectiveExpectedType = expectedType;
9145
- if (expectedType && (0, types_1.isUnion)(expectedType)) {
9183
+ let effectiveExpectedType = inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.expectedType;
9184
+ if (inferenceContext && (0, types_1.isUnion)(inferenceContext.expectedType)) {
9146
9185
  let matchingSubtype;
9147
9186
  let matchingSubtypeResult;
9148
- (0, typeUtils_1.doForEachSubtype)(expectedType, (subtype) => {
9187
+ (0, typeUtils_1.doForEachSubtype)(inferenceContext.expectedType, (subtype) => {
9149
9188
  // Use shortcut if we've already found a match.
9150
9189
  if (matchingSubtypeResult && !matchingSubtypeResult.typeErrors) {
9151
9190
  return;
9152
9191
  }
9153
9192
  const subtypeResult = useSpeculativeMode(node, () => {
9154
- return getTypeOfListOrSetExpected(node, subtype);
9193
+ return getTypeOfListOrSetWithContext(node, (0, typeUtils_1.makeInferenceContext)(subtype, inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.typeVarContext));
9155
9194
  });
9156
9195
  if (subtypeResult && assignType(subtype, subtypeResult.type)) {
9157
9196
  // If this is the first result we're seeing or it's the first result
@@ -9166,24 +9205,24 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9166
9205
  }
9167
9206
  let expectedTypeDiagAddendum;
9168
9207
  if (effectiveExpectedType) {
9169
- const result = getTypeOfListOrSetExpected(node, effectiveExpectedType);
9208
+ const result = getTypeOfListOrSetWithContext(node, (0, typeUtils_1.makeInferenceContext)(effectiveExpectedType, inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.typeVarContext));
9170
9209
  if (result && !result.typeErrors) {
9171
9210
  return result;
9172
9211
  }
9173
9212
  expectedTypeDiagAddendum = result === null || result === void 0 ? void 0 : result.expectedTypeDiagAddendum;
9174
9213
  }
9175
- const typeResult = getTypeOfListOrSetInferred(node, /* hasExpectedType */ expectedType !== undefined);
9214
+ const typeResult = getTypeOfListOrSetInferred(node, /* hasExpectedType */ inferenceContext !== undefined);
9176
9215
  return { ...typeResult, expectedTypeDiagAddendum };
9177
9216
  }
9178
9217
  // Attempts to determine the type of a list or set statement based on an expected type.
9179
9218
  // Returns undefined if that type cannot be honored.
9180
- function getTypeOfListOrSetExpected(node, expectedType) {
9219
+ function getTypeOfListOrSetWithContext(node, inferenceContext) {
9181
9220
  const builtInClassName = node.nodeType === 31 /* List */ ? 'list' : 'set';
9182
- expectedType = (0, typeUtils_1.transformPossibleRecursiveTypeAlias)(expectedType);
9221
+ inferenceContext.expectedType = (0, typeUtils_1.transformPossibleRecursiveTypeAlias)(inferenceContext.expectedType);
9183
9222
  let isIncomplete = false;
9184
9223
  let typeErrors = false;
9185
9224
  const verifyHashable = node.nodeType === 45 /* Set */;
9186
- if (!(0, types_1.isClassInstance)(expectedType)) {
9225
+ if (!(0, types_1.isClassInstance)(inferenceContext.expectedType)) {
9187
9226
  return undefined;
9188
9227
  }
9189
9228
  const builtInListOrSet = getBuiltInObject(node, builtInClassName);
@@ -9191,7 +9230,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9191
9230
  return undefined;
9192
9231
  }
9193
9232
  const typeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(builtInListOrSet));
9194
- if (!(0, constraintSolver_1.populateTypeVarContextBasedOnExpectedType)(evaluatorInterface, builtInListOrSet, expectedType, typeVarContext, getTypeVarScopesForNode(node))) {
9233
+ if (!(0, constraintSolver_1.populateTypeVarContextBasedOnExpectedType)(evaluatorInterface, builtInListOrSet, inferenceContext.expectedType, typeVarContext, getTypeVarScopesForNode(node))) {
9195
9234
  return undefined;
9196
9235
  }
9197
9236
  const specializedListOrSet = (0, typeUtils_1.applySolvedTypeVars)(types_1.ClassType.cloneAsInstantiable(builtInListOrSet), typeVarContext);
@@ -9207,7 +9246,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9207
9246
  entryTypeResult = getElementTypeFromListComprehension(entry, expectedEntryType);
9208
9247
  }
9209
9248
  else {
9210
- entryTypeResult = getTypeOfExpression(entry, /* flags */ undefined, expectedEntryType);
9249
+ entryTypeResult = getTypeOfExpression(entry,
9250
+ /* flags */ undefined, (0, typeUtils_1.makeInferenceContext)(expectedEntryType, inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.typeVarContext));
9211
9251
  }
9212
9252
  entryTypes.push(entryTypeResult.type);
9213
9253
  if (entryTypeResult.isIncomplete) {
@@ -9223,7 +9263,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9223
9263
  verifySetEntryOrDictKeyIsHashable(entry, entryTypeResult.type, /* isDictKey */ false);
9224
9264
  }
9225
9265
  });
9226
- const isExpectedTypeListOrSet = (0, types_1.isClassInstance)(expectedType) && types_1.ClassType.isBuiltIn(expectedType, builtInClassName);
9266
+ const isExpectedTypeListOrSet = (0, types_1.isClassInstance)(inferenceContext.expectedType) &&
9267
+ types_1.ClassType.isBuiltIn(inferenceContext.expectedType, builtInClassName);
9227
9268
  const specializedEntryType = inferTypeArgFromExpectedType(expectedEntryType, entryTypes,
9228
9269
  /* isNarrowable */ !isExpectedTypeListOrSet);
9229
9270
  if (!specializedEntryType) {
@@ -9346,13 +9387,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9346
9387
  }
9347
9388
  return undefined;
9348
9389
  }
9349
- function getTypeOfTernary(node, flags, expectedType) {
9390
+ function getTypeOfTernary(node, flags, inferenceContext) {
9350
9391
  getTypeOfExpression(node.testExpression);
9351
9392
  const typesToCombine = [];
9352
9393
  let isIncomplete = false;
9353
9394
  let typeErrors = false;
9354
9395
  if (isNodeReachable(node.ifExpression)) {
9355
- const ifType = getTypeOfExpression(node.ifExpression, flags, expectedType);
9396
+ const ifType = getTypeOfExpression(node.ifExpression, flags, inferenceContext);
9356
9397
  typesToCombine.push(ifType.type);
9357
9398
  if (ifType.isIncomplete) {
9358
9399
  isIncomplete = true;
@@ -9362,7 +9403,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9362
9403
  }
9363
9404
  }
9364
9405
  if (isNodeReachable(node.elseExpression)) {
9365
- const elseType = getTypeOfExpression(node.elseExpression, flags, expectedType);
9406
+ const elseType = getTypeOfExpression(node.elseExpression, flags, inferenceContext);
9366
9407
  typesToCombine.push(elseType.type);
9367
9408
  if (elseType.isIncomplete) {
9368
9409
  isIncomplete = true;
@@ -9396,7 +9437,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9396
9437
  }
9397
9438
  }
9398
9439
  if (node.expression) {
9399
- const exprResult = getTypeOfExpression(node.expression, /* flags */ undefined, expectedYieldType);
9440
+ const exprResult = getTypeOfExpression(node.expression,
9441
+ /* flags */ undefined, (0, typeUtils_1.makeInferenceContext)(expectedYieldType));
9400
9442
  if (exprResult.isIncomplete) {
9401
9443
  isIncomplete = true;
9402
9444
  }
@@ -9427,7 +9469,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9427
9469
  }
9428
9470
  return { type: returnedType || types_1.UnknownType.create() };
9429
9471
  }
9430
- function getTypeOfLambda(node, expectedType) {
9472
+ function getTypeOfLambda(node, inferenceContext) {
9431
9473
  let isIncomplete = false;
9432
9474
  const functionType = types_1.FunctionType.createInstance('', '', '', 131072 /* PartiallyEvaluated */);
9433
9475
  functionType.details.typeVarScopeId = getScopeIdForNode(node);
@@ -9435,8 +9477,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9435
9477
  // lambda depends on itself.
9436
9478
  writeTypeCache(node, { type: functionType, isIncomplete: true }, 0 /* None */);
9437
9479
  let expectedFunctionTypes = [];
9438
- if (expectedType) {
9439
- (0, typeUtils_1.mapSubtypes)(expectedType, (subtype) => {
9480
+ if (inferenceContext) {
9481
+ (0, typeUtils_1.mapSubtypes)(inferenceContext.expectedType, (subtype) => {
9440
9482
  if ((0, types_1.isFunction)(subtype)) {
9441
9483
  expectedFunctionTypes.push(subtype);
9442
9484
  }
@@ -9526,16 +9568,22 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9526
9568
  // evaluation scope for the return expression and do not allow retention
9527
9569
  // of the cached types.
9528
9570
  const inferLambdaReturnType = () => {
9529
- const returnTypeResult = getTypeOfExpression(node.expression, /* flags */ undefined, expectedReturnType);
9571
+ const returnTypeResult = getTypeOfExpression(node.expression,
9572
+ /* flags */ undefined, (0, typeUtils_1.makeInferenceContext)(expectedReturnType));
9530
9573
  functionType.inferredReturnType = returnTypeResult.type;
9531
9574
  if (returnTypeResult.isIncomplete) {
9532
9575
  isIncomplete = true;
9533
9576
  }
9534
9577
  };
9535
9578
  if (speculativeTypeTracker.isSpeculative(node)) {
9579
+ // We need to set allowCacheRetention to false because we don't want to
9580
+ // cache the type of the lambda return expression because it depends on
9581
+ // the parameter types that we set above, and the speculative type cache
9582
+ // doesn't know about that context.
9536
9583
  useSpeculativeMode(node.expression, () => {
9537
9584
  inferLambdaReturnType();
9538
- });
9585
+ },
9586
+ /* allowCacheRetention */ false);
9539
9587
  }
9540
9588
  else {
9541
9589
  inferLambdaReturnType();
@@ -9544,7 +9592,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9544
9592
  functionType.details.flags &= ~131072 /* PartiallyEvaluated */;
9545
9593
  return { type: functionType, isIncomplete };
9546
9594
  }
9547
- function getTypeOfListComprehension(node, expectedType) {
9595
+ function getTypeOfListComprehension(node, inferenceContext) {
9548
9596
  var _a;
9549
9597
  let isIncomplete = false;
9550
9598
  let typeErrors = false;
@@ -9558,8 +9606,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9558
9606
  isAsync = true;
9559
9607
  }
9560
9608
  let expectedElementType;
9561
- if (expectedType) {
9562
- expectedElementType = (_a = getTypeOfIterator({ type: expectedType }, isAsync, /* errorNode */ undefined)) === null || _a === void 0 ? void 0 : _a.type;
9609
+ if (inferenceContext) {
9610
+ expectedElementType = (_a = getTypeOfIterator({ type: inferenceContext.expectedType }, isAsync,
9611
+ /* errorNode */ undefined)) === null || _a === void 0 ? void 0 : _a.type;
9563
9612
  }
9564
9613
  const elementTypeResult = getElementTypeFromListComprehension(node, expectedElementType);
9565
9614
  if (elementTypeResult.isIncomplete) {
@@ -9572,9 +9621,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9572
9621
  // Handle the special case where a generator function (e.g. `(await x for x in y)`)
9573
9622
  // is expected to be an AsyncGenerator.
9574
9623
  if (!isAsync &&
9575
- expectedType &&
9576
- (0, types_1.isClassInstance)(expectedType) &&
9577
- types_1.ClassType.isBuiltIn(expectedType, 'AsyncGenerator')) {
9624
+ inferenceContext &&
9625
+ (0, types_1.isClassInstance)(inferenceContext.expectedType) &&
9626
+ types_1.ClassType.isBuiltIn(inferenceContext.expectedType, 'AsyncGenerator')) {
9578
9627
  isAsync = true;
9579
9628
  }
9580
9629
  const builtInIteratorType = getTypingType(node, isAsync ? 'AsyncGenerator' : 'Generator');
@@ -9652,7 +9701,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9652
9701
  if (node.expression.nodeType === 17 /* DictionaryKeyEntry */) {
9653
9702
  // Create a tuple with the key/value types.
9654
9703
  const keyTypeResult = getTypeOfExpression(node.expression.keyExpression,
9655
- /* flags */ undefined, expectedKeyType);
9704
+ /* flags */ undefined, (0, typeUtils_1.makeInferenceContext)(expectedKeyType));
9656
9705
  if (keyTypeResult.isIncomplete) {
9657
9706
  isIncomplete = true;
9658
9707
  }
@@ -9664,7 +9713,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9664
9713
  keyType = stripLiteralValue(keyType);
9665
9714
  }
9666
9715
  const valueTypeResult = getTypeOfExpression(node.expression.valueExpression,
9667
- /* flags */ undefined, expectedValueOrElementType);
9716
+ /* flags */ undefined, (0, typeUtils_1.makeInferenceContext)(expectedValueOrElementType));
9668
9717
  if (valueTypeResult.isIncomplete) {
9669
9718
  isIncomplete = true;
9670
9719
  }
@@ -9679,11 +9728,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9679
9728
  }
9680
9729
  else if (node.expression.nodeType === 16 /* DictionaryExpandEntry */) {
9681
9730
  // The parser should have reported an error in this case because it's not allowed.
9682
- getTypeOfExpression(node.expression.expandExpression, /* flags */ undefined, expectedValueOrElementType);
9731
+ getTypeOfExpression(node.expression.expandExpression,
9732
+ /* flags */ undefined, (0, typeUtils_1.makeInferenceContext)(expectedValueOrElementType));
9683
9733
  }
9684
9734
  else if ((0, parseNodes_1.isExpressionNode)(node)) {
9685
9735
  const exprTypeResult = getTypeOfExpression(node.expression,
9686
- /* flags */ undefined, expectedValueOrElementType);
9736
+ /* flags */ undefined, (0, typeUtils_1.makeInferenceContext)(expectedValueOrElementType));
9687
9737
  if (exprTypeResult.isIncomplete) {
9688
9738
  isIncomplete = true;
9689
9739
  }
@@ -9885,7 +9935,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9885
9935
  // If no type arguments are provided, the resulting type
9886
9936
  // depends on whether we're evaluating a type annotation or
9887
9937
  // we're in some other context.
9888
- if ((flags & 1024 /* ExpectingTypeAnnotation */) !== 0) {
9938
+ if ((flags & 256 /* ExpectingTypeAnnotation */) !== 0) {
9889
9939
  addError(localize_1.Localizer.Diagnostic.optionalExtraArgs(), errorNode);
9890
9940
  return types_1.UnknownType.create();
9891
9941
  }
@@ -10009,7 +10059,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10009
10059
  // Creates a ClassVar type.
10010
10060
  function createClassVarType(classType, errorNode, typeArgs, flags) {
10011
10061
  var _a;
10012
- if (flags & 131072 /* ClassVarDisallowed */) {
10062
+ if (flags & 131072 /* DisallowClassVar */) {
10013
10063
  addError(localize_1.Localizer.Diagnostic.classVarNotAllowed(), errorNode);
10014
10064
  return types_1.AnyType.create();
10015
10065
  }
@@ -10042,7 +10092,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10042
10092
  // depends on whether we're evaluating a type annotation or
10043
10093
  // we're in some other context.
10044
10094
  if (!typeArgs) {
10045
- if ((flags & 1024 /* ExpectingTypeAnnotation */) !== 0) {
10095
+ if ((flags & 256 /* ExpectingTypeAnnotation */) !== 0) {
10046
10096
  addError(localize_1.Localizer.Diagnostic.typeGuardArgCount(), errorNode);
10047
10097
  }
10048
10098
  return classType;
@@ -10103,7 +10153,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10103
10153
  // If no type arguments are provided, the resulting type
10104
10154
  // depends on whether we're evaluating a type annotation or
10105
10155
  // we're in some other context.
10106
- if (!typeArgs && (flags & 1024 /* ExpectingTypeAnnotation */) === 0) {
10156
+ if (!typeArgs && (flags & 256 /* ExpectingTypeAnnotation */) === 0) {
10107
10157
  return classType;
10108
10158
  }
10109
10159
  if (!typeArgs || typeArgs.length !== 1) {
@@ -10124,7 +10174,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10124
10174
  isUsageLegal = true;
10125
10175
  }
10126
10176
  }
10127
- if ((flags & 1048576 /* RequiredAllowed */) !== 0) {
10177
+ if ((flags & 1048576 /* AllowRequired */) !== 0) {
10128
10178
  isUsageLegal = true;
10129
10179
  }
10130
10180
  if (!isUsageLegal) {
@@ -10139,7 +10189,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10139
10189
  // If no type arguments are provided, the resulting type
10140
10190
  // depends on whether we're evaluating a type annotation or
10141
10191
  // we're in some other context.
10142
- if (!typeArgs && (flags & 1024 /* ExpectingTypeAnnotation */) === 0) {
10192
+ if (!typeArgs && (flags & 256 /* ExpectingTypeAnnotation */) === 0) {
10143
10193
  return classType;
10144
10194
  }
10145
10195
  if (!typeArgs || typeArgs.length !== 1) {
@@ -10173,7 +10223,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10173
10223
  }
10174
10224
  // Creates a "Final" type.
10175
10225
  function createFinalType(classType, errorNode, typeArgs, flags) {
10176
- if (flags & 16 /* FinalDisallowed */) {
10226
+ if (flags & 16 /* DisallowFinal */) {
10177
10227
  addError(localize_1.Localizer.Diagnostic.finalContext(), errorNode);
10178
10228
  return types_1.AnyType.create();
10179
10229
  }
@@ -10330,7 +10380,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10330
10380
  // If no type arguments are provided, the resulting type
10331
10381
  // depends on whether we're evaluating a type annotation or
10332
10382
  // we're in some other context.
10333
- if ((flags & 1024 /* ExpectingTypeAnnotation */) !== 0) {
10383
+ if ((flags & 256 /* ExpectingTypeAnnotation */) !== 0) {
10334
10384
  addError(localize_1.Localizer.Diagnostic.unionTypeArgCount(), errorNode);
10335
10385
  return types_1.NeverType.createNever();
10336
10386
  }
@@ -10381,7 +10431,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10381
10431
  // If no type arguments are provided, the resulting type
10382
10432
  // depends on whether we're evaluating a type annotation or
10383
10433
  // we're in some other context.
10384
- if ((flags & (1024 /* ExpectingTypeAnnotation */ | 262144 /* DisallowNakedGeneric */)) !== 0) {
10434
+ if ((flags & (256 /* ExpectingTypeAnnotation */ | 262144 /* DisallowNakedGeneric */)) !== 0) {
10385
10435
  addError(localize_1.Localizer.Diagnostic.genericTypeArgMissing(), errorNode);
10386
10436
  }
10387
10437
  return classType;
@@ -10611,7 +10661,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10611
10661
  let flags = 0 /* None */;
10612
10662
  if (fileInfo.isStubFile) {
10613
10663
  // An assignment of ellipsis means "Any" within a type stub file.
10614
- flags |= 256 /* ConvertEllipsisToUnknown */;
10664
+ flags |= 512 /* ConvertEllipsisToUnknown */;
10615
10665
  }
10616
10666
  if (node.rightExpression.nodeType === 38 /* Name */ ||
10617
10667
  node.rightExpression.nodeType === 35 /* MemberAccess */) {
@@ -10622,11 +10672,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10622
10672
  }
10623
10673
  if (isDeclaredTypeAlias(node.leftExpression)) {
10624
10674
  flags |=
10625
- 64 /* ExpectingType */ |
10675
+ 128 /* ExpectingType */ |
10626
10676
  8 /* EvaluateStringLiteralAsType */ |
10627
- 32 /* ParamSpecDisallowed */ |
10628
- 128 /* TypeVarTupleDisallowed */ |
10629
- 131072 /* ClassVarDisallowed */;
10677
+ 32 /* DisallowParamSpec */ |
10678
+ 64 /* DisallowTypeVarTuple */ |
10679
+ 131072 /* DisallowClassVar */;
10630
10680
  flags &= ~2 /* DoNotSpecialize */;
10631
10681
  }
10632
10682
  // Is this type already cached?
@@ -10684,7 +10734,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10684
10734
  /* flags */ undefined);
10685
10735
  }
10686
10736
  }
10687
- const srcTypeResult = getTypeOfExpression(node.rightExpression, flags, declaredType);
10737
+ const srcTypeResult = getTypeOfExpression(node.rightExpression, flags, (0, typeUtils_1.makeInferenceContext)(declaredType));
10688
10738
  let srcType = srcTypeResult.type;
10689
10739
  expectedTypeDiagAddendum = srcTypeResult.expectedTypeDiagAddendum;
10690
10740
  if (srcTypeResult.isIncomplete) {
@@ -10714,12 +10764,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10714
10764
  (0, enums_1.transformTypeForPossibleEnumClass)(evaluatorInterface, node.leftExpression, () => rightHandType) || rightHandType;
10715
10765
  }
10716
10766
  if (typeAliasNameNode) {
10717
- // Clear out the temporary types we wrote above.
10718
- deleteTypeCacheEntry(node);
10719
- deleteTypeCacheEntry(node.leftExpression);
10720
- if (node.leftExpression.nodeType === 54 /* TypeAnnotation */) {
10721
- deleteTypeCacheEntry(node.leftExpression.valueExpression);
10722
- }
10723
10767
  // If this was a speculative type alias, it becomes a real type alias
10724
10768
  // only if the evaluated type is an instantiable type.
10725
10769
  if (!isSpeculativeTypeAlias ||
@@ -10819,8 +10863,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10819
10863
  if (aliasTypeResult.isIncomplete) {
10820
10864
  isIncomplete = true;
10821
10865
  }
10822
- // Clear the temporary type we wrote above.
10823
- deleteTypeCacheEntry(node.name);
10824
10866
  aliasType = transformTypeForTypeAlias(aliasType, node.name, node.expression, typeParameters, (_a = node.typeParameters) === null || _a === void 0 ? void 0 : _a.parameters);
10825
10867
  if ((0, typeUtils_1.isTypeAliasRecursive)(typeAliasTypeVar, aliasType)) {
10826
10868
  addDiagnostic(AnalyzerNodeInfo.getFileInfo(node).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeAliasIsRecursiveDirect().format({
@@ -10843,7 +10885,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10843
10885
  if (readTypeCache(node, 0 /* None */)) {
10844
10886
  return;
10845
10887
  }
10846
- const destTypeResult = getTypeOfAugmentedAssignment(node, /* expectedType */ undefined);
10888
+ const destTypeResult = getTypeOfAugmentedAssignment(node, /* inferenceContext */ undefined);
10847
10889
  writeTypeCache(node, destTypeResult, 0 /* None */);
10848
10890
  }
10849
10891
  function getPseudoGenericTypeVarName(paramName) {
@@ -10917,8 +10959,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10917
10959
  let protocolTypeParameters;
10918
10960
  const initSubclassArgs = [];
10919
10961
  let metaclassNode;
10920
- let exprFlags = 64 /* ExpectingType */ |
10921
- 512 /* AllowGenericClassType */ |
10962
+ let exprFlags = 128 /* ExpectingType */ |
10963
+ 1024 /* AllowGenericClassType */ |
10922
10964
  262144 /* DisallowNakedGeneric */ |
10923
10965
  2048 /* DisallowTypeVarsWithScopeId */ |
10924
10966
  8192 /* AssociateTypeVarsWithCurrentScope */ |
@@ -11525,6 +11567,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
11525
11567
  originalClassType.details.classDataClassTransform = (0, dataClasses_1.validateDataClassTransformDecorator)(evaluatorInterface, decoratorNode.expression);
11526
11568
  }
11527
11569
  }
11570
+ if ((0, types_1.isOverloadedFunction)(decoratorCallType)) {
11571
+ if (decoratorCallType.overloads.length > 0 &&
11572
+ decoratorCallType.overloads[0].details.builtInName === 'deprecated') {
11573
+ originalClassType.details.deprecatedMessage = getCustomDeprecationMessage(decoratorNode);
11574
+ return inputClassType;
11575
+ }
11576
+ }
11528
11577
  }
11529
11578
  if ((0, types_1.isOverloadedFunction)(decoratorType)) {
11530
11579
  const dataclassBehaviors = (0, dataClasses_1.getDataclassDecoratorBehaviors)(decoratorType);
@@ -11533,6 +11582,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
11533
11582
  /* callNode */ undefined);
11534
11583
  return inputClassType;
11535
11584
  }
11585
+ if (decoratorType.overloads.length > 0 && decoratorType.overloads[0].details.builtInName === 'deprecated') {
11586
+ originalClassType.details.deprecatedMessage = getCustomDeprecationMessage(decoratorNode);
11587
+ return inputClassType;
11588
+ }
11536
11589
  }
11537
11590
  else if ((0, types_1.isFunction)(decoratorType)) {
11538
11591
  if (decoratorType.details.builtInName === 'final') {
@@ -11542,7 +11595,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
11542
11595
  // dependency between builtins, typing and _typeshed stubs.
11543
11596
  return inputClassType;
11544
11597
  }
11545
- else if (decoratorType.details.builtInName === 'runtime_checkable') {
11598
+ if (decoratorType.details.builtInName === 'runtime_checkable') {
11546
11599
  originalClassType.details.flags |= 32768 /* RuntimeCheckable */;
11547
11600
  // Don't call getTypeOfDecorator for runtime_checkable. It appears
11548
11601
  // frequently in stubs, and it's a waste of time to validate its
@@ -11568,6 +11621,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
11568
11621
  }
11569
11622
  return getTypeOfDecorator(decoratorNode, inputClassType);
11570
11623
  }
11624
+ // Given a @typing.deprecated decorator node, returns either '' or a custom
11625
+ // deprecation message if one is provided.
11626
+ function getCustomDeprecationMessage(decorator) {
11627
+ if (decorator.expression.nodeType === 9 /* Call */ &&
11628
+ decorator.expression.arguments.length > 0 &&
11629
+ decorator.expression.arguments[0].argumentCategory === 0 /* Simple */ &&
11630
+ decorator.expression.arguments[0].valueExpression.nodeType === 48 /* StringList */ &&
11631
+ decorator.expression.arguments[0].valueExpression.strings.length === 1) {
11632
+ return decorator.expression.arguments[0].valueExpression.strings[0].value;
11633
+ }
11634
+ return '';
11635
+ }
11571
11636
  // Runs any registered "callback hooks" that depend on the specified class type.
11572
11637
  // This allows us to complete any work that requires dependent classes to be
11573
11638
  // completed.
@@ -11602,7 +11667,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
11602
11667
  if (initSubclassMethodType) {
11603
11668
  validateCallArguments(errorNode, argList, { type: initSubclassMethodType },
11604
11669
  /* typeVarContext */ undefined,
11605
- /* skipUnknownArgCheck */ false, types_1.NoneType.createInstance());
11670
+ /* skipUnknownArgCheck */ false, (0, typeUtils_1.makeInferenceContext)(types_1.NoneType.createInstance()));
11606
11671
  }
11607
11672
  }
11608
11673
  else if (classType.details.effectiveMetaclass && (0, types_1.isClass)(classType.details.effectiveMetaclass)) {
@@ -11852,7 +11917,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
11852
11917
  }
11853
11918
  let defaultValueType;
11854
11919
  if (param.defaultValue) {
11855
- defaultValueType = getTypeOfExpression(param.defaultValue, 1 /* ConvertEllipsisToAny */, annotatedType).type;
11920
+ defaultValueType = getTypeOfExpression(param.defaultValue, 1 /* ConvertEllipsisToAny */, (0, typeUtils_1.makeInferenceContext)(annotatedType)).type;
11856
11921
  }
11857
11922
  if (annotatedType) {
11858
11923
  // If there was both a type annotation and a default value, verify
@@ -12110,7 +12175,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
12110
12175
  // parameter is annotated with a "raw" function-scoped type variable, as in:
12111
12176
  // "def foo(value: T = 3)"
12112
12177
  // In this case, we need to include the default value type in a union.
12113
- const defaultArgType = getTypeOfExpression(param.defaultValue, 1 /* ConvertEllipsisToAny */, type).type;
12178
+ const defaultArgType = getTypeOfExpression(param.defaultValue, 1 /* ConvertEllipsisToAny */, (0, typeUtils_1.makeInferenceContext)(type)).type;
12114
12179
  if (!(0, types_1.isAny)(defaultArgType)) {
12115
12180
  type = (0, types_1.combineTypes)([type, defaultArgType]);
12116
12181
  }
@@ -12310,6 +12375,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
12310
12375
  return inputFunctionType;
12311
12376
  }
12312
12377
  }
12378
+ if ((0, types_1.isOverloadedFunction)(decoratorCallType)) {
12379
+ if (decoratorCallType.overloads.length > 0 &&
12380
+ decoratorCallType.overloads[0].details.builtInName === 'deprecated') {
12381
+ undecoratedType.details.deprecatedMessage = getCustomDeprecationMessage(decoratorNode);
12382
+ return inputFunctionType;
12383
+ }
12384
+ }
12313
12385
  }
12314
12386
  let returnType = getTypeOfDecorator(decoratorNode, inputFunctionType);
12315
12387
  // Check for some built-in decorator types with known semantics.
@@ -12343,6 +12415,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
12343
12415
  }
12344
12416
  }
12345
12417
  }
12418
+ else if ((0, types_1.isOverloadedFunction)(decoratorType)) {
12419
+ if (decoratorType.overloads.length > 0 && decoratorType.overloads[0].details.builtInName === 'deprecated') {
12420
+ undecoratedType.details.deprecatedMessage = getCustomDeprecationMessage(decoratorNode);
12421
+ return inputFunctionType;
12422
+ }
12423
+ }
12346
12424
  else if ((0, types_1.isInstantiableClass)(decoratorType)) {
12347
12425
  if (types_1.ClassType.isBuiltIn(decoratorType)) {
12348
12426
  switch (decoratorType.details.name) {
@@ -12756,7 +12834,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
12756
12834
  const additionalHelp = new diagnostic_1.DiagnosticAddendum();
12757
12835
  if ((0, types_1.isClass)(subtype)) {
12758
12836
  let enterType = getTypeOfMagicMethodReturn(subtype, [], enterMethodName, node.expression,
12759
- /* expectedType */ undefined);
12837
+ /* inferenceContext */ undefined);
12760
12838
  if (enterType) {
12761
12839
  // For "async while", an implicit "await" is performed.
12762
12840
  if (isAsync) {
@@ -12766,7 +12844,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
12766
12844
  }
12767
12845
  if (!isAsync) {
12768
12846
  if (getTypeOfMagicMethodReturn(subtype, [], '__aenter__', node.expression,
12769
- /* expectedType */ undefined)) {
12847
+ /* inferenceContext */ undefined)) {
12770
12848
  additionalHelp.addMessage(localize_1.Localizer.DiagnosticAddendum.asyncHelp());
12771
12849
  }
12772
12850
  }
@@ -12786,7 +12864,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
12786
12864
  if ((0, types_1.isClass)(subtype)) {
12787
12865
  const anyArg = { type: types_1.AnyType.create() };
12788
12866
  const exitType = getTypeOfMagicMethodReturn(subtype, [anyArg, anyArg, anyArg], exitMethodName, node.expression,
12789
- /* expectedType */ undefined);
12867
+ /* inferenceContext */ undefined);
12790
12868
  if (exitType) {
12791
12869
  return;
12792
12870
  }
@@ -13170,18 +13248,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
13170
13248
  nodeToEvaluate = parent;
13171
13249
  continue;
13172
13250
  }
13173
- else {
13174
- // Check for expression types that are always contextual.
13175
- if (nodeToEvaluate.nodeType !== 15 /* Dictionary */ &&
13176
- nodeToEvaluate.nodeType !== 31 /* List */ &&
13177
- nodeToEvaluate.nodeType !== 30 /* Lambda */ &&
13178
- nodeToEvaluate.nodeType !== 45 /* Set */ &&
13179
- nodeToEvaluate.nodeType !== 52 /* Tuple */ &&
13180
- nodeToEvaluate.nodeType !== 56 /* Unpack */ &&
13181
- nodeToEvaluate.nodeType !== 32 /* ListComprehension */) {
13182
- break;
13183
- }
13184
- }
13185
13251
  if (!(0, parseNodes_1.isExpressionNode)(parent)) {
13186
13252
  // If we've hit a non-expression node, we generally want to
13187
13253
  // stop. However, there are a few special "pass through"
@@ -13268,7 +13334,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
13268
13334
  const declaredReturnType = enclosingFunctionNode
13269
13335
  ? getFunctionDeclaredReturnType(enclosingFunctionNode)
13270
13336
  : undefined;
13271
- getTypeOfExpression(parent.returnExpression, 0 /* None */, declaredReturnType);
13337
+ getTypeOfExpression(parent.returnExpression, 0 /* None */, (0, typeUtils_1.makeInferenceContext)(declaredReturnType));
13272
13338
  return;
13273
13339
  }
13274
13340
  break;
@@ -13444,16 +13510,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
13444
13510
  // don't bother doing additional work.
13445
13511
  let cacheEntry = readTypeCacheEntry(subnode);
13446
13512
  if (cacheEntry && !cacheEntry.typeResult.isIncomplete) {
13447
- return { type: cacheEntry.typeResult.type };
13513
+ return cacheEntry.typeResult;
13448
13514
  }
13449
13515
  callback();
13450
13516
  cacheEntry = readTypeCacheEntry(subnode);
13451
13517
  if (cacheEntry) {
13452
- return {
13453
- type: cacheEntry.typeResult.type,
13454
- isIncomplete: cacheEntry.typeResult.isIncomplete,
13455
- expectedTypeDiagAddendum: cacheEntry.typeResult.expectedTypeDiagAddendum,
13456
- };
13518
+ return cacheEntry.typeResult;
13457
13519
  }
13458
13520
  return undefined;
13459
13521
  }
@@ -13850,9 +13912,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
13850
13912
  return getTypeOfExpressionExpectingType(arg.valueExpression);
13851
13913
  }
13852
13914
  function getTypeOfExpressionExpectingType(node, options) {
13853
- let flags = 64 /* ExpectingType */ |
13854
- 8 /* EvaluateStringLiteralAsType */ |
13855
- 131072 /* ClassVarDisallowed */;
13915
+ let flags = 128 /* ExpectingType */ | 8 /* EvaluateStringLiteralAsType */ | 131072 /* DisallowClassVar */;
13856
13916
  const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
13857
13917
  if (fileInfo.isStubFile) {
13858
13918
  flags |= 4 /* AllowForwardReferences */;
@@ -13861,19 +13921,19 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
13861
13921
  flags |= 4194304 /* InterpreterParsesStringLiteral */;
13862
13922
  }
13863
13923
  if (!(options === null || options === void 0 ? void 0 : options.allowFinal)) {
13864
- flags |= 16 /* FinalDisallowed */;
13924
+ flags |= 16 /* DisallowFinal */;
13865
13925
  }
13866
13926
  if (options === null || options === void 0 ? void 0 : options.allowRequired) {
13867
- flags |= 1048576 /* RequiredAllowed */ | 1024 /* ExpectingTypeAnnotation */;
13927
+ flags |= 1048576 /* AllowRequired */ | 256 /* ExpectingTypeAnnotation */;
13868
13928
  }
13869
13929
  if (options === null || options === void 0 ? void 0 : options.allowUnpackedTuple) {
13870
13930
  flags |= 2097152 /* AllowUnpackedTupleOrTypeVarTuple */;
13871
13931
  }
13872
13932
  else {
13873
- flags |= 128 /* TypeVarTupleDisallowed */;
13933
+ flags |= 64 /* DisallowTypeVarTuple */;
13874
13934
  }
13875
13935
  if (!(options === null || options === void 0 ? void 0 : options.allowParamSpec)) {
13876
- flags |= 32 /* ParamSpecDisallowed */;
13936
+ flags |= 32 /* DisallowParamSpec */;
13877
13937
  }
13878
13938
  return getTypeOfExpression(node, flags);
13879
13939
  }
@@ -14004,8 +14064,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
14004
14064
  // Disables recording of errors and warnings and disables
14005
14065
  // any caching of types, under the assumption that we're
14006
14066
  // performing speculative evaluations.
14007
- function useSpeculativeMode(speculativeNode, callback) {
14008
- speculativeTypeTracker.enterSpeculativeContext(speculativeNode);
14067
+ function useSpeculativeMode(speculativeNode, callback, allowCacheRetention = true) {
14068
+ speculativeTypeTracker.enterSpeculativeContext(speculativeNode, allowCacheRetention);
14009
14069
  try {
14010
14070
  const result = callback();
14011
14071
  speculativeTypeTracker.leaveSpeculativeContext();
@@ -16225,6 +16285,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
16225
16285
  }
16226
16286
  });
16227
16287
  }
16288
+ else if (remainingSrcSubtypes.length === 0) {
16289
+ // If we've assigned all of the source subtypes but one or more dest
16290
+ // TypeVars have gone unmatched, treat this as success.
16291
+ return true;
16292
+ }
16228
16293
  else {
16229
16294
  // Try to assign a union of the remaining source types to
16230
16295
  // the first destination TypeVar.