@zzzen/pyright-internal 1.2.0-dev.20230226 → 1.2.0-dev.20230312

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 (148) hide show
  1. package/dist/analyzer/binder.d.ts +0 -1
  2. package/dist/analyzer/binder.js +32 -95
  3. package/dist/analyzer/binder.js.map +1 -1
  4. package/dist/analyzer/checker.js +48 -31
  5. package/dist/analyzer/checker.js.map +1 -1
  6. package/dist/analyzer/codeFlowEngine.js +4 -4
  7. package/dist/analyzer/codeFlowEngine.js.map +1 -1
  8. package/dist/analyzer/commentUtils.js +11 -1
  9. package/dist/analyzer/commentUtils.js.map +1 -1
  10. package/dist/analyzer/constructorTransform.d.ts +1 -0
  11. package/dist/analyzer/constructorTransform.js +19 -2
  12. package/dist/analyzer/constructorTransform.js.map +1 -1
  13. package/dist/analyzer/declaration.d.ts +2 -4
  14. package/dist/analyzer/declaration.js +6 -1
  15. package/dist/analyzer/declaration.js.map +1 -1
  16. package/dist/analyzer/declarationUtils.d.ts +1 -5
  17. package/dist/analyzer/declarationUtils.js +2 -47
  18. package/dist/analyzer/declarationUtils.js.map +1 -1
  19. package/dist/analyzer/docStringConversion.js +9 -3
  20. package/dist/analyzer/docStringConversion.js.map +1 -1
  21. package/dist/analyzer/packageTypeVerifier.js +40 -40
  22. package/dist/analyzer/packageTypeVerifier.js.map +1 -1
  23. package/dist/analyzer/parseTreeUtils.d.ts +4 -2
  24. package/dist/analyzer/parseTreeUtils.js +77 -2
  25. package/dist/analyzer/parseTreeUtils.js.map +1 -1
  26. package/dist/analyzer/patternMatching.js +20 -1
  27. package/dist/analyzer/patternMatching.js.map +1 -1
  28. package/dist/analyzer/program.d.ts +1 -0
  29. package/dist/analyzer/program.js +154 -13
  30. package/dist/analyzer/program.js.map +1 -1
  31. package/dist/analyzer/properties.js +3 -2
  32. package/dist/analyzer/properties.js.map +1 -1
  33. package/dist/analyzer/protocols.js +4 -2
  34. package/dist/analyzer/protocols.js.map +1 -1
  35. package/dist/analyzer/pythonPathUtils.js +27 -13
  36. package/dist/analyzer/pythonPathUtils.js.map +1 -1
  37. package/dist/analyzer/service.js +6 -1
  38. package/dist/analyzer/service.js.map +1 -1
  39. package/dist/analyzer/sourceFile.js +12 -1
  40. package/dist/analyzer/sourceFile.js.map +1 -1
  41. package/dist/analyzer/symbol.js +1 -4
  42. package/dist/analyzer/symbol.js.map +1 -1
  43. package/dist/analyzer/symbolUtils.d.ts +0 -3
  44. package/dist/analyzer/symbolUtils.js +1 -14
  45. package/dist/analyzer/symbolUtils.js.map +1 -1
  46. package/dist/analyzer/typeDocStringUtils.js +5 -3
  47. package/dist/analyzer/typeDocStringUtils.js.map +1 -1
  48. package/dist/analyzer/typeEvaluator.js +642 -259
  49. package/dist/analyzer/typeEvaluator.js.map +1 -1
  50. package/dist/analyzer/typeEvaluatorTypes.d.ts +11 -2
  51. package/dist/analyzer/typeGuards.js +2 -2
  52. package/dist/analyzer/typeGuards.js.map +1 -1
  53. package/dist/analyzer/typePrinter.js +63 -55
  54. package/dist/analyzer/typePrinter.js.map +1 -1
  55. package/dist/analyzer/typeUtils.d.ts +2 -0
  56. package/dist/analyzer/typeUtils.js +22 -3
  57. package/dist/analyzer/typeUtils.js.map +1 -1
  58. package/dist/analyzer/typedDicts.d.ts +2 -1
  59. package/dist/analyzer/typedDicts.js +83 -57
  60. package/dist/analyzer/typedDicts.js.map +1 -1
  61. package/dist/analyzer/types.d.ts +3 -1
  62. package/dist/analyzer/types.js +8 -0
  63. package/dist/analyzer/types.js.map +1 -1
  64. package/dist/commands/dumpFileDebugInfoCommand.js +78 -0
  65. package/dist/commands/dumpFileDebugInfoCommand.js.map +1 -1
  66. package/dist/common/collectionUtils.d.ts +1 -0
  67. package/dist/common/collectionUtils.js +8 -1
  68. package/dist/common/collectionUtils.js.map +1 -1
  69. package/dist/common/extensibility.js +25 -1
  70. package/dist/common/extensibility.js.map +1 -1
  71. package/dist/common/positionUtils.d.ts +2 -0
  72. package/dist/common/positionUtils.js +15 -1
  73. package/dist/common/positionUtils.js.map +1 -1
  74. package/dist/languageServerBase.js +9 -1
  75. package/dist/languageServerBase.js.map +1 -1
  76. package/dist/languageService/callHierarchyProvider.js +4 -2
  77. package/dist/languageService/callHierarchyProvider.js.map +1 -1
  78. package/dist/languageService/completionProvider.d.ts +4 -2
  79. package/dist/languageService/completionProvider.js +252 -191
  80. package/dist/languageService/completionProvider.js.map +1 -1
  81. package/dist/languageService/completionProviderUtils.d.ts +1 -1
  82. package/dist/languageService/completionProviderUtils.js +3 -3
  83. package/dist/languageService/completionProviderUtils.js.map +1 -1
  84. package/dist/languageService/definitionProvider.js +2 -1
  85. package/dist/languageService/definitionProvider.js.map +1 -1
  86. package/dist/languageService/documentSymbolProvider.js +2 -1
  87. package/dist/languageService/documentSymbolProvider.js.map +1 -1
  88. package/dist/languageService/hoverProvider.js +11 -3
  89. package/dist/languageService/hoverProvider.js.map +1 -1
  90. package/dist/languageService/importAdder.js +4 -1
  91. package/dist/languageService/importAdder.js.map +1 -1
  92. package/dist/languageService/indentationUtils.d.ts +1 -1
  93. package/dist/languageService/indentationUtils.js +22 -12
  94. package/dist/languageService/indentationUtils.js.map +1 -1
  95. package/dist/languageService/renameModuleProvider.d.ts +1 -1
  96. package/dist/languageService/renameModuleProvider.js +28 -8
  97. package/dist/languageService/renameModuleProvider.js.map +1 -1
  98. package/dist/languageService/tooltipUtils.js +2 -1
  99. package/dist/languageService/tooltipUtils.js.map +1 -1
  100. package/dist/localization/localize.d.ts +12 -2
  101. package/dist/localization/localize.js +5 -1
  102. package/dist/localization/localize.js.map +1 -1
  103. package/dist/localization/package.nls.en-us.json +6 -2
  104. package/dist/parser/parser.js +40 -25
  105. package/dist/parser/parser.js.map +1 -1
  106. package/dist/parser/tokenizer.js +1 -1
  107. package/dist/pyright.js +1 -1
  108. package/dist/pyright.js.map +1 -1
  109. package/dist/tests/completions.test.js +445 -0
  110. package/dist/tests/completions.test.js.map +1 -1
  111. package/dist/tests/docStringConversion.test.js +23 -0
  112. package/dist/tests/docStringConversion.test.js.map +1 -1
  113. package/dist/tests/fourslash/completions.dictionary.keys.expression.fourslash.js +13 -3
  114. package/dist/tests/fourslash/completions.dictionary.keys.expression.fourslash.js.map +1 -1
  115. package/dist/tests/fourslash/completions.dictionary.keys.stringLiterals.fourslash.js +7 -2
  116. package/dist/tests/fourslash/completions.dictionary.keys.stringLiterals.fourslash.js.map +1 -1
  117. package/dist/tests/fourslash/completions.errorNodes.fourslash.d.ts +1 -0
  118. package/dist/tests/fourslash/completions.errorNodes.fourslash.js +24 -0
  119. package/dist/tests/fourslash/completions.errorNodes.fourslash.js.map +1 -0
  120. package/dist/tests/fourslash/hover.slots.fourslash.d.ts +1 -0
  121. package/dist/tests/fourslash/hover.slots.fourslash.js +16 -0
  122. package/dist/tests/fourslash/hover.slots.fourslash.js.map +1 -0
  123. package/dist/tests/hoverProvider.test.js +1 -1
  124. package/dist/tests/hoverProvider.test.js.map +1 -1
  125. package/dist/tests/importAdder.test.js +13 -0
  126. package/dist/tests/importAdder.test.js.map +1 -1
  127. package/dist/tests/indentationUtils.reindent.test.js +21 -0
  128. package/dist/tests/indentationUtils.reindent.test.js.map +1 -1
  129. package/dist/tests/moveSymbol.importAdder.test.js +137 -31
  130. package/dist/tests/moveSymbol.importAdder.test.js.map +1 -1
  131. package/dist/tests/moveSymbol.insertion.test.js +98 -0
  132. package/dist/tests/moveSymbol.insertion.test.js.map +1 -1
  133. package/dist/tests/moveSymbol.misc.test.js +27 -0
  134. package/dist/tests/moveSymbol.misc.test.js.map +1 -1
  135. package/dist/tests/renameModuleTestUtils.d.ts +1 -1
  136. package/dist/tests/renameModuleTestUtils.js +9 -5
  137. package/dist/tests/renameModuleTestUtils.js.map +1 -1
  138. package/dist/tests/typeEvaluator1.test.js +8 -0
  139. package/dist/tests/typeEvaluator1.test.js.map +1 -1
  140. package/dist/tests/typeEvaluator2.test.js +5 -1
  141. package/dist/tests/typeEvaluator2.test.js.map +1 -1
  142. package/dist/tests/typeEvaluator3.test.js +20 -4
  143. package/dist/tests/typeEvaluator3.test.js.map +1 -1
  144. package/dist/tests/typeEvaluator4.test.js +4 -0
  145. package/dist/tests/typeEvaluator4.test.js.map +1 -1
  146. package/dist/tests/typeEvaluator5.test.js +6 -0
  147. package/dist/tests/typeEvaluator5.test.js.map +1 -1
  148. package/package.json +1 -1
@@ -253,6 +253,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
253
253
  let strClassType;
254
254
  let dictClassType;
255
255
  let typedDictClassType;
256
+ let typedDictPrivateClassType;
256
257
  let printExpressionSpaceCount = 0;
257
258
  let incompleteGenerationCount = 0;
258
259
  const returnTypeInferenceContextStack = [];
@@ -500,7 +501,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
500
501
  intClassType = getBuiltInType(node, 'int');
501
502
  strClassType = getBuiltInType(node, 'str');
502
503
  dictClassType = getBuiltInType(node, 'dict');
503
- typedDictClassType = getTypingType(node, '_TypedDict');
504
+ typedDictClassType = getTypingType(node, 'TypedDict');
505
+ typedDictPrivateClassType = getTypingType(node, '_TypedDict');
504
506
  }
505
507
  }
506
508
  function getTypeOfExpression(node, flags = 0 /* None */, inferenceContext) {
@@ -1000,6 +1002,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
1000
1002
  if (options === null || options === void 0 ? void 0 : options.notParsedByInterpreter) {
1001
1003
  evaluatorFlags |= 524288 /* NotParsedByInterpreter */;
1002
1004
  }
1005
+ if (options === null || options === void 0 ? void 0 : options.allowRequired) {
1006
+ evaluatorFlags |= 1048576 /* AllowRequired */;
1007
+ }
1003
1008
  if ((0, analyzerFileInfo_1.isAnnotationEvaluationPostponed)(fileInfo)) {
1004
1009
  evaluatorFlags |= 4 /* AllowForwardReferences */;
1005
1010
  }
@@ -1496,7 +1501,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
1496
1501
  /* honorCodeFlow */ false);
1497
1502
  if (symbolWithScope) {
1498
1503
  const symbol = symbolWithScope.symbol;
1499
- return symbol.getDeclarations().find((decl) => (0, declarationUtils_1.isExplicitTypeAliasDeclaration)(decl)) !== undefined;
1504
+ return symbol.getDeclarations().find((decl) => isExplicitTypeAliasDeclaration(decl)) !== undefined;
1500
1505
  }
1501
1506
  }
1502
1507
  }
@@ -1504,6 +1509,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
1504
1509
  }
1505
1510
  // Determines whether the specified expression is a symbol with a declared type.
1506
1511
  function getDeclaredTypeForExpression(expression, usage) {
1512
+ var _a, _b;
1507
1513
  let symbol;
1508
1514
  let classOrObjectBase;
1509
1515
  let memberAccessClass;
@@ -1516,7 +1522,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
1516
1522
  // Handle the case where the symbol is a class-level variable
1517
1523
  // where the type isn't declared in this class but is in
1518
1524
  // a parent class.
1519
- if (getDeclaredTypeOfSymbol(symbol, expression) === undefined &&
1525
+ if (!((_a = getDeclaredTypeOfSymbol(symbol, expression)) === null || _a === void 0 ? void 0 : _a.type) &&
1520
1526
  symbolWithScope.scope.type === 2 /* Class */) {
1521
1527
  const enclosingClass = ParseTreeUtils.getEnclosingClassOrFunction(expression);
1522
1528
  if (enclosingClass && enclosingClass.nodeType === 10 /* Class */) {
@@ -1590,7 +1596,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
1590
1596
  }
1591
1597
  }
1592
1598
  if (symbol) {
1593
- let declaredType = getDeclaredTypeOfSymbol(symbol);
1599
+ let declaredType = (_b = getDeclaredTypeOfSymbol(symbol)) === null || _b === void 0 ? void 0 : _b.type;
1594
1600
  if (declaredType) {
1595
1601
  // If it's a descriptor, we need to get the setter type.
1596
1602
  if ((0, types_1.isClassInstance)(declaredType)) {
@@ -1838,7 +1844,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
1838
1844
  return isTypeHashable;
1839
1845
  }
1840
1846
  function getTypedDictClassType() {
1841
- return typedDictClassType;
1847
+ return typedDictPrivateClassType;
1842
1848
  }
1843
1849
  function getTupleClassType() {
1844
1850
  return tupleClassType;
@@ -2038,7 +2044,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
2038
2044
  addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeExpectedClass().format({ type: printType(type) }) + diag.getString(), node);
2039
2045
  }
2040
2046
  function assignTypeToNameNode(nameNode, type, isTypeIncomplete, ignoreEmptyContainers, srcExpression, allowAssignmentToFinalVar = false, expectedTypeDiagAddendum) {
2041
- var _a, _b;
2047
+ var _a, _b, _c;
2042
2048
  const nameValue = nameNode.value;
2043
2049
  const symbolWithScope = lookUpSymbolRecursive(nameNode, nameValue, /* honorCodeFlow */ false);
2044
2050
  if (!symbolWithScope) {
@@ -2047,7 +2053,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
2047
2053
  return;
2048
2054
  }
2049
2055
  const declarations = symbolWithScope.symbol.getDeclarations();
2050
- let declaredType = getDeclaredTypeOfSymbol(symbolWithScope.symbol);
2056
+ let declaredType = (_a = getDeclaredTypeOfSymbol(symbolWithScope.symbol)) === null || _a === void 0 ? void 0 : _a.type;
2051
2057
  const fileInfo = AnalyzerNodeInfo.getFileInfo(nameNode);
2052
2058
  // If this is a class scope and there is no type declared for this class variable,
2053
2059
  // see if a parent class has a type declared.
@@ -2065,7 +2071,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
2065
2071
  }
2066
2072
  // We found an existing declared type. Make sure the type is assignable.
2067
2073
  let destType = type;
2068
- if (declaredType) {
2074
+ const isTypeAlias = !!declaredType && (0, types_1.isClassInstance)(declaredType) && types_1.ClassType.isBuiltIn(declaredType, 'TypeAlias');
2075
+ if (declaredType && !isTypeAlias) {
2069
2076
  let diagAddendum = new diagnostic_1.DiagnosticAddendum();
2070
2077
  if (!assignType(declaredType, type, diagAddendum)) {
2071
2078
  // If there was an expected type mismatch, use that diagnostic
@@ -2076,7 +2083,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
2076
2083
  addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeAssignmentMismatch().format({
2077
2084
  sourceType: printType(type),
2078
2085
  destType: printType(declaredType),
2079
- }) + diagAddendum.getString(), srcExpression !== null && srcExpression !== void 0 ? srcExpression : nameNode, (_b = (_a = diagAddendum.getEffectiveTextRange()) !== null && _a !== void 0 ? _a : srcExpression) !== null && _b !== void 0 ? _b : nameNode);
2086
+ }) + diagAddendum.getString(), srcExpression !== null && srcExpression !== void 0 ? srcExpression : nameNode, (_c = (_b = diagAddendum.getEffectiveTextRange()) !== null && _b !== void 0 ? _b : srcExpression) !== null && _c !== void 0 ? _c : nameNode);
2080
2087
  // Replace the assigned type with the (unnarrowed) declared type.
2081
2088
  destType = declaredType;
2082
2089
  }
@@ -2094,7 +2101,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
2094
2101
  if ((scope === null || scope === void 0 ? void 0 : scope.type) === 2 /* Class */) {
2095
2102
  if (types_1.TypeBase.isInstance(destType) &&
2096
2103
  !(0, symbolNameUtils_1.isConstantName)(nameValue) &&
2097
- !(0, symbolUtils_1.isFinalVariable)(symbolWithScope.symbol)) {
2104
+ !isFinalVariable(symbolWithScope.symbol)) {
2098
2105
  destType = stripLiteralValue(destType);
2099
2106
  }
2100
2107
  }
@@ -2108,7 +2115,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
2108
2115
  addDiagnostic(fileInfo.diagnosticRuleSet.reportConstantRedefinition, diagnosticRules_1.DiagnosticRule.reportConstantRedefinition, localize_1.Localizer.Diagnostic.constantRedefinition().format({ name: nameValue }), nameNode);
2109
2116
  }
2110
2117
  }
2111
- else if (varDecl.isFinal && !allowAssignmentToFinalVar) {
2118
+ else if (isFinalVariableDeclaration(varDecl) && !allowAssignmentToFinalVar) {
2112
2119
  addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.finalReassigned().format({ name: nameValue }), nameNode);
2113
2120
  }
2114
2121
  }
@@ -2163,6 +2170,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
2163
2170
  writeTypeCache(target, { type, isIncomplete: isTypeIncomplete }, 0 /* None */);
2164
2171
  }
2165
2172
  function assignTypeToMemberVariable(node, srcType, isTypeIncomplete, isInstanceMember, srcExprNode) {
2173
+ var _a;
2166
2174
  const memberName = node.memberName.value;
2167
2175
  const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
2168
2176
  const classDef = ParseTreeUtils.getEnclosingClass(node);
@@ -2221,7 +2229,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
2221
2229
  }
2222
2230
  else {
2223
2231
  // Is the target a property?
2224
- const declaredType = getDeclaredTypeOfSymbol(memberInfo.symbol);
2232
+ const declaredType = (_a = getDeclaredTypeOfSymbol(memberInfo.symbol)) === null || _a === void 0 ? void 0 : _a.type;
2225
2233
  if (declaredType && !(0, typeUtils_1.isProperty)(declaredType)) {
2226
2234
  // Handle the case where there is a class variable defined with the same
2227
2235
  // name, but there's also now an instance variable introduced. Combine the
@@ -3120,6 +3128,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3120
3128
  // type arguments. If so, it fills in these type arguments with Unknown
3121
3129
  // and optionally reports an error.
3122
3130
  function reportMissingTypeArguments(node, type, flags) {
3131
+ var _a;
3123
3132
  if ((flags & 2 /* DoNotSpecialize */) !== 0) {
3124
3133
  return type;
3125
3134
  }
@@ -3133,7 +3142,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3133
3142
  }
3134
3143
  }
3135
3144
  if (!type.typeArguments) {
3136
- type = createSpecializedClassType(type, /* typeArgs */ undefined, flags, node);
3145
+ type = (_a = createSpecializedClassType(type, /* typeArgs */ undefined, flags, node)) === null || _a === void 0 ? void 0 : _a.type;
3137
3146
  }
3138
3147
  }
3139
3148
  // Is this a generic type alias that needs to be specialized?
@@ -3351,7 +3360,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3351
3360
  return typeResult;
3352
3361
  }
3353
3362
  function getTypeOfMemberAccessWithBaseType(node, baseTypeResult, usage, flags) {
3354
- var _a, _b;
3363
+ var _a, _b, _c;
3355
3364
  let baseType = baseTypeResult.type;
3356
3365
  const memberName = node.memberName.value;
3357
3366
  let diag = new diagnostic_1.DiagnosticAddendum();
@@ -3359,6 +3368,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3359
3368
  let type;
3360
3369
  let isIncomplete = !!baseTypeResult.isIncomplete;
3361
3370
  let isAsymmetricDescriptor;
3371
+ const isRequired = false;
3372
+ const isNotRequired = false;
3362
3373
  // If the base type was incomplete and unbound, don't proceed
3363
3374
  // because false positive errors will be generated.
3364
3375
  if (baseTypeResult.isIncomplete && (0, types_1.isUnbound)(baseTypeResult.type)) {
@@ -3517,7 +3528,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3517
3528
  }
3518
3529
  if (!type) {
3519
3530
  if (!isIncomplete) {
3520
- addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.moduleUnknownMember().format({ name: memberName }), node.memberName);
3531
+ addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.moduleUnknownMember().format({
3532
+ memberName,
3533
+ moduleName: baseType.moduleName,
3534
+ }), node.memberName);
3521
3535
  }
3522
3536
  type = evaluatorOptions.evaluateUnknownImportsAsAny ? types_1.AnyType.create() : types_1.UnknownType.create();
3523
3537
  }
@@ -3631,7 +3645,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3631
3645
  // Should we specialize the class?
3632
3646
  if ((flags & 2 /* DoNotSpecialize */) === 0) {
3633
3647
  if ((0, types_1.isInstantiableClass)(type) && !type.typeArguments) {
3634
- type = createSpecializedClassType(type, /* typeArgs */ undefined, flags, node);
3648
+ type = (_b = createSpecializedClassType(type, /* typeArgs */ undefined, flags, node)) === null || _b === void 0 ? void 0 : _b.type;
3635
3649
  }
3636
3650
  }
3637
3651
  if (usage.method === 'get') {
@@ -3642,7 +3656,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3642
3656
  // It can also come up in cases like "isinstance(x, (list, dict))".
3643
3657
  if ((0, types_1.isInstantiableClass)(type)) {
3644
3658
  const argNode = ParseTreeUtils.getParentNodeOfType(node, 1 /* Argument */);
3645
- if (argNode && ((_b = argNode === null || argNode === void 0 ? void 0 : argNode.parent) === null || _b === void 0 ? void 0 : _b.nodeType) === 9 /* Call */) {
3659
+ if (argNode && ((_c = argNode === null || argNode === void 0 ? void 0 : argNode.parent) === null || _c === void 0 ? void 0 : _c.nodeType) === 9 /* Call */) {
3646
3660
  skipPartialUnknownCheck = true;
3647
3661
  }
3648
3662
  }
@@ -3651,10 +3665,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3651
3665
  /* ignoreEmptyContainers */ false);
3652
3666
  }
3653
3667
  }
3654
- return { type, isIncomplete, isAsymmetricDescriptor };
3668
+ return { type, isIncomplete, isAsymmetricDescriptor, isRequired, isNotRequired };
3655
3669
  }
3656
3670
  function getTypeOfClassMemberName(errorNode, classType, isAccessedThroughObject, memberName, usage, diag, flags, bindToType) {
3657
- var _a, _b;
3671
+ var _a, _b, _c;
3658
3672
  let classLookupFlags = 0 /* Default */;
3659
3673
  if (flags & 1 /* AccessClassMembersOnly */) {
3660
3674
  classLookupFlags |= 8 /* SkipInstanceVariables */;
@@ -3695,7 +3709,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3695
3709
  if (containingClassType &&
3696
3710
  (0, types_1.isInstantiableClass)(containingClassType) &&
3697
3711
  types_1.ClassType.isSameGenericClass(containingClassType, classType)) {
3698
- type = (_b = getDeclaredTypeOfSymbol(memberInfo.symbol)) !== null && _b !== void 0 ? _b : types_1.UnknownType.create();
3712
+ type = (_c = (_b = getDeclaredTypeOfSymbol(memberInfo.symbol)) === null || _b === void 0 ? void 0 : _b.type) !== null && _c !== void 0 ? _c : types_1.UnknownType.create();
3699
3713
  if (type && (0, types_1.isInstantiableClass)(memberInfo.classType)) {
3700
3714
  type = (0, typeUtils_1.partiallySpecializeType)(type, memberInfo.classType);
3701
3715
  }
@@ -4035,7 +4049,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4035
4049
  }
4036
4050
  }
4037
4051
  // Check for an attempt to overwrite a final member variable.
4038
- const finalVarTypeDecl = memberInfo === null || memberInfo === void 0 ? void 0 : memberInfo.symbol.getDeclarations().find((decl) => (0, declarationUtils_1.isFinalVariableDeclaration)(decl));
4052
+ const finalVarTypeDecl = memberInfo === null || memberInfo === void 0 ? void 0 : memberInfo.symbol.getDeclarations().find((decl) => isFinalVariableDeclaration(decl));
4039
4053
  if (finalVarTypeDecl && !ParseTreeUtils.isNodeContainedWithin(errorNode, finalVarTypeDecl.node)) {
4040
4054
  // If a Final instance variable is declared in the class body but is
4041
4055
  // being assigned within an __init__ method, it's allowed.
@@ -4256,20 +4270,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4256
4270
  if (baseTypeResult.isIncomplete) {
4257
4271
  indexTypeResult.isIncomplete = true;
4258
4272
  }
4259
- // Handle "Required" and "NotRequired" specially.
4260
- if ((flags & 1048576 /* AllowRequired */) !== 0) {
4261
- if ((0, types_1.isInstantiableClass)(baseTypeResult.type)) {
4262
- if (types_1.ClassType.isBuiltIn(baseTypeResult.type, 'Required')) {
4263
- indexTypeResult.isRequired = true;
4264
- }
4265
- else if (types_1.ClassType.isBuiltIn(baseTypeResult.type, 'NotRequired')) {
4266
- indexTypeResult.isNotRequired = true;
4267
- }
4268
- }
4269
- }
4270
4273
  return indexTypeResult;
4271
4274
  }
4272
- function adjustTypeArgumentsForVariadicTypeVar(typeArgs, typeParameters, typeVarScopeId, errorNode) {
4275
+ function adjustTypeArgumentsForVariadicTypeVar(typeArgs, typeParameters, errorNode) {
4273
4276
  const variadicIndex = typeParameters.findIndex((param) => (0, types_1.isVariadicTypeVar)(param));
4274
4277
  // Do we need to adjust the type arguments to map to a variadic type
4275
4278
  // param at the end of the list?
@@ -4341,7 +4344,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4341
4344
  // Handles index expressions that are providing type arguments for a
4342
4345
  // generic type alias.
4343
4346
  function createSpecializedTypeAlias(node, baseType, flags) {
4344
- var _a, _b;
4347
+ var _a, _b, _c;
4345
4348
  if (!((_a = baseType.typeAliasInfo) === null || _a === void 0 ? void 0 : _a.typeParameters) ||
4346
4349
  (baseType.typeAliasInfo.typeParameters.length === 0 && baseType.typeAliasInfo.typeArguments)) {
4347
4350
  return undefined;
@@ -4355,7 +4358,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4355
4358
  return undefined;
4356
4359
  }
4357
4360
  const typeParameters = baseType.typeAliasInfo.typeParameters;
4358
- let typeArgs = adjustTypeArgumentsForVariadicTypeVar(getTypeArgs(node, flags), typeParameters, baseType.typeAliasInfo.typeVarScopeId, node);
4361
+ let typeArgs = adjustTypeArgumentsForVariadicTypeVar(getTypeArgs(node, flags), typeParameters, node);
4359
4362
  // PEP 612 says that if the class has only one type parameter consisting
4360
4363
  // of a ParamSpec, the list of arguments does not need to be enclosed in
4361
4364
  // a list. We'll handle that case specially here. Presumably this applies to
@@ -4371,13 +4374,26 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4371
4374
  ];
4372
4375
  }
4373
4376
  }
4374
- if (typeArgs.length > typeParameters.length &&
4375
- !typeParameters.some((typeVar) => typeVar.details.isVariadic && !typeVar.isVariadicInUnion)) {
4376
- addError(localize_1.Localizer.Diagnostic.typeArgsTooMany().format({
4377
- name: printType(baseType),
4378
- expected: typeParameters.length,
4379
- received: typeArgs.length,
4380
- }), typeArgs[typeParameters.length].node);
4377
+ if (!typeParameters.some((typeVar) => typeVar.details.isVariadic && !typeVar.isVariadicInUnion)) {
4378
+ let minTypeArgCount = typeParameters.length;
4379
+ const firstNonDefaultParam = typeParameters.findIndex((param) => !!param.details.defaultType);
4380
+ if (firstNonDefaultParam >= 0) {
4381
+ minTypeArgCount = firstNonDefaultParam;
4382
+ }
4383
+ if (typeArgs.length > typeParameters.length) {
4384
+ addError(localize_1.Localizer.Diagnostic.typeArgsTooMany().format({
4385
+ name: printType(baseType),
4386
+ expected: typeParameters.length,
4387
+ received: typeArgs.length,
4388
+ }), typeArgs[typeParameters.length].node);
4389
+ }
4390
+ else if (typeArgs.length < minTypeArgCount) {
4391
+ addError(localize_1.Localizer.Diagnostic.typeArgsTooFew().format({
4392
+ name: printType(baseType),
4393
+ expected: typeParameters.length,
4394
+ received: typeArgs.length,
4395
+ }), node.items[node.items.length - 1]);
4396
+ }
4381
4397
  }
4382
4398
  // Handle the mypy_extensions.FlexibleAlias type specially.
4383
4399
  if ((0, types_1.isInstantiableClass)(baseType) &&
@@ -4391,7 +4407,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4391
4407
  if (param.details.isParamSpec && index < typeArgs.length) {
4392
4408
  const typeArgType = typeArgs[index].type;
4393
4409
  if (typeArgs[index].typeList) {
4394
- const functionType = types_1.FunctionType.createInstantiable(65536 /* ParamSpecValue */);
4410
+ const functionType = types_1.FunctionType.createSynthesizedInstance('', 65536 /* ParamSpecValue */);
4395
4411
  types_1.TypeBase.setSpecialForm(functionType);
4396
4412
  typeArgs[index].typeList.forEach((paramType, paramIndex) => {
4397
4413
  types_1.FunctionType.addParameter(functionType, {
@@ -4437,7 +4453,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4437
4453
  (0, constraintSolver_1.assignTypeToTypeVar)(evaluatorInterface, param, functionType, diag, typeVarContext, 128 /* RetainLiteralsForTypeVar */);
4438
4454
  }
4439
4455
  else if ((0, typeUtils_1.isEllipsisType)(typeArgType)) {
4440
- const functionType = types_1.FunctionType.createInstantiable(65536 /* ParamSpecValue */ | 32768 /* SkipArgsKwargsCompatibilityCheck */);
4456
+ const functionType = types_1.FunctionType.createSynthesizedInstance('', 65536 /* ParamSpecValue */ | 32768 /* SkipArgsKwargsCompatibilityCheck */);
4441
4457
  types_1.TypeBase.setSpecialForm(functionType);
4442
4458
  types_1.FunctionType.addDefaultParameters(functionType);
4443
4459
  (0, constraintSolver_1.assignTypeToTypeVar)(evaluatorInterface, param, functionType, diag, typeVarContext);
@@ -4460,15 +4476,31 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4460
4476
  else {
4461
4477
  typeArgType = types_1.UnknownType.create();
4462
4478
  }
4479
+ if ((flags & 16384 /* EnforceTypeVarVarianceConsistency */) !== 0) {
4480
+ if ((0, types_1.isTypeVar)(typeArgType)) {
4481
+ const usageVariances = inferTypeParameterVarianceForTypeAlias(baseType);
4482
+ if (usageVariances && index < usageVariances.length) {
4483
+ const usageVariance = usageVariances[index];
4484
+ if (!(0, typeUtils_1.isVarianceOfTypeArgumentCompatible)(typeArgType, usageVariance)) {
4485
+ const messageDiag = diag.createAddendum();
4486
+ messageDiag.addMessage(localize_1.Localizer.DiagnosticAddendum.varianceMismatchForTypeAlias().format({
4487
+ typeVarName: printType(typeArgType),
4488
+ typeAliasParam: printType(typeParameters[index]),
4489
+ }));
4490
+ messageDiag.addTextRange(typeArgs[index].node);
4491
+ }
4492
+ }
4493
+ }
4494
+ }
4463
4495
  (0, constraintSolver_1.assignTypeToTypeVar)(evaluatorInterface, param, typeArgType, diag, typeVarContext, 128 /* RetainLiteralsForTypeVar */);
4464
4496
  }
4465
4497
  });
4466
4498
  if (!diag.isEmpty()) {
4467
- addError(localize_1.Localizer.Diagnostic.typeNotSpecializable().format({ type: printType(baseType) }) + diag.getString(), node);
4499
+ addError(localize_1.Localizer.Diagnostic.typeNotSpecializable().format({ type: printType(baseType) }) + diag.getString(), node, (_b = diag.getEffectiveTextRange()) !== null && _b !== void 0 ? _b : node);
4468
4500
  }
4469
4501
  const primarySignatureContext = typeVarContext.getPrimarySignature();
4470
4502
  const aliasTypeArgs = [];
4471
- (_b = baseType.typeAliasInfo.typeParameters) === null || _b === void 0 ? void 0 : _b.forEach((typeParam) => {
4503
+ (_c = baseType.typeAliasInfo.typeParameters) === null || _c === void 0 ? void 0 : _c.forEach((typeParam) => {
4472
4504
  let typeVarType;
4473
4505
  if ((0, types_1.isParamSpec)(typeParam)) {
4474
4506
  const paramSpecType = primarySignatureContext.getParamSpecType(typeParam);
@@ -4494,9 +4526,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4494
4526
  return { type };
4495
4527
  }
4496
4528
  let isIncomplete = baseTypeResult.isIncomplete;
4529
+ let isRequired = false;
4530
+ let isNotRequired = false;
4497
4531
  const type = mapSubtypesExpandTypeVars(baseTypeResult.type,
4498
4532
  /* conditionFilter */ undefined, (concreteSubtype, unexpandedSubtype) => {
4499
- var _a, _b;
4533
+ var _a;
4500
4534
  if ((0, types_1.isAnyOrUnknown)(concreteSubtype)) {
4501
4535
  return concreteSubtype;
4502
4536
  }
@@ -4506,7 +4540,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4506
4540
  type: printType(unexpandedSubtype),
4507
4541
  }), node.baseExpression);
4508
4542
  // Evaluate the index expressions as though they are type arguments for error-reporting.
4509
- getTypeArgs(node, flags, /* isAnnotatedClass */ false, /* hasCustomClassGetItem */ false);
4543
+ getTypeArgs(node, flags);
4510
4544
  return types_1.UnknownType.create();
4511
4545
  }
4512
4546
  }
@@ -4561,9 +4595,19 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4561
4595
  types_1.ClassType.isPartiallyEvaluated(concreteSubtype);
4562
4596
  const isFinalAnnotation = (0, types_1.isInstantiableClass)(concreteSubtype) && types_1.ClassType.isBuiltIn(concreteSubtype, 'Final');
4563
4597
  const isClassVarAnnotation = (0, types_1.isInstantiableClass)(concreteSubtype) && types_1.ClassType.isBuiltIn(concreteSubtype, 'ClassVar');
4564
- let typeArgs = getTypeArgs(node, flags, isAnnotatedClass, hasCustomClassGetItem || !isGenericClass, isFinalAnnotation, isClassVarAnnotation);
4598
+ // Inlined TypedDicts are supported only for 'dict' (and not for 'Dict').
4599
+ const supportsTypedDictTypeArg = (0, types_1.isInstantiableClass)(concreteSubtype) &&
4600
+ types_1.ClassType.isBuiltIn(concreteSubtype, 'dict') &&
4601
+ !concreteSubtype.aliasName;
4602
+ let typeArgs = getTypeArgs(node, flags, {
4603
+ isAnnotatedClass,
4604
+ hasCustomClassGetItem: hasCustomClassGetItem || !isGenericClass,
4605
+ isFinalAnnotation,
4606
+ isClassVarAnnotation,
4607
+ supportsTypedDictTypeArg,
4608
+ });
4565
4609
  if (!isAnnotatedClass) {
4566
- typeArgs = adjustTypeArgumentsForVariadicTypeVar(typeArgs, concreteSubtype.details.typeParameters, (_b = (0, typeUtils_1.getTypeVarScopeId)(concreteSubtype)) !== null && _b !== void 0 ? _b : '', node);
4610
+ typeArgs = adjustTypeArgumentsForVariadicTypeVar(typeArgs, concreteSubtype.details.typeParameters, node);
4567
4611
  }
4568
4612
  // If this is a custom __class_getitem__, there's no need to specialize the class.
4569
4613
  // Just return it as is.
@@ -4576,7 +4620,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4576
4620
  }), node.baseExpression);
4577
4621
  return concreteSubtype;
4578
4622
  }
4579
- return createSpecializedClassType(concreteSubtype, typeArgs, flags, node);
4623
+ const result = createSpecializedClassType(concreteSubtype, typeArgs, flags, node);
4624
+ if (result.isRequired) {
4625
+ isRequired = true;
4626
+ }
4627
+ else if (result.isNotRequired) {
4628
+ isNotRequired = true;
4629
+ }
4630
+ return result.type;
4580
4631
  }
4581
4632
  if ((0, types_1.isClassInstance)(concreteSubtype)) {
4582
4633
  const typeResult = getTypeOfIndexedObjectOrClass(node, concreteSubtype, usage);
@@ -4607,7 +4658,81 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4607
4658
  }
4608
4659
  });
4609
4660
  }
4610
- return { type, isIncomplete };
4661
+ return { type, isIncomplete, isRequired, isNotRequired };
4662
+ }
4663
+ // Determines the effective variance of the type parameters for a generic
4664
+ // type alias. Normally, variance is not important for type aliases, but
4665
+ // it can be important in cases where the type alias is used to specify
4666
+ // a base class in a class definition.
4667
+ function inferTypeParameterVarianceForTypeAlias(type) {
4668
+ // If this isn't a generic type alias, there's nothing to do.
4669
+ if (!type.typeAliasInfo || !type.typeAliasInfo.typeParameters) {
4670
+ return undefined;
4671
+ }
4672
+ // Is the usage variance info already cached?
4673
+ if (type.typeAliasInfo.usageVariance) {
4674
+ return type.typeAliasInfo.usageVariance;
4675
+ }
4676
+ const typeParams = type.typeAliasInfo.typeParameters;
4677
+ // Start with all of the usage variances unknown.
4678
+ const usageVariances = typeParams.map(() => 1 /* Unknown */);
4679
+ // Prepopulate the cached value for the type alias to handle
4680
+ // recursive type aliases.
4681
+ type.typeAliasInfo.usageVariance = usageVariances;
4682
+ // Traverse the type alias type definition and adjust the usage
4683
+ // variances accordingly.
4684
+ updateUsageVariancesRecursive(type, typeParams, usageVariances);
4685
+ return usageVariances;
4686
+ }
4687
+ // Looks at uses of the type parameters within the type and adjusts the
4688
+ // variances accordingly. For example, if the type is `Mapping[T1, T2]`,
4689
+ // then T1 will be set to invariant and T2 will be set to covariant.
4690
+ function updateUsageVariancesRecursive(type, typeAliasTypeParams, usageVariances, recursionCount = 0) {
4691
+ if (recursionCount > types_1.maxTypeRecursionCount) {
4692
+ return;
4693
+ }
4694
+ recursionCount++;
4695
+ // Define a helper function that performs the actual usage variant update.
4696
+ function updateUsageVarianceForType(type, variance) {
4697
+ (0, typeUtils_1.doForEachSubtype)(type, (subtype) => {
4698
+ const typeParamIndex = typeAliasTypeParams.findIndex((param) => (0, types_1.isTypeSame)(param, subtype));
4699
+ if (typeParamIndex >= 0) {
4700
+ usageVariances[typeParamIndex] = (0, typeUtils_1.combineVariances)(usageVariances[typeParamIndex], variance);
4701
+ }
4702
+ else {
4703
+ updateUsageVariancesRecursive(subtype, typeAliasTypeParams, usageVariances, recursionCount);
4704
+ }
4705
+ });
4706
+ }
4707
+ (0, typeUtils_1.doForEachSubtype)(type, (subtype) => {
4708
+ if (subtype.category === 5 /* Function */) {
4709
+ if (subtype.specializedTypes) {
4710
+ subtype.specializedTypes.parameterTypes.forEach((paramType) => {
4711
+ updateUsageVarianceForType(paramType, 4 /* Contravariant */);
4712
+ });
4713
+ const returnType = subtype.specializedTypes.returnType;
4714
+ if (returnType) {
4715
+ updateUsageVarianceForType(returnType, 3 /* Covariant */);
4716
+ }
4717
+ }
4718
+ }
4719
+ else if (subtype.category === 7 /* Class */) {
4720
+ if (subtype.typeArguments) {
4721
+ // If the class includes type parameters that uses auto variance,
4722
+ // compute the calculated variance.
4723
+ inferTypeParameterVarianceForClass(subtype);
4724
+ // Is the class specialized using any type arguments that correspond to
4725
+ // the type alias' type parameters?
4726
+ subtype.typeArguments.forEach((typeArg, classParamIndex) => {
4727
+ var _a;
4728
+ if (classParamIndex < subtype.details.typeParameters.length) {
4729
+ const classTypeParam = subtype.details.typeParameters[classParamIndex];
4730
+ updateUsageVarianceForType(typeArg, (_a = classTypeParam.computedVariance) !== null && _a !== void 0 ? _a : classTypeParam.details.declaredVariance);
4731
+ }
4732
+ });
4733
+ }
4734
+ }
4735
+ });
4611
4736
  }
4612
4737
  function makeTupleObject(entryTypes, isUnspecifiedLength = false) {
4613
4738
  if (tupleClassType && (0, types_1.isInstantiableClass)(tupleClassType)) {
@@ -4832,19 +4957,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4832
4957
  isIncomplete: !!callResult.isTypeIncomplete,
4833
4958
  };
4834
4959
  }
4835
- function getTypeArgs(node, flags, isAnnotatedClass = false, hasCustomClassGetItem = false, isFinalAnnotation = false, isClassVarAnnotation = false) {
4960
+ function getTypeArgs(node, flags, options) {
4836
4961
  const typeArgs = [];
4837
4962
  let adjFlags = flags;
4838
- if (isFinalAnnotation || isClassVarAnnotation) {
4963
+ if ((options === null || options === void 0 ? void 0 : options.isFinalAnnotation) || (options === null || options === void 0 ? void 0 : options.isClassVarAnnotation)) {
4839
4964
  adjFlags |= 131072 /* DisallowClassVar */ | 16 /* DisallowFinal */;
4840
4965
  }
4841
4966
  else {
4842
4967
  adjFlags &= ~(2 /* DoNotSpecialize */ |
4843
4968
  32 /* DisallowParamSpec */ |
4844
4969
  64 /* DisallowTypeVarTuple */ |
4845
- 1048576 /* AllowRequired */ |
4846
- 16384 /* EnforceTypeVarVarianceConsistency */);
4847
- if (!isAnnotatedClass) {
4970
+ 1048576 /* AllowRequired */);
4971
+ if (!(options === null || options === void 0 ? void 0 : options.isAnnotatedClass)) {
4848
4972
  adjFlags |= 131072 /* DisallowClassVar */ | 16 /* DisallowFinal */;
4849
4973
  }
4850
4974
  adjFlags |= 2097152 /* AllowUnpackedTupleOrTypeVarTuple */;
@@ -4855,7 +4979,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4855
4979
  // If it's a custom __class_getitem__, none of the arguments should be
4856
4980
  // treated as types. If it's an Annotated[a, b, c], only the first index
4857
4981
  // should be treated as a type. The others can be regular (non-type) objects.
4858
- if (hasCustomClassGetItem || (isAnnotatedClass && argIndex > 0)) {
4982
+ if ((options === null || options === void 0 ? void 0 : options.hasCustomClassGetItem) || ((options === null || options === void 0 ? void 0 : options.isAnnotatedClass) && argIndex > 0)) {
4859
4983
  typeResult = {
4860
4984
  ...getTypeOfExpression(expr, 32 /* DisallowParamSpec */ |
4861
4985
  64 /* DisallowTypeVarTuple */ |
@@ -4865,7 +4989,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4865
4989
  };
4866
4990
  }
4867
4991
  else {
4868
- typeResult = getTypeArg(expr, adjFlags);
4992
+ typeResult = getTypeArg(expr, adjFlags, !!(options === null || options === void 0 ? void 0 : options.supportsTypedDictTypeArg) && argIndex === 0);
4869
4993
  }
4870
4994
  return typeResult;
4871
4995
  };
@@ -4904,7 +5028,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4904
5028
  }
4905
5029
  return typeArgs;
4906
5030
  }
4907
- function getTypeArg(node, flags) {
5031
+ function getTypeArg(node, flags, supportsDictExpression) {
4908
5032
  let typeResult;
4909
5033
  let adjustedFlags = flags |
4910
5034
  128 /* ExpectingType */ |
@@ -4925,8 +5049,22 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4925
5049
  // Set the node's type so it isn't reevaluated later.
4926
5050
  setTypeForNode(node, types_1.UnknownType.create());
4927
5051
  }
5052
+ else if (node.nodeType === 15 /* Dictionary */ && supportsDictExpression) {
5053
+ const inlinedTypeDict = typedDictClassType && (0, types_1.isInstantiableClass)(typedDictClassType)
5054
+ ? (0, typedDicts_1.createTypedDictTypeInlined)(evaluatorInterface, node, typedDictClassType)
5055
+ : undefined;
5056
+ const keyTypeFallback = strClassType && (0, types_1.isInstantiableClass)(strClassType) ? strClassType : types_1.UnknownType.create();
5057
+ typeResult = {
5058
+ type: keyTypeFallback,
5059
+ inlinedTypeDict,
5060
+ node,
5061
+ };
5062
+ }
4928
5063
  else {
4929
5064
  typeResult = { ...getTypeOfExpression(node, adjustedFlags), node };
5065
+ if (node.nodeType === 15 /* Dictionary */) {
5066
+ addError(localize_1.Localizer.Diagnostic.dictInAnnotation(), node);
5067
+ }
4930
5068
  // "Protocol" is not allowed as a type argument.
4931
5069
  if ((0, types_1.isClass)(typeResult.type) && types_1.ClassType.isBuiltIn(typeResult.type, 'Protocol')) {
4932
5070
  addError(localize_1.Localizer.Diagnostic.protocolNotAllowedInTypeArgument(), node);
@@ -5484,6 +5622,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5484
5622
  : typeVarContext;
5485
5623
  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));
5486
5624
  effectiveTypeVarContext.addSolveForScope((0, typeUtils_1.getTypeVarScopeId)(overload));
5625
+ if (overload.details.constructorTypeVarScopeId) {
5626
+ effectiveTypeVarContext.addSolveForScope(overload.details.constructorTypeVarScopeId);
5627
+ }
5487
5628
  effectiveTypeVarContext.unlock();
5488
5629
  // Use speculative mode so we don't output any diagnostics or
5489
5630
  // record any final types in the type cache.
@@ -5908,7 +6049,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5908
6049
  }
5909
6050
  typeVarContext.addSolveForScope((0, typeUtils_1.getTypeVarScopeId)(constructorMethodType));
5910
6051
  // Skip the unknown argument check if we've already checked for __init__.
5911
- const callResult = validateCallArguments(errorNode, argList, constructorMethodInfo, typeVarContext, skipUnknownArgCheck);
6052
+ let callResult;
6053
+ if ((0, constructorTransform_1.hasConstructorTransform)(type)) {
6054
+ // Use speculative mode if we're going to later apply
6055
+ // a constructor transform. This allows us to use bidirectional
6056
+ // type inference for arguments in the transform.
6057
+ callResult = useSpeculativeMode(errorNode, () => {
6058
+ return validateCallArguments(errorNode, argList, constructorMethodInfo, typeVarContext, skipUnknownArgCheck);
6059
+ });
6060
+ }
6061
+ else {
6062
+ callResult = validateCallArguments(errorNode, argList, constructorMethodInfo, typeVarContext, skipUnknownArgCheck);
6063
+ }
5912
6064
  if (callResult.isTypeIncomplete) {
5913
6065
  isTypeIncomplete = true;
5914
6066
  }
@@ -6123,7 +6275,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6123
6275
  }), exprNode);
6124
6276
  return { returnType: types_1.UnknownType.create(), argumentErrors: true, overloadsUsedForCall };
6125
6277
  }
6126
- const returnType = mapSubtypesExpandTypeVars(callTypeResult.type,
6278
+ let returnType = mapSubtypesExpandTypeVars(callTypeResult.type,
6127
6279
  /* conditionFilter */ undefined, (expandedSubtype, unexpandedSubtype) => {
6128
6280
  var _a, _b, _c, _d, _e, _f;
6129
6281
  switch (expandedSubtype.category) {
@@ -6142,6 +6294,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6142
6294
  return expandedSubtype;
6143
6295
  }
6144
6296
  case 5 /* Function */: {
6297
+ if (types_1.TypeBase.isInstantiable(expandedSubtype)) {
6298
+ addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.callableNotInstantiable().format({
6299
+ type: printType(expandedSubtype),
6300
+ }), errorNode);
6301
+ argumentErrors = true;
6302
+ return undefined;
6303
+ }
6145
6304
  // The stdlib collections/__init__.pyi stub file defines namedtuple
6146
6305
  // as a function rather than a class, so we need to check for it here.
6147
6306
  if (expandedSubtype.details.builtInName === 'namedtuple') {
@@ -6263,6 +6422,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6263
6422
  (0, types_1.isNoneInstance)(subtype)) {
6264
6423
  return (0, typeUtils_1.convertToInstantiable)(stripLiteralValue(subtype));
6265
6424
  }
6425
+ else if ((0, types_1.isFunction)(subtype) && types_1.TypeBase.isInstance(subtype)) {
6426
+ return types_1.FunctionType.cloneAsInstantiable(subtype);
6427
+ }
6266
6428
  return types_1.AnyType.create();
6267
6429
  });
6268
6430
  }
@@ -6447,9 +6609,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6447
6609
  }
6448
6610
  return undefined;
6449
6611
  });
6612
+ // If we ended up with a "Never" type because all code paths returned
6613
+ // undefined due to argument errors, transform the result into an Unknown
6614
+ // to avoid subsequent false positives.
6615
+ if (argumentErrors && (0, types_1.isNever)(returnType) && !returnType.isNoReturn) {
6616
+ returnType = types_1.UnknownType.create();
6617
+ }
6450
6618
  return {
6451
6619
  argumentErrors,
6452
- returnType: (0, types_1.isNever)(returnType) && !returnType.isNoReturn ? undefined : returnType,
6620
+ returnType,
6453
6621
  isTypeIncomplete,
6454
6622
  specializedInitSelfType,
6455
6623
  overloadsUsedForCall,
@@ -6504,7 +6672,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6504
6672
  // validation is left to the caller.
6505
6673
  // This logic is based on PEP 3102: https://www.python.org/dev/peps/pep-3102/
6506
6674
  function matchFunctionArgumentsToParameters(errorNode, argList, typeResult, overloadIndex) {
6507
- var _a, _b, _c, _d, _e;
6675
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
6508
6676
  const paramDetails = (0, typeUtils_1.getParameterListDetails)(typeResult.type);
6509
6677
  let argIndex = 0;
6510
6678
  let matchedUnpackedListOfUnknownLength = false;
@@ -6627,11 +6795,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6627
6795
  tooManyPositionals = true;
6628
6796
  }
6629
6797
  if (tooManyPositionals) {
6630
- addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, positionParamLimitIndex === 1
6631
- ? localize_1.Localizer.Diagnostic.argPositionalExpectedOne()
6632
- : localize_1.Localizer.Diagnostic.argPositionalExpectedCount().format({
6633
- expected: positionParamLimitIndex,
6634
- }), (_b = argList[argIndex].valueExpression) !== null && _b !== void 0 ? _b : errorNode);
6798
+ if (!isDiagnosticSuppressedForNode(errorNode)) {
6799
+ addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, positionParamLimitIndex === 1
6800
+ ? localize_1.Localizer.Diagnostic.argPositionalExpectedOne()
6801
+ : localize_1.Localizer.Diagnostic.argPositionalExpectedCount().format({
6802
+ expected: positionParamLimitIndex,
6803
+ }), (_b = argList[argIndex].valueExpression) !== null && _b !== void 0 ? _b : errorNode);
6804
+ }
6635
6805
  reportedArgError = true;
6636
6806
  }
6637
6807
  }
@@ -6655,11 +6825,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6655
6825
  // all positional parameters specified in the Concatenate must be
6656
6826
  // filled explicitly.
6657
6827
  if (typeResult.type.details.paramSpec && paramIndex < positionParamLimitIndex) {
6658
- addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, positionParamLimitIndex === 1
6659
- ? localize_1.Localizer.Diagnostic.argPositionalExpectedOne()
6660
- : localize_1.Localizer.Diagnostic.argPositionalExpectedCount().format({
6661
- expected: positionParamLimitIndex,
6662
- }), (_c = argList[argIndex].valueExpression) !== null && _c !== void 0 ? _c : errorNode);
6828
+ if (!isDiagnosticSuppressedForNode(errorNode)) {
6829
+ addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, positionParamLimitIndex === 1
6830
+ ? localize_1.Localizer.Diagnostic.argPositionalExpectedOne()
6831
+ : localize_1.Localizer.Diagnostic.argPositionalExpectedCount().format({
6832
+ expected: positionParamLimitIndex,
6833
+ }), (_c = argList[argIndex].valueExpression) !== null && _c !== void 0 ? _c : errorNode);
6834
+ }
6663
6835
  reportedArgError = true;
6664
6836
  }
6665
6837
  const argType = argTypeResult.type;
@@ -6718,7 +6890,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6718
6890
  // It's not allowed to use unpacked arguments with a variadic *args
6719
6891
  // parameter unless the argument is a variadic arg as well.
6720
6892
  if (isParamVariadic && !isArgCompatibleWithVariadic) {
6721
- addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.unpackedArgWithVariadicParam(), argList[argIndex].valueExpression || errorNode);
6893
+ if (!isDiagnosticSuppressedForNode(errorNode)) {
6894
+ addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.unpackedArgWithVariadicParam(), argList[argIndex].valueExpression || errorNode);
6895
+ }
6722
6896
  reportedArgError = true;
6723
6897
  }
6724
6898
  else {
@@ -6731,7 +6905,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6731
6905
  paramType,
6732
6906
  requiresTypeVarMatching: (0, typeUtils_1.requiresSpecialization)(paramType),
6733
6907
  argument: funcArg,
6734
- errorNode: argList[argIndex].valueExpression || errorNode,
6908
+ errorNode: (_f = argList[argIndex].valueExpression) !== null && _f !== void 0 ? _f : errorNode,
6735
6909
  paramName,
6736
6910
  isParamNameSynthesized: paramDetails.params[paramIndex].param.isNameSynthesized,
6737
6911
  mapsToVarArgList: isParamVariadic && remainingArgCount > remainingParamCount,
@@ -6774,12 +6948,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6774
6948
  : 0 /* Simple */;
6775
6949
  if (remainingArgCount <= remainingParamCount) {
6776
6950
  if (remainingArgCount < remainingParamCount) {
6777
- // Have we run out of arguments and still have parameters left to fill?
6778
- addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, remainingArgCount === 1
6779
- ? localize_1.Localizer.Diagnostic.argMorePositionalExpectedOne()
6780
- : localize_1.Localizer.Diagnostic.argMorePositionalExpectedCount().format({
6781
- expected: remainingArgCount,
6782
- }), argList[argIndex].valueExpression || errorNode);
6951
+ if (!isDiagnosticSuppressedForNode(errorNode)) {
6952
+ // Have we run out of arguments and still have parameters left to fill?
6953
+ addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, remainingArgCount === 1
6954
+ ? localize_1.Localizer.Diagnostic.argMorePositionalExpectedOne()
6955
+ : localize_1.Localizer.Diagnostic.argMorePositionalExpectedCount().format({
6956
+ expected: remainingArgCount,
6957
+ }), argList[argIndex].valueExpression || errorNode);
6958
+ }
6783
6959
  reportedArgError = true;
6784
6960
  }
6785
6961
  paramIndex++;
@@ -6854,13 +7030,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6854
7030
  argsRemainingCount--;
6855
7031
  }
6856
7032
  if (argsRemainingCount > 0) {
6857
- addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, argsRemainingCount === 1
6858
- ? localize_1.Localizer.Diagnostic.argMorePositionalExpectedOne()
6859
- : localize_1.Localizer.Diagnostic.argMorePositionalExpectedCount().format({
6860
- expected: argsRemainingCount,
6861
- }), argList.length > positionalArgCount
6862
- ? argList[positionalArgCount].valueExpression || errorNode
6863
- : errorNode);
7033
+ if (!isDiagnosticSuppressedForNode(errorNode)) {
7034
+ addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, argsRemainingCount === 1
7035
+ ? localize_1.Localizer.Diagnostic.argMorePositionalExpectedOne()
7036
+ : localize_1.Localizer.Diagnostic.argMorePositionalExpectedCount().format({
7037
+ expected: argsRemainingCount,
7038
+ }), argList.length > positionalArgCount
7039
+ ? argList[positionalArgCount].valueExpression || errorNode
7040
+ : errorNode);
7041
+ }
6864
7042
  reportedArgError = true;
6865
7043
  }
6866
7044
  }
@@ -6934,7 +7112,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6934
7112
  }
6935
7113
  });
6936
7114
  if (!diag.isEmpty()) {
6937
- addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.unpackedTypedDictArgument() + diag.getString(), argList[argIndex].valueExpression || errorNode);
7115
+ if (!isDiagnosticSuppressedForNode(errorNode)) {
7116
+ addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.unpackedTypedDictArgument() + diag.getString(), argList[argIndex].valueExpression || errorNode);
7117
+ }
6938
7118
  reportedArgError = true;
6939
7119
  }
6940
7120
  }
@@ -6980,7 +7160,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6980
7160
  }
6981
7161
  }
6982
7162
  if (!isValidMappingType) {
6983
- addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.unpackedDictArgumentNotMapping(), argList[argIndex].valueExpression || errorNode);
7163
+ if (!isDiagnosticSuppressedForNode(errorNode)) {
7164
+ addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet
7165
+ .reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.unpackedDictArgumentNotMapping(), argList[argIndex].valueExpression || errorNode);
7166
+ }
6984
7167
  reportedArgError = true;
6985
7168
  }
6986
7169
  }
@@ -7012,7 +7195,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
7012
7195
  paramType,
7013
7196
  requiresTypeVarMatching: (0, typeUtils_1.requiresSpecialization)(paramType),
7014
7197
  argument: argList[argIndex],
7015
- errorNode: argList[argIndex].valueExpression || errorNode,
7198
+ errorNode: (_g = argList[argIndex].valueExpression) !== null && _g !== void 0 ? _g : errorNode,
7016
7199
  paramName: paramNameValue,
7017
7200
  });
7018
7201
  trySetActive(argList[argIndex], paramDetails.params[paramInfoIndex].param);
@@ -7029,7 +7212,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
7029
7212
  paramType,
7030
7213
  requiresTypeVarMatching: (0, typeUtils_1.requiresSpecialization)(paramType),
7031
7214
  argument: argList[argIndex],
7032
- errorNode: argList[argIndex].valueExpression || errorNode,
7215
+ errorNode: (_h = argList[argIndex].valueExpression) !== null && _h !== void 0 ? _h : errorNode,
7033
7216
  paramName: paramNameValue,
7034
7217
  });
7035
7218
  // Remember that this parameter has already received a value.
@@ -7048,12 +7231,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
7048
7231
  }
7049
7232
  }
7050
7233
  else if (argList[argIndex].argumentCategory === 0 /* Simple */) {
7051
- const fileInfo = AnalyzerNodeInfo.getFileInfo(errorNode);
7052
- addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, positionParamLimitIndex === 1
7053
- ? localize_1.Localizer.Diagnostic.argPositionalExpectedOne()
7054
- : localize_1.Localizer.Diagnostic.argPositionalExpectedCount().format({
7055
- expected: positionParamLimitIndex,
7056
- }), argList[argIndex].valueExpression || errorNode);
7234
+ if (!isDiagnosticSuppressedForNode(errorNode)) {
7235
+ const fileInfo = AnalyzerNodeInfo.getFileInfo(errorNode);
7236
+ addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, positionParamLimitIndex === 1
7237
+ ? localize_1.Localizer.Diagnostic.argPositionalExpectedOne()
7238
+ : localize_1.Localizer.Diagnostic.argPositionalExpectedCount().format({
7239
+ expected: positionParamLimitIndex,
7240
+ }), argList[argIndex].valueExpression || errorNode);
7241
+ }
7057
7242
  reportedArgError = true;
7058
7243
  }
7059
7244
  else if (argList[argIndex].argumentCategory === 1 /* UnpackedList */) {
@@ -7071,7 +7256,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
7071
7256
  paramType: typeResult.type.details.paramSpec,
7072
7257
  requiresTypeVarMatching: false,
7073
7258
  argument: argList[argIndex],
7074
- errorNode: argList[argIndex].valueExpression || errorNode,
7259
+ errorNode: (_j = argList[argIndex].valueExpression) !== null && _j !== void 0 ? _j : errorNode,
7075
7260
  });
7076
7261
  }
7077
7262
  }
@@ -7121,10 +7306,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
7121
7306
  return !entry || entry.argsReceived < entry.argsNeeded;
7122
7307
  });
7123
7308
  if (unassignedParams.length > 0) {
7124
- const missingParamNames = unassignedParams.map((p) => `"${p}"`).join(', ');
7125
- addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, unassignedParams.length === 1
7126
- ? localize_1.Localizer.Diagnostic.argMissingForParam().format({ name: missingParamNames })
7127
- : localize_1.Localizer.Diagnostic.argMissingForParams().format({ names: missingParamNames }), errorNode);
7309
+ if (!isDiagnosticSuppressedForNode(errorNode)) {
7310
+ const missingParamNames = unassignedParams.map((p) => `"${p}"`).join(', ');
7311
+ addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, unassignedParams.length === 1
7312
+ ? localize_1.Localizer.Diagnostic.argMissingForParam().format({ name: missingParamNames })
7313
+ : localize_1.Localizer.Diagnostic.argMissingForParams().format({ names: missingParamNames }), errorNode);
7314
+ }
7128
7315
  reportedArgError = true;
7129
7316
  }
7130
7317
  // Add any implicit (default) arguments that are needed for resolving
@@ -7150,7 +7337,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
7150
7337
  argumentCategory: 0 /* Simple */,
7151
7338
  typeResult: { type: defaultArgType },
7152
7339
  },
7153
- errorNode: errorNode,
7340
+ errorNode,
7154
7341
  paramName: param.name,
7155
7342
  isParamNameSynthesized: param.isNameSynthesized,
7156
7343
  });
@@ -7188,7 +7375,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
7188
7375
  if (containsVariadicTypeVar &&
7189
7376
  argParam.argument.argumentCategory !== 1 /* UnpackedList */ &&
7190
7377
  !argParam.mapsToVarArgList) {
7191
- addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeVarTupleMustBeUnpacked(), (_a = argParam.argument.valueExpression) !== null && _a !== void 0 ? _a : errorNode);
7378
+ if (!isDiagnosticSuppressedForNode(errorNode)) {
7379
+ addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet
7380
+ .reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeVarTupleMustBeUnpacked(), (_a = argParam.argument.valueExpression) !== null && _a !== void 0 ? _a : errorNode);
7381
+ }
7192
7382
  reportedArgError = true;
7193
7383
  }
7194
7384
  return {
@@ -8141,8 +8331,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8141
8331
  types_1.FunctionType.addDefaultParameters(functionType);
8142
8332
  return functionType;
8143
8333
  }
8144
- if (node.nodeType === 52 /* Tuple */) {
8145
- node.expressions.forEach((paramExpr, index) => {
8334
+ if (node.nodeType === 31 /* List */) {
8335
+ node.entries.forEach((paramExpr, index) => {
8146
8336
  const typeResult = getTypeOfExpressionExpectingType(paramExpr);
8147
8337
  types_1.FunctionType.addParameter(functionType, {
8148
8338
  category: 0 /* Simple */,
@@ -9210,6 +9400,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9210
9400
  if (!keyTypeResult.isIncomplete && !keyTypeResult.typeErrors) {
9211
9401
  verifySetEntryOrDictKeyIsHashable(entryNode.keyExpression, keyType, /* isDictKey */ true);
9212
9402
  }
9403
+ if (expectedDiagAddendum && keyTypeResult.expectedTypeDiagAddendum) {
9404
+ expectedDiagAddendum.addAddendum(keyTypeResult.expectedTypeDiagAddendum);
9405
+ }
9213
9406
  let valueTypeResult;
9214
9407
  if (expectedTypedDictEntries &&
9215
9408
  (0, types_1.isClassInstance)(keyType) &&
@@ -9239,7 +9432,22 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9239
9432
  addUnknown = false;
9240
9433
  }
9241
9434
  else if (entryNode.nodeType === 16 /* DictionaryExpandEntry */) {
9242
- const unexpandedTypeResult = getTypeOfExpression(entryNode.expandExpression);
9435
+ // Verify that the type supports the `keys` and `__getitem__` methods.
9436
+ // This protocol is defined in the _typeshed stub. If we can't find
9437
+ // it there, fall back on typing.Mapping.
9438
+ let mappingType = getTypeshedType(node, 'SupportsKeysAndGetItem');
9439
+ if (!mappingType) {
9440
+ mappingType = getTypingType(node, 'Mapping');
9441
+ }
9442
+ let expectedType;
9443
+ if (expectedKeyType && expectedValueType) {
9444
+ if (mappingType && (0, types_1.isInstantiableClass)(mappingType)) {
9445
+ expectedType = types_1.ClassType.cloneAsInstance(types_1.ClassType.cloneForSpecialization(mappingType, [expectedKeyType, expectedValueType],
9446
+ /* isTypeArgumentExplicit */ true));
9447
+ }
9448
+ }
9449
+ const unexpandedTypeResult = getTypeOfExpression(entryNode.expandExpression,
9450
+ /* flags */ undefined, (0, typeUtils_1.makeInferenceContext)(expectedType));
9243
9451
  if (unexpandedTypeResult.isIncomplete) {
9244
9452
  isIncomplete = true;
9245
9453
  }
@@ -9263,20 +9471,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9263
9471
  }
9264
9472
  }
9265
9473
  else {
9266
- // Verify that the type supports the `keys` and `__getitem__` methods.
9267
- // This protocol is defined in the _typeshed stub. If we can't find
9268
- // it there, fall back on typing.Mapping.
9269
- let mappingType = getTypeshedType(node, 'SupportsKeysAndGetItem');
9270
- if (!mappingType) {
9271
- mappingType = getTypingType(node, 'Mapping');
9272
- }
9273
9474
  if (mappingType && (0, types_1.isInstantiableClass)(mappingType)) {
9274
9475
  const mappingTypeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(mappingType));
9275
9476
  // Self-specialize the class.
9276
9477
  mappingType = types_1.ClassType.cloneForSpecialization(mappingType, mappingType.details.typeParameters,
9277
9478
  /* isTypeArgumentExplicit */ true);
9278
9479
  if (assignType(types_1.ClassType.cloneAsInstance(mappingType), unexpandedType,
9279
- /* diag */ undefined, mappingTypeVarContext)) {
9480
+ /* diag */ undefined, mappingTypeVarContext,
9481
+ /* srcTypeVarContext */ undefined, 128 /* RetainLiteralsForTypeVar */)) {
9280
9482
  const specializedMapping = (0, typeUtils_1.applySolvedTypeVars)(mappingType, mappingTypeVarContext);
9281
9483
  const typeArgs = specializedMapping.typeArguments;
9282
9484
  if (typeArgs && typeArgs.length >= 2) {
@@ -10296,16 +10498,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10296
10498
  return (0, typeUtils_1.synthesizeTypeVarForSelfCls)(enclosingClassTypeResult.classType, /* isClsParam */ true);
10297
10499
  }
10298
10500
  function createRequiredType(classType, errorNode, isRequired, typeArgs, flags) {
10299
- var _a;
10300
10501
  // If no type arguments are provided, the resulting type
10301
10502
  // depends on whether we're evaluating a type annotation or
10302
10503
  // we're in some other context.
10303
10504
  if (!typeArgs && (flags & 256 /* ExpectingTypeAnnotation */) === 0) {
10304
- return classType;
10505
+ return { type: classType };
10305
10506
  }
10306
10507
  if (!typeArgs || typeArgs.length !== 1) {
10307
10508
  addError(isRequired ? localize_1.Localizer.Diagnostic.requiredArgCount() : localize_1.Localizer.Diagnostic.notRequiredArgCount(), errorNode);
10308
- return classType;
10509
+ return { type: classType };
10309
10510
  }
10310
10511
  const typeArgType = typeArgs[0].type;
10311
10512
  // Make sure this is used only in a dataclass.
@@ -10316,21 +10517,24 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10316
10517
  (0, types_1.isInstantiableClass)(classTypeInfo.classType) &&
10317
10518
  types_1.ClassType.isTypedDictClass(classTypeInfo.classType)) {
10318
10519
  // The only legal usage is when used in a type annotation statement.
10319
- if (((_a = errorNode.parent) === null || _a === void 0 ? void 0 : _a.nodeType) === 54 /* TypeAnnotation */ &&
10320
- errorNode.parent.typeAnnotation === errorNode) {
10520
+ if (ParseTreeUtils.isNodeContainedWithinNodeType(errorNode, 54 /* TypeAnnotation */)) {
10321
10521
  isUsageLegal = true;
10322
10522
  }
10323
10523
  }
10324
10524
  if ((flags & 1048576 /* AllowRequired */) !== 0) {
10325
10525
  isUsageLegal = true;
10326
10526
  }
10527
+ // Nested Required/NotRequired are not allowed.
10528
+ if (typeArgs[0].isRequired || typeArgs[0].isNotRequired) {
10529
+ isUsageLegal = false;
10530
+ }
10327
10531
  if (!isUsageLegal) {
10328
10532
  addError(isRequired
10329
10533
  ? localize_1.Localizer.Diagnostic.requiredNotInTypedDict()
10330
10534
  : localize_1.Localizer.Diagnostic.notRequiredNotInTypedDict(), errorNode);
10331
- return types_1.ClassType.cloneForSpecialization(classType, [(0, typeUtils_1.convertToInstance)(typeArgType)], !!typeArgs);
10535
+ return { type: types_1.ClassType.cloneForSpecialization(classType, [(0, typeUtils_1.convertToInstance)(typeArgType)], !!typeArgs) };
10332
10536
  }
10333
- return typeArgType;
10537
+ return { type: typeArgType, isRequired, isNotRequired: !isRequired };
10334
10538
  }
10335
10539
  function createUnpackType(classType, errorNode, typeArgs, flags) {
10336
10540
  // If no type arguments are provided, the resulting type
@@ -10397,6 +10601,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10397
10601
  if ((0, types_1.isParamSpec)(typeArg.type)) {
10398
10602
  addError(localize_1.Localizer.Diagnostic.paramSpecContext(), typeArg.node);
10399
10603
  }
10604
+ else if ((0, types_1.isUnpackedVariadicTypeVar)(typeArg.type)) {
10605
+ addError(localize_1.Localizer.Diagnostic.typeVarTupleContext(), typeArg.node);
10606
+ }
10607
+ else if ((0, types_1.isUnpackedClass)(typeArg.type)) {
10608
+ addError(localize_1.Localizer.Diagnostic.unpackedArgInTypeArgument(), typeArg.node);
10609
+ }
10400
10610
  }
10401
10611
  });
10402
10612
  }
@@ -10407,9 +10617,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10407
10617
  addError(localize_1.Localizer.Diagnostic.annotatedTypeArgMissing(), errorNode);
10408
10618
  }
10409
10619
  if (!typeArgs || typeArgs.length === 0) {
10410
- return types_1.AnyType.create();
10620
+ return { type: types_1.AnyType.create() };
10411
10621
  }
10412
- return types_1.TypeBase.cloneForAnnotated(typeArgs[0].type);
10622
+ return {
10623
+ type: types_1.TypeBase.cloneForAnnotated(typeArgs[0].type),
10624
+ isRequired: typeArgs[0].isRequired,
10625
+ isNotRequired: typeArgs[0].isNotRequired,
10626
+ };
10413
10627
  }
10414
10628
  // Creates one of several "special" types that are defined in typing.pyi
10415
10629
  // but not declared in their entirety. This includes the likes of "Tuple",
@@ -10849,7 +11063,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10849
11063
  let isSpeculativeTypeAlias = false;
10850
11064
  if (isDeclaredTypeAlias(node.leftExpression)) {
10851
11065
  typeAliasNameNode = node.leftExpression.valueExpression;
10852
- if (!(0, declarationUtils_1.isLegalTypeAliasExpressionForm)(node.rightExpression)) {
11066
+ if (!isLegalTypeAliasExpressionForm(node.rightExpression)) {
10853
11067
  addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeAliasIllegalExpressionForm(), node.rightExpression);
10854
11068
  }
10855
11069
  }
@@ -10946,7 +11160,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10946
11160
  }
10947
11161
  function isPossibleTypeAliasOrTypedDict(decl) {
10948
11162
  var _a;
10949
- if ((0, declarationUtils_1.isPossibleTypeAliasDeclaration)(decl)) {
11163
+ if (isPossibleTypeAliasDeclaration(decl)) {
10950
11164
  return true;
10951
11165
  }
10952
11166
  if (decl.type === 1 /* Variable */ &&
@@ -11002,7 +11216,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
11002
11216
  typeParameters = evaluateTypeParameterList(node.typeParameters);
11003
11217
  typeAliasTypeVar.details.recursiveTypeParameters = typeParameters;
11004
11218
  }
11005
- if (!(0, declarationUtils_1.isLegalTypeAliasExpressionForm)(node.expression)) {
11219
+ if (!isLegalTypeAliasExpressionForm(node.expression)) {
11006
11220
  addDiagnostic(AnalyzerNodeInfo.getFileInfo(node).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeAliasIllegalExpressionForm(), node.expression);
11007
11221
  }
11008
11222
  const aliasTypeResult = getTypeOfExpressionExpectingType(node.expression);
@@ -11141,7 +11355,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
11141
11355
  argType = (0, types_1.removeUnbound)(argType);
11142
11356
  }
11143
11357
  if (!(0, types_1.isAnyOrUnknown)(argType) && !(0, types_1.isUnbound)(argType)) {
11144
- if ((0, types_1.isClass)(argType) && types_1.TypeBase.isInstance(argType) && types_1.ClassType.isBuiltIn(argType, 'type')) {
11358
+ if ((0, typeUtils_1.isMetaclassInstance)(argType)) {
11359
+ (0, debug_1.assert)((0, types_1.isClassInstance)(argType));
11145
11360
  argType =
11146
11361
  argType.typeArguments && argType.typeArguments.length > 0
11147
11362
  ? argType.typeArguments[0]
@@ -11577,7 +11792,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
11577
11792
  // Presumptively mark the variance inference as complete. This
11578
11793
  // prevents potential recursion.
11579
11794
  classType.details.requiresVarianceInference = false;
11580
- // Presumptively mark the computed variance to "in progress". We'll
11795
+ // Presumptively mark the computed variance to "unknown". We'll
11581
11796
  // replace this below once the variance has been inferred.
11582
11797
  classType.details.typeParameters.forEach((param) => {
11583
11798
  if (param.details.declaredVariance === 0 /* Auto */) {
@@ -11614,13 +11829,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
11614
11829
  /* isTypeArgumentExplicit */ true);
11615
11830
  const destType = types_1.ClassType.cloneForSpecialization(updatedClassType, destTypeArgs,
11616
11831
  /* isTypeArgumentExplicit */ true);
11617
- const isDestSubtypeOfSrc = assignClassToSelf(srcType, destType);
11832
+ const isDestSubtypeOfSrc = assignClassToSelf(srcType, destType, /* ignoreBaseClassVariance */ false);
11618
11833
  let inferredVariance;
11619
11834
  if (isDestSubtypeOfSrc) {
11620
11835
  inferredVariance = 3 /* Covariant */;
11621
11836
  }
11622
11837
  else {
11623
- const isSrcSubtypeOfDest = assignClassToSelf(destType, srcType);
11838
+ const isSrcSubtypeOfDest = assignClassToSelf(destType, srcType, /* ignoreBaseClassVariance */ false);
11624
11839
  if (isSrcSubtypeOfDest) {
11625
11840
  inferredVariance = 4 /* Contravariant */;
11626
11841
  }
@@ -11637,9 +11852,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
11637
11852
  function evaluateTypeParameterList(node) {
11638
11853
  const paramTypes = [];
11639
11854
  node.parameters.forEach((param) => {
11855
+ var _a;
11640
11856
  const paramSymbol = AnalyzerNodeInfo.getTypeParameterSymbol(param.name);
11641
11857
  (0, debug_1.assert)(paramSymbol);
11642
- const typeOfParam = getDeclaredTypeOfSymbol(paramSymbol, param.name);
11858
+ const typeOfParam = (_a = getDeclaredTypeOfSymbol(paramSymbol, param.name)) === null || _a === void 0 ? void 0 : _a.type;
11643
11859
  if (!typeOfParam || !(0, types_1.isTypeVar)(typeOfParam)) {
11644
11860
  return;
11645
11861
  }
@@ -11727,6 +11943,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
11727
11943
  decoratorCallType.details.builtInName === 'dataclass_transform') {
11728
11944
  originalClassType.details.classDataClassTransform = (0, dataClasses_1.validateDataClassTransformDecorator)(evaluatorInterface, decoratorNode.expression);
11729
11945
  }
11946
+ else if (decoratorCallType.details.name === 'deprecated') {
11947
+ originalClassType.details.deprecatedMessage = getCustomDeprecationMessage(decoratorNode);
11948
+ return inputClassType;
11949
+ }
11730
11950
  }
11731
11951
  if ((0, types_1.isOverloadedFunction)(decoratorCallType)) {
11732
11952
  if (decoratorCallType.overloads.length > 0 &&
@@ -11756,6 +11976,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
11756
11976
  // dependency between builtins, typing and _typeshed stubs.
11757
11977
  return inputClassType;
11758
11978
  }
11979
+ if (decoratorType.details.builtInName === 'deprecated') {
11980
+ originalClassType.details.deprecatedMessage = getCustomDeprecationMessage(decoratorNode);
11981
+ return inputClassType;
11982
+ }
11759
11983
  if (decoratorType.details.builtInName === 'runtime_checkable') {
11760
11984
  originalClassType.details.flags |= 32768 /* RuntimeCheckable */;
11761
11985
  // Don't call getTypeOfDecorator for runtime_checkable. It appears
@@ -12548,6 +12772,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
12548
12772
  undecoratedType.details.decoratorDataClassBehaviors = (0, dataClasses_1.validateDataClassTransformDecorator)(evaluatorInterface, decoratorNode.expression);
12549
12773
  return inputFunctionType;
12550
12774
  }
12775
+ if (decoratorCallType.details.name === 'deprecated') {
12776
+ undecoratedType.details.deprecatedMessage = getCustomDeprecationMessage(decoratorNode);
12777
+ return inputFunctionType;
12778
+ }
12551
12779
  }
12552
12780
  if ((0, types_1.isOverloadedFunction)(decoratorCallType)) {
12553
12781
  if (decoratorCallType.overloads.length > 0 &&
@@ -12563,6 +12791,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
12563
12791
  if (decoratorType.details.builtInName === 'abstractmethod') {
12564
12792
  return inputFunctionType;
12565
12793
  }
12794
+ if (decoratorType.details.builtInName === 'deprecated') {
12795
+ undecoratedType.details.deprecatedMessage = getCustomDeprecationMessage(decoratorNode);
12796
+ return inputFunctionType;
12797
+ }
12566
12798
  // Handle property setters and deleters.
12567
12799
  if (decoratorNode.expression.nodeType === 35 /* MemberAccess */) {
12568
12800
  const baseType = getTypeOfExpression(decoratorNode.expression.leftExpression, evaluatorFlags | 2 /* DoNotSpecialize */).type;
@@ -12672,7 +12904,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
12672
12904
  getTypeOfFunction(decl.node);
12673
12905
  }
12674
12906
  }
12675
- const overloadedTypes = [];
12907
+ let overloadedTypes = [];
12676
12908
  // Look at the previous declaration's type.
12677
12909
  const prevDecl = decls[declIndex - 1];
12678
12910
  if (prevDecl.type === 5 /* Function */) {
@@ -12694,6 +12926,17 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
12694
12926
  if (overloadedTypes.length === 1) {
12695
12927
  return overloadedTypes[0];
12696
12928
  }
12929
+ // Apply the implementation's docstring to any overloads that don't
12930
+ // have their own docstrings.
12931
+ const implementation = overloadedTypes.find((signature) => !types_1.FunctionType.isOverloaded(signature));
12932
+ if (implementation === null || implementation === void 0 ? void 0 : implementation.details.docString) {
12933
+ overloadedTypes = overloadedTypes.map((overload) => {
12934
+ if (types_1.FunctionType.isOverloaded(overload) && !overload.details.docString) {
12935
+ return types_1.FunctionType.cloneWithDocString(overload, implementation.details.docString);
12936
+ }
12937
+ return overload;
12938
+ });
12939
+ }
12697
12940
  // Create a new overloaded type that copies the contents of the previous
12698
12941
  // one and adds a new function.
12699
12942
  const newOverload = types_1.OverloadedFunctionType.create(overloadedTypes);
@@ -13778,22 +14021,22 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
13778
14021
  const aliasedName = classType.aliasName || classType.details.name;
13779
14022
  switch (aliasedName) {
13780
14023
  case 'Callable': {
13781
- return createCallableType(typeArgs, errorNode);
14024
+ return { type: createCallableType(typeArgs, errorNode) };
13782
14025
  }
13783
14026
  case 'Never': {
13784
14027
  if (typeArgs && typeArgs.length > 0) {
13785
14028
  addError(localize_1.Localizer.Diagnostic.typeArgsExpectingNone().format({ name: 'Never' }), typeArgs[0].node);
13786
14029
  }
13787
- return types_1.NeverType.createNever();
14030
+ return { type: types_1.NeverType.createNever() };
13788
14031
  }
13789
14032
  case 'NoReturn': {
13790
14033
  if (typeArgs && typeArgs.length > 0) {
13791
14034
  addError(localize_1.Localizer.Diagnostic.typeArgsExpectingNone().format({ name: 'NoReturn' }), typeArgs[0].node);
13792
14035
  }
13793
- return types_1.NeverType.createNoReturn();
14036
+ return { type: types_1.NeverType.createNoReturn() };
13794
14037
  }
13795
14038
  case 'Optional': {
13796
- return createOptionalType(classType, errorNode, typeArgs, flags);
14039
+ return { type: createOptionalType(classType, errorNode, typeArgs, flags) };
13797
14040
  }
13798
14041
  case 'Type': {
13799
14042
  // PEP 484 says that Type[Any] should be considered
@@ -13802,56 +14045,58 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
13802
14045
  (0, types_1.isAnyOrUnknown)(typeArgs[0].type) &&
13803
14046
  typeClassType &&
13804
14047
  (0, types_1.isInstantiableClass)(typeClassType)) {
13805
- return typeClassType;
14048
+ return { type: typeClassType };
13806
14049
  }
13807
14050
  let typeType = createSpecialType(classType, typeArgs, 1);
13808
14051
  if ((0, types_1.isInstantiableClass)(typeType)) {
13809
14052
  typeType = (0, typeUtils_1.explodeGenericClass)(typeType);
13810
14053
  }
13811
- return typeType;
14054
+ return { type: typeType };
13812
14055
  }
13813
14056
  case 'ClassVar': {
13814
- return createClassVarType(classType, errorNode, typeArgs, flags);
14057
+ return { type: createClassVarType(classType, errorNode, typeArgs, flags) };
13815
14058
  }
13816
14059
  case 'Protocol': {
13817
- return createSpecialType(classType, typeArgs,
13818
- /* paramLimit */ undefined,
13819
- /* allowParamSpec */ true);
14060
+ return {
14061
+ type: createSpecialType(classType, typeArgs,
14062
+ /* paramLimit */ undefined,
14063
+ /* allowParamSpec */ true),
14064
+ };
13820
14065
  }
13821
14066
  case 'Tuple': {
13822
- return createSpecialType(classType, typeArgs, /* paramLimit */ undefined);
14067
+ return { type: createSpecialType(classType, typeArgs, /* paramLimit */ undefined) };
13823
14068
  }
13824
14069
  case 'Union': {
13825
- return createUnionType(classType, errorNode, typeArgs, flags);
14070
+ return { type: createUnionType(classType, errorNode, typeArgs, flags) };
13826
14071
  }
13827
14072
  case 'Generic': {
13828
- return createGenericType(classType, errorNode, typeArgs, flags);
14073
+ return { type: createGenericType(classType, errorNode, typeArgs, flags) };
13829
14074
  }
13830
14075
  case 'Final': {
13831
- return createFinalType(classType, errorNode, typeArgs, flags);
14076
+ return { type: createFinalType(classType, errorNode, typeArgs, flags) };
13832
14077
  }
13833
14078
  case 'Annotated': {
13834
14079
  return createAnnotatedType(errorNode, typeArgs);
13835
14080
  }
13836
14081
  case 'Concatenate': {
13837
- return createConcatenateType(errorNode, classType, typeArgs);
14082
+ return { type: createConcatenateType(errorNode, classType, typeArgs) };
13838
14083
  }
13839
14084
  case 'TypeGuard':
13840
14085
  case 'StrictTypeGuard': {
13841
- return createTypeGuardType(errorNode, classType, typeArgs, flags);
14086
+ return { type: createTypeGuardType(errorNode, classType, typeArgs, flags) };
13842
14087
  }
13843
14088
  case 'Unpack': {
13844
- return createUnpackType(classType, errorNode, typeArgs, flags);
14089
+ return { type: createUnpackType(classType, errorNode, typeArgs, flags) };
13845
14090
  }
13846
14091
  case 'Required':
13847
14092
  case 'NotRequired': {
13848
14093
  return createRequiredType(classType, errorNode, aliasedName === 'Required', typeArgs, flags);
13849
14094
  }
13850
14095
  case 'Self': {
13851
- return createSelfType(classType, errorNode, typeArgs);
14096
+ return { type: createSelfType(classType, errorNode, typeArgs) };
13852
14097
  }
13853
14098
  case 'LiteralString': {
13854
- return createSpecialType(classType, typeArgs, 0);
14099
+ return { type: createSpecialType(classType, typeArgs, 0) };
13855
14100
  }
13856
14101
  }
13857
14102
  }
@@ -13866,7 +14111,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
13866
14111
  // PEP 484 says that type[Any] should be considered
13867
14112
  // equivalent to type.
13868
14113
  if (typeArgs.length === 1 && (0, types_1.isAnyOrUnknown)(typeArgs[0].type)) {
13869
- return classType;
14114
+ return { type: classType };
13870
14115
  }
13871
14116
  const typeClass = getTypingType(errorNode, 'Type');
13872
14117
  if (typeClass && (0, types_1.isInstantiableClass)(typeClass)) {
@@ -13876,16 +14121,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
13876
14121
  if ((0, types_1.isInstantiableClass)(typeType)) {
13877
14122
  typeType = (0, typeUtils_1.explodeGenericClass)(typeType);
13878
14123
  }
13879
- return typeType;
14124
+ return { type: typeType };
13880
14125
  }
13881
14126
  }
13882
14127
  // Handle "tuple" specially, since it needs to act like "Tuple"
13883
14128
  // in Python 3.9 and newer.
13884
14129
  if ((0, typeUtils_1.isTupleClass)(classType)) {
13885
- return createSpecialType(classType, typeArgs,
13886
- /* paramLimit */ undefined,
13887
- /* allowParamSpec */ undefined,
13888
- /* isCallable */ true);
14130
+ return {
14131
+ type: createSpecialType(classType, typeArgs,
14132
+ /* paramLimit */ undefined,
14133
+ /* allowParamSpec */ undefined,
14134
+ /* isCallable */ true),
14135
+ };
13889
14136
  }
13890
14137
  }
13891
14138
  let typeArgCount = typeArgs ? typeArgs.length : 0;
@@ -13894,7 +14141,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
13894
14141
  // If there are no type parameters or args, the class is already specialized.
13895
14142
  // No need to do any more work.
13896
14143
  if (typeParameters.length === 0 && typeArgCount === 0) {
13897
- return classType;
14144
+ return { type: classType };
13898
14145
  }
13899
14146
  const variadicTypeParamIndex = typeParameters.findIndex((param) => (0, types_1.isVariadicTypeVar)(param));
13900
14147
  if (typeArgs) {
@@ -13903,7 +14150,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
13903
14150
  if (firstNonDefaultParam >= 0) {
13904
14151
  minTypeArgCount = firstNonDefaultParam;
13905
14152
  }
13906
- if (typeArgCount > typeParameters.length) {
14153
+ // Classes that accept inlined type dict type args allow only one.
14154
+ if (typeArgs[0].inlinedTypeDict) {
14155
+ if (typeArgs.length > 1) {
14156
+ addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeArgsTooMany().format({
14157
+ name: classType.aliasName || classType.details.name,
14158
+ expected: 1,
14159
+ received: typeArgCount,
14160
+ }), typeArgs[1].node);
14161
+ }
14162
+ return { type: typeArgs[0].inlinedTypeDict };
14163
+ }
14164
+ else if (typeArgCount > typeParameters.length) {
13907
14165
  if (!types_1.ClassType.isPartiallyEvaluated(classType) && !types_1.ClassType.isTupleClass(classType)) {
13908
14166
  const fileInfo = AnalyzerNodeInfo.getFileInfo(errorNode);
13909
14167
  if (typeParameters.length === 0) {
@@ -13987,7 +14245,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
13987
14245
  if (typeArgs && index < typeArgs.length) {
13988
14246
  if (typeParam.details.isParamSpec) {
13989
14247
  const typeArg = typeArgs[index];
13990
- const functionType = types_1.FunctionType.createInstantiable(65536 /* ParamSpecValue */);
14248
+ const functionType = types_1.FunctionType.createSynthesizedInstance('', 65536 /* ParamSpecValue */);
13991
14249
  types_1.TypeBase.setSpecialForm(functionType);
13992
14250
  if ((0, typeUtils_1.isEllipsisType)(typeArg.type)) {
13993
14251
  types_1.FunctionType.addDefaultParameters(functionType);
@@ -14057,7 +14315,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
14057
14315
  const destType = typeParameters[index];
14058
14316
  const declaredVariance = destType.details.declaredVariance;
14059
14317
  if (!(0, typeUtils_1.isVarianceOfTypeArgumentCompatible)(adjustedTypeArgType, declaredVariance)) {
14060
- diag.addMessage(localize_1.Localizer.DiagnosticAddendum.varianceMismatch().format({
14318
+ diag.addMessage(localize_1.Localizer.DiagnosticAddendum.varianceMismatchForClass().format({
14061
14319
  typeVarName: printType(adjustedTypeArgType),
14062
14320
  className: classType.details.name,
14063
14321
  }));
@@ -14087,7 +14345,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
14087
14345
  typeArgTypes = typeArgs.map((t) => (0, typeUtils_1.convertToInstance)(t.type));
14088
14346
  }
14089
14347
  const specializedClass = types_1.ClassType.cloneForSpecialization(classType, typeArgTypes, typeArgs !== undefined);
14090
- return specializedClass;
14348
+ return { type: specializedClass };
14091
14349
  }
14092
14350
  function getTypeOfArgument(arg) {
14093
14351
  if (arg.typeResult) {
@@ -14173,7 +14431,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
14173
14431
  const scopeTypeHonorsCodeFlow = scopeType !== 1 /* Function */ && scopeType !== 0 /* ListComprehension */;
14174
14432
  if (symbolWithScope && honorCodeFlow && scopeTypeHonorsCodeFlow) {
14175
14433
  // Filter the declarations based on flow reachability.
14176
- const reachableDecls = symbolWithScope.symbol.getDeclarations().filter((decl) => {
14434
+ const reachableDecl = symbolWithScope.symbol.getDeclarations().find((decl) => {
14177
14435
  if (decl.type !== 8 /* Alias */ && decl.type !== 0 /* Intrinsic */) {
14178
14436
  // Is the declaration in the same execution scope as the "usageNode" node?
14179
14437
  const usageScope = ParseTreeUtils.getExecutionScopeNode(node);
@@ -14201,7 +14459,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
14201
14459
  });
14202
14460
  // If none of the declarations are reachable from the current node,
14203
14461
  // search for the symbol in outer scopes.
14204
- if (reachableDecls.length === 0) {
14462
+ if (!reachableDecl) {
14205
14463
  if (symbolWithScope.scope.type !== 1 /* Function */) {
14206
14464
  let nextScopeToSearch = symbolWithScope.scope.parent;
14207
14465
  const isOutsideCallerModule = symbolWithScope.isOutsideCallerModule || symbolWithScope.scope.type === 3 /* Module */;
@@ -14508,59 +14766,63 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
14508
14766
  return declarations;
14509
14767
  }
14510
14768
  function getTypeForDeclaration(declaration) {
14511
- var _a, _b;
14769
+ var _a, _b, _c;
14512
14770
  switch (declaration.type) {
14513
14771
  case 0 /* Intrinsic */: {
14514
14772
  if (declaration.intrinsicType === 'Any') {
14515
- return types_1.AnyType.create();
14773
+ return { type: types_1.AnyType.create() };
14516
14774
  }
14517
14775
  if (declaration.intrinsicType === 'class') {
14518
14776
  const classNode = ParseTreeUtils.getEnclosingClass(declaration.node);
14519
14777
  const classTypeInfo = getTypeOfClass(classNode);
14520
- return classTypeInfo === null || classTypeInfo === void 0 ? void 0 : classTypeInfo.classType;
14778
+ return { type: classTypeInfo === null || classTypeInfo === void 0 ? void 0 : classTypeInfo.classType };
14521
14779
  }
14522
14780
  const strType = getBuiltInObject(declaration.node, 'str');
14523
14781
  const intType = getBuiltInObject(declaration.node, 'int');
14524
14782
  if ((0, types_1.isClassInstance)(intType) && (0, types_1.isClassInstance)(strType)) {
14525
14783
  if (declaration.intrinsicType === 'str') {
14526
- return strType;
14784
+ return { type: strType };
14527
14785
  }
14528
14786
  if (declaration.intrinsicType === 'str | None') {
14529
- return (0, types_1.combineTypes)([strType, types_1.NoneType.createInstance()]);
14787
+ return { type: (0, types_1.combineTypes)([strType, types_1.NoneType.createInstance()]) };
14530
14788
  }
14531
14789
  if (declaration.intrinsicType === 'int') {
14532
- return intType;
14790
+ return { type: intType };
14533
14791
  }
14534
14792
  if (declaration.intrinsicType === 'Iterable[str]') {
14535
14793
  const iterableType = getBuiltInType(declaration.node, 'Iterable');
14536
14794
  if ((0, types_1.isInstantiableClass)(iterableType)) {
14537
- return types_1.ClassType.cloneAsInstance(types_1.ClassType.cloneForSpecialization(iterableType, [strType],
14538
- /* isTypeArgumentExplicit */ true));
14795
+ return {
14796
+ type: types_1.ClassType.cloneAsInstance(types_1.ClassType.cloneForSpecialization(iterableType, [strType],
14797
+ /* isTypeArgumentExplicit */ true)),
14798
+ };
14539
14799
  }
14540
14800
  }
14541
14801
  if (declaration.intrinsicType === 'Dict[str, Any]') {
14542
14802
  const dictType = getBuiltInType(declaration.node, 'dict');
14543
14803
  if ((0, types_1.isInstantiableClass)(dictType)) {
14544
- return types_1.ClassType.cloneAsInstance(types_1.ClassType.cloneForSpecialization(dictType, [strType, types_1.AnyType.create()],
14545
- /* isTypeArgumentExplicit */ true));
14804
+ return {
14805
+ type: types_1.ClassType.cloneAsInstance(types_1.ClassType.cloneForSpecialization(dictType, [strType, types_1.AnyType.create()],
14806
+ /* isTypeArgumentExplicit */ true)),
14807
+ };
14546
14808
  }
14547
14809
  }
14548
14810
  }
14549
- return types_1.UnknownType.create();
14811
+ return { type: types_1.UnknownType.create() };
14550
14812
  }
14551
14813
  case 6 /* Class */: {
14552
14814
  const classTypeInfo = getTypeOfClass(declaration.node);
14553
- return classTypeInfo === null || classTypeInfo === void 0 ? void 0 : classTypeInfo.decoratedType;
14815
+ return { type: classTypeInfo === null || classTypeInfo === void 0 ? void 0 : classTypeInfo.decoratedType };
14554
14816
  }
14555
14817
  case 7 /* SpecialBuiltInClass */: {
14556
- return getTypeOfAnnotation(declaration.node.typeAnnotation);
14818
+ return { type: getTypeOfAnnotation(declaration.node.typeAnnotation) };
14557
14819
  }
14558
14820
  case 5 /* Function */: {
14559
14821
  const functionTypeInfo = getTypeOfFunction(declaration.node);
14560
- return functionTypeInfo === null || functionTypeInfo === void 0 ? void 0 : functionTypeInfo.decoratedType;
14822
+ return { type: functionTypeInfo === null || functionTypeInfo === void 0 ? void 0 : functionTypeInfo.decoratedType };
14561
14823
  }
14562
14824
  case 4 /* TypeAlias */: {
14563
- return getTypeOfTypeAlias(declaration.node);
14825
+ return { type: getTypeOfTypeAlias(declaration.node) };
14564
14826
  }
14565
14827
  case 2 /* Parameter */: {
14566
14828
  let typeAnnotationNode = declaration.node.typeAnnotation || declaration.node.typeAnnotationComment;
@@ -14579,19 +14841,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
14579
14841
  }
14580
14842
  if (typeAnnotationNode) {
14581
14843
  const declaredType = getTypeOfParameterAnnotation(typeAnnotationNode, declaration.node.category);
14582
- return transformVariadicParamType(declaration.node, declaration.node.category, adjustParameterAnnotatedType(declaration.node, declaredType));
14844
+ return {
14845
+ type: transformVariadicParamType(declaration.node, declaration.node.category, adjustParameterAnnotatedType(declaration.node, declaredType)),
14846
+ };
14583
14847
  }
14584
- return undefined;
14848
+ return { type: undefined };
14585
14849
  }
14586
14850
  case 3 /* TypeParameter */: {
14587
- return getTypeOfTypeParameter(declaration.node);
14851
+ return { type: getTypeOfTypeParameter(declaration.node) };
14588
14852
  }
14589
14853
  case 1 /* Variable */: {
14590
14854
  const typeAnnotationNode = declaration.typeAnnotationNode;
14591
14855
  if (typeAnnotationNode) {
14592
- const typeAliasNode = isDeclaredTypeAlias(typeAnnotationNode)
14593
- ? ParseTreeUtils.getTypeAnnotationNode(typeAnnotationNode)
14594
- : undefined;
14595
14856
  let declaredType;
14596
14857
  if (declaration.isRuntimeTypeExpression) {
14597
14858
  declaredType = (0, typeUtils_1.convertToInstance)(getTypeOfExpressionExpectingType(typeAnnotationNode, {
@@ -14608,24 +14869,25 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
14608
14869
  isVariableAnnotation: true,
14609
14870
  allowClassVar: ParseTreeUtils.isClassVarAllowedForAssignmentTarget(declNode),
14610
14871
  allowFinal: ParseTreeUtils.isFinalAllowedForAssignmentTarget(declNode),
14872
+ allowRequired: ParseTreeUtils.isRequiredAllowedForAssignmentTarget(declNode),
14611
14873
  });
14612
14874
  }
14613
14875
  if (declaredType) {
14614
14876
  // Apply enum transform if appropriate.
14615
14877
  if (declaration.node.nodeType === 38 /* Name */) {
14616
14878
  declaredType =
14617
- (0, enums_1.transformTypeForPossibleEnumClass)(evaluatorInterface, declaration.node, () => declaredType) || declaredType;
14879
+ (_c = (0, enums_1.transformTypeForPossibleEnumClass)(evaluatorInterface, declaration.node, () => declaredType)) !== null && _c !== void 0 ? _c : declaredType;
14618
14880
  }
14619
- if (typeAliasNode && typeAliasNode.valueExpression.nodeType === 38 /* Name */) {
14620
- declaredType = transformTypeForTypeAlias(declaredType, typeAliasNode.valueExpression, declaration.node);
14881
+ if ((0, types_1.isClassInstance)(declaredType) && types_1.ClassType.isBuiltIn(declaredType, 'TypeAlias')) {
14882
+ return { type: undefined, isTypeAlias: true };
14621
14883
  }
14622
- return declaredType;
14884
+ return { type: declaredType };
14623
14885
  }
14624
14886
  }
14625
- return undefined;
14887
+ return { type: undefined };
14626
14888
  }
14627
14889
  case 8 /* Alias */: {
14628
- return undefined;
14890
+ return { type: undefined };
14629
14891
  }
14630
14892
  }
14631
14893
  }
@@ -14723,7 +14985,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
14723
14985
  return typeVar;
14724
14986
  }
14725
14987
  function getInferredTypeOfDeclaration(symbol, decl) {
14726
- var _a, _b;
14988
+ var _a, _b, _c;
14727
14989
  const resolvedDecl = resolveAliasDeclaration(decl,
14728
14990
  /* resolveLocalNames */ true,
14729
14991
  /* allowExternallyHiddenAccess */ AnalyzerNodeInfo.getFileInfo(decl.node).isStubFile);
@@ -14739,7 +15001,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
14739
15001
  moduleType.fields = lookupResults.symbolTable;
14740
15002
  moduleType.docString = lookupResults.docString;
14741
15003
  }
14742
- else {
15004
+ else if (!loaderActions.implicitImports) {
14743
15005
  return evaluatorOptions.evaluateUnknownImportsAsAny ? types_1.AnyType.create() : types_1.UnknownType.create();
14744
15006
  }
14745
15007
  }
@@ -14794,8 +15056,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
14794
15056
  }
14795
15057
  }
14796
15058
  const declaredType = getTypeForDeclaration(resolvedDecl);
14797
- if (declaredType) {
14798
- return declaredType;
15059
+ if (declaredType.type) {
15060
+ return declaredType.type;
14799
15061
  }
14800
15062
  // If this is part of a "py.typed" package, don't fall back on type inference
14801
15063
  // unless it's marked Final, is a constant, or is a declared type alias.
@@ -14815,28 +15077,45 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
14815
15077
  isUnambiguousType = true;
14816
15078
  }
14817
15079
  }
14818
- if (resolvedDecl.isFinal || resolvedDecl.isConstant) {
15080
+ // Special-case constants, which are treated as unambiguous.
15081
+ if (isFinalVariableDeclaration(resolvedDecl) || resolvedDecl.isConstant) {
14819
15082
  isUnambiguousType = true;
14820
15083
  }
15084
+ // Special-case calls to certain built-in type functions.
15085
+ if (((_a = resolvedDecl.inferredTypeSource) === null || _a === void 0 ? void 0 : _a.nodeType) === 9 /* Call */) {
15086
+ const baseTypeResult = getTypeOfExpression(resolvedDecl.inferredTypeSource.leftExpression, 2 /* DoNotSpecialize */);
15087
+ const callType = baseTypeResult.type;
15088
+ if ((0, types_1.isInstantiableClass)(callType) &&
15089
+ types_1.ClassType.isBuiltIn(callType, [
15090
+ 'TypeVar',
15091
+ 'ParamSpec',
15092
+ 'TypeVarTuple',
15093
+ 'TypedDict',
15094
+ 'NamedTuple',
15095
+ 'NewType',
15096
+ ])) {
15097
+ isUnambiguousType = true;
15098
+ }
15099
+ }
14821
15100
  }
14822
15101
  }
14823
15102
  // If the resolved declaration had no defined type, use the
14824
15103
  // inferred type for this node.
14825
15104
  if (resolvedDecl.type === 2 /* Parameter */) {
14826
- return (_a = evaluateTypeForSubnode(resolvedDecl.node.name, () => {
15105
+ return (_b = evaluateTypeForSubnode(resolvedDecl.node.name, () => {
14827
15106
  evaluateTypeOfParameter(resolvedDecl.node);
14828
- })) === null || _a === void 0 ? void 0 : _a.type;
15107
+ })) === null || _b === void 0 ? void 0 : _b.type;
14829
15108
  }
14830
15109
  if (resolvedDecl.type === 1 /* Variable */ && resolvedDecl.inferredTypeSource) {
14831
- const isTypeAlias = (0, declarationUtils_1.isExplicitTypeAliasDeclaration)(resolvedDecl) || isPossibleTypeAliasOrTypedDict(resolvedDecl);
15110
+ const isTypeAlias = isExplicitTypeAliasDeclaration(resolvedDecl) || isPossibleTypeAliasOrTypedDict(resolvedDecl);
14832
15111
  // If this is a type alias, evaluate types for the entire assignment
14833
15112
  // statement rather than just the RHS of the assignment.
14834
15113
  const typeSource = isTypeAlias && resolvedDecl.inferredTypeSource.parent
14835
15114
  ? resolvedDecl.inferredTypeSource.parent
14836
15115
  : resolvedDecl.inferredTypeSource;
14837
- let inferredType = (_b = evaluateTypeForSubnode(resolvedDecl.node, () => {
15116
+ let inferredType = (_c = evaluateTypeForSubnode(resolvedDecl.node, () => {
14838
15117
  evaluateTypesForStatement(typeSource);
14839
- })) === null || _b === void 0 ? void 0 : _b.type;
15118
+ })) === null || _c === void 0 ? void 0 : _c.type;
14840
15119
  if (inferredType && resolvedDecl.node.nodeType === 38 /* Name */) {
14841
15120
  // See if this is an enum member. If so, we need to handle it as a special case.
14842
15121
  const enumMemberType = (0, enums_1.transformTypeForPossibleEnumClass)(evaluatorInterface, resolvedDecl.node, () => {
@@ -14950,28 +15229,34 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
14950
15229
  }
14951
15230
  function getEffectiveTypeOfSymbolForUsage(symbol, usageNode, useLastDecl = false) {
14952
15231
  var _a, _b;
15232
+ let declaredTypeInfo;
14953
15233
  // If there's a declared type, it takes precedence over inferred types.
14954
15234
  if (symbol.hasTypedDeclarations()) {
14955
- const declaredType = getDeclaredTypeOfSymbol(symbol, usageNode);
14956
- const typedDecls = symbol.getTypedDeclarations();
14957
- let isIncomplete = false;
14958
- if (declaredType) {
14959
- if ((0, types_1.isFunction)(declaredType) && types_1.FunctionType.isPartiallyEvaluated(declaredType)) {
14960
- isIncomplete = true;
14961
- }
14962
- else if ((0, types_1.isClass)(declaredType) && types_1.ClassType.isPartiallyEvaluated(declaredType)) {
14963
- isIncomplete = true;
15235
+ declaredTypeInfo = getDeclaredTypeOfSymbol(symbol, usageNode);
15236
+ const declaredType = declaredTypeInfo === null || declaredTypeInfo === void 0 ? void 0 : declaredTypeInfo.type;
15237
+ const hasMetadata = !!declaredTypeInfo.isTypeAlias;
15238
+ if (declaredType || !hasMetadata) {
15239
+ let isIncomplete = false;
15240
+ if (declaredType) {
15241
+ if ((0, types_1.isFunction)(declaredType) && types_1.FunctionType.isPartiallyEvaluated(declaredType)) {
15242
+ isIncomplete = true;
15243
+ }
15244
+ else if ((0, types_1.isClass)(declaredType) && types_1.ClassType.isPartiallyEvaluated(declaredType)) {
15245
+ isIncomplete = true;
15246
+ }
14964
15247
  }
15248
+ const typedDecls = symbol.getTypedDeclarations();
15249
+ const result = {
15250
+ type: declaredType !== null && declaredType !== void 0 ? declaredType : types_1.UnknownType.create(),
15251
+ isIncomplete,
15252
+ includesVariableDecl: typedDecls.some((decl) => decl.type === 1 /* Variable */),
15253
+ includesIllegalTypeAliasDecl: !typedDecls.every((decl) => isPossibleTypeAliasDeclaration(decl)),
15254
+ isRecursiveDefinition: !declaredType,
15255
+ };
15256
+ return result;
14965
15257
  }
14966
- return {
14967
- type: declaredType !== null && declaredType !== void 0 ? declaredType : types_1.UnknownType.create(),
14968
- isIncomplete,
14969
- includesVariableDecl: typedDecls.some((decl) => decl.type === 1 /* Variable */),
14970
- includesIllegalTypeAliasDecl: !typedDecls.every((decl) => (0, declarationUtils_1.isPossibleTypeAliasDeclaration)(decl)),
14971
- isRecursiveDefinition: !declaredType,
14972
- };
14973
15258
  }
14974
- // Look in the cache to see if we've computed this already.
15259
+ // Look in the inferred type cache to see if we've computed this already.
14975
15260
  let cacheEntries = effectiveTypeCache.get(symbol.id);
14976
15261
  const usageNodeId = usageNode ? usageNode.id : undefined;
14977
15262
  const effectiveTypeCacheKey = `${usageNodeId === undefined ? '.' : usageNodeId.toString()}${useLastDecl ? '*' : ''}`;
@@ -14993,13 +15278,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
14993
15278
  let declIndexToConsider;
14994
15279
  // Limit the number of declarations to explore.
14995
15280
  if (decls.length > maxDeclarationsToUseForInference) {
14996
- return {
15281
+ const result = {
14997
15282
  type: types_1.UnknownType.create(),
14998
15283
  isIncomplete: false,
14999
15284
  includesVariableDecl: false,
15000
- includesIllegalTypeAliasDecl: !decls.every((decl) => (0, declarationUtils_1.isPossibleTypeAliasDeclaration)(decl)),
15285
+ includesIllegalTypeAliasDecl: !decls.every((decl) => isPossibleTypeAliasDeclaration(decl)),
15001
15286
  isRecursiveDefinition: false,
15002
15287
  };
15288
+ addToEffectiveTypeCache(result);
15289
+ return result;
15003
15290
  }
15004
15291
  // If the caller has requested that we use only the last decl, we
15005
15292
  // will use only the last one, but we'll ignore decls that are in
@@ -15049,7 +15336,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
15049
15336
  const resolvedDecl = (_a = resolveAliasDeclaration(decl,
15050
15337
  /* resolveLocalNames */ true,
15051
15338
  /* allowExternallyHiddenAccess */ AnalyzerNodeInfo.getFileInfo(decl.node).isStubFile)) !== null && _a !== void 0 ? _a : decl;
15052
- const isExplicitTypeAlias = (0, declarationUtils_1.isExplicitTypeAliasDeclaration)(decl);
15339
+ const isExplicitTypeAlias = isExplicitTypeAliasDeclaration(decl);
15053
15340
  const isTypeAlias = isExplicitTypeAlias || isPossibleTypeAliasOrTypedDict(decl);
15054
15341
  if (isExplicitTypeAlias) {
15055
15342
  sawExplicitTypeAlias = true;
@@ -15060,14 +15347,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
15060
15347
  resolvedDecl.type === 1 /* Variable */ &&
15061
15348
  ((_c = (_b = resolvedDecl.inferredTypeSource) === null || _b === void 0 ? void 0 : _b.parent) === null || _c === void 0 ? void 0 : _c.nodeType) === 3 /* Assignment */) {
15062
15349
  evaluateTypesForAssignmentStatement(resolvedDecl.inferredTypeSource.parent);
15063
- if (resolvedDecl.typeAliasAnnotation) {
15064
- // Mark "TypeAlias" declaration as accessed.
15065
- getTypeOfAnnotation(resolvedDecl.typeAliasAnnotation, {
15066
- isVariableAnnotation: true,
15067
- allowFinal: ParseTreeUtils.isFinalAllowedForAssignmentTarget(resolvedDecl.node),
15068
- allowClassVar: ParseTreeUtils.isClassVarAllowedForAssignmentTarget(resolvedDecl.node),
15069
- });
15070
- }
15071
15350
  }
15072
15351
  if (pushSymbolResolution(symbol, decl)) {
15073
15352
  try {
@@ -15085,7 +15364,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
15085
15364
  }
15086
15365
  let isConstant = false;
15087
15366
  if (resolvedDecl.type === 1 /* Variable */) {
15088
- if (resolvedDecl.isConstant || resolvedDecl.isFinal) {
15367
+ if (resolvedDecl.isConstant || isFinalVariableDeclaration(resolvedDecl)) {
15089
15368
  isConstant = true;
15090
15369
  }
15091
15370
  }
@@ -15142,17 +15421,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
15142
15421
  type: (0, types_1.combineTypes)(typesToCombine),
15143
15422
  isIncomplete,
15144
15423
  includesVariableDecl,
15145
- includesIllegalTypeAliasDecl: !decls.every((decl) => (0, declarationUtils_1.isPossibleTypeAliasDeclaration)(decl)),
15424
+ includesIllegalTypeAliasDecl: !decls.every((decl) => isPossibleTypeAliasDeclaration(decl)),
15146
15425
  isRecursiveDefinition: false,
15147
15426
  evaluationAttempts,
15148
15427
  };
15149
15428
  if (!includesSpeculativeResult) {
15150
- // Add the entry to the cache so we don't need to compute it next time.
15151
- if (!cacheEntries) {
15152
- cacheEntries = new Map();
15153
- effectiveTypeCache.set(symbol.id, cacheEntries);
15154
- }
15155
- cacheEntries.set(effectiveTypeCacheKey, result);
15429
+ addToEffectiveTypeCache(result);
15156
15430
  }
15157
15431
  return result;
15158
15432
  }
@@ -15160,19 +15434,33 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
15160
15434
  type: types_1.UnboundType.create(),
15161
15435
  isIncomplete,
15162
15436
  includesVariableDecl,
15163
- includesIllegalTypeAliasDecl: !decls.every((decl) => (0, declarationUtils_1.isPossibleTypeAliasDeclaration)(decl)),
15437
+ includesIllegalTypeAliasDecl: !decls.every((decl) => isPossibleTypeAliasDeclaration(decl)),
15164
15438
  isRecursiveDefinition: false,
15165
15439
  };
15440
+ function addToEffectiveTypeCache(result) {
15441
+ // Add the entry to the cache so we don't need to compute it next time.
15442
+ if (!cacheEntries) {
15443
+ cacheEntries = new Map();
15444
+ effectiveTypeCache.set(symbol.id, cacheEntries);
15445
+ }
15446
+ cacheEntries.set(effectiveTypeCacheKey, result);
15447
+ }
15166
15448
  }
15449
+ // If a declaration has an explicit type (e.g. a variable with an annotation),
15450
+ // this function evaluates the type and returns it. If the symbol has no
15451
+ // explicit declared type, its type will need to be inferred instead. In some
15452
+ // cases, non-type information (such as Final or ClassVar attributes) may be
15453
+ // provided, but type inference is still required. In such cases, the attributes
15454
+ // are returned as flags.
15167
15455
  function getDeclaredTypeOfSymbol(symbol, usageNode) {
15168
15456
  const synthesizedType = symbol.getSynthesizedType();
15169
15457
  if (synthesizedType) {
15170
- return synthesizedType;
15458
+ return { type: synthesizedType };
15171
15459
  }
15172
15460
  let typedDecls = symbol.getTypedDeclarations();
15173
15461
  if (typedDecls.length === 0) {
15174
15462
  // There was no declaration with a defined type.
15175
- return undefined;
15463
+ return { type: undefined };
15176
15464
  }
15177
15465
  // If there is more than one typed decl, filter out any that are not
15178
15466
  // reachable from the usage node (if specified). This can happen in
@@ -15193,7 +15481,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
15193
15481
  return true;
15194
15482
  });
15195
15483
  if (filteredTypedDecls.length === 0) {
15196
- return types_1.UnboundType.create();
15484
+ return { type: types_1.UnboundType.create() };
15197
15485
  }
15198
15486
  typedDecls = filteredTypedDecls;
15199
15487
  }
@@ -15209,12 +15497,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
15209
15497
  // for recursive symbol resolution, return it as the resolved type.
15210
15498
  const partialType = getSymbolResolutionPartialType(symbol, decl);
15211
15499
  if (partialType) {
15212
- return partialType;
15500
+ return { type: partialType };
15213
15501
  }
15214
15502
  if (getIndexOfSymbolResolution(symbol, decl) < 0) {
15215
15503
  if (pushSymbolResolution(symbol, decl)) {
15216
15504
  try {
15217
- const type = getTypeForDeclaration(decl);
15505
+ const declaredTypeInfo = getTypeForDeclaration(decl);
15218
15506
  // If there was recursion detected, don't use this declaration.
15219
15507
  // The exception is it's a class declaration because getTypeOfClass
15220
15508
  // handles recursion by populating a partially-created class type
@@ -15222,7 +15510,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
15222
15510
  // circular dependency between the "type" and "object" classes in
15223
15511
  // builtins.pyi (since "object" is a "type" and "type" is an "object").
15224
15512
  if (popSymbolResolution(symbol) || decl.type === 6 /* Class */) {
15225
- return type;
15513
+ return declaredTypeInfo;
15226
15514
  }
15227
15515
  }
15228
15516
  catch (e) {
@@ -15234,7 +15522,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
15234
15522
  }
15235
15523
  declIndex--;
15236
15524
  }
15237
- return undefined;
15525
+ return { type: undefined };
15238
15526
  }
15239
15527
  function inferReturnTypeIfNecessary(type) {
15240
15528
  if ((0, types_1.isFunction)(type)) {
@@ -15595,18 +15883,22 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
15595
15883
  return false;
15596
15884
  }
15597
15885
  // This function is used to validate or infer the variance of type
15598
- // parameters within a class.
15599
- function assignClassToSelf(destType, srcType, recursionCount = 0) {
15886
+ // parameters within a class. If ignoreBaseClassVariance is set to false,
15887
+ // the type parameters for the base class are honored. This is useful for
15888
+ // variance inference (PEP 695). For validation of protocol variance, we
15889
+ // want to ignore the variance for all base classes in the class hierarchy.
15890
+ function assignClassToSelf(destType, srcType, ignoreBaseClassVariance = true, recursionCount = 0) {
15600
15891
  (0, debug_1.assert)(types_1.ClassType.isSameGenericClass(destType, srcType));
15601
15892
  (0, debug_1.assert)(destType.details.typeParameters.length > 0);
15602
15893
  const diag = new diagnostic_1.DiagnosticAddendum();
15603
15894
  const typeVarContext = new typeVarContext_1.TypeVarContext();
15604
15895
  let isAssignable = true;
15605
15896
  destType.details.fields.forEach((symbol, name) => {
15897
+ var _a;
15606
15898
  if (isAssignable && symbol.isClassMember() && !symbol.isIgnoredForProtocolMatch()) {
15607
15899
  const memberInfo = (0, typeUtils_1.lookUpClassMember)(srcType, name);
15608
15900
  (0, debug_1.assert)(memberInfo !== undefined);
15609
- let destMemberType = getDeclaredTypeOfSymbol(symbol);
15901
+ let destMemberType = (_a = getDeclaredTypeOfSymbol(symbol)) === null || _a === void 0 ? void 0 : _a.type;
15610
15902
  if (destMemberType) {
15611
15903
  const srcMemberType = getTypeOfMember(memberInfo);
15612
15904
  destMemberType = (0, typeUtils_1.partiallySpecializeType)(destMemberType, destType);
@@ -15624,7 +15916,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
15624
15916
  const primaryDecl = symbol.getDeclarations()[0];
15625
15917
  // Class and instance variables that are mutable need to
15626
15918
  // enforce invariance.
15627
- const flags = (primaryDecl === null || primaryDecl === void 0 ? void 0 : primaryDecl.type) === 1 /* Variable */ && !primaryDecl.isFinal
15919
+ const flags = (primaryDecl === null || primaryDecl === void 0 ? void 0 : primaryDecl.type) === 1 /* Variable */ && !isFinalVariableDeclaration(primaryDecl)
15628
15920
  ? 1 /* EnforceInvariance */
15629
15921
  : 0 /* Default */;
15630
15922
  if (!assignType(destMemberType, srcMemberType, diag, typeVarContext,
@@ -15644,7 +15936,31 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
15644
15936
  baseClass.details.typeParameters.length > 0) {
15645
15937
  const specializedDestBaseClass = (0, typeUtils_1.specializeForBaseClass)(destType, baseClass);
15646
15938
  const specializedSrcBaseClass = (0, typeUtils_1.specializeForBaseClass)(srcType, baseClass);
15647
- if (!assignClassToSelf(specializedDestBaseClass, specializedSrcBaseClass, recursionCount)) {
15939
+ if (!ignoreBaseClassVariance) {
15940
+ specializedDestBaseClass.details.typeParameters.forEach((param, index) => {
15941
+ if (!param.details.isParamSpec &&
15942
+ !param.details.isVariadic &&
15943
+ !param.details.isSynthesized &&
15944
+ specializedSrcBaseClass.typeArguments &&
15945
+ index < specializedSrcBaseClass.typeArguments.length &&
15946
+ specializedDestBaseClass.typeArguments &&
15947
+ index < specializedDestBaseClass.typeArguments.length) {
15948
+ const paramVariance = param.details.declaredVariance;
15949
+ if ((0, types_1.isTypeVar)(specializedSrcBaseClass.typeArguments[index])) {
15950
+ if (paramVariance === 2 /* Invariant */ || paramVariance === 4 /* Contravariant */) {
15951
+ isAssignable = false;
15952
+ }
15953
+ }
15954
+ else if ((0, types_1.isTypeVar)(specializedDestBaseClass.typeArguments[index])) {
15955
+ if (paramVariance === 2 /* Invariant */ || paramVariance === 3 /* Covariant */) {
15956
+ isAssignable = false;
15957
+ }
15958
+ }
15959
+ }
15960
+ });
15961
+ }
15962
+ if (isAssignable &&
15963
+ !assignClassToSelf(specializedDestBaseClass, specializedSrcBaseClass, ignoreBaseClassVariance, recursionCount)) {
15648
15964
  isAssignable = false;
15649
15965
  }
15650
15966
  }
@@ -15817,12 +16133,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
15817
16133
  return true;
15818
16134
  }
15819
16135
  function getGetterTypeFromProperty(propertyClass, inferTypeIfNeeded) {
16136
+ var _a;
15820
16137
  if (!types_1.ClassType.isPropertyClass(propertyClass)) {
15821
16138
  return undefined;
15822
16139
  }
15823
16140
  const fgetSymbol = propertyClass.details.fields.get('fget');
15824
16141
  if (fgetSymbol) {
15825
- const fgetType = getDeclaredTypeOfSymbol(fgetSymbol);
16142
+ const fgetType = (_a = getDeclaredTypeOfSymbol(fgetSymbol)) === null || _a === void 0 ? void 0 : _a.type;
15826
16143
  if (fgetType && (0, types_1.isFunction)(fgetType)) {
15827
16144
  return getFunctionEffectiveReturnType(fgetType, /* args */ undefined, inferTypeIfNeeded);
15828
16145
  }
@@ -16542,6 +16859,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
16542
16859
  });
16543
16860
  }
16544
16861
  else if (remainingSrcSubtypes.length === 0) {
16862
+ if ((flags & 1024 /* PopulatingExpectedType */) !== 0) {
16863
+ // If we're populating an expected type, try not to leave
16864
+ // any TypeVars unsolved. Assign the full type to the remaining
16865
+ // dest TypeVars.
16866
+ remainingDestSubtypes.forEach((destSubtype) => {
16867
+ assignType(destSubtype, srcType,
16868
+ /* diag */ undefined, destTypeVarContext, srcTypeVarContext, flags, recursionCount);
16869
+ });
16870
+ }
16545
16871
  // If we've assigned all of the source subtypes but one or more dest
16546
16872
  // TypeVars have gone unmatched, treat this as success.
16547
16873
  return true;
@@ -17544,14 +17870,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
17544
17870
  // When a value is assigned to a variable with a declared type,
17545
17871
  // we may be able to narrow the type based on the assignment.
17546
17872
  function narrowTypeBasedOnAssignment(declaredType, assignedType) {
17547
- const diag = new diagnostic_1.DiagnosticAddendum();
17548
17873
  const narrowedType = (0, typeUtils_1.mapSubtypes)(assignedType, (assignedSubtype) => {
17549
17874
  const narrowedSubtype = (0, typeUtils_1.mapSubtypes)(declaredType, (declaredSubtype) => {
17550
17875
  // We can't narrow "Any".
17551
17876
  if ((0, types_1.isAnyOrUnknown)(declaredType)) {
17552
17877
  return declaredType;
17553
17878
  }
17554
- if (assignType(declaredSubtype, assignedSubtype, diag)) {
17879
+ if (assignType(declaredSubtype, assignedSubtype)) {
17555
17880
  // If the source is generic and has unspecified type arguments,
17556
17881
  // see if we can determine then based on the declared type.
17557
17882
  if ((0, types_1.isInstantiableClass)(declaredSubtype) && (0, types_1.isInstantiableClass)(assignedSubtype)) {
@@ -18035,7 +18360,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
18035
18360
  }
18036
18361
  else if ((0, types_1.isOverloadedFunction)(memberType)) {
18037
18362
  const newOverloadType = types_1.OverloadedFunctionType.create([]);
18038
- memberType.overloads.forEach((overload) => {
18363
+ // Don't bother binding the implementation.
18364
+ types_1.OverloadedFunctionType.getOverloads(memberType).forEach((overload) => {
18039
18365
  const boundMethod = bindFunctionToClassOrObject(baseType, overload, memberClass,
18040
18366
  /* errorNode */ undefined, recursionCount, treatConstructorAsClassMember, firstParamType);
18041
18367
  if (boundMethod) {
@@ -18072,7 +18398,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
18072
18398
  const memberTypeFirstParamType = types_1.FunctionType.getEffectiveParameterType(memberType, 0);
18073
18399
  // Fill out the typeVarContext for the "self" or "cls" parameter.
18074
18400
  typeVarContext.addSolveForScope((0, typeUtils_1.getTypeVarScopeId)(memberType));
18075
- const diag = new diagnostic_1.DiagnosticAddendum();
18401
+ const diag = errorNode ? new diagnostic_1.DiagnosticAddendum() : undefined;
18076
18402
  if ((0, types_1.isTypeVar)(memberTypeFirstParamType) &&
18077
18403
  memberTypeFirstParamType.details.boundType &&
18078
18404
  (0, types_1.isClassInstance)(memberTypeFirstParamType.details.boundType) &&
@@ -18098,7 +18424,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
18098
18424
  type: printType(baseType),
18099
18425
  methodName: methodName,
18100
18426
  paramName: memberTypeFirstParam.name,
18101
- }) + diag.getString(), errorNode);
18427
+ }) + (diag === null || diag === void 0 ? void 0 : diag.getString()), errorNode);
18102
18428
  }
18103
18429
  else {
18104
18430
  // If there was no errorNode, we couldn't report the error,
@@ -18115,6 +18441,60 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
18115
18441
  const specializedFunction = (0, typeUtils_1.applySolvedTypeVars)(memberType, typeVarContext);
18116
18442
  return types_1.FunctionType.clone(specializedFunction, stripFirstParam, baseType, (0, typeUtils_1.getTypeVarScopeId)(baseType));
18117
18443
  }
18444
+ function isFinalVariable(symbol) {
18445
+ return symbol.getDeclarations().some((decl) => isFinalVariableDeclaration(decl));
18446
+ }
18447
+ function isFinalVariableDeclaration(decl) {
18448
+ return decl.type === 1 /* Variable */ && !!decl.isFinal;
18449
+ }
18450
+ function isExplicitTypeAliasDeclaration(decl) {
18451
+ if (decl.type !== 1 /* Variable */ || !decl.typeAnnotationNode) {
18452
+ return false;
18453
+ }
18454
+ if (decl.typeAnnotationNode.nodeType !== 38 /* Name */ &&
18455
+ decl.typeAnnotationNode.nodeType !== 35 /* MemberAccess */ &&
18456
+ decl.typeAnnotationNode.nodeType !== 48 /* StringList */) {
18457
+ return false;
18458
+ }
18459
+ const type = getTypeOfAnnotation(decl.typeAnnotationNode, { isVariableAnnotation: true, allowClassVar: true });
18460
+ return (0, types_1.isClassInstance)(type) && types_1.ClassType.isBuiltIn(type, 'TypeAlias');
18461
+ }
18462
+ function isPossibleTypeAliasDeclaration(decl) {
18463
+ var _a;
18464
+ if (decl.type !== 1 /* Variable */ || !decl.typeAliasName || decl.typeAnnotationNode) {
18465
+ return false;
18466
+ }
18467
+ if (((_a = decl.node.parent) === null || _a === void 0 ? void 0 : _a.nodeType) !== 3 /* Assignment */) {
18468
+ return false;
18469
+ }
18470
+ // Perform a sanity check on the RHS expression. Some expression
18471
+ // forms should never be considered legitimate for type aliases.
18472
+ return isLegalTypeAliasExpressionForm(decl.node.parent.rightExpression);
18473
+ }
18474
+ function isLegalTypeAliasExpressionForm(node) {
18475
+ switch (node.nodeType) {
18476
+ case 0 /* Error */:
18477
+ case 55 /* UnaryOperation */:
18478
+ case 4 /* AssignmentExpression */:
18479
+ case 54 /* TypeAnnotation */:
18480
+ case 6 /* Await */:
18481
+ case 51 /* Ternary */:
18482
+ case 56 /* Unpack */:
18483
+ case 52 /* Tuple */:
18484
+ case 9 /* Call */:
18485
+ case 32 /* ListComprehension */:
18486
+ case 46 /* Slice */:
18487
+ case 60 /* Yield */:
18488
+ case 61 /* YieldFrom */:
18489
+ case 30 /* Lambda */:
18490
+ case 40 /* Number */:
18491
+ case 15 /* Dictionary */:
18492
+ case 31 /* List */:
18493
+ case 45 /* Set */:
18494
+ return false;
18495
+ }
18496
+ return true;
18497
+ }
18118
18498
  function printObjectTypeForClass(type) {
18119
18499
  return TypePrinter.printObjectTypeForClass(type, evaluatorOptions.printTypeFlags, getFunctionEffectiveReturnType);
18120
18500
  }
@@ -18257,6 +18637,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
18257
18637
  verifyTypeArgumentsAssignable,
18258
18638
  inferReturnTypeIfNecessary,
18259
18639
  inferTypeParameterVarianceForClass,
18640
+ isFinalVariable,
18641
+ isFinalVariableDeclaration,
18642
+ isExplicitTypeAliasDeclaration,
18260
18643
  addError,
18261
18644
  addWarning,
18262
18645
  addInformation,