@zzzen/pyright-internal 1.2.0-dev.20220717 → 1.2.0-dev.20220807

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 (156) hide show
  1. package/dist/analyzer/analysis.js +1 -1
  2. package/dist/analyzer/analysis.js.map +1 -1
  3. package/dist/analyzer/checker.d.ts +1 -0
  4. package/dist/analyzer/checker.js +57 -10
  5. package/dist/analyzer/checker.js.map +1 -1
  6. package/dist/analyzer/codeFlowEngine.js +4 -5
  7. package/dist/analyzer/codeFlowEngine.js.map +1 -1
  8. package/dist/analyzer/constraintSolver.js +9 -15
  9. package/dist/analyzer/constraintSolver.js.map +1 -1
  10. package/dist/analyzer/dataClasses.js +13 -4
  11. package/dist/analyzer/dataClasses.js.map +1 -1
  12. package/dist/analyzer/enums.d.ts +13 -0
  13. package/dist/analyzer/enums.js +207 -0
  14. package/dist/analyzer/enums.js.map +1 -0
  15. package/dist/analyzer/importResolver.js +2 -2
  16. package/dist/analyzer/importResolver.js.map +1 -1
  17. package/dist/analyzer/importStatementUtils.d.ts +8 -2
  18. package/dist/analyzer/importStatementUtils.js +75 -27
  19. package/dist/analyzer/importStatementUtils.js.map +1 -1
  20. package/dist/analyzer/parseTreeUtils.d.ts +2 -0
  21. package/dist/analyzer/parseTreeUtils.js +76 -2
  22. package/dist/analyzer/parseTreeUtils.js.map +1 -1
  23. package/dist/analyzer/patternMatching.js +3 -3
  24. package/dist/analyzer/patternMatching.js.map +1 -1
  25. package/dist/analyzer/program.d.ts +2 -2
  26. package/dist/analyzer/program.js +10 -9
  27. package/dist/analyzer/program.js.map +1 -1
  28. package/dist/analyzer/properties.js +1 -1
  29. package/dist/analyzer/properties.js.map +1 -1
  30. package/dist/analyzer/service.d.ts +3 -2
  31. package/dist/analyzer/service.js +40 -39
  32. package/dist/analyzer/service.js.map +1 -1
  33. package/dist/analyzer/sourceFile.d.ts +3 -1
  34. package/dist/analyzer/sourceFile.js +6 -0
  35. package/dist/analyzer/sourceFile.js.map +1 -1
  36. package/dist/analyzer/typeEvaluator.js +631 -654
  37. package/dist/analyzer/typeEvaluator.js.map +1 -1
  38. package/dist/analyzer/typeEvaluatorTypes.d.ts +14 -12
  39. package/dist/analyzer/typeEvaluatorTypes.js.map +1 -1
  40. package/dist/analyzer/typeEvaluatorWithTracker.js +1 -0
  41. package/dist/analyzer/typeEvaluatorWithTracker.js.map +1 -1
  42. package/dist/analyzer/typeGuards.d.ts +2 -0
  43. package/dist/analyzer/typeGuards.js +42 -14
  44. package/dist/analyzer/typeGuards.js.map +1 -1
  45. package/dist/analyzer/typePrinter.js +8 -4
  46. package/dist/analyzer/typePrinter.js.map +1 -1
  47. package/dist/analyzer/typeUtils.d.ts +4 -4
  48. package/dist/analyzer/typeUtils.js +23 -38
  49. package/dist/analyzer/typeUtils.js.map +1 -1
  50. package/dist/analyzer/typedDicts.js +2 -2
  51. package/dist/analyzer/typedDicts.js.map +1 -1
  52. package/dist/analyzer/types.d.ts +7 -1
  53. package/dist/analyzer/types.js +70 -31
  54. package/dist/analyzer/types.js.map +1 -1
  55. package/dist/backgroundAnalysisBase.d.ts +2 -1
  56. package/dist/backgroundAnalysisBase.js +7 -0
  57. package/dist/backgroundAnalysisBase.js.map +1 -1
  58. package/dist/backgroundThreadBase.js +1 -0
  59. package/dist/backgroundThreadBase.js.map +1 -1
  60. package/dist/commands/commandResult.js +1 -1
  61. package/dist/commands/commandResult.js.map +1 -1
  62. package/dist/common/chokidarFileWatcherProvider.d.ts +1 -2
  63. package/dist/common/chokidarFileWatcherProvider.js +0 -3
  64. package/dist/common/chokidarFileWatcherProvider.js.map +1 -1
  65. package/dist/common/configOptions.d.ts +1 -0
  66. package/dist/common/configOptions.js +4 -0
  67. package/dist/common/configOptions.js.map +1 -1
  68. package/dist/common/diagnosticRules.d.ts +1 -0
  69. package/dist/common/diagnosticRules.js +1 -0
  70. package/dist/common/diagnosticRules.js.map +1 -1
  71. package/dist/common/editAction.d.ts +6 -0
  72. package/dist/common/editAction.js +15 -0
  73. package/dist/common/editAction.js.map +1 -1
  74. package/dist/common/fileSystem.d.ts +4 -1
  75. package/dist/common/fileSystem.js +6 -4
  76. package/dist/common/fileSystem.js.map +1 -1
  77. package/dist/common/realFileSystem.d.ts +2 -2
  78. package/dist/common/realFileSystem.js.map +1 -1
  79. package/dist/common/textEditUtils.d.ts +22 -2
  80. package/dist/common/textEditUtils.js +138 -1
  81. package/dist/common/textEditUtils.js.map +1 -1
  82. package/dist/common/uriParser.d.ts +2 -0
  83. package/dist/common/uriParser.js +11 -0
  84. package/dist/common/uriParser.js.map +1 -1
  85. package/dist/common/workspaceEditUtils.d.ts +1 -0
  86. package/dist/common/workspaceEditUtils.js +10 -6
  87. package/dist/common/workspaceEditUtils.js.map +1 -1
  88. package/dist/languageServerBase.d.ts +15 -10
  89. package/dist/languageServerBase.js +47 -43
  90. package/dist/languageServerBase.js.map +1 -1
  91. package/dist/languageService/autoImporter.d.ts +23 -17
  92. package/dist/languageService/autoImporter.js +42 -30
  93. package/dist/languageService/autoImporter.js.map +1 -1
  94. package/dist/languageService/codeActionProvider.d.ts +2 -2
  95. package/dist/languageService/codeActionProvider.js +1 -1
  96. package/dist/languageService/codeActionProvider.js.map +1 -1
  97. package/dist/languageService/completionProvider.d.ts +2 -1
  98. package/dist/languageService/completionProvider.js +5 -2
  99. package/dist/languageService/completionProvider.js.map +1 -1
  100. package/dist/languageService/importAdder.d.ts +2 -1
  101. package/dist/languageService/importAdder.js +9 -3
  102. package/dist/languageService/importAdder.js.map +1 -1
  103. package/dist/languageService/quickActions.js +1 -1
  104. package/dist/languageService/quickActions.js.map +1 -1
  105. package/dist/languageService/renameModuleProvider.d.ts +1 -4
  106. package/dist/languageService/renameModuleProvider.js +12 -48
  107. package/dist/languageService/renameModuleProvider.js.map +1 -1
  108. package/dist/localization/localize.d.ts +8 -0
  109. package/dist/localization/localize.js +2 -0
  110. package/dist/localization/localize.js.map +1 -1
  111. package/dist/localization/package.nls.en-us.json +2 -0
  112. package/dist/parser/parser.d.ts +1 -0
  113. package/dist/parser/parser.js +9 -2
  114. package/dist/parser/parser.js.map +1 -1
  115. package/dist/pyrightFileSystem.d.ts +19 -1
  116. package/dist/pyrightFileSystem.js +18 -1
  117. package/dist/pyrightFileSystem.js.map +1 -1
  118. package/dist/server.js +4 -4
  119. package/dist/server.js.map +1 -1
  120. package/dist/tests/chainedSourceFiles.test.js +2 -0
  121. package/dist/tests/chainedSourceFiles.test.js.map +1 -1
  122. package/dist/tests/checker.test.js +9 -0
  123. package/dist/tests/checker.test.js.map +1 -1
  124. package/dist/tests/fourslash/completions.commitChars.fourslash.js +12 -0
  125. package/dist/tests/fourslash/completions.commitChars.fourslash.js.map +1 -1
  126. package/dist/tests/fourslash/hover.init.fourslash.js +2 -2
  127. package/dist/tests/fourslash/hover.init.fourslash.js.map +1 -1
  128. package/dist/tests/harness/fourslash/fourSlashParser.js +5 -1
  129. package/dist/tests/harness/fourslash/fourSlashParser.js.map +1 -1
  130. package/dist/tests/harness/fourslash/testLanguageService.js +1 -1
  131. package/dist/tests/harness/fourslash/testLanguageService.js.map +1 -1
  132. package/dist/tests/harness/fourslash/testState.js +8 -7
  133. package/dist/tests/harness/fourslash/testState.js.map +1 -1
  134. package/dist/tests/importAdder.test.js +17 -2
  135. package/dist/tests/importAdder.test.js.map +1 -1
  136. package/dist/tests/renameModuleTestUtils.js +5 -87
  137. package/dist/tests/renameModuleTestUtils.js.map +1 -1
  138. package/dist/tests/testState.test.js +1 -1
  139. package/dist/tests/testState.test.js.map +1 -1
  140. package/dist/tests/testStateUtils.d.ts +8 -0
  141. package/dist/tests/testStateUtils.js +110 -0
  142. package/dist/tests/testStateUtils.js.map +1 -0
  143. package/dist/tests/testUtils.js +1 -1
  144. package/dist/tests/testUtils.js.map +1 -1
  145. package/dist/tests/typeEvaluator1.test.js +5 -5
  146. package/dist/tests/typeEvaluator1.test.js.map +1 -1
  147. package/dist/tests/typeEvaluator2.test.js +4 -0
  148. package/dist/tests/typeEvaluator2.test.js.map +1 -1
  149. package/dist/tests/typeEvaluator3.test.js +12 -0
  150. package/dist/tests/typeEvaluator3.test.js.map +1 -1
  151. package/dist/tests/typeEvaluator4.test.js +12 -0
  152. package/dist/tests/typeEvaluator4.test.js.map +1 -1
  153. package/dist/workspaceMap.d.ts +1 -0
  154. package/dist/workspaceMap.js +11 -1
  155. package/dist/workspaceMap.js.map +1 -1
  156. package/package.json +3 -3
@@ -55,6 +55,7 @@ const constraintSolver_1 = require("./constraintSolver");
55
55
  const constructorTransform_1 = require("./constructorTransform");
56
56
  const dataClasses_1 = require("./dataClasses");
57
57
  const declarationUtils_1 = require("./declarationUtils");
58
+ const enums_1 = require("./enums");
58
59
  const functionTransform_1 = require("./functionTransform");
59
60
  const namedTuples_1 = require("./namedTuples");
60
61
  const ParseTreeUtils = __importStar(require("./parseTreeUtils"));
@@ -106,33 +107,31 @@ var MemberAccessFlags;
106
107
  MemberAccessFlags[MemberAccessFlags["SkipTypeBaseClass"] = 256] = "SkipTypeBaseClass";
107
108
  })(MemberAccessFlags || (MemberAccessFlags = {}));
108
109
  // Maps binary operators to the magic methods that implement them.
109
- // The boolean indicates whether the operators "chain" together.
110
110
  const binaryOperatorMap = {
111
- [0 /* Add */]: ['__add__', '__radd__', false],
112
- [33 /* Subtract */]: ['__sub__', '__rsub__', false],
113
- [26 /* Multiply */]: ['__mul__', '__rmul__', false],
114
- [13 /* FloorDivide */]: ['__floordiv__', '__rfloordiv__', false],
115
- [10 /* Divide */]: ['__truediv__', '__rtruediv__', false],
116
- [24 /* Mod */]: ['__mod__', '__rmod__', false],
117
- [29 /* Power */]: ['__pow__', '__rpow__', false],
118
- [22 /* MatrixMultiply */]: ['__matmul__', '__rmatmul__', false],
119
- [3 /* BitwiseAnd */]: ['__and__', '__rand__', false],
120
- [6 /* BitwiseOr */]: ['__or__', '__ror__', false],
121
- [8 /* BitwiseXor */]: ['__xor__', '__rxor__', false],
122
- [17 /* LeftShift */]: ['__lshift__', '__rlshift__', false],
123
- [31 /* RightShift */]: ['__rshift__', '__rrshift__', false],
124
- [12 /* Equals */]: ['__eq__', '__ne__', true],
125
- [28 /* NotEquals */]: ['__ne__', '__eq__', true],
126
- [20 /* LessThan */]: ['__lt__', '__ge__', true],
127
- [21 /* LessThanOrEqual */]: ['__le__', '__gt__', true],
128
- [15 /* GreaterThan */]: ['__gt__', '__le__', true],
129
- [16 /* GreaterThanOrEqual */]: ['__ge__', '__lt__', true],
111
+ [0 /* Add */]: ['__add__', '__radd__'],
112
+ [33 /* Subtract */]: ['__sub__', '__rsub__'],
113
+ [26 /* Multiply */]: ['__mul__', '__rmul__'],
114
+ [13 /* FloorDivide */]: ['__floordiv__', '__rfloordiv__'],
115
+ [10 /* Divide */]: ['__truediv__', '__rtruediv__'],
116
+ [24 /* Mod */]: ['__mod__', '__rmod__'],
117
+ [29 /* Power */]: ['__pow__', '__rpow__'],
118
+ [22 /* MatrixMultiply */]: ['__matmul__', '__rmatmul__'],
119
+ [3 /* BitwiseAnd */]: ['__and__', '__rand__'],
120
+ [6 /* BitwiseOr */]: ['__or__', '__ror__'],
121
+ [8 /* BitwiseXor */]: ['__xor__', '__rxor__'],
122
+ [17 /* LeftShift */]: ['__lshift__', '__rlshift__'],
123
+ [31 /* RightShift */]: ['__rshift__', '__rrshift__'],
124
+ [12 /* Equals */]: ['__eq__', '__ne__'],
125
+ [28 /* NotEquals */]: ['__ne__', '__eq__'],
126
+ [20 /* LessThan */]: ['__lt__', '__ge__'],
127
+ [21 /* LessThanOrEqual */]: ['__le__', '__gt__'],
128
+ [15 /* GreaterThan */]: ['__gt__', '__le__'],
129
+ [16 /* GreaterThanOrEqual */]: ['__ge__', '__lt__'],
130
130
  };
131
- // Maps boolean operators to a boolean value indicating whether
132
- // the operators "chain" together with other comparison operators.
131
+ // Map of operators that always return a bool result.
133
132
  const booleanOperatorMap = {
134
- [36 /* And */]: false,
135
- [37 /* Or */]: false,
133
+ [36 /* And */]: true,
134
+ [37 /* Or */]: true,
136
135
  [39 /* Is */]: true,
137
136
  [40 /* IsNot */]: true,
138
137
  [41 /* In */]: true,
@@ -219,7 +218,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
219
218
  const asymmetricDescriptorAssignmentCache = new Set();
220
219
  const speculativeTypeTracker = new typeCache_1.SpeculativeTypeTracker();
221
220
  const suppressedNodeStack = [];
222
- let functionRecursionMap = new Map();
221
+ let functionRecursionMap = new Set();
223
222
  let codeFlowAnalyzerCache = new Map();
224
223
  let typeCache = new Map();
225
224
  let effectiveTypeCache = new Map();
@@ -266,7 +265,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
266
265
  // circular references in complex data structures, so it fails
267
266
  // to clean up the objects if we don't help it out.
268
267
  function disposeEvaluator() {
269
- functionRecursionMap = new Map();
268
+ functionRecursionMap = new Set();
270
269
  codeFlowAnalyzerCache = new Map();
271
270
  typeCache = new Map();
272
271
  effectiveTypeCache = new Map();
@@ -283,6 +282,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
283
282
  return cachedType !== undefined;
284
283
  }
285
284
  function readTypeCache(node, flags) {
285
+ var _a, _b;
286
286
  let cachedType;
287
287
  // Should we use a temporary cache associated with a contextual
288
288
  // analysis of a function, contextualized based on call-site argument types?
@@ -299,8 +299,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
299
299
  if (flags !== undefined) {
300
300
  const expectedFlags = typeCacheFlags.get(node.id);
301
301
  if (expectedFlags !== undefined && flags !== expectedFlags) {
302
- (0, debug_1.fail)(`Type cache flag mismatch for node type ${node.nodeType}: ` +
303
- `cached flags = ${expectedFlags}, access flags = ${flags}`);
302
+ const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
303
+ const position = (0, positionUtils_1.convertOffsetToPosition)(node.start, fileInfo.lines);
304
+ const message = `Type cache flag mismatch for node type ${node.nodeType} ` +
305
+ `(parent ${(_b = (_a = node.parent) === null || _a === void 0 ? void 0 : _a.nodeType) !== null && _b !== void 0 ? _b : 'none'}): ` +
306
+ `cached flags = ${expectedFlags}, access flags = ${flags}, ` +
307
+ `file = {${fileInfo.filePath} [${position.line + 1}:${position.character + 1}]}`;
308
+ if (evaluatorOptions.verifyTypeCacheEvaluatorFlags) {
309
+ (0, debug_1.fail)(message);
310
+ }
311
+ else {
312
+ console.log(message);
313
+ }
304
314
  }
305
315
  }
306
316
  }
@@ -428,9 +438,22 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
428
438
  // context. For example, if it's a subexpression of an argument expression,
429
439
  // the associated parameter type might inform the expected type.
430
440
  function getExpectedType(node) {
431
- evaluateTypesForExpressionInContext(node);
441
+ // Scan up the parse tree to find the top-most expression node
442
+ // so we can evaluate the entire expression.
443
+ let topExpression = node;
432
444
  let curNode = node;
433
- while (curNode !== undefined) {
445
+ while (curNode) {
446
+ if ((0, parseNodes_1.isExpressionNode)(curNode)) {
447
+ topExpression = curNode;
448
+ }
449
+ curNode = curNode.parent;
450
+ }
451
+ // Evaluate the expression. This will have the side effect of
452
+ // storing an expected type in the expected type cache.
453
+ evaluateTypesForExpressionInContext(topExpression);
454
+ // Look for the resulting expected type by scanning up the parse tree.
455
+ curNode = node;
456
+ while (curNode) {
434
457
  const expectedType = expectedTypeCache.get(curNode.id);
435
458
  if (expectedType) {
436
459
  return {
@@ -438,6 +461,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
438
461
  node: curNode,
439
462
  };
440
463
  }
464
+ if (curNode === topExpression) {
465
+ break;
466
+ }
441
467
  curNode = curNode.parent;
442
468
  }
443
469
  return undefined;
@@ -470,7 +496,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
470
496
  if (printExpressionTypes) {
471
497
  console.log(`${getPrintExpressionTypesSpaces()}${ParseTreeUtils.printExpression(node)} (${getLineNum(node)}): Cached ${printType(cachedType)}`);
472
498
  }
473
- return { type: cachedType, node };
499
+ return { type: cachedType };
474
500
  }
475
501
  else {
476
502
  // Is it cached in the speculative type cache?
@@ -479,7 +505,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
479
505
  if (printExpressionTypes) {
480
506
  console.log(`${getPrintExpressionTypesSpaces()}${ParseTreeUtils.printExpression(node)} (${getLineNum(node)}): Speculative ${printType(speculativeCachedType)}`);
481
507
  }
482
- return { type: speculativeCachedType, node };
508
+ return { type: speculativeCachedType };
483
509
  }
484
510
  }
485
511
  if (printExpressionTypes) {
@@ -631,7 +657,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
631
657
  getTypeOfExpression(node.child);
632
658
  }
633
659
  });
634
- typeResult = { type: types_1.UnknownType.create(), node };
660
+ typeResult = { type: types_1.UnknownType.create() };
635
661
  break;
636
662
  }
637
663
  default:
@@ -683,7 +709,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
683
709
  const exprTypeResult = getTypeOfExpression(node.expression, flags, effectiveExpectedType);
684
710
  const typeResult = {
685
711
  type: getTypeOfAwaitable(exprTypeResult.type, node.expression),
686
- node,
687
712
  };
688
713
  if (exprTypeResult.isIncomplete) {
689
714
  typeResult.isIncomplete = true;
@@ -692,26 +717,26 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
692
717
  }
693
718
  function getTypeOfEllipsis(flags, typeResult, node) {
694
719
  if ((flags & 1 /* ConvertEllipsisToAny */) !== 0) {
695
- typeResult = { type: types_1.AnyType.create(/* isEllipsis */ true), node };
720
+ typeResult = { type: types_1.AnyType.create(/* isEllipsis */ true) };
696
721
  }
697
722
  else if ((flags & 256 /* ConvertEllipsisToUnknown */) !== 0) {
698
- typeResult = { type: types_1.UnknownType.create(), node };
723
+ typeResult = { type: types_1.UnknownType.create() };
699
724
  }
700
725
  else {
701
726
  const ellipsisType = getBuiltInObject(node, 'ellipsis') || types_1.AnyType.create();
702
- typeResult = { type: ellipsisType, node };
727
+ typeResult = { type: ellipsisType };
703
728
  }
704
729
  return typeResult;
705
730
  }
706
731
  function getTypeOfNumber(node, typeResult) {
707
732
  if (node.isImaginary) {
708
- typeResult = { node, type: getBuiltInObject(node, 'complex') };
733
+ typeResult = { type: getBuiltInObject(node, 'complex') };
709
734
  }
710
735
  else if (node.isInteger) {
711
- typeResult = { node, type: cloneBuiltinObjectWithLiteral(node, 'int', node.value) };
736
+ typeResult = { type: cloneBuiltinObjectWithLiteral(node, 'int', node.value) };
712
737
  }
713
738
  else {
714
- typeResult = { node, type: getBuiltInObject(node, 'float') };
739
+ typeResult = { type: getBuiltInObject(node, 'float') };
715
740
  }
716
741
  return typeResult;
717
742
  }
@@ -730,17 +755,17 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
730
755
  if ((flags & 128 /* TypeVarTupleDisallowed */) === 0 &&
731
756
  (0, types_1.isVariadicTypeVar)(iterType) &&
732
757
  !iterType.isVariadicUnpacked) {
733
- typeResult = { type: types_1.TypeVarType.cloneForUnpacked(iterType), node };
758
+ typeResult = { type: types_1.TypeVarType.cloneForUnpacked(iterType) };
734
759
  }
735
760
  else {
736
761
  if ((flags & 2097152 /* AllowUnpackedTupleOrTypeVarTuple */) !== 0 &&
737
762
  (0, types_1.isInstantiableClass)(iterType) &&
738
763
  types_1.ClassType.isBuiltIn(iterType, 'tuple')) {
739
- typeResult = { type: types_1.ClassType.cloneForUnpacked(iterType), node };
764
+ typeResult = { type: types_1.ClassType.cloneForUnpacked(iterType) };
740
765
  }
741
766
  else {
742
767
  const type = (_a = getTypeOfIterator(iterType, /* isAsync */ false, node)) !== null && _a !== void 0 ? _a : types_1.UnknownType.create(!!iterTypeResult.isIncomplete);
743
- typeResult = { type, unpackedType: iterType, node, isIncomplete: iterTypeResult.isIncomplete };
768
+ typeResult = { type, unpackedType: iterType, isIncomplete: iterTypeResult.isIncomplete };
744
769
  }
745
770
  }
746
771
  return typeResult;
@@ -769,30 +794,51 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
769
794
  if (!typeResult) {
770
795
  const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
771
796
  addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.expectedTypeNotString(), node);
772
- typeResult = { node, type: types_1.UnknownType.create() };
797
+ typeResult = { type: types_1.UnknownType.create() };
773
798
  }
774
799
  }
775
800
  else {
776
801
  // Evaluate the format string expressions in this context.
777
- node.strings.forEach((str) => {
778
- if (str.nodeType === 27 /* FormatString */) {
779
- str.expressions.forEach((expr) => {
780
- getTypeOfExpression(expr);
781
- });
802
+ let isLiteralString = true;
803
+ let isIncomplete = false;
804
+ node.strings.forEach((expr) => {
805
+ const typeResult = getTypeOfString(expr);
806
+ if (typeResult.isIncomplete) {
807
+ isIncomplete = true;
808
+ }
809
+ let isExprLiteralString = false;
810
+ if ((0, types_1.isClassInstance)(typeResult.type)) {
811
+ if (types_1.ClassType.isBuiltIn(typeResult.type, 'str') && typeResult.type.literalValue !== undefined) {
812
+ isExprLiteralString = true;
813
+ }
814
+ else if (types_1.ClassType.isBuiltIn(typeResult === null || typeResult === void 0 ? void 0 : typeResult.type, 'LiteralString')) {
815
+ isExprLiteralString = true;
816
+ }
817
+ }
818
+ if (!isExprLiteralString) {
819
+ isLiteralString = false;
782
820
  }
783
821
  });
784
822
  const isBytes = (node.strings[0].token.flags & 32 /* Bytes */) !== 0;
785
823
  // Don't create a literal type if it's an f-string.
786
824
  if (node.strings.some((str) => str.nodeType === 27 /* FormatString */)) {
787
- typeResult = {
788
- node,
789
- type: getBuiltInObject(node, isBytes ? 'bytes' : 'str'),
790
- };
825
+ if (isLiteralString) {
826
+ const literalStringType = getTypingType(node, 'LiteralString');
827
+ if (literalStringType && (0, types_1.isInstantiableClass)(literalStringType)) {
828
+ typeResult = { type: types_1.ClassType.cloneAsInstance(literalStringType) };
829
+ }
830
+ }
831
+ if (!typeResult) {
832
+ typeResult = {
833
+ type: getBuiltInObject(node, isBytes ? 'bytes' : 'str'),
834
+ isIncomplete,
835
+ };
836
+ }
791
837
  }
792
838
  else {
793
839
  typeResult = {
794
- node,
795
840
  type: cloneBuiltinObjectWithLiteral(node, isBytes ? 'bytes' : 'str', node.strings.map((s) => s.value).join('')),
841
+ isIncomplete,
796
842
  };
797
843
  }
798
844
  }
@@ -803,19 +849,62 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
803
849
  let typeResult;
804
850
  // Don't create a literal type if it's an f-string.
805
851
  if (node.nodeType === 27 /* FormatString */) {
806
- typeResult = {
807
- node,
808
- type: getBuiltInObject(node, isBytes ? 'bytes' : 'str'),
809
- };
852
+ let isLiteralString = true;
853
+ // If all of the format expressions are of type LiteralString, then
854
+ // the resulting formatted string is also LiteralString.
855
+ node.expressions.forEach((expr) => {
856
+ const exprType = getTypeOfExpression(expr).type;
857
+ if (!(0, types_1.isClassInstance)(exprType)) {
858
+ isLiteralString = false;
859
+ return;
860
+ }
861
+ if (types_1.ClassType.isBuiltIn(exprType, 'LiteralString')) {
862
+ return;
863
+ }
864
+ if (types_1.ClassType.isBuiltIn(exprType, 'str') && exprType.literalValue !== undefined) {
865
+ return;
866
+ }
867
+ isLiteralString = false;
868
+ });
869
+ if (!isBytes && isLiteralString) {
870
+ const literalStringType = getTypingType(node, 'LiteralString');
871
+ if (literalStringType && (0, types_1.isInstantiableClass)(literalStringType)) {
872
+ typeResult = { type: types_1.ClassType.cloneAsInstance(literalStringType) };
873
+ }
874
+ }
875
+ if (!typeResult) {
876
+ typeResult = {
877
+ type: getBuiltInObject(node, isBytes ? 'bytes' : 'str'),
878
+ };
879
+ }
810
880
  }
811
881
  else {
812
882
  typeResult = {
813
- node,
814
883
  type: cloneBuiltinObjectWithLiteral(node, isBytes ? 'bytes' : 'str', node.value),
815
884
  };
816
885
  }
817
886
  return typeResult;
818
887
  }
888
+ function stripLiteralValue(type) {
889
+ if ((0, types_1.isClass)(type)) {
890
+ if (type.literalValue !== undefined) {
891
+ type = types_1.ClassType.cloneWithLiteral(type, /* value */ undefined);
892
+ }
893
+ else if (types_1.ClassType.isBuiltIn(type, 'LiteralString')) {
894
+ // Handle "LiteralString" specially.
895
+ if (strClassType && (0, types_1.isInstantiableClass)(strClassType)) {
896
+ type = types_1.ClassType.cloneAsInstance(strClassType);
897
+ }
898
+ }
899
+ return type;
900
+ }
901
+ if ((0, types_1.isUnion)(type)) {
902
+ return (0, typeUtils_1.mapSubtypes)(type, (subtype) => {
903
+ return stripLiteralValue(subtype);
904
+ });
905
+ }
906
+ return type;
907
+ }
819
908
  function getTypeOfParameterAnnotation(paramTypeNode, paramCategory) {
820
909
  return getTypeOfAnnotation(paramTypeNode, {
821
910
  associateTypeVarsWithScope: true,
@@ -1143,8 +1232,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
1143
1232
  /* isAccessedThroughObject */ true, memberName, usage, diag, memberAccessFlags | 8 /* DisallowClassVarWrites */, bindToType);
1144
1233
  if (memberInfo) {
1145
1234
  return {
1146
- node: errorNode,
1147
1235
  type: memberInfo.type,
1236
+ classType: memberInfo.classType,
1148
1237
  isIncomplete: !!memberInfo.isTypeIncomplete,
1149
1238
  isAsymmetricDescriptor: memberInfo.isAsymmetricDescriptor,
1150
1239
  };
@@ -1157,7 +1246,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
1157
1246
  let memberInfo;
1158
1247
  if (types_1.ClassType.isPartiallyEvaluated(classType)) {
1159
1248
  addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.classDefinitionCycle().format({ name: classType.details.name }), errorNode);
1160
- return { node: errorNode, type: types_1.UnknownType.create() };
1249
+ return { type: types_1.UnknownType.create() };
1161
1250
  }
1162
1251
  if ((memberAccessFlags & 32 /* ConsiderMetaclassOnly */) === 0) {
1163
1252
  memberInfo = getTypeOfClassMemberName(errorNode, classType,
@@ -1189,7 +1278,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
1189
1278
  }
1190
1279
  if (memberInfo) {
1191
1280
  return {
1192
- node: errorNode,
1193
1281
  type: memberInfo.type,
1194
1282
  isIncomplete: !!memberInfo.isTypeIncomplete,
1195
1283
  isAsymmetricDescriptor: memberInfo.isAsymmetricDescriptor,
@@ -1509,6 +1597,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
1509
1597
  const iterMethodName = isAsync ? '__aiter__' : '__iter__';
1510
1598
  const nextMethodName = isAsync ? '__anext__' : '__next__';
1511
1599
  let isValidIterator = true;
1600
+ type = (0, typeUtils_1.transformPossibleRecursiveTypeAlias)(type);
1512
1601
  type = makeTopLevelTypeVarsConcrete(type);
1513
1602
  if ((0, typeUtils_1.isOptionalType)(type)) {
1514
1603
  if (errorNode) {
@@ -1884,7 +1973,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
1884
1973
  if (types_1.TypeBase.isInstance(destType) &&
1885
1974
  !(0, symbolNameUtils_1.isConstantName)(nameValue) &&
1886
1975
  !(0, symbolUtils_1.isFinalVariable)(symbolWithScope.symbol)) {
1887
- destType = (0, typeUtils_1.stripLiteralValue)(destType);
1976
+ destType = stripLiteralValue(destType);
1888
1977
  }
1889
1978
  }
1890
1979
  }
@@ -1904,9 +1993,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
1904
1993
  if (!isTypeIncomplete) {
1905
1994
  reportPossibleUnknownAssignment(fileInfo.diagnosticRuleSet.reportUnknownVariableType, diagnosticRules_1.DiagnosticRule.reportUnknownVariableType, nameNode, destType, nameNode, ignoreEmptyContainers);
1906
1995
  }
1907
- writeTypeCache(nameNode, destType, 0 /* None */, isTypeIncomplete,
1908
- /* expectedType */ undefined,
1909
- /* allowSpeculativeCaching */ false);
1996
+ writeTypeCache(nameNode, destType, 0 /* None */, isTypeIncomplete);
1910
1997
  }
1911
1998
  function assignTypeToMemberAccessNode(target, type, isTypeIncomplete, srcExpr, expectedTypeDiagAddendum) {
1912
1999
  const baseTypeResult = getTypeOfExpression(target.leftExpression, 2 /* DoNotSpecialize */);
@@ -1950,12 +2037,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
1950
2037
  if (setTypeResult.isAsymmetricDescriptor) {
1951
2038
  setAsymmetricDescriptorAssignment(target);
1952
2039
  }
1953
- writeTypeCache(target.memberName, type, 0 /* None */, isTypeIncomplete,
1954
- /* expectedType */ undefined,
1955
- /* allowSpeculativeCaching */ false);
1956
- writeTypeCache(target, type, 0 /* None */, isTypeIncomplete,
1957
- /* expectedType */ undefined,
1958
- /* allowSpeculativeCaching */ false);
2040
+ writeTypeCache(target.memberName, type, 0 /* None */, isTypeIncomplete);
2041
+ writeTypeCache(target, type, 0 /* None */, isTypeIncomplete);
1959
2042
  }
1960
2043
  function assignTypeToMemberVariable(node, srcType, isTypeIncomplete, isInstanceMember, srcExprNode) {
1961
2044
  const memberName = node.memberName.value;
@@ -2077,7 +2160,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
2077
2160
  const removedEntries = sourceEntryTypes.splice(targetUnpackIndex, sourceEntryTypes.length - targetTypes.length + 1);
2078
2161
  let combinedTypes = (0, types_1.combineTypes)(removedEntries);
2079
2162
  if (target.nodeType === 31 /* List */) {
2080
- combinedTypes = (0, typeUtils_1.stripLiteralValue)(combinedTypes);
2163
+ combinedTypes = stripLiteralValue(combinedTypes);
2081
2164
  }
2082
2165
  sourceEntryTypes.splice(targetUnpackIndex, 0, combinedTypes);
2083
2166
  }
@@ -2164,7 +2247,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
2164
2247
  if (tupleClassType && (0, types_1.isInstantiableClass)(tupleClassType)) {
2165
2248
  return (0, typeUtils_1.convertToInstance)((0, typeUtils_1.specializeTupleClass)(tupleClassType, [],
2166
2249
  /* isTypeArgumentExplicit */ true,
2167
- /* stripLiterals */ true,
2168
2250
  /* isUnpackedTuple */ true));
2169
2251
  }
2170
2252
  }
@@ -2476,7 +2558,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
2476
2558
  break;
2477
2559
  }
2478
2560
  case 35 /* MemberAccess */: {
2479
- const baseTypeResult = getTypeOfExpression(node.leftExpression);
2561
+ const baseTypeResult = getTypeOfExpression(node.leftExpression, 2 /* DoNotSpecialize */);
2480
2562
  const memberType = getTypeOfMemberAccessWithBaseType(node, baseTypeResult, { method: 'del' }, 0 /* None */);
2481
2563
  writeTypeCache(node.memberName, memberType.type, 0 /* None */, /* isIncomplete */ false);
2482
2564
  writeTypeCache(node, memberType.type, 0 /* None */, /* isIncomplete */ false);
@@ -2574,7 +2656,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
2574
2656
  const containingFunction = ParseTreeUtils.getEnclosingFunction(node);
2575
2657
  if (containingFunction && ParseTreeUtils.isUnannotatedFunction(containingFunction)) {
2576
2658
  return {
2577
- node,
2578
2659
  type: types_1.AnyType.create(),
2579
2660
  isIncomplete: false,
2580
2661
  };
@@ -2718,7 +2799,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
2718
2799
  if ((0, types_1.isTypeVar)(type) && !type.details.isSynthesized) {
2719
2800
  type = validateTypeVarUsage(node, type, flags);
2720
2801
  }
2721
- return { type, node, isIncomplete };
2802
+ return { type, isIncomplete };
2722
2803
  }
2723
2804
  // Handles the case where a variable or parameter is defined in an outer
2724
2805
  // scope and captured by an inner scope (either a function or a lambda).
@@ -3016,7 +3097,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3016
3097
  const baseTypeResult = getTypeOfExpression(node.leftExpression, baseTypeFlags);
3017
3098
  if ((0, typeUtils_1.isTypeAliasPlaceholder)(baseTypeResult.type)) {
3018
3099
  return {
3019
- node,
3020
3100
  type: types_1.UnknownType.create(/* isIncomplete */ true),
3021
3101
  isIncomplete: true,
3022
3102
  };
@@ -3076,7 +3156,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3076
3156
  // If the base type was incomplete and unbound, don't proceed
3077
3157
  // because false positive errors will be generated.
3078
3158
  if (baseTypeResult.isIncomplete && (0, types_1.isUnbound)(baseTypeResult.type)) {
3079
- return { type: types_1.UnknownType.create(/* isIncomplete */ true), node, isIncomplete: true };
3159
+ return { type: types_1.UnknownType.create(/* isIncomplete */ true), isIncomplete: true };
3080
3160
  }
3081
3161
  // Handle the special case where the expression is an actual
3082
3162
  // UnionType special form.
@@ -3112,22 +3192,22 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3112
3192
  const paramNode = ParseTreeUtils.getEnclosingParameter(node);
3113
3193
  if (!paramNode || paramNode.category !== 1 /* VarArgList */) {
3114
3194
  addError(localize_1.Localizer.Diagnostic.paramSpecArgsUsage(), node);
3115
- return { type: types_1.UnknownType.create(isIncomplete), node, isIncomplete };
3195
+ return { type: types_1.UnknownType.create(isIncomplete), isIncomplete };
3116
3196
  }
3117
- return { type: types_1.TypeVarType.cloneForParamSpecAccess(baseType, 'args'), node, isIncomplete };
3197
+ return { type: types_1.TypeVarType.cloneForParamSpecAccess(baseType, 'args'), isIncomplete };
3118
3198
  }
3119
3199
  if (memberName === 'kwargs') {
3120
3200
  const paramNode = ParseTreeUtils.getEnclosingParameter(node);
3121
3201
  if (!paramNode || paramNode.category !== 2 /* VarArgDictionary */) {
3122
3202
  addError(localize_1.Localizer.Diagnostic.paramSpecKwargsUsage(), node);
3123
- return { type: types_1.UnknownType.create(isIncomplete), node, isIncomplete };
3203
+ return { type: types_1.UnknownType.create(isIncomplete), isIncomplete };
3124
3204
  }
3125
- return { type: types_1.TypeVarType.cloneForParamSpecAccess(baseType, 'kwargs'), node, isIncomplete };
3205
+ return { type: types_1.TypeVarType.cloneForParamSpecAccess(baseType, 'kwargs'), isIncomplete };
3126
3206
  }
3127
3207
  if (!isIncomplete) {
3128
3208
  addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.paramSpecUnknownMember().format({ name: memberName }), node);
3129
3209
  }
3130
- return { type: types_1.UnknownType.create(isIncomplete), node, isIncomplete };
3210
+ return { type: types_1.UnknownType.create(isIncomplete), isIncomplete };
3131
3211
  }
3132
3212
  if (flags & 64 /* ExpectingType */) {
3133
3213
  if (!isIncomplete) {
@@ -3136,14 +3216,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3136
3216
  name: memberName,
3137
3217
  }), node.leftExpression);
3138
3218
  }
3139
- return { type: types_1.UnknownType.create(isIncomplete), node, isIncomplete };
3219
+ return { type: types_1.UnknownType.create(isIncomplete), isIncomplete };
3140
3220
  }
3141
3221
  if (baseType.details.recursiveTypeAliasName) {
3142
- return { type: types_1.UnknownType.create(/* isIncomplete */ true), node, isIncomplete: true };
3222
+ return { type: types_1.UnknownType.create(/* isIncomplete */ true), isIncomplete: true };
3143
3223
  }
3144
3224
  return getTypeOfMemberAccessWithBaseType(node, {
3145
3225
  type: makeTopLevelTypeVarsConcrete(baseType),
3146
- node,
3147
3226
  bindToType: baseType,
3148
3227
  isIncomplete,
3149
3228
  }, usage, 0 /* None */);
@@ -3167,23 +3246,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3167
3246
  baseType = types_1.ClassType.cloneAsInstance(strClassType);
3168
3247
  }
3169
3248
  // Handle the special case of 'name' and 'value' members within an enum.
3170
- if (types_1.ClassType.isEnumClass(baseType)) {
3171
- const literalValue = baseType.literalValue;
3172
- if (literalValue instanceof types_1.EnumLiteral) {
3173
- if (memberName === 'name' || memberName === '_name_') {
3174
- const strClass = getBuiltInType(node, 'str');
3175
- if ((0, types_1.isInstantiableClass)(strClass)) {
3176
- return {
3177
- node,
3178
- type: types_1.ClassType.cloneAsInstance(types_1.ClassType.cloneWithLiteral(strClass, literalValue.itemName)),
3179
- isIncomplete,
3180
- };
3181
- }
3182
- }
3183
- else if (memberName === 'value' || memberName === '_value_') {
3184
- return { node, type: literalValue.itemType, isIncomplete };
3185
- }
3186
- }
3249
+ const enumMemberResult = (0, enums_1.getTypeOfEnumMember)(evaluatorInterface, node, baseType, memberName, isIncomplete);
3250
+ if (enumMemberResult) {
3251
+ return enumMemberResult;
3187
3252
  }
3188
3253
  const typeResult = getTypeOfObjectMember(node.memberName, baseType, memberName, usage, diag,
3189
3254
  /* memberAccessFlags */ undefined, baseTypeResult.bindToType);
@@ -3285,7 +3350,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3285
3350
  else {
3286
3351
  const typeResult = getTypeOfMemberAccessWithBaseType(node, {
3287
3352
  type: subtype,
3288
- node,
3289
3353
  isIncomplete: baseTypeResult.isIncomplete,
3290
3354
  }, usage, 0 /* None */);
3291
3355
  if (typeResult.isIncomplete) {
@@ -3317,7 +3381,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3317
3381
  type = types_1.AnyType.create();
3318
3382
  }
3319
3383
  else {
3320
- type = getTypeOfMemberAccessWithBaseType(node, { type: functionObj, node }, usage, flags).type;
3384
+ type = getTypeOfMemberAccessWithBaseType(node, { type: functionObj }, usage, flags).type;
3321
3385
  }
3322
3386
  }
3323
3387
  break;
@@ -3387,7 +3451,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3387
3451
  /* ignoreEmptyContainers */ false);
3388
3452
  }
3389
3453
  }
3390
- return { type, node, isIncomplete, isAsymmetricDescriptor };
3454
+ return { type, isIncomplete, isAsymmetricDescriptor };
3391
3455
  }
3392
3456
  function getTypeOfClassMemberName(errorNode, classType, isAccessedThroughObject, memberName, usage, diag, flags, bindToType) {
3393
3457
  var _a, _b;
@@ -3444,7 +3508,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3444
3508
  // because that case is used for super() calls where we want
3445
3509
  // to leave the Self type generic (not specialized).
3446
3510
  const selfClass = bindToType ? undefined : classType;
3447
- const typeResult = getTypeOfMemberInternal(errorNode, memberInfo, selfClass);
3511
+ const typeResult = getTypeOfMemberInternal(memberInfo, selfClass);
3448
3512
  if (typeResult) {
3449
3513
  type = typeResult.type;
3450
3514
  if (typeResult.isIncomplete) {
@@ -4026,7 +4090,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4026
4090
  }
4027
4091
  const tupleObject = (0, typeUtils_1.convertToInstance)((0, typeUtils_1.specializeTupleClass)(tupleClassType, variadicTypes,
4028
4092
  /* isTypeArgumentExplicit */ true,
4029
- /* stripLiterals */ true,
4030
4093
  /* isUnpackedTuple */ true));
4031
4094
  typeArgs = [
4032
4095
  ...typeArgs.slice(0, variadicIndex),
@@ -4041,7 +4104,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4041
4104
  node: errorNode,
4042
4105
  type: (0, typeUtils_1.convertToInstance)((0, typeUtils_1.specializeTupleClass)(tupleClassType, [],
4043
4106
  /* isTypeArgumentExplicit */ true,
4044
- /* stripLiterals */ true,
4045
4107
  /* isUnpackedTuple */ true)),
4046
4108
  });
4047
4109
  }
@@ -4202,9 +4264,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4202
4264
  if ((0, typeUtils_1.isTypeAliasPlaceholder)(baseTypeResult.type)) {
4203
4265
  const typeArgTypes = getTypeArgs(node, flags).map((t) => (0, typeUtils_1.convertToInstance)(t.type));
4204
4266
  const type = types_1.TypeBase.cloneForTypeAlias(baseTypeResult.type, baseTypeResult.type.details.recursiveTypeAliasName, '', baseTypeResult.type.details.recursiveTypeAliasScopeId, baseTypeResult.type.details.recursiveTypeParameters, typeArgTypes);
4205
- return { type, node };
4267
+ return { type };
4206
4268
  }
4207
- let isIncomplete = false;
4269
+ let isIncomplete = baseTypeResult.isIncomplete;
4208
4270
  const type = mapSubtypesExpandTypeVars(baseTypeResult.type,
4209
4271
  /* conditionFilter */ undefined, (concreteSubtype, unexpandedSubtype) => {
4210
4272
  var _a;
@@ -4299,11 +4361,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4299
4361
  if ((0, types_1.isNever)(concreteSubtype)) {
4300
4362
  return types_1.UnknownType.create();
4301
4363
  }
4302
- if ((0, types_1.isNoneInstance)(concreteSubtype)) {
4364
+ if ((0, types_1.isNoneInstance)(concreteSubtype) && !isIncomplete) {
4303
4365
  addDiagnostic(AnalyzerNodeInfo.getFileInfo(node).diagnosticRuleSet.reportOptionalSubscript, diagnosticRules_1.DiagnosticRule.reportOptionalSubscript, localize_1.Localizer.Diagnostic.noneNotSubscriptable(), node.baseExpression);
4304
4366
  return types_1.UnknownType.create();
4305
4367
  }
4306
- if (!(0, types_1.isUnbound)(concreteSubtype)) {
4368
+ if (!(0, types_1.isUnbound)(concreteSubtype) && !isIncomplete) {
4307
4369
  const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
4308
4370
  addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeNotSubscriptable().format({ type: printType(concreteSubtype) }), node.baseExpression);
4309
4371
  }
@@ -4318,7 +4380,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4318
4380
  }
4319
4381
  });
4320
4382
  }
4321
- return { type, node, isIncomplete };
4383
+ return { type, isIncomplete };
4322
4384
  }
4323
4385
  function makeTupleObject(entryTypes, isUnspecifiedLength = false) {
4324
4386
  if (tupleClassType && (0, types_1.isInstantiableClass)(tupleClassType)) {
@@ -4363,7 +4425,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4363
4425
  name: magicMethodName,
4364
4426
  type: printType(baseType),
4365
4427
  }), node.baseExpression);
4366
- return { node, type: types_1.UnknownType.create() };
4428
+ return { type: types_1.UnknownType.create() };
4367
4429
  }
4368
4430
  // Handle the special case where the object is a Tuple and
4369
4431
  // the index is a constant number (integer) or a slice with integer
@@ -4384,11 +4446,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4384
4446
  const tupleType = (0, typeUtils_1.getSpecializedTupleType)(baseType);
4385
4447
  if (tupleType && tupleType.tupleTypeArguments && !(0, typeUtils_1.isUnboundedTupleClass)(tupleType)) {
4386
4448
  if (indexValue >= 0 && indexValue < tupleType.tupleTypeArguments.length) {
4387
- return { node, type: tupleType.tupleTypeArguments[indexValue].type };
4449
+ return { type: tupleType.tupleTypeArguments[indexValue].type };
4388
4450
  }
4389
4451
  else if (indexValue < 0 && tupleType.tupleTypeArguments.length + indexValue >= 0) {
4390
4452
  return {
4391
- node,
4392
4453
  type: tupleType.tupleTypeArguments[tupleType.tupleTypeArguments.length + indexValue].type,
4393
4454
  };
4394
4455
  }
@@ -4426,7 +4487,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4426
4487
  tupleClassType &&
4427
4488
  (0, types_1.isInstantiableClass)(tupleClassType)) {
4428
4489
  return {
4429
- node,
4430
4490
  type: types_1.ClassType.cloneAsInstance((0, typeUtils_1.specializeTupleClass)(tupleClassType, tupleType.tupleTypeArguments.slice(startValue, endValue))),
4431
4491
  };
4432
4492
  }
@@ -4541,7 +4601,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4541
4601
  }
4542
4602
  callResult = validateCallArguments(node, argList, { type: itemMethodType });
4543
4603
  return {
4544
- node,
4545
4604
  type: (_c = callResult.returnType) !== null && _c !== void 0 ? _c : types_1.UnknownType.create(),
4546
4605
  isIncomplete: !!callResult.isTypeIncomplete,
4547
4606
  };
@@ -4569,10 +4628,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4569
4628
  // treated as types. If it's an Annotated[a, b, c], only the first index
4570
4629
  // should be treated as a type. The others can be regular (non-type) objects.
4571
4630
  if (hasCustomClassGetItem || (isAnnotatedClass && argIndex > 0)) {
4572
- typeResult = getTypeOfExpression(expr, 32 /* ParamSpecDisallowed */ |
4573
- 128 /* TypeVarTupleDisallowed */ |
4574
- 2 /* DoNotSpecialize */ |
4575
- 131072 /* ClassVarDisallowed */);
4631
+ typeResult = {
4632
+ ...getTypeOfExpression(expr, 32 /* ParamSpecDisallowed */ |
4633
+ 128 /* TypeVarTupleDisallowed */ |
4634
+ 2 /* DoNotSpecialize */ |
4635
+ 131072 /* ClassVarDisallowed */),
4636
+ node: expr,
4637
+ };
4576
4638
  }
4577
4639
  else {
4578
4640
  typeResult = getTypeArg(expr, adjFlags);
@@ -4618,7 +4680,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4618
4680
  let typeResult;
4619
4681
  let adjustedFlags = flags |
4620
4682
  64 /* ExpectingType */ |
4621
- 1024 /* ExpectingTypeAnnotation */ |
4622
4683
  1 /* ConvertEllipsisToAny */ |
4623
4684
  8 /* EvaluateStringLiteralAsType */;
4624
4685
  const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
@@ -4628,14 +4689,16 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4628
4689
  if (node.nodeType === 31 /* List */) {
4629
4690
  typeResult = {
4630
4691
  type: types_1.UnknownType.create(),
4631
- typeList: node.entries.map((entry) => getTypeOfExpression(entry, adjustedFlags)),
4692
+ typeList: node.entries.map((entry) => {
4693
+ return { ...getTypeOfExpression(entry, adjustedFlags), node: entry };
4694
+ }),
4632
4695
  node,
4633
4696
  };
4634
4697
  // Set the node's type so it isn't reevaluated later.
4635
4698
  setTypeForNode(node, types_1.UnknownType.create());
4636
4699
  }
4637
4700
  else {
4638
- typeResult = getTypeOfExpression(node, adjustedFlags);
4701
+ typeResult = { ...getTypeOfExpression(node, adjustedFlags), node };
4639
4702
  // "Protocol" is not allowed as a type argument.
4640
4703
  if ((0, types_1.isClass)(typeResult.type) && types_1.ClassType.isBuiltIn(typeResult.type, 'Protocol')) {
4641
4704
  addError(localize_1.Localizer.Diagnostic.protocolNotAllowedInTypeArgument(), node);
@@ -4651,7 +4714,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4651
4714
  }
4652
4715
  function getTypeOfTuple(node, expectedType, flags) {
4653
4716
  if ((flags & 64 /* ExpectingType */) !== 0 && node.expressions.length === 0 && !expectedType) {
4654
- return { type: makeTupleObject([]), node, isEmptyTupleShorthand: true };
4717
+ return { type: makeTupleObject([]), isEmptyTupleShorthand: true };
4655
4718
  }
4656
4719
  // If the expected type is a union, recursively call for each of the subtypes
4657
4720
  // to find one that matches.
@@ -4728,20 +4791,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4728
4791
  }
4729
4792
  const entryTypeResults = node.expressions.map((expr, index) => getTypeOfExpression(expr,
4730
4793
  /* flags */ undefined, index < expectedTypes.length ? expectedTypes[index] : undefined));
4731
- const expectedTypesContainLiterals = expectedTypes.some((type) => (0, typeUtils_1.isLiteralTypeOrUnion)(type));
4732
4794
  const type = (0, typeUtils_1.convertToInstance)((0, typeUtils_1.specializeTupleClass)(tupleClassType, buildTupleTypesList(entryTypeResults),
4733
- /* isTypeArgumentExplicit */ true,
4734
- /* stripLiterals */ !expectedTypesContainLiterals));
4735
- return { type, node };
4795
+ /* isTypeArgumentExplicit */ true));
4796
+ return { type };
4736
4797
  }
4737
4798
  function getTypeOfTupleInferred(node) {
4738
4799
  const entryTypeResults = node.expressions.map((expr) => getTypeOfExpression(expr));
4739
4800
  const isIncomplete = entryTypeResults.some((result) => result.isIncomplete);
4740
4801
  if (!tupleClassType || !(0, types_1.isInstantiableClass)(tupleClassType)) {
4741
- return { type: types_1.UnknownType.create(), node };
4802
+ return { type: types_1.UnknownType.create() };
4742
4803
  }
4743
4804
  const type = (0, typeUtils_1.convertToInstance)((0, typeUtils_1.specializeTupleClass)(tupleClassType, buildTupleTypesList(entryTypeResults)));
4744
- return { type, node, isIncomplete };
4805
+ return { type, isIncomplete };
4745
4806
  }
4746
4807
  function buildTupleTypesList(entryTypeResults) {
4747
4808
  const entryTypes = [];
@@ -4797,7 +4858,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4797
4858
  };
4798
4859
  return functionArg;
4799
4860
  });
4800
- let typeResult = { node, type: types_1.UnknownType.create() };
4861
+ let typeResult = { type: types_1.UnknownType.create() };
4801
4862
  if (!(0, typeUtils_1.isTypeAliasPlaceholder)(baseTypeResult.type)) {
4802
4863
  if (node.leftExpression.nodeType === 38 /* Name */ && node.leftExpression.value === 'super') {
4803
4864
  // Handle the built-in "super" call specially.
@@ -4871,7 +4932,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4871
4932
  }
4872
4933
  if ((flags & 1024 /* ExpectingTypeAnnotation */) !== 0) {
4873
4934
  addDiagnostic(AnalyzerNodeInfo.getFileInfo(node).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeAnnotationCall(), node);
4874
- typeResult = { node, type: types_1.UnknownType.create() };
4935
+ typeResult = { type: types_1.UnknownType.create() };
4875
4936
  }
4876
4937
  return typeResult;
4877
4938
  }
@@ -4882,21 +4943,21 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4882
4943
  node.arguments[0].argumentCategory !== 0 /* Simple */ ||
4883
4944
  node.arguments[1].name !== undefined) {
4884
4945
  addError(localize_1.Localizer.Diagnostic.assertTypeArgs(), node);
4885
- return { node, type: types_1.UnknownType.create() };
4946
+ return { type: types_1.UnknownType.create() };
4886
4947
  }
4887
4948
  const arg0TypeResult = getTypeOfExpression(node.arguments[0].valueExpression,
4888
4949
  /* flags */ undefined, expectedType);
4889
4950
  if (arg0TypeResult.isIncomplete) {
4890
- return { node, type: types_1.UnknownType.create(/* isIncomplete */ true), isIncomplete: true };
4951
+ return { type: types_1.UnknownType.create(/* isIncomplete */ true), isIncomplete: true };
4891
4952
  }
4892
4953
  const assertedType = (0, typeUtils_1.convertToInstance)(getTypeOfArgumentExpectingType(node.arguments[1]).type);
4893
- if (!(0, types_1.isTypeSame)(assertedType, arg0TypeResult.type)) {
4954
+ if (!(0, types_1.isTypeSame)(assertedType, arg0TypeResult.type, { treatAnySameAsUnknown: true })) {
4894
4955
  addDiagnostic(AnalyzerNodeInfo.getFileInfo(node).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.assertTypeTypeMismatch().format({
4895
4956
  expected: printType(assertedType),
4896
4957
  received: printType(arg0TypeResult.type),
4897
4958
  }), node.arguments[0].valueExpression);
4898
4959
  }
4899
- return { node, type: arg0TypeResult.type };
4960
+ return { type: arg0TypeResult.type };
4900
4961
  }
4901
4962
  function getTypeOfRevealType(node, expectedType) {
4902
4963
  let arg0Value;
@@ -4933,7 +4994,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4933
4994
  });
4934
4995
  if (!arg0Value) {
4935
4996
  addError(localize_1.Localizer.Diagnostic.revealTypeArgs(), node);
4936
- return { node, type: types_1.UnknownType.create() };
4997
+ return { type: types_1.UnknownType.create() };
4937
4998
  }
4938
4999
  const typeResult = getTypeOfExpression(arg0Value, /* flags */ undefined, expectedType);
4939
5000
  const type = typeResult.type;
@@ -4957,11 +5018,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4957
5018
  }
4958
5019
  }
4959
5020
  addInformation(localize_1.Localizer.DiagnosticAddendum.typeOfSymbol().format({ name: exprString, type: typeString }), node.arguments[0]);
4960
- return {
4961
- node,
4962
- type,
4963
- isIncomplete: typeResult.isIncomplete,
4964
- };
5021
+ return { type, isIncomplete: typeResult.isIncomplete };
4965
5022
  }
4966
5023
  function getTypeOfRevealLocals(node) {
4967
5024
  let curNode = node;
@@ -5110,7 +5167,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5110
5167
  type: resultIsInstance
5111
5168
  ? types_1.ClassType.cloneAsInstance(lookupResults.classType)
5112
5169
  : lookupResults.classType,
5113
- node,
5114
5170
  bindToType: resultIsInstance && bindToType && (0, types_1.isInstantiableClass)(bindToType)
5115
5171
  ? types_1.ClassType.cloneAsInstance(bindToType)
5116
5172
  : bindToType,
@@ -5123,10 +5179,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5123
5179
  // If the class derives from one or more unknown classes,
5124
5180
  // return unknown here to prevent spurious errors.
5125
5181
  if (targetClassType.details.mro.some((mroBase) => (0, types_1.isAnyOrUnknown)(mroBase))) {
5126
- return {
5127
- type: types_1.UnknownType.create(),
5128
- node,
5129
- };
5182
+ return { type: types_1.UnknownType.create() };
5130
5183
  }
5131
5184
  const baseClasses = targetClassType.details.baseClasses;
5132
5185
  if (baseClasses.length > 0) {
@@ -5134,15 +5187,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5134
5187
  if ((0, types_1.isInstantiableClass)(baseClassType)) {
5135
5188
  return {
5136
5189
  type: resultIsInstance ? types_1.ClassType.cloneAsInstance(baseClassType) : baseClassType,
5137
- node,
5138
5190
  };
5139
5191
  }
5140
5192
  }
5141
5193
  }
5142
- return {
5143
- type: types_1.UnknownType.create(),
5144
- node,
5145
- };
5194
+ return { type: types_1.UnknownType.create() };
5146
5195
  }
5147
5196
  // Attempts to find an overloaded function for each set of argument
5148
5197
  // types in the expandedArgTypes list. If an argument type is undefined,
@@ -5152,6 +5201,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5152
5201
  // (one for each argument) will be undefined. On subsequent calls, this
5153
5202
  // list will grow to include union expansions.
5154
5203
  function validateOverloadsWithExpandedTypes(errorNode, expandedArgTypes, argParamMatches, typeVarContext, skipUnknownArgCheck, expectedType) {
5204
+ var _a;
5155
5205
  const returnTypes = [];
5156
5206
  const matchedOverloads = [];
5157
5207
  let isTypeIncomplete = false;
@@ -5173,11 +5223,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5173
5223
  return argParamCopy;
5174
5224
  });
5175
5225
  }
5176
- // Clone the typeVarContext so we don't modify the original.
5177
- const effectiveTypeVarContext = typeVarContext
5178
- ? typeVarContext.clone()
5179
- : new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(overload));
5226
+ // Clone the typeVarContext so we don't modify the original. If this is
5227
+ // not the first time through the loop, clone the type var context
5228
+ // from the previous successful match.
5229
+ const typeVarContextToClone = matchedOverloads.length > 0
5230
+ ? matchedOverloads[matchedOverloads.length - 1].typeVarContext.clone()
5231
+ : typeVarContext;
5232
+ const effectiveTypeVarContext = (_a = typeVarContextToClone === null || typeVarContextToClone === void 0 ? void 0 : typeVarContextToClone.clone()) !== null && _a !== void 0 ? _a : new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(overload));
5180
5233
  effectiveTypeVarContext.addSolveForScope((0, typeUtils_1.getTypeVarScopeId)(overload));
5234
+ effectiveTypeVarContext.unlock();
5181
5235
  // Use speculative mode so we don't output any diagnostics or
5182
5236
  // record any final types in the type cache.
5183
5237
  const callResult = useSpeculativeMode(errorNode, () => {
@@ -5202,24 +5256,19 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5202
5256
  return { argumentErrors: true, isTypeIncomplete };
5203
5257
  }
5204
5258
  }
5205
- // We found a match for all of the expanded argument lists.
5206
- // Run through them again to populate the original typeVarContext.
5259
+ // We found a match for all of the expanded argument lists. Copy the
5260
+ // resulting type var context back into the caller's type var context.
5261
+ // Use the type var context from the last matched overload because it
5262
+ // includes the type var solutions for all earlier matched overloads.
5207
5263
  if (typeVarContext) {
5208
- for (let expandedTypesIndex = 0; expandedTypesIndex < expandedArgTypes.length; expandedTypesIndex++) {
5209
- const overload = matchedOverloads[expandedTypesIndex].overload;
5210
- const matchResults = matchedOverloads[expandedTypesIndex].matchResults;
5211
- useSpeculativeMode(errorNode, () => {
5212
- typeVarContext.addSolveForScope((0, typeUtils_1.getTypeVarScopeId)(overload));
5213
- typeVarContext.unlock();
5214
- return validateFunctionArgumentTypesWithExpectedType(errorNode, matchResults, typeVarContext,
5215
- /* skipUnknownArgCheck */ true, expectedType);
5216
- });
5217
- }
5264
+ typeVarContext.copyFromClone(matchedOverloads[matchedOverloads.length - 1].typeVarContext);
5218
5265
  }
5219
5266
  // And run through the first expanded argument list one more time to
5220
5267
  // populate the type cache.
5221
- matchedOverloads[0].typeVarContext.unlock();
5222
- const finalCallResult = validateFunctionArgumentTypesWithExpectedType(errorNode, matchedOverloads[0].matchResults, matchedOverloads[0].typeVarContext, skipUnknownArgCheck, expectedType);
5268
+ const finalTypeVarContext = typeVarContext !== null && typeVarContext !== void 0 ? typeVarContext : matchedOverloads[0].typeVarContext;
5269
+ finalTypeVarContext.unlock();
5270
+ finalTypeVarContext.addSolveForScope((0, typeUtils_1.getTypeVarScopeId)(matchedOverloads[0].overload));
5271
+ const finalCallResult = validateFunctionArgumentTypesWithExpectedType(errorNode, matchedOverloads[0].matchResults, finalTypeVarContext, skipUnknownArgCheck, expectedType);
5223
5272
  if (finalCallResult.isTypeIncomplete) {
5224
5273
  isTypeIncomplete = true;
5225
5274
  }
@@ -5806,8 +5855,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5806
5855
  const castToType = getTypeOfArgumentExpectingType(argList[0]).type;
5807
5856
  const castFromType = getTypeOfArgument(argList[1]).type;
5808
5857
  if ((0, types_1.isInstantiableClass)(castToType) && (0, types_1.isClassInstance)(castFromType)) {
5809
- if ((0, types_1.isTypeSame)(castToType, types_1.ClassType.cloneAsInstantiable(castFromType),
5810
- /* ignorePseudoGeneric */ true)) {
5858
+ if ((0, types_1.isTypeSame)(castToType, types_1.ClassType.cloneAsInstantiable(castFromType), {
5859
+ ignorePseudoGeneric: true,
5860
+ })) {
5811
5861
  addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportUnnecessaryCast, diagnosticRules_1.DiagnosticRule.reportUnnecessaryCast, localize_1.Localizer.Diagnostic.unnecessaryCast().format({
5812
5862
  type: printType(castFromType),
5813
5863
  }), errorNode);
@@ -5861,7 +5911,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5861
5911
  if ((0, types_1.isClassInstance)(subtype) ||
5862
5912
  ((0, types_1.isTypeVar)(subtype) && types_1.TypeBase.isInstance(subtype)) ||
5863
5913
  (0, types_1.isNoneInstance)(subtype)) {
5864
- return (0, typeUtils_1.convertToInstantiable)((0, typeUtils_1.stripLiteralValue)(subtype));
5914
+ return (0, typeUtils_1.convertToInstantiable)(stripLiteralValue(subtype));
5865
5915
  }
5866
5916
  return types_1.AnyType.create();
5867
5917
  });
@@ -5900,18 +5950,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5900
5950
  addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeNotIntantiable().format({ type: className }), errorNode);
5901
5951
  return types_1.AnyType.create();
5902
5952
  }
5903
- if (className === 'Enum' ||
5904
- className === 'IntEnum' ||
5905
- className === 'StrEnum' ||
5906
- className === 'Flag' ||
5907
- className === 'IntFlag') {
5908
- return createEnumType(errorNode, expandedSubtype, argList);
5953
+ if ((0, enums_1.isKnownEnumType)(className)) {
5954
+ return (0, enums_1.createEnumType)(errorNode, expandedSubtype, argList);
5909
5955
  }
5910
5956
  if (className === 'TypedDict') {
5911
5957
  return (0, typedDicts_1.createTypedDictType)(evaluatorInterface, errorNode, expandedSubtype, argList);
5912
5958
  }
5913
5959
  if (className === 'auto' && argList.length === 0) {
5914
- return getBuiltInObject(errorNode, 'int');
5960
+ return (0, enums_1.getEnumAutoValueType)(evaluatorInterface, errorNode);
5915
5961
  }
5916
5962
  }
5917
5963
  if (types_1.ClassType.supportsAbstractMethods(expandedSubtype)) {
@@ -6702,13 +6748,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6702
6748
  reportedArgError = true;
6703
6749
  }
6704
6750
  return {
6705
- type: (0, typeUtils_1.stripLiteralValue)(argType),
6751
+ type: stripLiteralValue(argType),
6706
6752
  isUnbounded: argParam.argument.argumentCategory === 1 /* UnpackedList */,
6707
6753
  };
6708
6754
  });
6709
6755
  const specializedTuple = types_1.ClassType.cloneAsInstance((0, typeUtils_1.specializeTupleClass)(tupleClassType, tupleTypeArgs,
6710
6756
  /* isTypeArgumentExplicit */ true,
6711
- /* stripLiterals */ true,
6712
6757
  /* isUnpackedTuple */ true));
6713
6758
  const combinedArg = {
6714
6759
  paramCategory: 1 /* VarArgList */,
@@ -6847,7 +6892,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6847
6892
  type.strippedFirstParamType.typeArguments.forEach((typeArg, index) => {
6848
6893
  if (index < typeParams.length) {
6849
6894
  const typeParam = typeParams[index];
6850
- if (!(0, types_1.isTypeSame)(typeParam, typeArg, /* ignorePseudoGeneric */ true)) {
6895
+ if (!(0, types_1.isTypeSame)(typeParam, typeArg, { ignorePseudoGeneric: true })) {
6851
6896
  typeVarContext.setTypeVarType(typeParams[index], typeArg);
6852
6897
  }
6853
6898
  }
@@ -7240,7 +7285,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
7240
7285
  // strip literals before performing the assignment. This is used in
7241
7286
  // places like a dict constructor.
7242
7287
  if (argParam.paramCategory === 2 /* VarArgDictionary */ && (0, types_1.isTypeVar)(argParam.paramType)) {
7243
- argType = (0, typeUtils_1.stripLiteralValue)(argType);
7288
+ argType = stripLiteralValue(argType);
7244
7289
  }
7245
7290
  // If there's a constraint filter, apply it to top-level type variables
7246
7291
  // if appropriate. This doesn't properly handle non-top-level constrained
@@ -7427,9 +7472,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
7427
7472
  addError(localize_1.Localizer.Diagnostic.typeVarBoundAndConstrained(), argList[i].valueExpression || errorNode);
7428
7473
  }
7429
7474
  else {
7430
- const argType = (_b = (_a = argList[i].typeResult) === null || _a === void 0 ? void 0 : _a.type) !== null && _b !== void 0 ? _b : getTypeOfExpressionExpectingType(argList[i].valueExpression,
7431
- /* allowFinal */ undefined,
7432
- /* allowRequired */ undefined).type;
7475
+ const argType = (_b = (_a = argList[i].typeResult) === null || _a === void 0 ? void 0 : _a.type) !== null && _b !== void 0 ? _b : getTypeOfExpressionExpectingType(argList[i].valueExpression).type;
7433
7476
  if ((0, typeUtils_1.requiresSpecialization)(argType, /* ignorePseudoGeneric */ true)) {
7434
7477
  addError(localize_1.Localizer.Diagnostic.typeVarGeneric(), argList[i].valueExpression || errorNode);
7435
7478
  }
@@ -7466,9 +7509,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
7466
7509
  addError(localize_1.Localizer.Diagnostic.typeVarBoundAndConstrained(), argList[i].valueExpression || errorNode);
7467
7510
  }
7468
7511
  else {
7469
- const argType = (_e = (_d = argList[i].typeResult) === null || _d === void 0 ? void 0 : _d.type) !== null && _e !== void 0 ? _e : getTypeOfExpressionExpectingType(argList[i].valueExpression,
7470
- /* allowFinal */ undefined,
7471
- /* allowRequired */ undefined).type;
7512
+ const argType = (_e = (_d = argList[i].typeResult) === null || _d === void 0 ? void 0 : _d.type) !== null && _e !== void 0 ? _e : getTypeOfExpressionExpectingType(argList[i].valueExpression).type;
7472
7513
  if ((0, typeUtils_1.requiresSpecialization)(argType, /* ignorePseudoGeneric */ true)) {
7473
7514
  addError(localize_1.Localizer.Diagnostic.typeVarGeneric(), argList[i].valueExpression || errorNode);
7474
7515
  }
@@ -7558,80 +7599,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
7558
7599
  nameParts.push(moduleName);
7559
7600
  return nameParts.reverse().join('.');
7560
7601
  }
7561
- // Creates a new custom enum class with named values.
7562
- function createEnumType(errorNode, enumClass, argList) {
7563
- const fileInfo = AnalyzerNodeInfo.getFileInfo(errorNode);
7564
- let className = 'enum';
7565
- if (argList.length === 0) {
7566
- return undefined;
7567
- }
7568
- else {
7569
- const nameArg = argList[0];
7570
- if (nameArg.argumentCategory === 0 /* Simple */ &&
7571
- nameArg.valueExpression &&
7572
- nameArg.valueExpression.nodeType === 48 /* StringList */) {
7573
- className = nameArg.valueExpression.strings.map((s) => s.value).join('');
7574
- }
7575
- else {
7576
- return undefined;
7577
- }
7578
- }
7579
- const classType = types_1.ClassType.createInstantiable(className, ParseTreeUtils.getClassFullName(errorNode, fileInfo.moduleName, className), fileInfo.moduleName, fileInfo.filePath, 1048576 /* EnumClass */, ParseTreeUtils.getTypeSourceId(errorNode),
7580
- /* declaredMetaclass */ undefined, enumClass.details.effectiveMetaclass);
7581
- classType.details.baseClasses.push(enumClass);
7582
- (0, typeUtils_1.computeMroLinearization)(classType);
7583
- const classFields = classType.details.fields;
7584
- classFields.set('__class__', symbol_1.Symbol.createWithType(4 /* ClassMember */ | 64 /* IgnoredForProtocolMatch */, classType));
7585
- if (argList.length < 2) {
7586
- return undefined;
7587
- }
7588
- else {
7589
- const entriesArg = argList[1];
7590
- if (entriesArg.argumentCategory !== 0 /* Simple */ ||
7591
- !entriesArg.valueExpression ||
7592
- entriesArg.valueExpression.nodeType !== 48 /* StringList */) {
7593
- // Technically, the Enum constructor supports a bunch of different
7594
- // ways to specify the items: space-delimited string, a string
7595
- // iterator, an iterator of name/value tuples, and a dictionary
7596
- // of name/value pairs. We support only the simple space-delimited
7597
- // string here. For users who are interested in type checking, we
7598
- // recommend using the more standard class declaration syntax.
7599
- return undefined;
7600
- }
7601
- else {
7602
- const entries = entriesArg.valueExpression.strings
7603
- .map((s) => s.value)
7604
- .join('')
7605
- .split(' ');
7606
- entries.forEach((entryName) => {
7607
- entryName = entryName.trim();
7608
- if (entryName) {
7609
- const entryType = types_1.UnknownType.create();
7610
- const newSymbol = symbol_1.Symbol.createWithType(4 /* ClassMember */, entryType);
7611
- // We need to associate the declaration with a parse node.
7612
- // In this case it's just part of a string literal value.
7613
- // The definition provider won't necessarily take the
7614
- // user to the exact spot in the string, but it's close enough.
7615
- const stringNode = entriesArg.valueExpression;
7616
- (0, debug_1.assert)(stringNode.nodeType === 48 /* StringList */);
7617
- const fileInfo = AnalyzerNodeInfo.getFileInfo(errorNode);
7618
- const declaration = {
7619
- type: 1 /* Variable */,
7620
- node: stringNode,
7621
- isRuntimeTypeExpression: true,
7622
- path: fileInfo.filePath,
7623
- range: (0, positionUtils_1.convertOffsetsToRange)(stringNode.start, textRange_1.TextRange.getEnd(stringNode), fileInfo.lines),
7624
- moduleName: fileInfo.moduleName,
7625
- isInExceptSuite: false,
7626
- };
7627
- newSymbol.addDeclaration(declaration);
7628
- classFields.set(entryName, newSymbol);
7629
- }
7630
- });
7631
- }
7632
- }
7633
- return classType;
7634
- }
7635
7602
  // Implements the semantics of the NewType call as documented
7636
7603
  // in the Python specification: The static type checker will treat
7637
7604
  // the new type as if it were a subclass of the original type.
@@ -7746,14 +7713,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
7746
7713
  if (!type) {
7747
7714
  return undefined;
7748
7715
  }
7749
- return { type, node };
7716
+ return { type };
7750
7717
  }
7751
7718
  function getTypeOfUnaryOperation(node, expectedType) {
7752
7719
  const exprTypeResult = getTypeOfExpression(node.expression);
7753
7720
  let exprType = makeTopLevelTypeVarsConcrete(exprTypeResult.type);
7754
7721
  const isIncomplete = exprTypeResult.isIncomplete;
7755
7722
  if ((0, types_1.isNever)(exprType)) {
7756
- return { node, type: types_1.NeverType.createNever(), isIncomplete };
7723
+ return { type: types_1.NeverType.createNever(), isIncomplete };
7757
7724
  }
7758
7725
  // Map unary operators to magic functions. Note that the bitwise
7759
7726
  // invert has two magic functions that are aliases of each other.
@@ -7832,16 +7799,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
7832
7799
  }
7833
7800
  }
7834
7801
  }
7835
- return { type, node, isIncomplete };
7836
- }
7837
- function operatorSupportsComparisonChaining(op) {
7838
- if (binaryOperatorMap[op] && binaryOperatorMap[op][2]) {
7839
- return true;
7840
- }
7841
- if (booleanOperatorMap[op]) {
7842
- return true;
7843
- }
7844
- return false;
7802
+ return { type, isIncomplete };
7845
7803
  }
7846
7804
  function getTypeOfBinaryOperation(node, expectedType, flags) {
7847
7805
  const leftExpression = node.leftExpression;
@@ -7850,10 +7808,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
7850
7808
  // If this is a comparison and the left expression is also a comparison,
7851
7809
  // we need to change the behavior to accommodate python's "chained
7852
7810
  // comparisons" feature.
7853
- if (operatorSupportsComparisonChaining(node.operator)) {
7811
+ if (ParseTreeUtils.operatorSupportsChaining(node.operator)) {
7854
7812
  if (rightExpression.nodeType === 7 /* BinaryOperation */ &&
7855
7813
  !rightExpression.parenthesized &&
7856
- operatorSupportsComparisonChaining(rightExpression.operator)) {
7814
+ ParseTreeUtils.operatorSupportsChaining(rightExpression.operator)) {
7857
7815
  // Evaluate the right expression so it is type checked.
7858
7816
  getTypeOfBinaryOperation(rightExpression, expectedType, flags);
7859
7817
  // Use the left side of the right expression for comparison purposes.
@@ -7930,15 +7888,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
7930
7888
  addError(localize_1.Localizer.Diagnostic.unionSyntaxIllegal(), node, node.operatorToken);
7931
7889
  }
7932
7890
  }
7933
- if (!validateTypeArg(leftTypeResult, { allowVariadicTypeVar: true, allowUnpackedTuples: true }) ||
7934
- !validateTypeArg(rightTypeResult, { allowVariadicTypeVar: true, allowUnpackedTuples: true })) {
7935
- return { type: types_1.UnknownType.create(), node };
7891
+ if (!validateTypeArg({ ...leftTypeResult, node: leftExpression }, { allowVariadicTypeVar: true, allowUnpackedTuples: true }) ||
7892
+ !validateTypeArg({ ...rightTypeResult, node: rightExpression }, { allowVariadicTypeVar: true, allowUnpackedTuples: true })) {
7893
+ return { type: types_1.UnknownType.create() };
7936
7894
  }
7937
7895
  const newUnion = (0, types_1.combineTypes)([adjustedLeftType, adjustedRightType]);
7938
7896
  if ((0, types_1.isUnion)(newUnion)) {
7939
7897
  types_1.TypeBase.setSpecialForm(newUnion);
7940
7898
  }
7941
- return { type: newUnion, node };
7899
+ return { type: newUnion };
7942
7900
  }
7943
7901
  }
7944
7902
  // Optional checks apply to all operations except for boolean operations.
@@ -7984,7 +7942,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
7984
7942
  }
7985
7943
  type = types_1.UnknownType.create();
7986
7944
  }
7987
- return { type, node, isIncomplete };
7945
+ return { type, isIncomplete };
7988
7946
  }
7989
7947
  function customMetaclassSupportsMethod(type, methodName) {
7990
7948
  if (!(0, types_1.isInstantiableClass)(type)) {
@@ -8037,7 +7995,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8037
7995
  const rightType = rightTypeResult.type;
8038
7996
  const isIncomplete = !!rightTypeResult.isIncomplete || !!leftTypeResult.isIncomplete;
8039
7997
  if ((0, types_1.isNever)(leftType) || (0, types_1.isNever)(rightType)) {
8040
- typeResult = { node, type: types_1.NeverType.createNever(), isIncomplete };
7998
+ typeResult = { type: types_1.NeverType.createNever(), isIncomplete };
8041
7999
  }
8042
8000
  else {
8043
8001
  type = mapSubtypesExpandTypeVars(leftType,
@@ -8085,7 +8043,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8085
8043
  }
8086
8044
  type = types_1.UnknownType.create();
8087
8045
  }
8088
- typeResult = { node, type, isIncomplete };
8046
+ typeResult = { type, isIncomplete };
8089
8047
  }
8090
8048
  assignTypeToExpression(node.destExpression, typeResult.type, !!typeResult.isIncomplete, node.rightExpression);
8091
8049
  return typeResult;
@@ -8487,7 +8445,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8487
8445
  if (resultTypedDict) {
8488
8446
  return {
8489
8447
  type: resultTypedDict,
8490
- node,
8491
8448
  isIncomplete,
8492
8449
  };
8493
8450
  }
@@ -8525,7 +8482,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8525
8482
  return undefined;
8526
8483
  }
8527
8484
  const type = getBuiltInObject(node, 'dict', [specializedKeyType, specializedValueType]);
8528
- return { type, node, isIncomplete };
8485
+ return { type, isIncomplete };
8529
8486
  }
8530
8487
  // Attempts to infer the type of a dictionary statement. If hasExpectedType
8531
8488
  // is true, strict inference is used for the subexpressions.
@@ -8542,8 +8499,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8542
8499
  isIncomplete = true;
8543
8500
  }
8544
8501
  // Strip any literal values.
8545
- keyTypes = keyTypes.map((t) => (0, typeUtils_1.stripLiteralValue)(t));
8546
- valueTypes = valueTypes.map((t) => (0, typeUtils_1.stripLiteralValue)(t));
8502
+ keyTypes = keyTypes.map((t) => stripLiteralValue(t));
8503
+ valueTypes = valueTypes.map((t) => stripLiteralValue(t));
8547
8504
  keyType = keyTypes.length > 0 ? (0, types_1.combineTypes)(keyTypes) : fallbackType;
8548
8505
  // If the value type differs and we're not using "strict inference mode",
8549
8506
  // we need to back off because we can't properly represent the mappings
@@ -8555,7 +8512,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8555
8512
  valueType = (0, types_1.combineTypes)(valueTypes);
8556
8513
  }
8557
8514
  else {
8558
- valueType = (0, typeUtils_1.areTypesSame)(valueTypes, /* ignorePseudoGeneric */ true) ? valueTypes[0] : fallbackType;
8515
+ valueType = (0, typeUtils_1.areTypesSame)(valueTypes, { ignorePseudoGeneric: true }) ? valueTypes[0] : fallbackType;
8559
8516
  }
8560
8517
  }
8561
8518
  else {
@@ -8569,7 +8526,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8569
8526
  /* includeSubclasses */ undefined,
8570
8527
  /* TupleTypeArguments */ undefined, isEmptyContainer))
8571
8528
  : types_1.UnknownType.create();
8572
- return { type, node, isIncomplete };
8529
+ return { type, isIncomplete };
8573
8530
  }
8574
8531
  function getKeyAndValueTypesFromDictionary(node, keyTypes, valueTypes, forceStrictInference, expectedKeyType, expectedValueType, expectedTypedDictEntries, expectedDiagAddendum) {
8575
8532
  let isIncomplete = false;
@@ -8710,6 +8667,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8710
8667
  const builtInClassName = node.nodeType === 31 /* List */ ? 'list' : 'set';
8711
8668
  expectedType = (0, typeUtils_1.transformPossibleRecursiveTypeAlias)(expectedType);
8712
8669
  let isIncomplete = false;
8670
+ let typeErrors = false;
8713
8671
  if (!(0, types_1.isClassInstance)(expectedType)) {
8714
8672
  return undefined;
8715
8673
  }
@@ -8739,6 +8697,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8739
8697
  if (entryTypeResult.isIncomplete) {
8740
8698
  isIncomplete = true;
8741
8699
  }
8700
+ if (entryTypeResult.typeErrors) {
8701
+ typeErrors = true;
8702
+ }
8742
8703
  });
8743
8704
  const isExpectedTypeListOrSet = (0, types_1.isClassInstance)(expectedType) && types_1.ClassType.isBuiltIn(expectedType, builtInClassName);
8744
8705
  const specializedEntryType = inferTypeArgFromExpectedType(expectedEntryType, entryTypes,
@@ -8747,13 +8708,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8747
8708
  return undefined;
8748
8709
  }
8749
8710
  const type = getBuiltInObject(node, builtInClassName, [specializedEntryType]);
8750
- return { type, node, isIncomplete };
8711
+ return { type, isIncomplete, typeErrors };
8751
8712
  }
8752
8713
  // Attempts to infer the type of a list or set statement with no "expected type".
8753
8714
  function getTypeOfListOrSetInferred(node, hasExpectedType) {
8754
8715
  const builtInClassName = node.nodeType === 31 /* List */ ? 'list' : 'set';
8755
8716
  let isEmptyContainer = false;
8756
8717
  let isIncomplete = false;
8718
+ let typeErrors = false;
8757
8719
  let entryTypes = [];
8758
8720
  node.entries.forEach((entry, index) => {
8759
8721
  let entryTypeResult;
@@ -8767,11 +8729,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8767
8729
  if (entryTypeResult.isIncomplete) {
8768
8730
  isIncomplete = true;
8769
8731
  }
8732
+ if (entryTypeResult.typeErrors) {
8733
+ typeErrors = true;
8734
+ }
8770
8735
  if (index < maxEntriesToUseForInference) {
8771
8736
  entryTypes.push(entryTypeResult.type);
8772
8737
  }
8773
8738
  });
8774
- entryTypes = entryTypes.map((t) => (0, typeUtils_1.stripLiteralValue)(t));
8739
+ entryTypes = entryTypes.map((t) => stripLiteralValue(t));
8775
8740
  let inferredEntryType = hasExpectedType ? types_1.AnyType.create() : types_1.UnknownType.create();
8776
8741
  if (entryTypes.length > 0) {
8777
8742
  const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
@@ -8784,7 +8749,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8784
8749
  }
8785
8750
  else {
8786
8751
  // Is the list or set homogeneous? If so, use stricter rules. Otherwise relax the rules.
8787
- inferredEntryType = (0, typeUtils_1.areTypesSame)(entryTypes, /* ignorePseudoGeneric */ true)
8752
+ inferredEntryType = (0, typeUtils_1.areTypesSame)(entryTypes, { ignorePseudoGeneric: true })
8788
8753
  ? entryTypes[0]
8789
8754
  : inferredEntryType;
8790
8755
  }
@@ -8799,7 +8764,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8799
8764
  /* includeSubclasses */ undefined,
8800
8765
  /* TupleTypeArguments */ undefined, isEmptyContainer))
8801
8766
  : types_1.UnknownType.create();
8802
- return { type, node, isIncomplete };
8767
+ return { type, isIncomplete, typeErrors };
8803
8768
  }
8804
8769
  function inferTypeArgFromExpectedType(expectedType, entryTypes, isNarrowable) {
8805
8770
  let targetTypeVar;
@@ -8829,7 +8794,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8829
8794
  if (useSynthesizedTypeVar) {
8830
8795
  typeVarContext.setTypeVarType(targetTypeVar, isNarrowable ? undefined : expectedType, expectedType);
8831
8796
  }
8832
- if (entryTypes.every((entryType) => assignType(targetTypeVar, (0, typeUtils_1.stripLiteralValue)(entryType), /* diag */ undefined, typeVarContext))) {
8797
+ if (entryTypes.every((entryType) => assignType(targetTypeVar, stripLiteralValue(entryType), /* diag */ undefined, typeVarContext))) {
8833
8798
  return (0, typeUtils_1.applySolvedTypeVars)(targetTypeVar, typeVarContext);
8834
8799
  }
8835
8800
  // Allocate a fresh typeVarContext before we try again with literals not stripped.
@@ -8847,12 +8812,16 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8847
8812
  getTypeOfExpression(node.testExpression);
8848
8813
  const typesToCombine = [];
8849
8814
  let isIncomplete = false;
8815
+ let typeErrors = false;
8850
8816
  if (isNodeReachable(node.ifExpression)) {
8851
8817
  const ifType = getTypeOfExpression(node.ifExpression, flags, expectedType);
8852
8818
  typesToCombine.push(ifType.type);
8853
8819
  if (ifType.isIncomplete) {
8854
8820
  isIncomplete = true;
8855
8821
  }
8822
+ if (ifType.typeErrors) {
8823
+ typeErrors = true;
8824
+ }
8856
8825
  }
8857
8826
  if (isNodeReachable(node.elseExpression)) {
8858
8827
  const elseType = getTypeOfExpression(node.elseExpression, flags, expectedType);
@@ -8860,8 +8829,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8860
8829
  if (elseType.isIncomplete) {
8861
8830
  isIncomplete = true;
8862
8831
  }
8832
+ if (elseType.typeErrors) {
8833
+ typeErrors = true;
8834
+ }
8863
8835
  }
8864
- return { type: (0, types_1.combineTypes)(typesToCombine), node, isIncomplete };
8836
+ return { type: (0, types_1.combineTypes)(typesToCombine), isIncomplete, typeErrors };
8865
8837
  }
8866
8838
  function getTypeOfYield(node) {
8867
8839
  let expectedYieldType;
@@ -8891,7 +8863,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8891
8863
  isIncomplete = true;
8892
8864
  }
8893
8865
  }
8894
- return { type: sentType || types_1.UnknownType.create(), node, isIncomplete };
8866
+ return { type: sentType || types_1.UnknownType.create(), isIncomplete };
8895
8867
  }
8896
8868
  function getTypeOfYieldFrom(node) {
8897
8869
  var _a;
@@ -8914,7 +8886,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8914
8886
  returnedType = generatorTypeArgs.length >= 2 ? generatorTypeArgs[2] : types_1.UnknownType.create();
8915
8887
  }
8916
8888
  }
8917
- return { type: returnedType || types_1.UnknownType.create(), node };
8889
+ return { type: returnedType || types_1.UnknownType.create() };
8918
8890
  }
8919
8891
  function getTypeOfLambda(node, expectedType) {
8920
8892
  let isIncomplete = false;
@@ -9033,14 +9005,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9033
9005
  }
9034
9006
  // Mark the function type as no longer being evaluated.
9035
9007
  functionType.details.flags &= ~131072 /* PartiallyEvaluated */;
9036
- return { type: functionType, node, isIncomplete };
9008
+ return { type: functionType, isIncomplete };
9037
9009
  }
9038
9010
  function getTypeOfListComprehension(node, expectedType) {
9039
9011
  let isIncomplete = false;
9012
+ let typeErrors = false;
9040
9013
  const elementTypeResult = getElementTypeFromListComprehension(node);
9041
9014
  if (elementTypeResult.isIncomplete) {
9042
9015
  isIncomplete = true;
9043
9016
  }
9017
+ if (elementTypeResult.typeErrors) {
9018
+ typeErrors = true;
9019
+ }
9044
9020
  const elementType = elementTypeResult.type;
9045
9021
  let isAsync = node.forIfNodes.some((comp) => {
9046
9022
  return ((comp.nodeType === 33 /* ListComprehensionFor */ && comp.isAsync) ||
@@ -9066,7 +9042,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9066
9042
  : [elementType, types_1.NoneType.createInstance(), types_1.NoneType.createInstance()],
9067
9043
  /* isTypeArgumentExplicit */ true));
9068
9044
  }
9069
- return { type, node, isIncomplete };
9045
+ return { type, isIncomplete, typeErrors };
9070
9046
  }
9071
9047
  function reportPossibleUnknownAssignment(diagLevel, rule, target, type, errorNode, ignoreEmptyContainers) {
9072
9048
  // Don't bother if the feature is disabled.
@@ -9103,7 +9079,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9103
9079
  if (iterableTypeResult.isIncomplete) {
9104
9080
  isIncomplete = true;
9105
9081
  }
9106
- const iterableType = (0, typeUtils_1.stripLiteralValue)(iterableTypeResult.type);
9082
+ const iterableType = stripLiteralValue(iterableTypeResult.type);
9107
9083
  const itemType = (_a = getTypeOfIterator(iterableType, !!node.isAsync, node.iterableExpression)) !== null && _a !== void 0 ? _a : types_1.UnknownType.create();
9108
9084
  const targetExpr = node.targetExpression;
9109
9085
  assignTypeToExpression(targetExpr, itemType, !!iterableTypeResult.isIncomplete, node.iterableExpression);
@@ -9123,6 +9099,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9123
9099
  // as opposed to the entire list.
9124
9100
  function getElementTypeFromListComprehension(node, expectedValueOrElementType, expectedKeyType) {
9125
9101
  let isIncomplete = false;
9102
+ let typeErrors = false;
9126
9103
  // "Execute" the list comprehensions from start to finish.
9127
9104
  for (const forIfNode of node.forIfNodes) {
9128
9105
  if (evaluateListComprehensionForIf(forIfNode)) {
@@ -9137,18 +9114,24 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9137
9114
  if (keyTypeResult.isIncomplete) {
9138
9115
  isIncomplete = true;
9139
9116
  }
9117
+ if (keyTypeResult.typeErrors) {
9118
+ typeErrors = true;
9119
+ }
9140
9120
  let keyType = keyTypeResult.type;
9141
9121
  if (!expectedKeyType || !(0, typeUtils_1.containsLiteralType)(expectedKeyType)) {
9142
- keyType = (0, typeUtils_1.stripLiteralValue)(keyType);
9122
+ keyType = stripLiteralValue(keyType);
9143
9123
  }
9144
9124
  const valueTypeResult = getTypeOfExpression(node.expression.valueExpression,
9145
9125
  /* flags */ undefined, expectedValueOrElementType);
9146
9126
  if (valueTypeResult.isIncomplete) {
9147
9127
  isIncomplete = true;
9148
9128
  }
9129
+ if (valueTypeResult.typeErrors) {
9130
+ typeErrors = true;
9131
+ }
9149
9132
  let valueType = valueTypeResult.type;
9150
9133
  if (!expectedValueOrElementType || !(0, typeUtils_1.containsLiteralType)(expectedValueOrElementType)) {
9151
- valueType = (0, typeUtils_1.stripLiteralValue)(valueType);
9134
+ valueType = stripLiteralValue(valueType);
9152
9135
  }
9153
9136
  type = makeTupleObject([keyType, valueType]);
9154
9137
  }
@@ -9162,9 +9145,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9162
9145
  if (exprTypeResult.isIncomplete) {
9163
9146
  isIncomplete = true;
9164
9147
  }
9148
+ if (exprTypeResult.typeErrors) {
9149
+ typeErrors = true;
9150
+ }
9165
9151
  type = exprTypeResult.type;
9166
9152
  }
9167
- return { type, node, isIncomplete };
9153
+ return { type, isIncomplete, typeErrors };
9168
9154
  }
9169
9155
  function getTypeOfSlice(node) {
9170
9156
  // Evaluate the expressions to report errors and record symbol
@@ -9180,7 +9166,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9180
9166
  getTypeOfExpression(node.stepValue);
9181
9167
  }
9182
9168
  }
9183
- return { type: getBuiltInObject(node, 'slice'), node };
9169
+ return { type: getBuiltInObject(node, 'slice') };
9184
9170
  }
9185
9171
  // Verifies that a type argument's type is not disallowed.
9186
9172
  function validateTypeArg(argResult, options) {
@@ -9785,8 +9771,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9785
9771
  }
9786
9772
  });
9787
9773
  }
9788
- returnType = (0, typeUtils_1.specializeTupleClass)(classType, tupleTypeArgTypes, typeArgs !== undefined,
9789
- /* stripLiterals */ false);
9774
+ returnType = (0, typeUtils_1.specializeTupleClass)(classType, tupleTypeArgTypes, typeArgs !== undefined);
9790
9775
  }
9791
9776
  else {
9792
9777
  returnType = types_1.ClassType.cloneForSpecialization(classType, typeArgTypes, typeArgs !== undefined);
@@ -9880,49 +9865,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9880
9865
  }
9881
9866
  return createSpecialType(classType, typeArgs, /* paramLimit */ undefined, /* allowParamSpec */ true);
9882
9867
  }
9883
- function transformTypeForPossibleEnumClass(node, getValueType) {
9884
- var _a, _b, _c, _d;
9885
- // If the node is within a class that derives from the metaclass
9886
- // "EnumMeta", we need to treat assignments differently.
9887
- const enclosingClassNode = ParseTreeUtils.getEnclosingClass(node, /* stopAtFunction */ true);
9888
- if (enclosingClassNode) {
9889
- const enumClassInfo = getTypeOfClass(enclosingClassNode);
9890
- if (enumClassInfo && types_1.ClassType.isEnumClass(enumClassInfo.classType)) {
9891
- // In ".py" files, the transform applies only to members that are
9892
- // assigned within the class. In stub files, it applies to most variables
9893
- // even if they are not assigned. This unfortunate convention means
9894
- // there is no way in a stub to specify both enum members and instance
9895
- // variables used within each enum instance. Unless/until there is
9896
- // a change to this convention and all type checkers and stubs adopt
9897
- // it, we're stuck with this limitation.
9898
- let isMemberOfEnumeration = (((_a = node.parent) === null || _a === void 0 ? void 0 : _a.nodeType) === 3 /* Assignment */ && node.parent.leftExpression === node) ||
9899
- (((_b = node.parent) === null || _b === void 0 ? void 0 : _b.nodeType) === 54 /* TypeAnnotation */ &&
9900
- node.parent.valueExpression === node &&
9901
- ((_c = node.parent.parent) === null || _c === void 0 ? void 0 : _c.nodeType) === 3 /* Assignment */) ||
9902
- (AnalyzerNodeInfo.getFileInfo(node).isStubFile &&
9903
- ((_d = node.parent) === null || _d === void 0 ? void 0 : _d.nodeType) === 54 /* TypeAnnotation */ &&
9904
- node.parent.valueExpression === node);
9905
- // The spec specifically excludes names that start and end with a single underscore.
9906
- // This also includes dunder names.
9907
- if ((0, symbolNameUtils_1.isSingleDunderName)(node.value)) {
9908
- isMemberOfEnumeration = false;
9909
- }
9910
- // Specifically exclude "value" and "name". These are reserved by the enum metaclass.
9911
- if (node.value === 'name' || node.value === 'value') {
9912
- isMemberOfEnumeration = false;
9913
- }
9914
- const valueType = getValueType();
9915
- // The spec excludes descriptors.
9916
- if ((0, types_1.isClassInstance)(valueType) && valueType.details.fields.get('__get__')) {
9917
- isMemberOfEnumeration = false;
9918
- }
9919
- if (isMemberOfEnumeration) {
9920
- return types_1.ClassType.cloneAsInstance(types_1.ClassType.cloneWithLiteral(enumClassInfo.classType, new types_1.EnumLiteral(enumClassInfo.classType.details.name, node.value, valueType)));
9921
- }
9922
- }
9923
- }
9924
- return undefined;
9925
- }
9926
9868
  function transformTypeForTypeAlias(type, name, errorNode, typeParameters) {
9927
9869
  if (!types_1.TypeBase.isInstantiable(type)) {
9928
9870
  return type;
@@ -10212,7 +10154,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10212
10154
  rightHandType = srcType;
10213
10155
  if (node.leftExpression.nodeType === 38 /* Name */ && !node.typeAnnotationComment) {
10214
10156
  rightHandType =
10215
- transformTypeForPossibleEnumClass(node.leftExpression, () => rightHandType) || rightHandType;
10157
+ (0, enums_1.transformTypeForPossibleEnumClass)(evaluatorInterface, node.leftExpression, () => rightHandType) || rightHandType;
10216
10158
  }
10217
10159
  if (typeAliasNameNode) {
10218
10160
  // Clear out the temporary types we wrote above.
@@ -10316,6 +10258,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10316
10258
  const destTypeResult = getTypeOfAugmentedAssignment(node, /* expectedType */ undefined);
10317
10259
  writeTypeCache(node, destTypeResult.type, 0 /* None */, !!destTypeResult.isIncomplete);
10318
10260
  }
10261
+ function getPseudoGenericTypeVarName(paramName) {
10262
+ return `__type_of_${paramName}`;
10263
+ }
10319
10264
  function getTypeOfClass(node) {
10320
10265
  // Is this type already cached?
10321
10266
  const cachedClassType = readTypeCache(node.name, 0 /* None */);
@@ -10629,13 +10574,16 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10629
10574
  const initParams = initDeclNode.parameters;
10630
10575
  if (initParams.length > 1 &&
10631
10576
  !initParams.some((param, index) => !!ParseTreeUtils.getTypeAnnotationForParameter(initDeclNode, index))) {
10632
- const genericParams = initParams.filter((param, index) => index > 0 && param.name && param.category === 0 /* Simple */);
10577
+ const genericParams = initParams.filter((param, index) => index > 0 &&
10578
+ param.name &&
10579
+ param.category === 0 /* Simple */ &&
10580
+ !param.defaultValue);
10633
10581
  if (genericParams.length > 0) {
10634
10582
  classType.details.flags |= 16384 /* PseudoGenericClass */;
10635
10583
  // Create a type parameter for each simple, named parameter
10636
10584
  // in the __init__ method.
10637
10585
  classType.details.typeParameters = genericParams.map((param) => {
10638
- const typeVar = types_1.TypeVarType.createInstance(`__type_of_${param.name.value}`);
10586
+ const typeVar = types_1.TypeVarType.createInstance(getPseudoGenericTypeVarName(param.name.value));
10639
10587
  typeVar.details.isSynthesized = true;
10640
10588
  typeVar.scopeId = getScopeIdForNode(initDeclNode);
10641
10589
  typeVar.details.boundType = types_1.UnknownType.create();
@@ -11169,7 +11117,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
11169
11117
  types_1.ClassType.isPseudoGenericClass(containingClassType) &&
11170
11118
  node.name.value === '__init__';
11171
11119
  const paramTypes = [];
11172
- let typeParamIndex = 0;
11173
11120
  // Determine if the first parameter should be skipped for comment-based
11174
11121
  // function annotations.
11175
11122
  let firstCommentAnnotationIndex = 0;
@@ -11252,9 +11199,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
11252
11199
  }
11253
11200
  }
11254
11201
  if (!annotatedType && addGenericParamTypes) {
11255
- if (index > 0 && param.category === 0 /* Simple */ && param.name) {
11256
- annotatedType = containingClassType.details.typeParameters[typeParamIndex];
11257
- typeParamIndex++;
11202
+ if (index > 0 && param.category === 0 /* Simple */ && param.name && !param.defaultValue) {
11203
+ const typeParamName = getPseudoGenericTypeVarName(param.name.value);
11204
+ annotatedType = containingClassType.details.typeParameters.find((param) => param.details.name === typeParamName);
11258
11205
  }
11259
11206
  }
11260
11207
  if (annotatedType) {
@@ -11543,7 +11490,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
11543
11490
  // more restrictive (narrower) than intended.
11544
11491
  if (!(0, types_1.isClassInstance)(defaultValueType) ||
11545
11492
  !types_1.ClassType.isBuiltIn(defaultValueType, ['tuple', 'list', 'set', 'dict'])) {
11546
- inferredParamType = (0, typeUtils_1.stripLiteralValue)(defaultValueType);
11493
+ inferredParamType = stripLiteralValue(defaultValueType);
11547
11494
  }
11548
11495
  }
11549
11496
  if (inferredParamType) {
@@ -11573,8 +11520,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
11573
11520
  }
11574
11521
  if (tupleClassType && (0, types_1.isInstantiableClass)(tupleClassType)) {
11575
11522
  return types_1.ClassType.cloneAsInstance((0, typeUtils_1.specializeTupleClass)(tupleClassType, [{ type, isUnbounded: !(0, types_1.isVariadicTypeVar)(type) }],
11576
- /* isTypeArgumentExplicit */ true,
11577
- /* stripLiterals */ true));
11523
+ /* isTypeArgumentExplicit */ true));
11578
11524
  }
11579
11525
  return types_1.UnknownType.create();
11580
11526
  }
@@ -11889,12 +11835,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
11889
11835
  if (inferredReturnType) {
11890
11836
  return { type: inferredReturnType, isIncomplete };
11891
11837
  }
11892
- if (functionRecursionMap.has(node.id) && functionRecursionMap.size < maxInferFunctionReturnRecursionCount) {
11838
+ if (functionRecursionMap.has(node.id) || functionRecursionMap.size >= maxInferFunctionReturnRecursionCount) {
11893
11839
  inferredReturnType = types_1.UnknownType.create();
11894
11840
  isIncomplete = true;
11895
11841
  }
11896
11842
  else {
11897
- functionRecursionMap.set(node.id, true);
11843
+ functionRecursionMap.add(node.id);
11898
11844
  try {
11899
11845
  let functionDecl;
11900
11846
  const decl = AnalyzerNodeInfo.getDeclaration(node);
@@ -12328,6 +12274,23 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
12328
12274
  assignTypeToNameNode(symbolNameNode, symbolType, /* isIncomplete */ false, /* ignoreEmptyContainers */ false);
12329
12275
  writeTypeCache(node, symbolType, 0 /* None */, /* isIncomplete */ false);
12330
12276
  }
12277
+ function evaluateTypesForTypeAnnotationNode(node) {
12278
+ var _a;
12279
+ // If this node is part of an assignment statement, use specialized
12280
+ // logic that performs bidirectional inference and assignment
12281
+ // type narrowing.
12282
+ if (((_a = node.parent) === null || _a === void 0 ? void 0 : _a.nodeType) === 3 /* Assignment */) {
12283
+ evaluateTypesForAssignmentStatement(node.parent);
12284
+ }
12285
+ else {
12286
+ const annotationType = getTypeOfAnnotation(node.typeAnnotation, {
12287
+ isVariableAnnotation: true,
12288
+ allowFinal: ParseTreeUtils.isFinalAllowedForAssignmentTarget(node.valueExpression),
12289
+ allowClassVar: ParseTreeUtils.isClassVarAllowedForAssignmentTarget(node.valueExpression),
12290
+ });
12291
+ writeTypeCache(node.valueExpression, annotationType, 0 /* None */, /* isIncomplete */ false);
12292
+ }
12293
+ }
12331
12294
  function getAliasedSymbolTypeForName(node, name) {
12332
12295
  const symbolWithScope = lookUpSymbolRecursive(node, name, /* honorCodeFlow */ true);
12333
12296
  if (!symbolWithScope) {
@@ -12388,62 +12351,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
12388
12351
  // allows for bidirectional type evaluation.
12389
12352
  function evaluateTypesForExpressionInContext(node) {
12390
12353
  var _a, _b, _c, _d, _e;
12391
- let lastContextualExpression = node;
12392
- let curNode = node;
12393
- function isContextual(node) {
12394
- var _a, _b, _c, _d, _e;
12395
- // Parameters are contextual only for lambdas.
12396
- if (node.nodeType === 41 /* Parameter */ && ((_a = node.parent) === null || _a === void 0 ? void 0 : _a.nodeType) === 30 /* Lambda */) {
12397
- return true;
12398
- }
12399
- // Arguments are contextual only for call and index nodes.
12400
- if (node.nodeType === 1 /* Argument */ &&
12401
- (((_b = node.parent) === null || _b === void 0 ? void 0 : _b.nodeType) === 9 /* Call */ || ((_c = node.parent) === null || _c === void 0 ? void 0 : _c.nodeType) === 24 /* Index */)) {
12402
- return true;
12403
- }
12404
- // All nodes within a type annotation need to be evaluated
12405
- // contextually so we pass the "type expected" flag to
12406
- // the evaluator.
12407
- if (((_d = node.parent) === null || _d === void 0 ? void 0 : _d.nodeType) === 54 /* TypeAnnotation */) {
12408
- return true;
12409
- }
12410
- if (((_e = node.parent) === null || _e === void 0 ? void 0 : _e.nodeType) === 41 /* Parameter */ &&
12411
- (node === node.parent.typeAnnotation || node === node.parent.typeAnnotationComment)) {
12412
- return true;
12413
- }
12414
- // The | operator is contextual in cases where it is used to describe a
12415
- // union in type annotations.
12416
- if (node.nodeType === 7 /* BinaryOperation */ && node.operator === 6 /* BitwiseOr */) {
12417
- return true;
12418
- }
12419
- return (node.nodeType === 9 /* Call */ ||
12420
- node.nodeType === 24 /* Index */ ||
12421
- node.nodeType === 15 /* Dictionary */ ||
12422
- node.nodeType === 27 /* FormatString */ ||
12423
- node.nodeType === 31 /* List */ ||
12424
- node.nodeType === 30 /* Lambda */ ||
12425
- node.nodeType === 35 /* MemberAccess */ ||
12426
- node.nodeType === 45 /* Set */ ||
12427
- node.nodeType === 49 /* String */ ||
12428
- node.nodeType === 48 /* StringList */ ||
12429
- node.nodeType === 52 /* Tuple */ ||
12430
- node.nodeType === 56 /* Unpack */ ||
12431
- node.nodeType === 17 /* DictionaryKeyEntry */ ||
12432
- node.nodeType === 16 /* DictionaryExpandEntry */ ||
12433
- node.nodeType === 32 /* ListComprehension */ ||
12434
- node.nodeType === 33 /* ListComprehensionFor */ ||
12435
- node.nodeType === 34 /* ListComprehensionIf */ ||
12436
- node.nodeType === 65 /* PatternSequence */ ||
12437
- node.nodeType === 67 /* PatternLiteral */ ||
12438
- node.nodeType === 68 /* PatternClass */ ||
12439
- node.nodeType === 74 /* PatternClassArgument */ ||
12440
- node.nodeType === 66 /* PatternAs */ ||
12441
- node.nodeType === 69 /* PatternCapture */ ||
12442
- node.nodeType === 70 /* PatternMapping */ ||
12443
- node.nodeType === 73 /* PatternValue */ ||
12444
- node.nodeType === 71 /* PatternMappingKeyEntry */ ||
12445
- node.nodeType === 72 /* PatternMappingExpandEntry */);
12446
- }
12447
12354
  // Check for a couple of special cases where the node is a NameNode but
12448
12355
  // is technically not part of an expression. We'll handle these here so
12449
12356
  // callers don't need to include special-case logic.
@@ -12452,154 +12359,235 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
12452
12359
  getTypeOfFunction(node.parent);
12453
12360
  return;
12454
12361
  }
12455
- else if (node.parent.nodeType === 10 /* Class */ && node.parent.name === node) {
12362
+ if (node.parent.nodeType === 10 /* Class */ && node.parent.name === node) {
12456
12363
  getTypeOfClass(node.parent);
12457
12364
  return;
12458
12365
  }
12459
- else if (node.parent.nodeType === 77 /* TypeAlias */ && node.parent.name === node) {
12366
+ if (node.parent.nodeType === 77 /* TypeAlias */ && node.parent.name === node) {
12460
12367
  getTypeOfTypeAlias(node.parent);
12461
12368
  return;
12462
12369
  }
12463
- else if (node.parent.nodeType === 29 /* Global */ ||
12464
- node.parent.nodeType === 39 /* Nonlocal */) {
12370
+ if (node.parent.nodeType === 29 /* Global */ || node.parent.nodeType === 39 /* Nonlocal */) {
12465
12371
  // For global and nonlocal statements, allow forward references so
12466
12372
  // we don't use code flow during symbol lookups.
12467
12373
  getTypeOfExpression(node, 4 /* AllowForwardReferences */);
12468
12374
  return;
12469
12375
  }
12376
+ if (node.parent.nodeType === 37 /* ModuleName */) {
12377
+ // A name within a module name isn't an expression,
12378
+ // so there's nothing we can evaluate here.
12379
+ return;
12380
+ }
12470
12381
  }
12471
- // Scan up the parse tree until we find a non-expression (while
12472
- // looking for contextual expressions in the process).
12473
- while (curNode) {
12474
- const isNodeContextual = isContextual(curNode);
12475
- if (!isNodeContextual && !(0, parseNodes_1.isExpressionNode)(curNode)) {
12476
- break;
12382
+ // If the expression is part of a type annotation, we need to evaluate
12383
+ // it with special evaluation flags.
12384
+ const annotationNode = ParseTreeUtils.getParentAnnotationNode(node);
12385
+ if (annotationNode) {
12386
+ // Annotations need to be evaluated with specialized evaluation flags.
12387
+ const annotationParent = annotationNode.parent;
12388
+ (0, debug_1.assert)(annotationParent !== undefined);
12389
+ if (annotationParent.nodeType === 3 /* Assignment */) {
12390
+ if (annotationNode === annotationParent.typeAnnotationComment) {
12391
+ getTypeOfAnnotation(annotationNode, {
12392
+ isVariableAnnotation: true,
12393
+ allowFinal: ParseTreeUtils.isFinalAllowedForAssignmentTarget(annotationParent.leftExpression),
12394
+ allowClassVar: ParseTreeUtils.isClassVarAllowedForAssignmentTarget(annotationParent.leftExpression),
12395
+ });
12396
+ }
12397
+ else {
12398
+ evaluateTypesForAssignmentStatement(annotationParent);
12399
+ }
12400
+ return;
12477
12401
  }
12478
- if (isNodeContextual) {
12479
- lastContextualExpression = curNode;
12402
+ if (annotationParent.nodeType === 54 /* TypeAnnotation */) {
12403
+ evaluateTypesForTypeAnnotationNode(annotationParent);
12404
+ return;
12480
12405
  }
12481
- curNode = curNode.parent;
12482
- }
12483
- const parent = lastContextualExpression.parent;
12484
- if (parent.nodeType === 3 /* Assignment */) {
12485
- if (lastContextualExpression === parent.typeAnnotationComment) {
12486
- getTypeOfAnnotation(lastContextualExpression, {
12487
- isVariableAnnotation: true,
12488
- allowFinal: ParseTreeUtils.isFinalAllowedForAssignmentTarget(parent.leftExpression),
12489
- allowClassVar: ParseTreeUtils.isClassVarAllowedForAssignmentTarget(parent.leftExpression),
12406
+ if (annotationParent.nodeType === 28 /* Function */ &&
12407
+ annotationNode === annotationParent.returnTypeAnnotation) {
12408
+ getTypeOfAnnotation(annotationNode, {
12409
+ associateTypeVarsWithScope: true,
12410
+ disallowRecursiveTypeAlias: true,
12490
12411
  });
12412
+ return;
12491
12413
  }
12492
- else {
12493
- evaluateTypesForAssignmentStatement(parent);
12494
- }
12495
- return;
12496
- }
12497
- else if (parent.nodeType === 14 /* Del */) {
12498
- verifyDeleteExpression(lastContextualExpression);
12414
+ getTypeOfAnnotation(annotationNode);
12499
12415
  return;
12500
12416
  }
12501
- // If this is the name node within a type parameter list, see if it's a type alias
12502
- // definition. If so, we need to evaluate the type alias contextually.
12503
- if (parent.nodeType === 75 /* TypeParameter */ && lastContextualExpression === parent.name) {
12504
- if (((_a = parent.parent) === null || _a === void 0 ? void 0 : _a.nodeType) === 76 /* TypeParameterList */ &&
12505
- ((_b = parent.parent.parent) === null || _b === void 0 ? void 0 : _b.nodeType) === 77 /* TypeAlias */) {
12506
- getTypeOfTypeAlias(parent.parent.parent);
12417
+ // See if the expression is part of a pattern used in a case statement.
12418
+ const possibleCaseNode = ParseTreeUtils.getParentNodeOfType(node, 64 /* Case */);
12419
+ if (possibleCaseNode) {
12420
+ const caseNode = possibleCaseNode;
12421
+ if (ParseTreeUtils.isNodeContainedWithin(node, caseNode.pattern)) {
12422
+ evaluateTypesForCaseStatement(caseNode);
12507
12423
  return;
12508
12424
  }
12509
12425
  }
12510
- if (parent.nodeType === 75 /* TypeParameter */) {
12511
- getTypeOfExpression(parent.name);
12512
- return;
12513
- }
12514
- if (parent.nodeType === 77 /* TypeAlias */) {
12515
- getTypeOfTypeAlias(parent);
12516
- return;
12517
- }
12518
- if (parent.nodeType === 5 /* AugmentedAssignment */) {
12519
- evaluateTypesForAugmentedAssignment(parent);
12520
- return;
12521
- }
12522
- if (parent.nodeType === 13 /* Decorator */) {
12523
- if (((_c = parent.parent) === null || _c === void 0 ? void 0 : _c.nodeType) === 10 /* Class */) {
12524
- getTypeOfClass(parent.parent);
12426
+ // Scan up the parse tree until we find a node that doesn't
12427
+ // require any context to be evaluated.
12428
+ let nodeToEvaluate = node;
12429
+ let flags = 0 /* None */;
12430
+ while (true) {
12431
+ // If we're within an argument node in a call or index expression, skip
12432
+ // all of the nodes between because the entire argument expression
12433
+ // needs to be evaluated contextually.
12434
+ const argumentNode = ParseTreeUtils.getParentNodeOfType(nodeToEvaluate, 1 /* Argument */);
12435
+ if (argumentNode && argumentNode !== nodeToEvaluate) {
12436
+ (0, debug_1.assert)(argumentNode.parent !== undefined);
12437
+ if (argumentNode.parent.nodeType === 9 /* Call */ ||
12438
+ argumentNode.parent.nodeType === 24 /* Index */) {
12439
+ nodeToEvaluate = argumentNode.parent;
12440
+ continue;
12441
+ }
12525
12442
  }
12526
- else if (((_d = parent.parent) === null || _d === void 0 ? void 0 : _d.nodeType) === 28 /* Function */) {
12527
- getTypeOfFunction(parent.parent);
12443
+ let parent = nodeToEvaluate.parent;
12444
+ if (!parent) {
12445
+ break;
12528
12446
  }
12529
- return;
12530
- }
12531
- const evaluateTypeAnnotationExpression = (node) => {
12532
- const annotationParent = node.parent;
12533
- if ((annotationParent === null || annotationParent === void 0 ? void 0 : annotationParent.nodeType) === 3 /* Assignment */ && annotationParent.leftExpression === parent) {
12534
- evaluateTypesForAssignmentStatement(annotationParent);
12447
+ // If this is the target of an assignment expression, evaluate the
12448
+ // assignment expression node instead.
12449
+ if (parent.nodeType === 4 /* AssignmentExpression */ && nodeToEvaluate === parent.name) {
12450
+ nodeToEvaluate = parent;
12451
+ continue;
12452
+ }
12453
+ // The left expression of a call or member access expression is not contextual.
12454
+ if (parent.nodeType === 9 /* Call */ || parent.nodeType === 35 /* MemberAccess */) {
12455
+ if (nodeToEvaluate === parent.leftExpression) {
12456
+ // Handle the special case where the LHS is a call to super().
12457
+ if (nodeToEvaluate.nodeType === 9 /* Call */ &&
12458
+ nodeToEvaluate.leftExpression.nodeType === 38 /* Name */ &&
12459
+ nodeToEvaluate.leftExpression.value === 'super') {
12460
+ nodeToEvaluate = parent;
12461
+ continue;
12462
+ }
12463
+ flags = 2 /* DoNotSpecialize */;
12464
+ break;
12465
+ }
12466
+ }
12467
+ else if (parent.nodeType === 24 /* Index */) {
12468
+ // The base expression of an index expression is not contextual.
12469
+ if (nodeToEvaluate === parent.baseExpression) {
12470
+ flags = 2 /* DoNotSpecialize */;
12471
+ break;
12472
+ }
12535
12473
  }
12536
12474
  else {
12537
- const annotationType = getTypeOfAnnotation(node.typeAnnotation, {
12538
- isVariableAnnotation: true,
12539
- allowFinal: ParseTreeUtils.isFinalAllowedForAssignmentTarget(node.valueExpression),
12540
- allowClassVar: ParseTreeUtils.isClassVarAllowedForAssignmentTarget(node.valueExpression),
12541
- });
12542
- writeTypeCache(node.valueExpression, annotationType, 0 /* None */, /* isIncomplete */ false);
12475
+ // Check for expression types that are always contextual.
12476
+ if (nodeToEvaluate.nodeType !== 15 /* Dictionary */ &&
12477
+ nodeToEvaluate.nodeType !== 31 /* List */ &&
12478
+ nodeToEvaluate.nodeType !== 30 /* Lambda */ &&
12479
+ nodeToEvaluate.nodeType !== 45 /* Set */ &&
12480
+ nodeToEvaluate.nodeType !== 52 /* Tuple */ &&
12481
+ nodeToEvaluate.nodeType !== 56 /* Unpack */ &&
12482
+ nodeToEvaluate.nodeType !== 32 /* ListComprehension */) {
12483
+ break;
12484
+ }
12543
12485
  }
12544
- };
12545
- if (parent.nodeType === 64 /* Case */ && lastContextualExpression !== parent.guardExpression) {
12546
- evaluateTypesForCaseStatement(parent);
12547
- return;
12548
- }
12549
- if (parent.nodeType === 54 /* TypeAnnotation */) {
12550
- evaluateTypeAnnotationExpression(parent);
12551
- return;
12552
- }
12553
- if (parent.nodeType === 41 /* Parameter */ && lastContextualExpression !== parent.defaultValue) {
12554
- evaluateTypeOfParameter(parent);
12555
- return;
12486
+ if (!(0, parseNodes_1.isExpressionNode)(parent)) {
12487
+ // If we've hit a non-expression node, we generally want to
12488
+ // stop. However, there are a few special "pass through"
12489
+ // node types that we can skip over to get to a known
12490
+ // expression node.
12491
+ if (parent.nodeType === 17 /* DictionaryKeyEntry */ ||
12492
+ parent.nodeType === 16 /* DictionaryExpandEntry */ ||
12493
+ parent.nodeType === 33 /* ListComprehensionFor */ ||
12494
+ parent.nodeType === 34 /* ListComprehensionIf */) {
12495
+ (0, debug_1.assert)(parent.parent !== undefined && (0, parseNodes_1.isExpressionNode)(parent.parent));
12496
+ parent = parent.parent;
12497
+ }
12498
+ else if (parent.nodeType === 41 /* Parameter */) {
12499
+ (0, debug_1.assert)(parent.parent !== undefined);
12500
+ // Parameters are contextual for lambdas.
12501
+ if (parent.parent.nodeType === 30 /* Lambda */) {
12502
+ parent = parent.parent;
12503
+ }
12504
+ else {
12505
+ break;
12506
+ }
12507
+ }
12508
+ else {
12509
+ break;
12510
+ }
12511
+ }
12512
+ nodeToEvaluate = parent;
12556
12513
  }
12557
- if (parent.nodeType === 28 /* Function */) {
12558
- if (lastContextualExpression === parent.returnTypeAnnotation) {
12559
- getTypeOfAnnotation(lastContextualExpression, {
12560
- associateTypeVarsWithScope: true,
12561
- disallowRecursiveTypeAlias: true,
12562
- });
12514
+ const parent = nodeToEvaluate.parent;
12515
+ (0, debug_1.assert)(parent !== undefined);
12516
+ switch (parent.nodeType) {
12517
+ case 14 /* Del */: {
12518
+ verifyDeleteExpression(nodeToEvaluate);
12519
+ return;
12520
+ }
12521
+ case 75 /* TypeParameter */: {
12522
+ // If this is the name node within a type parameter list, see if it's a type alias
12523
+ // definition. If so, we need to evaluate the type alias contextually.
12524
+ if (nodeToEvaluate === parent.name &&
12525
+ ((_a = parent.parent) === null || _a === void 0 ? void 0 : _a.nodeType) === 76 /* TypeParameterList */ &&
12526
+ ((_b = parent.parent.parent) === null || _b === void 0 ? void 0 : _b.nodeType) === 77 /* TypeAlias */) {
12527
+ getTypeOfTypeAlias(parent.parent.parent);
12528
+ return;
12529
+ }
12530
+ break;
12531
+ }
12532
+ case 77 /* TypeAlias */: {
12533
+ getTypeOfTypeAlias(parent);
12534
+ return;
12535
+ }
12536
+ case 13 /* Decorator */: {
12537
+ if (((_c = parent.parent) === null || _c === void 0 ? void 0 : _c.nodeType) === 10 /* Class */) {
12538
+ getTypeOfClass(parent.parent);
12539
+ }
12540
+ else if (((_d = parent.parent) === null || _d === void 0 ? void 0 : _d.nodeType) === 28 /* Function */) {
12541
+ getTypeOfFunction(parent.parent);
12542
+ }
12543
+ return;
12544
+ }
12545
+ case 41 /* Parameter */: {
12546
+ if (nodeToEvaluate !== parent.defaultValue) {
12547
+ evaluateTypeOfParameter(parent);
12548
+ return;
12549
+ }
12550
+ break;
12551
+ }
12552
+ case 1 /* Argument */: {
12553
+ if (nodeToEvaluate === parent.name) {
12554
+ // A name used to specify a named parameter in an argument isn't an
12555
+ // expression, so there's nothing we can evaluate here.
12556
+ return;
12557
+ }
12558
+ if (((_e = parent.parent) === null || _e === void 0 ? void 0 : _e.nodeType) === 10 /* Class */) {
12559
+ // A class argument must be evaluated in the context of the class declaration.
12560
+ getTypeOfClass(parent.parent);
12561
+ return;
12562
+ }
12563
+ break;
12564
+ }
12565
+ case 44 /* Return */: {
12566
+ // Return expressions must be evaluated in the context of the expected return type.
12567
+ if (parent.returnExpression) {
12568
+ const enclosingFunctionNode = ParseTreeUtils.getEnclosingFunction(node);
12569
+ const declaredReturnType = enclosingFunctionNode
12570
+ ? getFunctionDeclaredReturnType(enclosingFunctionNode)
12571
+ : undefined;
12572
+ getTypeOfExpression(parent.returnExpression, 0 /* None */, declaredReturnType);
12573
+ return;
12574
+ }
12575
+ break;
12576
+ }
12577
+ case 54 /* TypeAnnotation */: {
12578
+ evaluateTypesForTypeAnnotationNode(parent);
12579
+ return;
12580
+ }
12581
+ case 3 /* Assignment */: {
12582
+ evaluateTypesForAssignmentStatement(parent);
12563
12583
  return;
12564
12584
  }
12565
12585
  }
12566
- if (parent.nodeType === 37 /* ModuleName */) {
12567
- // A name within a module name isn't an expression,
12568
- // so there's nothing we can evaluate here.
12569
- return;
12570
- }
12571
- if (parent.nodeType === 1 /* Argument */ && lastContextualExpression === parent.name) {
12572
- // A name used to specify a named parameter in an argument isn't an
12573
- // expression, so there's nothing we can evaluate here.
12574
- return;
12575
- }
12576
- // A class argument must be evaluated in the context of the class declaration.
12577
- if (parent.nodeType === 1 /* Argument */ && ((_e = parent.parent) === null || _e === void 0 ? void 0 : _e.nodeType) === 10 /* Class */) {
12578
- getTypeOfClass(parent.parent);
12579
- return;
12580
- }
12581
- if (parent.nodeType === 44 /* Return */ && parent.returnExpression) {
12582
- const enclosingFunctionNode = ParseTreeUtils.getEnclosingFunction(node);
12583
- const declaredReturnType = enclosingFunctionNode
12584
- ? getFunctionDeclaredReturnType(enclosingFunctionNode)
12585
- : undefined;
12586
- getTypeOfExpression(parent.returnExpression, 0 /* None */, declaredReturnType);
12587
- return;
12588
- }
12589
- // If the parent is an expression, we'll evaluate it to provide
12590
- // the context for its child. If it's not, we'll evaluate the
12591
- // child directly without any context.
12592
- const nodeToEvaluate = (0, parseNodes_1.isExpressionNode)(parent) && parent.nodeType !== 0 /* Error */
12593
- ? parent
12594
- : lastContextualExpression;
12595
12586
  if (nodeToEvaluate.nodeType === 54 /* TypeAnnotation */) {
12596
- evaluateTypeAnnotationExpression(nodeToEvaluate);
12597
- }
12598
- else {
12599
- const fileInfo = AnalyzerNodeInfo.getFileInfo(nodeToEvaluate);
12600
- const flags = fileInfo.isStubFile ? 4 /* AllowForwardReferences */ : 0 /* None */;
12601
- getTypeOfExpression(nodeToEvaluate, flags);
12587
+ evaluateTypesForTypeAnnotationNode(nodeToEvaluate);
12588
+ return;
12602
12589
  }
12590
+ getTypeOfExpression(nodeToEvaluate, flags);
12603
12591
  }
12604
12592
  function evaluateTypeOfParameter(node) {
12605
12593
  // If this parameter has no name, we have nothing to do.
@@ -12627,24 +12615,23 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
12627
12615
  return;
12628
12616
  }
12629
12617
  const containingClassNode = ParseTreeUtils.getEnclosingClass(functionNode, /* stopAtFunction */ true);
12630
- if (containingClassNode) {
12631
- const classInfo = getTypeOfClass(containingClassNode);
12632
- if (classInfo) {
12633
- // See if the function is a method in a child class. We may be able to
12634
- // infer the type of the parameter from a method of the same name in
12635
- // a parent class if it has an annotated type.
12636
- const functionFlags = getFunctionFlagsFromDecorators(functionNode, /* isInClass */ true);
12637
- const inferredParamType = inferParameterType(functionNode, functionFlags, paramIndex, classInfo.classType);
12638
- if (inferredParamType) {
12639
- writeTypeCache(node.name, transformVariadicParamType(node, node.category, inferredParamType), 0 /* None */,
12640
- /* isIncomplete */ false);
12641
- return;
12642
- }
12618
+ const classInfo = containingClassNode ? getTypeOfClass(containingClassNode) : undefined;
12619
+ if (classInfo &&
12620
+ types_1.ClassType.isPseudoGenericClass(classInfo === null || classInfo === void 0 ? void 0 : classInfo.classType) &&
12621
+ functionNode.name.value === '__init__') {
12622
+ const typeParamName = getPseudoGenericTypeVarName(node.name.value);
12623
+ const paramType = classInfo.classType.details.typeParameters.find((param) => param.details.name === typeParamName);
12624
+ if (paramType) {
12625
+ writeTypeCache(node.name, paramType, 0 /* None */, /* isIncomplete */ false);
12626
+ return;
12643
12627
  }
12644
12628
  }
12645
- // We weren't able to infer the input parameter type. Set its
12646
- // type to unknown.
12647
- writeTypeCache(node.name, transformVariadicParamType(node, node.category, types_1.UnknownType.create()), 0 /* None */,
12629
+ // See if the function is a method in a child class. We may be able to
12630
+ // infer the type of the parameter from a method of the same name in
12631
+ // a parent class if it has an annotated type.
12632
+ const functionFlags = getFunctionFlagsFromDecorators(functionNode, /* isInClass */ true);
12633
+ const inferredParamType = inferParameterType(functionNode, functionFlags, paramIndex, classInfo === null || classInfo === void 0 ? void 0 : classInfo.classType);
12634
+ writeTypeCache(node.name, transformVariadicParamType(node, node.category, inferredParamType !== null && inferredParamType !== void 0 ? inferredParamType : types_1.UnknownType.create()), 0 /* None */,
12648
12635
  /* isIncomplete */ false);
12649
12636
  }
12650
12637
  // Evaluates the types that are assigned within the statement that contains
@@ -12759,7 +12746,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
12759
12746
  // doing additional work.
12760
12747
  let subnodeType = readTypeCache(subnode, /* flags */ undefined);
12761
12748
  if (subnodeType) {
12762
- return { node: subnode, type: subnodeType };
12749
+ return { type: subnodeType };
12763
12750
  }
12764
12751
  const oldIncompleteCache = incompleteTypeCache;
12765
12752
  try {
@@ -12772,11 +12759,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
12772
12759
  callback();
12773
12760
  subnodeType = readTypeCache(subnode, /* flags */ undefined);
12774
12761
  if (subnodeType) {
12775
- return { node: subnode, type: subnodeType };
12762
+ return { type: subnodeType };
12776
12763
  }
12777
12764
  subnodeType = incompleteTypeCache.get(subnode.id);
12778
12765
  if (subnodeType) {
12779
- return { node: subnode, type: subnodeType, isIncomplete: true };
12766
+ return { type: subnodeType, isIncomplete: true };
12780
12767
  }
12781
12768
  incompleteTypeCache = oldIncompleteCache;
12782
12769
  }
@@ -13685,7 +13672,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
13685
13672
  // Apply enum transform if appropriate.
13686
13673
  if (declaration.node.nodeType === 38 /* Name */) {
13687
13674
  declaredType =
13688
- transformTypeForPossibleEnumClass(declaration.node, () => declaredType) || declaredType;
13675
+ (0, enums_1.transformTypeForPossibleEnumClass)(evaluatorInterface, declaration.node, () => declaredType) || declaredType;
13689
13676
  }
13690
13677
  if (typeAliasNode && typeAliasNode.valueExpression.nodeType === 38 /* Name */) {
13691
13678
  declaredType = transformTypeForTypeAlias(declaredType, typeAliasNode.valueExpression, declaration.node);
@@ -13816,7 +13803,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
13816
13803
  })) === null || _b === void 0 ? void 0 : _b.type;
13817
13804
  if (inferredType && resolvedDecl.node.nodeType === 38 /* Name */) {
13818
13805
  // See if this is an enum member. If so, we need to handle it as a special case.
13819
- const enumMemberType = transformTypeForPossibleEnumClass(resolvedDecl.node, () => {
13806
+ const enumMemberType = (0, enums_1.transformTypeForPossibleEnumClass)(evaluatorInterface, resolvedDecl.node, () => {
13820
13807
  var _a, _b;
13821
13808
  return ((_b = (_a = evaluateTypeForSubnode(resolvedDecl.inferredTypeSource, () => {
13822
13809
  evaluateTypesForStatement(resolvedDecl.inferredTypeSource);
@@ -14041,13 +14028,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
14041
14028
  let isConstant = decl.type === 1 /* Variable */ && !!decl.isConstant;
14042
14029
  // Treat enum values declared within an enum class as though they are const even
14043
14030
  // though they may not be named as such.
14044
- if ((0, types_1.isClassInstance)(type) && types_1.ClassType.isEnumClass(type) && isDeclInEnumClass(decl)) {
14031
+ if ((0, types_1.isClassInstance)(type) &&
14032
+ types_1.ClassType.isEnumClass(type) &&
14033
+ (0, enums_1.isDeclInEnumClass)(evaluatorInterface, decl)) {
14045
14034
  isConstant = true;
14046
14035
  }
14047
14036
  // If the symbol is constant, we can retain the literal
14048
14037
  // value. Otherwise, strip literal values to widen the type.
14049
14038
  if (types_1.TypeBase.isInstance(type) && !isExplicitTypeAlias && !isConstant && !isFinalVar) {
14050
- type = (0, typeUtils_1.stripLiteralValue)(type);
14039
+ type = stripLiteralValue(type);
14051
14040
  }
14052
14041
  }
14053
14042
  typesToCombine.push(type);
@@ -14168,17 +14157,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
14168
14157
  }
14169
14158
  return undefined;
14170
14159
  }
14171
- function isDeclInEnumClass(decl) {
14172
- const classNode = ParseTreeUtils.getEnclosingClass(decl.node, /* stopAtFunction */ true);
14173
- if (!classNode) {
14174
- return false;
14175
- }
14176
- const classInfo = getTypeOfClass(classNode);
14177
- if (!classInfo) {
14178
- return false;
14179
- }
14180
- return types_1.ClassType.isEnumClass(classInfo.classType);
14181
- }
14182
14160
  function inferReturnTypeIfNecessary(type) {
14183
14161
  if ((0, types_1.isFunction)(type)) {
14184
14162
  getFunctionEffectiveReturnType(type);
@@ -14433,7 +14411,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
14433
14411
  }
14434
14412
  return types_1.UnknownType.create();
14435
14413
  }
14436
- function getTypeOfMemberInternal(node, member, selfClass) {
14414
+ function getTypeOfMemberInternal(member, selfClass) {
14437
14415
  if ((0, types_1.isInstantiableClass)(member.classType)) {
14438
14416
  const typeResult = getEffectiveTypeOfSymbolForUsage(member.symbol);
14439
14417
  if (typeResult) {
@@ -14442,7 +14420,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
14442
14420
  // prior to specializing.
14443
14421
  inferReturnTypeIfNecessary(typeResult.type);
14444
14422
  return {
14445
- node,
14446
14423
  type: (0, typeUtils_1.partiallySpecializeType)(typeResult.type, member.classType, selfClass),
14447
14424
  isIncomplete: !!typeResult.isIncomplete,
14448
14425
  };
@@ -14622,10 +14599,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
14622
14599
  const removedArgs = srcTypeArgs.splice(destVariadicIndex, srcArgsToCapture);
14623
14600
  // Package up the remaining type arguments into a tuple object.
14624
14601
  const variadicTuple = (0, typeUtils_1.convertToInstance)((0, typeUtils_1.specializeTupleClass)(tupleClassType, removedArgs.map((typeArg) => {
14625
- return { type: (0, typeUtils_1.stripLiteralValue)(typeArg.type), isUnbounded: false };
14602
+ return { type: stripLiteralValue(typeArg.type), isUnbounded: false };
14626
14603
  }),
14627
14604
  /* isTypeArgumentExplicit */ true,
14628
- /* stripLiterals */ true,
14629
14605
  /* isUnpackedTuple */ true));
14630
14606
  srcTypeArgs.splice(destVariadicIndex, 0, {
14631
14607
  type: variadicTuple,
@@ -14960,9 +14936,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
14960
14936
  (0, typeUtils_1.isTupleClass)(srcType) &&
14961
14937
  srcType.tupleTypeArguments &&
14962
14938
  srcType.tupleTypeArguments.length === 1) {
14963
- if ((0, types_1.isTypeSame)(destType, srcType.tupleTypeArguments[0].type,
14964
- /* ignorePseudoGeneric */ undefined,
14965
- /* ignoreTypeFlags */ undefined, recursionCount)) {
14939
+ if ((0, types_1.isTypeSame)(destType, srcType.tupleTypeArguments[0].type, {}, recursionCount)) {
14966
14940
  return true;
14967
14941
  }
14968
14942
  }
@@ -15183,7 +15157,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
15183
15157
  }
15184
15158
  }
15185
15159
  }
15186
- const concreteSrcType = makeTopLevelTypeVarsConcrete(srcType);
15160
+ let concreteSrcType = makeTopLevelTypeVarsConcrete(srcType);
15187
15161
  if ((0, types_1.isClass)(concreteSrcType) && types_1.TypeBase.isInstance(concreteSrcType)) {
15188
15162
  if (destType.literalValue !== undefined) {
15189
15163
  const srcLiteral = concreteSrcType.literalValue;
@@ -15196,15 +15170,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
15196
15170
  }
15197
15171
  }
15198
15172
  // Handle LiteralString special form.
15199
- if (types_1.ClassType.isBuiltIn(destType, 'LiteralString') && types_1.ClassType.isBuiltIn(concreteSrcType, 'str')) {
15200
- if (concreteSrcType.literalValue !== undefined) {
15173
+ if (types_1.ClassType.isBuiltIn(destType, 'LiteralString')) {
15174
+ if (types_1.ClassType.isBuiltIn(concreteSrcType, 'str') && concreteSrcType.literalValue !== undefined) {
15175
+ return true;
15176
+ }
15177
+ else if (types_1.ClassType.isBuiltIn(concreteSrcType, 'LiteralString')) {
15201
15178
  return true;
15202
15179
  }
15203
15180
  }
15204
15181
  else if (types_1.ClassType.isBuiltIn(concreteSrcType, 'LiteralString') &&
15205
- types_1.ClassType.isBuiltIn(destType, 'str') &&
15206
- destType.literalValue === undefined) {
15207
- return true;
15182
+ strClassType &&
15183
+ (0, types_1.isInstantiableClass)(strClassType)) {
15184
+ concreteSrcType = types_1.ClassType.cloneAsInstance(strClassType);
15208
15185
  }
15209
15186
  if (!assignClass(types_1.ClassType.cloneAsInstantiable(destType), types_1.ClassType.cloneAsInstantiable(concreteSrcType), diag, destTypeVarContext, srcTypeVarContext, flags, recursionCount,
15210
15187
  /* reportErrorsUsingObjType */ true)) {
@@ -15357,9 +15334,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
15357
15334
  function assignFromUnionType(destType, srcType, diag, destTypeVarContext, srcTypeVarContext, flags, recursionCount) {
15358
15335
  // Start by checking for an exact match. This is needed to handle unions
15359
15336
  // that contain recursive type aliases.
15360
- if ((0, types_1.isTypeSame)(srcType, destType,
15361
- /* ignorePseudoGeneric */ undefined,
15362
- /* ignoreTypeFlags */ undefined, recursionCount)) {
15337
+ if ((0, types_1.isTypeSame)(srcType, destType, {}, recursionCount)) {
15363
15338
  return true;
15364
15339
  }
15365
15340
  // Handle the case where the source and dest are both unions. Try
@@ -15388,9 +15363,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
15388
15363
  remainingDestSubtypes.push(destSubtype);
15389
15364
  }
15390
15365
  else {
15391
- const srcTypeIndex = remainingSrcSubtypes.findIndex((srcSubtype) => (0, types_1.isTypeSame)(srcSubtype, destSubtype,
15392
- /* ignorePseudoGeneric */ undefined,
15393
- /* ignoreTypeFlags */ undefined, recursionCount));
15366
+ const srcTypeIndex = remainingSrcSubtypes.findIndex((srcSubtype) => (0, types_1.isTypeSame)(srcSubtype, destSubtype, {}, recursionCount));
15394
15367
  if (srcTypeIndex >= 0) {
15395
15368
  remainingSrcSubtypes.splice(srcTypeIndex, 1);
15396
15369
  }
@@ -15549,54 +15522,69 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
15549
15522
  // For union destinations, we just need to match one of the types.
15550
15523
  const diagAddendum = diag ? new diagnostic_1.DiagnosticAddendum() : undefined;
15551
15524
  let foundMatch = false;
15552
- // Run through all subtypes in the union. Don't stop at the first
15553
- // match we find because we may need to match TypeVars in other
15554
- // subtypes. We special-case "None" so we can handle Optional[T]
15555
- // without matching the None to the type var.
15556
- if ((0, types_1.isNoneInstance)(srcType) && (0, typeUtils_1.isOptionalType)(destType)) {
15557
- foundMatch = true;
15525
+ // Does the union contain any type variables that need to be solved?
15526
+ // If so, we need to use a slower path.
15527
+ if (!(0, typeUtils_1.requiresSpecialization)(destType)) {
15528
+ for (const subtype of destType.subtypes) {
15529
+ if (assignType(subtype, srcType, diagAddendum === null || diagAddendum === void 0 ? void 0 : diagAddendum.createAddendum(),
15530
+ /* destTypeVarContextClone */ undefined,
15531
+ /* srcTypeVarContextClone */ undefined, flags, recursionCount)) {
15532
+ foundMatch = true;
15533
+ break;
15534
+ }
15535
+ }
15558
15536
  }
15559
15537
  else {
15560
- let bestDestTypeVarContext;
15561
- let bestSrcTypeVarContext;
15562
- let bestTypeVarContextScore;
15563
- // If the srcType is a literal, try to use the fast-path lookup
15564
- // in case the destType is a union with hundreds of literals.
15565
- if ((0, types_1.isClassInstance)(srcType) &&
15566
- (0, typeUtils_1.isLiteralType)(srcType) &&
15567
- types_1.UnionType.containsType(destType, srcType, recursionCount)) {
15568
- return true;
15538
+ // Run through all subtypes in the union. Don't stop at the first
15539
+ // match we find because we may need to match TypeVars in other
15540
+ // subtypes. We special-case "None" so we can handle Optional[T]
15541
+ // without matching the None to the type var.
15542
+ if ((0, types_1.isNoneInstance)(srcType) && (0, typeUtils_1.isOptionalType)(destType)) {
15543
+ foundMatch = true;
15569
15544
  }
15570
- (0, typeUtils_1.doForEachSubtype)(destType, (subtype) => {
15571
- // Make a temporary clone of the typeVarContext. We don't want to modify
15572
- // the original typeVarContext until we find the "optimal" typeVar mapping.
15573
- const destTypeVarContextClone = destTypeVarContext === null || destTypeVarContext === void 0 ? void 0 : destTypeVarContext.clone();
15574
- const srcTypeVarContextClone = srcTypeVarContext === null || srcTypeVarContext === void 0 ? void 0 : srcTypeVarContext.clone();
15575
- if (assignType(subtype, srcType, diagAddendum === null || diagAddendum === void 0 ? void 0 : diagAddendum.createAddendum(), destTypeVarContextClone, srcTypeVarContextClone, flags, recursionCount)) {
15576
- foundMatch = true;
15577
- if (destTypeVarContextClone) {
15578
- // Ask the typeVarContext to compute a "score" for the current
15579
- // contents of the table.
15580
- let typeVarContextScore = destTypeVarContextClone.getScore();
15581
- // If the type matches exactly, prefer it over other types.
15582
- if ((0, types_1.isTypeSame)(subtype, (0, typeUtils_1.stripLiteralValue)(srcType))) {
15583
- typeVarContextScore = Number.POSITIVE_INFINITY;
15584
- }
15585
- if (bestTypeVarContextScore === undefined || bestTypeVarContextScore <= typeVarContextScore) {
15586
- // We found a typeVar mapping with a higher score than before.
15587
- bestTypeVarContextScore = typeVarContextScore;
15588
- bestDestTypeVarContext = destTypeVarContextClone;
15589
- bestSrcTypeVarContext = srcTypeVarContextClone;
15545
+ else {
15546
+ let bestDestTypeVarContext;
15547
+ let bestSrcTypeVarContext;
15548
+ let bestTypeVarContextScore;
15549
+ // If the srcType is a literal, try to use the fast-path lookup
15550
+ // in case the destType is a union with hundreds of literals.
15551
+ if ((0, types_1.isClassInstance)(srcType) &&
15552
+ (0, typeUtils_1.isLiteralType)(srcType) &&
15553
+ types_1.UnionType.containsType(destType, srcType, recursionCount)) {
15554
+ return true;
15555
+ }
15556
+ (0, typeUtils_1.doForEachSubtype)(destType, (subtype) => {
15557
+ // Make a temporary clone of the typeVarContext. We don't want to modify
15558
+ // the original typeVarContext until we find the "optimal" typeVar mapping.
15559
+ const destTypeVarContextClone = destTypeVarContext === null || destTypeVarContext === void 0 ? void 0 : destTypeVarContext.clone();
15560
+ const srcTypeVarContextClone = srcTypeVarContext === null || srcTypeVarContext === void 0 ? void 0 : srcTypeVarContext.clone();
15561
+ if (assignType(subtype, srcType, diagAddendum === null || diagAddendum === void 0 ? void 0 : diagAddendum.createAddendum(), destTypeVarContextClone, srcTypeVarContextClone, flags, recursionCount)) {
15562
+ foundMatch = true;
15563
+ if (destTypeVarContextClone) {
15564
+ // Ask the typeVarContext to compute a "score" for the current
15565
+ // contents of the table.
15566
+ let typeVarContextScore = destTypeVarContextClone.getScore();
15567
+ // If the type matches exactly, prefer it over other types.
15568
+ if ((0, types_1.isTypeSame)(subtype, stripLiteralValue(srcType))) {
15569
+ typeVarContextScore = Number.POSITIVE_INFINITY;
15570
+ }
15571
+ if (bestTypeVarContextScore === undefined ||
15572
+ bestTypeVarContextScore <= typeVarContextScore) {
15573
+ // We found a typeVar mapping with a higher score than before.
15574
+ bestTypeVarContextScore = typeVarContextScore;
15575
+ bestDestTypeVarContext = destTypeVarContextClone;
15576
+ bestSrcTypeVarContext = srcTypeVarContextClone;
15577
+ }
15590
15578
  }
15591
15579
  }
15580
+ });
15581
+ // If we found a winning type var mapping, copy it back to typeVarContext.
15582
+ if (destTypeVarContext && bestDestTypeVarContext) {
15583
+ destTypeVarContext.copyFromClone(bestDestTypeVarContext);
15584
+ }
15585
+ if (srcTypeVarContext && bestSrcTypeVarContext) {
15586
+ srcTypeVarContext.copyFromClone(bestSrcTypeVarContext);
15592
15587
  }
15593
- });
15594
- // If we found a winning type var mapping, copy it back to typeVarContext.
15595
- if (destTypeVarContext && bestDestTypeVarContext) {
15596
- destTypeVarContext.copyFromClone(bestDestTypeVarContext);
15597
- }
15598
- if (srcTypeVarContext && bestSrcTypeVarContext) {
15599
- srcTypeVarContext.copyFromClone(bestSrcTypeVarContext);
15600
15588
  }
15601
15589
  }
15602
15590
  // If the source is a constrained TypeVar, see if we can assign all of the
@@ -15622,9 +15610,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
15622
15610
  // The srcType is assignable only if all of its subtypes are assignable.
15623
15611
  return !(0, types_1.findSubtype)(srcType, (srcSubtype) => {
15624
15612
  var _a;
15625
- if ((0, types_1.isTypeSame)(destType, srcSubtype,
15626
- /* ignorePseudoGeneric */ true,
15627
- /* ignoreTypeFlags */ undefined, recursionCount)) {
15613
+ if ((0, types_1.isTypeSame)(destType, srcSubtype, { ignorePseudoGeneric: true }, recursionCount)) {
15628
15614
  return false;
15629
15615
  }
15630
15616
  const destTypeVarName = types_1.TypeVarType.getNameWithScope(destType);
@@ -15884,7 +15870,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
15884
15870
  if (tupleClassType && (0, types_1.isInstantiableClass)(tupleClassType)) {
15885
15871
  srcPositionalsType = (0, typeUtils_1.convertToInstance)((0, typeUtils_1.specializeTupleClass)(tupleClassType, srcTupleTypes,
15886
15872
  /* isTypeArgumentExplicit */ true,
15887
- /* stripLiterals */ true,
15888
15873
  /* isUnpackedTuple */ true));
15889
15874
  }
15890
15875
  else {
@@ -16070,13 +16055,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
16070
16055
  if (tupleClassType && (0, types_1.isInstantiableClass)(tupleClassType)) {
16071
16056
  if (!(0, types_1.isUnpacked)(destArgsType)) {
16072
16057
  destArgsType = types_1.ClassType.cloneForUnpacked(types_1.ClassType.cloneAsInstance((0, typeUtils_1.specializeTupleClass)(tupleClassType, [{ type: destArgsType, isUnbounded: true }],
16073
- /* isTypeArgumentExplicit */ true,
16074
- /* stripLiterals */ true)));
16058
+ /* isTypeArgumentExplicit */ true)));
16075
16059
  }
16076
16060
  if (!(0, types_1.isUnpacked)(srcArgsType)) {
16077
16061
  srcArgsType = types_1.ClassType.cloneForUnpacked(types_1.ClassType.cloneAsInstance((0, typeUtils_1.specializeTupleClass)(tupleClassType, [{ type: srcArgsType, isUnbounded: true }],
16078
- /* isTypeArgumentExplicit */ true,
16079
- /* stripLiterals */ true)));
16062
+ /* isTypeArgumentExplicit */ true)));
16080
16063
  }
16081
16064
  }
16082
16065
  if (!assignFunctionParameter(destArgsType, srcArgsType, destParamDetails.params[destParamDetails.argsIndex].index, diag === null || diag === void 0 ? void 0 : diag.createAddendum(), destTypeVarContext, srcTypeVarContext, flags, recursionCount)) {
@@ -16303,9 +16286,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
16303
16286
  // If there are any remaining parameters or the source doesn't include the
16304
16287
  // dest param spec itself, it is not assignable in this case.
16305
16288
  if (!srcParamSpec ||
16306
- !(0, types_1.isTypeSame)(srcParamSpec, destParamSpec,
16307
- /* ignorePseudoGeneric */ false,
16308
- /* ignoreTypeFlags */ true) ||
16289
+ !(0, types_1.isTypeSame)(srcParamSpec, destParamSpec, { ignoreTypeFlags: true }) ||
16309
16290
  remainingParams.length > 0) {
16310
16291
  canAssign = false;
16311
16292
  }
@@ -16703,9 +16684,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
16703
16684
  }
16704
16685
  let effectiveSrcType = srcType;
16705
16686
  if ((0, types_1.isTypeVar)(srcType)) {
16706
- if ((0, types_1.isTypeSame)(srcType, destType,
16707
- /* ignorePseudoGeneric */ undefined,
16708
- /* ignoreTypeFlags */ undefined, recursionCount)) {
16687
+ if ((0, types_1.isTypeSame)(srcType, destType, {}, recursionCount)) {
16709
16688
  return srcType;
16710
16689
  }
16711
16690
  effectiveSrcType = makeTopLevelTypeVarsConcrete(srcType);
@@ -16917,9 +16896,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
16917
16896
  if (firstParamType && memberType.details.parameters.length > 0) {
16918
16897
  const memberTypeFirstParam = memberType.details.parameters[0];
16919
16898
  const memberTypeFirstParamType = types_1.FunctionType.getEffectiveParameterType(memberType, 0);
16920
- // If the type has a literal associated with it, strip it now. This
16921
- // is needed to handle generic functions in the enum.Flag class.
16922
- const nonLiteralFirstParamType = (0, typeUtils_1.stripLiteralValue)(firstParamType);
16923
16899
  // Fill out the typeVarContext for the "self" or "cls" parameter.
16924
16900
  typeVarContext.addSolveForScope((0, typeUtils_1.getTypeVarScopeId)(memberType));
16925
16901
  const diag = new diagnostic_1.DiagnosticAddendum();
@@ -16933,11 +16909,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
16933
16909
  // Instead, we'll assume it's assignable.
16934
16910
  if (!typeVarContext.isLocked()) {
16935
16911
  typeVarContext.setTypeVarType(memberTypeFirstParamType, types_1.TypeBase.isInstantiable(memberTypeFirstParamType)
16936
- ? (0, typeUtils_1.convertToInstance)(nonLiteralFirstParamType)
16937
- : nonLiteralFirstParamType);
16912
+ ? (0, typeUtils_1.convertToInstance)(firstParamType)
16913
+ : firstParamType);
16938
16914
  }
16939
16915
  }
16940
- else if (!assignType(memberTypeFirstParamType, nonLiteralFirstParamType, diag, typeVarContext,
16916
+ else if (!assignType(memberTypeFirstParamType, firstParamType, diag, typeVarContext,
16941
16917
  /* srcTypeVarContext */ undefined, 0 /* Default */, recursionCount)) {
16942
16918
  if (memberTypeFirstParam.name &&
16943
16919
  !memberTypeFirstParam.isNameSynthesized &&
@@ -17042,6 +17018,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
17042
17018
  evaluateTypeOfParameter,
17043
17019
  canBeTruthy,
17044
17020
  canBeFalsy,
17021
+ stripLiteralValue,
17045
17022
  removeTruthinessFromType,
17046
17023
  removeFalsinessFromType,
17047
17024
  verifyRaiseExceptionType,