@zzzen/pyright-internal 1.2.0-dev.20231022 → 1.2.0-dev.20231105

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 (214) hide show
  1. package/dist/analyzer/analyzerFileInfo.d.ts +1 -0
  2. package/dist/analyzer/analyzerFileInfo.js.map +1 -1
  3. package/dist/analyzer/backgroundAnalysisProgram.d.ts +3 -4
  4. package/dist/analyzer/backgroundAnalysisProgram.js +6 -3
  5. package/dist/analyzer/backgroundAnalysisProgram.js.map +1 -1
  6. package/dist/analyzer/binder.js +1 -6
  7. package/dist/analyzer/binder.js.map +1 -1
  8. package/dist/analyzer/cacheManager.d.ts +3 -0
  9. package/dist/analyzer/cacheManager.js +11 -0
  10. package/dist/analyzer/cacheManager.js.map +1 -1
  11. package/dist/analyzer/checker.js +57 -50
  12. package/dist/analyzer/checker.js.map +1 -1
  13. package/dist/analyzer/codeFlowEngine.d.ts +0 -1
  14. package/dist/analyzer/codeFlowEngine.js +5 -5
  15. package/dist/analyzer/codeFlowEngine.js.map +1 -1
  16. package/dist/analyzer/codeFlowUtils.js +4 -2
  17. package/dist/analyzer/codeFlowUtils.js.map +1 -1
  18. package/dist/analyzer/constraintSolver.js +11 -9
  19. package/dist/analyzer/constraintSolver.js.map +1 -1
  20. package/dist/analyzer/constructorTransform.js +1 -1
  21. package/dist/analyzer/constructorTransform.js.map +1 -1
  22. package/dist/analyzer/constructors.js +41 -36
  23. package/dist/analyzer/constructors.js.map +1 -1
  24. package/dist/analyzer/dataClasses.js +3 -3
  25. package/dist/analyzer/dataClasses.js.map +1 -1
  26. package/dist/analyzer/enums.d.ts +2 -5
  27. package/dist/analyzer/enums.js +4 -2
  28. package/dist/analyzer/enums.js.map +1 -1
  29. package/dist/analyzer/functionTransform.js +1 -1
  30. package/dist/analyzer/functionTransform.js.map +1 -1
  31. package/dist/analyzer/importResolver.d.ts +6 -2
  32. package/dist/analyzer/importResolver.js +51 -8
  33. package/dist/analyzer/importResolver.js.map +1 -1
  34. package/dist/analyzer/namedTuples.js +3 -2
  35. package/dist/analyzer/namedTuples.js.map +1 -1
  36. package/dist/analyzer/operations.js +22 -25
  37. package/dist/analyzer/operations.js.map +1 -1
  38. package/dist/analyzer/packageTypeVerifier.js +19 -21
  39. package/dist/analyzer/packageTypeVerifier.js.map +1 -1
  40. package/dist/analyzer/parseTreeUtils.d.ts +1 -0
  41. package/dist/analyzer/parseTreeUtils.js +11 -2
  42. package/dist/analyzer/parseTreeUtils.js.map +1 -1
  43. package/dist/analyzer/patternMatching.js +5 -5
  44. package/dist/analyzer/patternMatching.js.map +1 -1
  45. package/dist/analyzer/program.d.ts +3 -4
  46. package/dist/analyzer/program.js +22 -27
  47. package/dist/analyzer/program.js.map +1 -1
  48. package/dist/analyzer/properties.js +5 -5
  49. package/dist/analyzer/properties.js.map +1 -1
  50. package/dist/analyzer/protocols.js +24 -6
  51. package/dist/analyzer/protocols.js.map +1 -1
  52. package/dist/analyzer/service.d.ts +1 -2
  53. package/dist/analyzer/service.js +16 -6
  54. package/dist/analyzer/service.js.map +1 -1
  55. package/dist/analyzer/sourceFile.d.ts +1 -1
  56. package/dist/analyzer/sourceFile.js +3 -9
  57. package/dist/analyzer/sourceFile.js.map +1 -1
  58. package/dist/analyzer/sourceFileInfo.d.ts +0 -4
  59. package/dist/analyzer/sourceFileInfo.js +0 -9
  60. package/dist/analyzer/sourceFileInfo.js.map +1 -1
  61. package/dist/analyzer/tracePrinter.js +11 -13
  62. package/dist/analyzer/tracePrinter.js.map +1 -1
  63. package/dist/analyzer/typeDocStringUtils.js +3 -3
  64. package/dist/analyzer/typeDocStringUtils.js.map +1 -1
  65. package/dist/analyzer/typeEvaluator.js +867 -889
  66. package/dist/analyzer/typeEvaluator.js.map +1 -1
  67. package/dist/analyzer/typeEvaluatorTypes.d.ts +13 -25
  68. package/dist/analyzer/typeEvaluatorTypes.js +4 -36
  69. package/dist/analyzer/typeEvaluatorTypes.js.map +1 -1
  70. package/dist/analyzer/typeGuards.js +27 -23
  71. package/dist/analyzer/typeGuards.js.map +1 -1
  72. package/dist/analyzer/typePrinter.js +19 -18
  73. package/dist/analyzer/typePrinter.js.map +1 -1
  74. package/dist/analyzer/typeUtils.d.ts +17 -8
  75. package/dist/analyzer/typeUtils.js +146 -93
  76. package/dist/analyzer/typeUtils.js.map +1 -1
  77. package/dist/analyzer/typeVarContext.js +6 -7
  78. package/dist/analyzer/typeVarContext.js.map +1 -1
  79. package/dist/analyzer/typeWalker.d.ts +1 -2
  80. package/dist/analyzer/typeWalker.js +7 -13
  81. package/dist/analyzer/typeWalker.js.map +1 -1
  82. package/dist/analyzer/typedDicts.js +17 -17
  83. package/dist/analyzer/typedDicts.js.map +1 -1
  84. package/dist/analyzer/types.d.ts +10 -22
  85. package/dist/analyzer/types.js +48 -86
  86. package/dist/analyzer/types.js.map +1 -1
  87. package/dist/backgroundAnalysisBase.d.ts +3 -1
  88. package/dist/backgroundAnalysisBase.js +27 -0
  89. package/dist/backgroundAnalysisBase.js.map +1 -1
  90. package/dist/commands/dumpFileDebugInfoCommand.js +7 -9
  91. package/dist/commands/dumpFileDebugInfoCommand.js.map +1 -1
  92. package/dist/common/commandLineOptions.d.ts +2 -1
  93. package/dist/common/commandLineOptions.js +1 -1
  94. package/dist/common/commandLineOptions.js.map +1 -1
  95. package/dist/common/configOptions.d.ts +1 -1
  96. package/dist/common/configOptions.js +22 -24
  97. package/dist/common/configOptions.js.map +1 -1
  98. package/dist/common/fullAccessHost.js +16 -1
  99. package/dist/common/fullAccessHost.js.map +1 -1
  100. package/dist/common/pathUtils.d.ts +2 -2
  101. package/dist/common/pathUtils.js +18 -11
  102. package/dist/common/pathUtils.js.map +1 -1
  103. package/dist/common/realFileSystem.js +15 -14
  104. package/dist/common/realFileSystem.js.map +1 -1
  105. package/dist/common/serviceProviderExtensions.d.ts +2 -0
  106. package/dist/common/serviceProviderExtensions.js +5 -0
  107. package/dist/common/serviceProviderExtensions.js.map +1 -1
  108. package/dist/languageServerBase.d.ts +4 -6
  109. package/dist/languageServerBase.js +16 -19
  110. package/dist/languageServerBase.js.map +1 -1
  111. package/dist/languageService/analyzerServiceExecutor.js +1 -1
  112. package/dist/languageService/analyzerServiceExecutor.js.map +1 -1
  113. package/dist/languageService/callHierarchyProvider.js +3 -3
  114. package/dist/languageService/callHierarchyProvider.js.map +1 -1
  115. package/dist/languageService/completionProvider.js +8 -8
  116. package/dist/languageService/completionProvider.js.map +1 -1
  117. package/dist/languageService/completionProviderUtils.js +1 -1
  118. package/dist/languageService/definitionProvider.js +1 -1
  119. package/dist/languageService/documentSymbolCollector.d.ts +7 -0
  120. package/dist/languageService/documentSymbolCollector.js +29 -4
  121. package/dist/languageService/documentSymbolCollector.js.map +1 -1
  122. package/dist/languageService/hoverProvider.js +1 -1
  123. package/dist/languageService/signatureHelpProvider.js +0 -14
  124. package/dist/languageService/signatureHelpProvider.js.map +1 -1
  125. package/dist/languageService/tooltipUtils.js +4 -4
  126. package/dist/languageService/tooltipUtils.js.map +1 -1
  127. package/dist/localization/localize.d.ts +12 -0
  128. package/dist/localization/localize.js +6 -0
  129. package/dist/localization/localize.js.map +1 -1
  130. package/dist/localization/package.nls.cs.json +1 -0
  131. package/dist/localization/package.nls.de.json +1 -0
  132. package/dist/localization/package.nls.en-us.json +4 -0
  133. package/dist/localization/package.nls.es.json +1 -0
  134. package/dist/localization/package.nls.fr.json +1 -0
  135. package/dist/localization/package.nls.it.json +1 -0
  136. package/dist/localization/package.nls.ja.json +1 -0
  137. package/dist/localization/package.nls.ko.json +1 -0
  138. package/dist/localization/package.nls.pl.json +1 -0
  139. package/dist/localization/package.nls.pt-br.json +1 -0
  140. package/dist/localization/package.nls.qps-ploc.json +1 -0
  141. package/dist/localization/package.nls.ru.json +1 -0
  142. package/dist/localization/package.nls.tr.json +1 -0
  143. package/dist/localization/package.nls.zh-cn.json +1 -0
  144. package/dist/localization/package.nls.zh-tw.json +1 -0
  145. package/dist/pyright.js +3 -6
  146. package/dist/pyright.js.map +1 -1
  147. package/dist/server.d.ts +1 -0
  148. package/dist/server.js +12 -1
  149. package/dist/server.js.map +1 -1
  150. package/dist/tests/fourslash/completions.importInterimFile.fourslash.disabled.js +46 -0
  151. package/dist/tests/fourslash/completions.importInterimFile.fourslash.disabled.js.map +1 -0
  152. package/dist/tests/fourslash/completions.wildcardimports.fourslash.js +4 -0
  153. package/dist/tests/fourslash/completions.wildcardimports.fourslash.js.map +1 -1
  154. package/dist/tests/fourslash/findDefinitions.classes.fourslash.js +4 -0
  155. package/dist/tests/fourslash/findDefinitions.classes.fourslash.js.map +1 -1
  156. package/dist/tests/fourslash/findDefinitions.fields.fourslash.js +4 -0
  157. package/dist/tests/fourslash/findDefinitions.fields.fourslash.js.map +1 -1
  158. package/dist/tests/fourslash/findDefinitions.functions.fourslash.js +4 -0
  159. package/dist/tests/fourslash/findDefinitions.functions.fourslash.js.map +1 -1
  160. package/dist/tests/fourslash/findDefinitions.methods.fourslash.js +4 -0
  161. package/dist/tests/fourslash/findDefinitions.methods.fourslash.js.map +1 -1
  162. package/dist/tests/fourslash/findDefinitions.parameters.fourslash.js +4 -0
  163. package/dist/tests/fourslash/findDefinitions.parameters.fourslash.js.map +1 -1
  164. package/dist/tests/fourslash/findDefinitions.variables.fourslash.js +4 -0
  165. package/dist/tests/fourslash/findDefinitions.variables.fourslash.js.map +1 -1
  166. package/dist/tests/fourslash/findDefinitions.wildcardimports.fourslash.js +4 -0
  167. package/dist/tests/fourslash/findDefinitions.wildcardimports.fourslash.js.map +1 -1
  168. package/dist/tests/fourslash/findTypeDefinitions.classes.fourslash.js +4 -0
  169. package/dist/tests/fourslash/findTypeDefinitions.classes.fourslash.js.map +1 -1
  170. package/dist/tests/fourslash/fourslash.d.ts +3 -0
  171. package/dist/tests/fourslash/hover.class.docString.fourslash.js +4 -0
  172. package/dist/tests/fourslash/hover.class.docString.fourslash.js.map +1 -1
  173. package/dist/tests/fourslash/hover.docFromSrc.fourslash.js +4 -0
  174. package/dist/tests/fourslash/hover.docFromSrc.fourslash.js.map +1 -1
  175. package/dist/tests/fourslash/hover.docFromSrc.stringFormat.fourslash.d.ts +1 -0
  176. package/dist/tests/fourslash/{hover.docFromScr.stringFormat.fourslash.js → hover.docFromSrc.stringFormat.fourslash.js} +1 -1
  177. package/dist/tests/fourslash/{hover.docFromScr.stringFormat.fourslash.js.map → hover.docFromSrc.stringFormat.fourslash.js.map} +1 -1
  178. package/dist/tests/fourslash/hover.unpackedTypedDict.key.fourslash.d.ts +1 -0
  179. package/dist/tests/fourslash/hover.unpackedTypedDict.key.fourslash.js +24 -0
  180. package/dist/tests/fourslash/hover.unpackedTypedDict.key.fourslash.js.map +1 -0
  181. package/dist/tests/fourslash/hover.wildcardimports.fourslash.js +4 -0
  182. package/dist/tests/fourslash/hover.wildcardimports.fourslash.js.map +1 -1
  183. package/dist/tests/fourslash/signature.complicated.fourslash.js +6 -6
  184. package/dist/tests/fourslash/signature.complicated.fourslash.js.map +1 -1
  185. package/dist/tests/fourslash/signature.docstrings.wildcardimports.fourslash.js +4 -0
  186. package/dist/tests/fourslash/signature.docstrings.wildcardimports.fourslash.js.map +1 -1
  187. package/dist/tests/harness/fourslash/testLanguageService.js +2 -2
  188. package/dist/tests/harness/fourslash/testLanguageService.js.map +1 -1
  189. package/dist/tests/harness/fourslash/testState.d.ts +6 -4
  190. package/dist/tests/harness/fourslash/testState.js +24 -9
  191. package/dist/tests/harness/fourslash/testState.js.map +1 -1
  192. package/dist/tests/harness/vfs/filesystem.js +1 -1
  193. package/dist/tests/harness/vfs/filesystem.js.map +1 -1
  194. package/dist/tests/ipythonMode.test.js +0 -31
  195. package/dist/tests/ipythonMode.test.js.map +1 -1
  196. package/dist/tests/logger.test.js +0 -1
  197. package/dist/tests/logger.test.js.map +1 -1
  198. package/dist/tests/pathUtils.test.js +47 -0
  199. package/dist/tests/pathUtils.test.js.map +1 -1
  200. package/dist/tests/testUtils.js +2 -1
  201. package/dist/tests/testUtils.js.map +1 -1
  202. package/dist/tests/typeEvaluator1.test.js +3 -3
  203. package/dist/tests/typeEvaluator2.test.js +2 -2
  204. package/dist/tests/typeEvaluator3.test.js +7 -1
  205. package/dist/tests/typeEvaluator3.test.js.map +1 -1
  206. package/dist/tests/typeEvaluator4.test.js +5 -1
  207. package/dist/tests/typeEvaluator4.test.js.map +1 -1
  208. package/dist/tests/typeEvaluator5.test.js +1 -1
  209. package/dist/tests/typePrinter.test.js +12 -16
  210. package/dist/tests/typePrinter.test.js.map +1 -1
  211. package/dist/tests/zipfs.test.js +2 -0
  212. package/dist/tests/zipfs.test.js.map +1 -1
  213. package/package.json +1 -1
  214. /package/dist/tests/fourslash/{hover.docFromScr.stringFormat.fourslash.d.ts → completions.importInterimFile.fourslash.disabled.d.ts} +0 -0
@@ -178,6 +178,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
178
178
  let deferredClassCompletions = [];
179
179
  let cancellationToken;
180
180
  let isBasicTypesInitialized = false;
181
+ let noneClassType;
181
182
  let noneType;
182
183
  let objectType;
183
184
  let typeClassType;
@@ -374,6 +375,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
374
375
  // type of surrounding statements to be evaluated.
375
376
  function getType(node) {
376
377
  var _a, _b;
378
+ initializedBasicTypes(node);
377
379
  let type = (_a = evaluateTypeForSubnode(node, () => {
378
380
  evaluateTypesForExpressionInContext(node);
379
381
  })) === null || _a === void 0 ? void 0 : _a.type;
@@ -447,6 +449,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
447
449
  return undefined;
448
450
  }
449
451
  function initializedBasicTypes(node) {
452
+ var _a;
450
453
  if (!isBasicTypesInitialized) {
451
454
  // Some of these types have cyclical dependencies on each other,
452
455
  // so don't re-enter this block once we start executing it.
@@ -457,7 +460,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
457
460
  // Initialize and cache "Collection" to break a cyclical dependency
458
461
  // that occurs when resolving tuple below.
459
462
  getTypingType(node, 'Collection');
460
- noneType = getTypeshedType(node, 'NoneType') || types_1.AnyType.create();
463
+ noneClassType = (_a = getTypeshedType(node, 'NoneType')) !== null && _a !== void 0 ? _a : types_1.UnknownType.create();
464
+ noneType = (0, types_1.isInstantiableClass)(noneClassType)
465
+ ? types_1.ClassType.cloneAsInstance(noneClassType)
466
+ : types_1.UnknownType.create();
461
467
  tupleClassType = getBuiltInType(node, 'tuple');
462
468
  boolClassType = getBuiltInType(node, 'bool');
463
469
  intClassType = getBuiltInType(node, 'int');
@@ -1079,20 +1085,19 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
1079
1085
  case 0 /* Unbound */:
1080
1086
  case 1 /* Unknown */:
1081
1087
  case 2 /* Any */:
1082
- case 4 /* Never */:
1083
- case 3 /* None */: {
1088
+ case 3 /* Never */: {
1084
1089
  return true;
1085
1090
  }
1086
- case 9 /* Union */: {
1091
+ case 8 /* Union */: {
1087
1092
  return (0, types_1.findSubtype)(type, (subtype) => canBeFalsy(subtype, recursionCount)) !== undefined;
1088
1093
  }
1089
- case 5 /* Function */:
1090
- case 6 /* OverloadedFunction */:
1091
- case 8 /* Module */:
1092
- case 10 /* TypeVar */: {
1094
+ case 4 /* Function */:
1095
+ case 5 /* OverloadedFunction */:
1096
+ case 7 /* Module */:
1097
+ case 9 /* TypeVar */: {
1093
1098
  return false;
1094
1099
  }
1095
- case 7 /* Class */: {
1100
+ case 6 /* Class */: {
1096
1101
  if (types_1.TypeBase.isInstantiable(type)) {
1097
1102
  return false;
1098
1103
  }
@@ -1133,7 +1138,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
1133
1138
  }
1134
1139
  return true;
1135
1140
  }
1136
- return false;
1141
+ // If the class is not final, it's possible that it could be overridden
1142
+ // such that it is falsy. To be fully correct, we'd need to do the
1143
+ // following:
1144
+ // return !ClassType.isFinal(type);
1145
+ // However, pragmatically if the class is not an `object`, it's typically
1146
+ // OK to assume that it will not be overridden in this manner.
1147
+ return types_1.ClassType.isBuiltIn(type, 'object');
1137
1148
  }
1138
1149
  }
1139
1150
  }
@@ -1145,25 +1156,27 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
1145
1156
  recursionCount++;
1146
1157
  switch (type.category) {
1147
1158
  case 1 /* Unknown */:
1148
- case 5 /* Function */:
1149
- case 6 /* OverloadedFunction */:
1150
- case 8 /* Module */:
1151
- case 10 /* TypeVar */:
1152
- case 4 /* Never */:
1159
+ case 4 /* Function */:
1160
+ case 5 /* OverloadedFunction */:
1161
+ case 7 /* Module */:
1162
+ case 9 /* TypeVar */:
1163
+ case 3 /* Never */:
1153
1164
  case 2 /* Any */: {
1154
1165
  return true;
1155
1166
  }
1156
- case 9 /* Union */: {
1167
+ case 8 /* Union */: {
1157
1168
  return (0, types_1.findSubtype)(type, (subtype) => canBeTruthy(subtype, recursionCount)) !== undefined;
1158
1169
  }
1159
- case 0 /* Unbound */:
1160
- case 3 /* None */: {
1170
+ case 0 /* Unbound */: {
1161
1171
  return false;
1162
1172
  }
1163
- case 7 /* Class */: {
1173
+ case 6 /* Class */: {
1164
1174
  if (types_1.TypeBase.isInstantiable(type)) {
1165
1175
  return true;
1166
1176
  }
1177
+ if ((0, typeUtils_1.isNoneInstance)(type)) {
1178
+ return false;
1179
+ }
1167
1180
  // Check for Tuple[()] (an empty tuple).
1168
1181
  if ((0, typeUtils_1.isTupleClass)(type)) {
1169
1182
  if (type.tupleTypeArguments && type.tupleTypeArguments.length === 0) {
@@ -1255,6 +1268,22 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
1255
1268
  if (types_1.ClassType.isBuiltIn(concreteSubtype, 'bool')) {
1256
1269
  return types_1.ClassType.cloneWithLiteral(concreteSubtype, /* value */ true);
1257
1270
  }
1271
+ // If the object is a "None" instance, we can eliminate it.
1272
+ if ((0, typeUtils_1.isNoneInstance)(concreteSubtype)) {
1273
+ return undefined;
1274
+ }
1275
+ // If this is an instance of a class that cannot be subclassed,
1276
+ // we cannot say definitively that it's not falsy because a subclass
1277
+ // could override `__bool__`. For this reason, the code should not
1278
+ // remove any classes that are not final.
1279
+ // if (!ClassType.isFinal(concreteSubtype)) {
1280
+ // return subtype;
1281
+ // }
1282
+ // However, we're going to pragmatically assume that any classes
1283
+ // other than `object` will not be overridden in this manner.
1284
+ if (types_1.ClassType.isBuiltIn(concreteSubtype, 'object')) {
1285
+ return subtype;
1286
+ }
1258
1287
  }
1259
1288
  // If it's possible for the type to be truthy, include it.
1260
1289
  if (canBeTruthy(subtype)) {
@@ -1263,113 +1292,109 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
1263
1292
  return undefined;
1264
1293
  });
1265
1294
  }
1266
- // Gets a member type from an object and if it's a function binds
1267
- // it to the object. If bindToClass is undefined, the binding is done
1295
+ // Gets a member type from an object or class. If it's a function, binds
1296
+ // it to the object or class. If selfType is undefined, the binding is done
1268
1297
  // using the objectType parameter. Callers can specify these separately
1269
1298
  // to handle the case where we're fetching the object member from a
1270
1299
  // metaclass but binding to the class.
1271
- function getTypeOfObjectMember(errorNode, objectType, memberName, usage = { method: 'get' }, diag = undefined, memberAccessFlags = 0 /* None */, bindToType) {
1272
- let memberInfo = getTypeOfClassMemberName(errorNode, types_1.ClassType.cloneAsInstantiable(objectType),
1273
- /* isAccessedThroughObject */ true, memberName, usage, diag, memberAccessFlags | 16 /* DisallowClassVarWrites */, bindToType);
1274
- if (!memberInfo) {
1275
- const metaclass = objectType.details.effectiveMetaclass;
1276
- if (metaclass &&
1277
- (0, types_1.isInstantiableClass)(metaclass) &&
1278
- !types_1.ClassType.isBuiltIn(metaclass, 'type') &&
1279
- !types_1.ClassType.isSameGenericClass(metaclass, objectType)) {
1280
- memberInfo = getTypeOfClassMemberName(errorNode, metaclass,
1281
- /* isAccessedThroughObject */ false, memberName, usage,
1282
- /* diag */ undefined, memberAccessFlags |
1283
- 2 /* AccessInstanceMembersOnly */ |
1284
- 128 /* SkipAttributeAccessOverride */, types_1.ClassType.cloneAsInstantiable(objectType));
1285
- }
1286
- }
1287
- if (memberInfo && !memberInfo.isSetTypeError) {
1288
- return {
1289
- type: memberInfo.type,
1290
- classType: memberInfo.classType,
1291
- isIncomplete: !!memberInfo.isTypeIncomplete,
1292
- isAsymmetricAccessor: memberInfo.isAsymmetricAccessor,
1293
- memberAccessDeprecationInfo: memberInfo.memberAccessDeprecationInfo,
1294
- };
1295
- }
1296
- return undefined;
1297
- }
1298
- // Gets a member type from a class and if it's a function binds
1299
- // it to the class.
1300
- function getTypeOfClassMember(errorNode, classType, memberName, usage = { method: 'get' }, diag = undefined, memberAccessFlags = 0 /* None */, bindToType) {
1301
- let memberInfo;
1302
- const classDiag = diag ? new diagnostic_1.DiagnosticAddendum() : undefined;
1303
- const metaclassDiag = diag ? new diagnostic_1.DiagnosticAddendum() : undefined;
1304
- let considerMetaclassOnly = (memberAccessFlags & 64 /* ConsiderMetaclassOnly */) !== 0;
1305
- if (types_1.ClassType.isPartiallyEvaluated(classType)) {
1306
- addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.classDefinitionCycle().format({ name: classType.details.name }), errorNode);
1300
+ function getTypeOfObjectMember(errorNode, objectType, memberName, usage = { method: 'get' }, diag = undefined, flags = 0 /* Default */, selfType) {
1301
+ if (types_1.ClassType.isPartiallyEvaluated(objectType)) {
1302
+ addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.classDefinitionCycle().format({ name: objectType.details.name }), errorNode);
1307
1303
  return { type: types_1.UnknownType.create() };
1308
1304
  }
1309
- const metaclass = classType.details.effectiveMetaclass;
1305
+ // Determine the class that was used to instantiate the objectType.
1306
+ // If the objectType is a class itself, then the class used to instantiate
1307
+ // it is the metaclass.
1308
+ const objectTypeIsInstantiable = types_1.TypeBase.isInstantiable(objectType);
1309
+ const metaclass = objectType.details.effectiveMetaclass;
1310
+ let memberInfo;
1311
+ // If the object type is an instantiable (i.e. it derives from "type") and
1312
+ // we've been asked not to consider instance members, don't look in the class.
1313
+ // Consider only the metaclass class variables in this case.
1314
+ let skipObjectTypeLookup = objectTypeIsInstantiable && (flags & 16 /* SkipInstanceMembers */) !== 0;
1310
1315
  // Look up the attribute in the metaclass first. If the member is a descriptor
1311
- // (an object with a __get__ method) and the access is a 'get', the Python runtime
1312
- // uses this metaclass descriptor to satisfy the lookup. Skip this costly lookup
1313
- // in the common case where the metaclass is 'type' since we know that `type` doesn't
1314
- // have any attributes that are descriptors.
1316
+ // (an object with a __get__ and __set__ method) and the access is a 'get',
1317
+ // the Python runtime uses this descriptor to satisfy the lookup. Skip this
1318
+ // costly lookup in the common case where the metaclass is 'type' since we know
1319
+ // that `type` doesn't have any attributes that are descriptors.
1315
1320
  if (usage.method === 'get' &&
1321
+ objectTypeIsInstantiable &&
1316
1322
  metaclass &&
1317
1323
  (0, types_1.isInstantiableClass)(metaclass) &&
1318
1324
  !types_1.ClassType.isBuiltIn(metaclass, 'type') &&
1319
- !types_1.ClassType.isSameGenericClass(metaclass, classType)) {
1320
- const metaclassMemberInfo = getTypeOfClassMemberName(errorNode, metaclass,
1321
- /* isAccessedThroughObject */ false, memberName, usage, metaclassDiag, memberAccessFlags | 128 /* SkipAttributeAccessOverride */, classType);
1322
- if (metaclassMemberInfo && (0, typeUtils_1.isDescriptorInstance)(metaclassMemberInfo.type)) {
1323
- considerMetaclassOnly = true;
1325
+ !types_1.ClassType.isSameGenericClass(metaclass, objectType)) {
1326
+ const descMemberInfo = getTypeOfClassMemberName(errorNode, metaclass, memberName, usage,
1327
+ /* diag */ undefined, flags | 512 /* SkipAttributeAccessOverride */, objectType);
1328
+ if (descMemberInfo) {
1329
+ const isProperty = (0, types_1.isClassInstance)(descMemberInfo.type) && types_1.ClassType.isPropertyClass(descMemberInfo.type);
1330
+ if ((0, typeUtils_1.isDescriptorInstance)(descMemberInfo.type, /* requireSetter */ true) || isProperty) {
1331
+ skipObjectTypeLookup = true;
1332
+ }
1324
1333
  }
1325
1334
  }
1326
- // Look up the attribute in the class object.
1327
- if (!memberInfo && !considerMetaclassOnly) {
1328
- memberInfo = getTypeOfClassMemberName(errorNode, classType,
1329
- /* isAccessedThroughObject */ false, memberName, usage, classDiag, memberAccessFlags | 1 /* AccessClassMembersOnly */, bindToType);
1330
- }
1331
- const isMemberPresentOnClass = (memberInfo === null || memberInfo === void 0 ? void 0 : memberInfo.classType) !== undefined;
1332
- // If it wasn't found on the class, see if it's part of the metaclass.
1333
- if (!memberInfo) {
1334
- const metaclass = classType.details.effectiveMetaclass;
1335
- if (metaclass && (0, types_1.isInstantiableClass)(metaclass) && !types_1.ClassType.isSameGenericClass(metaclass, classType)) {
1336
- memberInfo = getTypeOfClassMemberName(errorNode, metaclass,
1337
- /* isAccessedThroughObject */ true, memberName, usage, metaclassDiag, memberAccessFlags, classType);
1335
+ let subDiag;
1336
+ if (!skipObjectTypeLookup) {
1337
+ let effectiveFlags = flags;
1338
+ if (objectTypeIsInstantiable) {
1339
+ effectiveFlags |= 16 /* SkipInstanceMembers */ | 512 /* SkipAttributeAccessOverride */;
1340
+ effectiveFlags &= ~32 /* SkipClassMembers */;
1338
1341
  }
1339
- }
1340
- if (memberInfo && !memberInfo.isSetTypeError) {
1342
+ else {
1343
+ effectiveFlags |= 128 /* DisallowClassVarWrites */;
1344
+ }
1345
+ subDiag = diag ? new diagnostic_1.DiagnosticAddendum() : undefined;
1346
+ // See if the member is present in the object itself.
1347
+ memberInfo = getTypeOfClassMemberName(errorNode, objectType, memberName, usage, subDiag, effectiveFlags, selfType);
1348
+ }
1349
+ // If it wasn't found on the object, see if it's part of the metaclass.
1350
+ if (!memberInfo && metaclass && (0, types_1.isInstantiableClass)(metaclass)) {
1351
+ let effectiveFlags = flags;
1352
+ // Class members cannot be accessed on a class's metaclass through
1353
+ // an instance of a class. Limit access to metaclass instance members
1354
+ // in this case.
1355
+ if (!objectTypeIsInstantiable) {
1356
+ effectiveFlags |= 32 /* SkipClassMembers */ | 512 /* SkipAttributeAccessOverride */;
1357
+ effectiveFlags &= ~16 /* SkipInstanceMembers */;
1358
+ }
1359
+ const metaclassDiag = diag ? new diagnostic_1.DiagnosticAddendum() : undefined;
1360
+ memberInfo = getTypeOfClassMemberName(errorNode, types_1.ClassType.cloneAsInstance(metaclass), memberName, usage, metaclassDiag, effectiveFlags, objectTypeIsInstantiable ? objectType : types_1.ClassType.cloneAsInstantiable(objectType));
1361
+ // If there was a descriptor error (as opposed to an error where the members
1362
+ // was simply not found), use this diagnostic message.
1363
+ if (memberInfo === null || memberInfo === void 0 ? void 0 : memberInfo.isDescriptorError) {
1364
+ subDiag = metaclassDiag;
1365
+ }
1366
+ }
1367
+ if (memberInfo && !memberInfo.isDescriptorError) {
1341
1368
  return {
1342
1369
  type: memberInfo.type,
1370
+ classType: memberInfo.classType,
1343
1371
  isIncomplete: !!memberInfo.isTypeIncomplete,
1344
1372
  isAsymmetricAccessor: memberInfo.isAsymmetricAccessor,
1345
1373
  memberAccessDeprecationInfo: memberInfo.memberAccessDeprecationInfo,
1346
1374
  };
1347
1375
  }
1348
- // Determine whether to use the class or metaclass diagnostic addendum.
1349
- const subDiag = isMemberPresentOnClass ? classDiag : metaclassDiag;
1350
1376
  if (diag && subDiag) {
1351
1377
  diag.addAddendum(subDiag);
1352
1378
  }
1353
1379
  return undefined;
1354
1380
  }
1355
- function getBoundMethod(classType, memberName, recursionCount = 0, treatConstructorAsClassMember = false) {
1356
- const memberInfo = (0, typeUtils_1.lookUpClassMember)(classType, memberName, 8 /* SkipInstanceVariables */);
1357
- if (memberInfo) {
1358
- const unboundMethodType = getTypeOfMember(memberInfo);
1359
- if ((0, types_1.isFunction)(unboundMethodType) || (0, types_1.isOverloadedFunction)(unboundMethodType)) {
1360
- const boundMethod = bindFunctionToClassOrObject(types_1.ClassType.cloneAsInstance(classType), unboundMethodType,
1361
- /* memberClass */ undefined, treatConstructorAsClassMember,
1362
- /* firstParamType */ undefined,
1363
- /* diag */ undefined, recursionCount);
1364
- if (boundMethod) {
1365
- return boundMethod;
1366
- }
1367
- }
1368
- else if ((0, types_1.isAnyOrUnknown)(unboundMethodType)) {
1369
- const unknownFunction = types_1.FunctionType.createSynthesizedInstance('', 32768 /* SkipArgsKwargsCompatibilityCheck */);
1370
- types_1.FunctionType.addDefaultParameters(unknownFunction);
1371
- return unknownFunction;
1372
- }
1381
+ function getBoundMethod(classType, memberName, recursionCount = 0) {
1382
+ const memberInfo = (0, typeUtils_1.lookUpClassMember)(classType, memberName, 16 /* SkipInstanceMembers */);
1383
+ if (!memberInfo) {
1384
+ return undefined;
1385
+ }
1386
+ const unboundMethodType = getTypeOfMember(memberInfo);
1387
+ if ((0, types_1.isFunction)(unboundMethodType) || (0, types_1.isOverloadedFunction)(unboundMethodType)) {
1388
+ return bindFunctionToClassOrObject(types_1.ClassType.cloneAsInstance(classType), unboundMethodType,
1389
+ /* memberClass */ undefined,
1390
+ /* treatConstructorAsClassMember */ true,
1391
+ /* selfType */ undefined,
1392
+ /* diag */ undefined, recursionCount);
1393
+ }
1394
+ if ((0, types_1.isAnyOrUnknown)(unboundMethodType)) {
1395
+ const unknownFunction = types_1.FunctionType.createSynthesizedInstance('', 32768 /* SkipArgsKwargsCompatibilityCheck */);
1396
+ types_1.FunctionType.addDefaultParameters(unknownFunction);
1397
+ return unknownFunction;
1373
1398
  }
1374
1399
  return undefined;
1375
1400
  }
@@ -1379,7 +1404,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
1379
1404
  function getCallSignatureInfo(callNode, activeIndex, activeOrFake) {
1380
1405
  const exprNode = callNode.leftExpression;
1381
1406
  const callType = getType(exprNode);
1382
- if (callType === undefined) {
1407
+ if (!callType) {
1383
1408
  return undefined;
1384
1409
  }
1385
1410
  const argList = [];
@@ -1424,7 +1449,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
1424
1449
  /* skipUnknownArgCheck */ true);
1425
1450
  });
1426
1451
  signatures.push({
1427
- type,
1452
+ type: expandTypedKwargs(type),
1428
1453
  activeParam: callResult === null || callResult === void 0 ? void 0 : callResult.activeParam,
1429
1454
  });
1430
1455
  }
@@ -1440,12 +1465,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
1440
1465
  }
1441
1466
  (0, typeUtils_1.doForEachSubtype)(callType, (subtype) => {
1442
1467
  switch (subtype.category) {
1443
- case 5 /* Function */:
1444
- case 6 /* OverloadedFunction */: {
1468
+ case 4 /* Function */:
1469
+ case 5 /* OverloadedFunction */: {
1445
1470
  addFunctionToSignature(subtype);
1446
1471
  break;
1447
1472
  }
1448
- case 7 /* Class */: {
1473
+ case 6 /* Class */: {
1449
1474
  if (types_1.TypeBase.isInstantiable(subtype)) {
1450
1475
  let methodType;
1451
1476
  // Try to get the `__init__` method first because it typically has more
@@ -1460,9 +1485,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
1460
1485
  // the `object` class or accepts only default parameters(* args, ** kwargs),
1461
1486
  // see if we can find a better signature from the `__new__` method.
1462
1487
  if (!methodType || isObjectInit || isDefaultParams) {
1463
- const constructorType = getBoundMethod(subtype, '__new__',
1464
- /* recursionCount */ undefined,
1465
- /* treatConstructorAsClassMember */ true);
1488
+ const constructorType = getBoundMethod(subtype, '__new__');
1466
1489
  if (constructorType) {
1467
1490
  // Is this the __new__ method provided by the object class?
1468
1491
  const isObjectNew = (0, types_1.isFunction)(constructorType) &&
@@ -1494,6 +1517,48 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
1494
1517
  signatures,
1495
1518
  };
1496
1519
  }
1520
+ // If the function includes a `**kwargs: Unpack[TypedDict]` parameter, the
1521
+ // parameter is expanded to include individual keyword args.
1522
+ function expandTypedKwargs(functionType) {
1523
+ var _a;
1524
+ const kwargsIndex = functionType.details.parameters.findIndex((param) => param.category === 2 /* KwargsDict */);
1525
+ if (kwargsIndex < 0) {
1526
+ return functionType;
1527
+ }
1528
+ (0, debug_1.assert)(kwargsIndex === functionType.details.parameters.length - 1);
1529
+ const kwargsType = types_1.FunctionType.getEffectiveParameterType(functionType, kwargsIndex);
1530
+ if (!(0, types_1.isClassInstance)(kwargsType) || !types_1.ClassType.isTypedDictClass(kwargsType) || !kwargsType.isUnpacked) {
1531
+ return functionType;
1532
+ }
1533
+ const tdEntries = (_a = kwargsType.typedDictNarrowedEntries) !== null && _a !== void 0 ? _a : kwargsType.details.typedDictEntries;
1534
+ if (!tdEntries) {
1535
+ return functionType;
1536
+ }
1537
+ const newFunction = types_1.FunctionType.clone(functionType);
1538
+ newFunction.details.parameters.splice(kwargsIndex);
1539
+ if (newFunction.specializedTypes) {
1540
+ newFunction.specializedTypes.parameterTypes.splice(kwargsIndex);
1541
+ }
1542
+ const kwSeparatorIndex = functionType.details.parameters.findIndex((param) => param.category === 1 /* ArgsList */);
1543
+ // Add a keyword separator if necessary.
1544
+ if (kwSeparatorIndex < 0) {
1545
+ types_1.FunctionType.addParameter(newFunction, {
1546
+ category: 1 /* ArgsList */,
1547
+ type: types_1.AnyType.create(),
1548
+ });
1549
+ }
1550
+ tdEntries.forEach((tdEntry, name) => {
1551
+ types_1.FunctionType.addParameter(newFunction, {
1552
+ category: 0 /* Simple */,
1553
+ name,
1554
+ hasDeclaredType: true,
1555
+ type: tdEntry.valueType,
1556
+ hasDefault: !tdEntry.isRequired,
1557
+ defaultType: tdEntry.valueType,
1558
+ });
1559
+ });
1560
+ return newFunction;
1561
+ }
1497
1562
  // Determines whether the specified expression is an explicit TypeAlias declaration.
1498
1563
  function isDeclaredTypeAlias(expression) {
1499
1564
  if (expression.nodeType === 54 /* TypeAnnotation */) {
@@ -1530,8 +1595,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
1530
1595
  if (enclosingClass && enclosingClass.nodeType === 10 /* Class */) {
1531
1596
  const classTypeInfo = getTypeOfClass(enclosingClass);
1532
1597
  if (classTypeInfo) {
1533
- const classMemberInfo = (0, typeUtils_1.lookUpClassMember)(classTypeInfo.classType, expression.value, 8 /* SkipInstanceVariables */ |
1534
- 32 /* DeclaredTypesOnly */);
1598
+ const classMemberInfo = (0, typeUtils_1.lookUpClassMember)(classTypeInfo.classType, expression.value, 16 /* SkipInstanceMembers */ | 64 /* DeclaredTypesOnly */);
1535
1599
  if (classMemberInfo) {
1536
1600
  symbol = classMemberInfo.symbol;
1537
1601
  }
@@ -1548,7 +1612,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
1548
1612
  const baseType = makeTopLevelTypeVarsConcrete(getTypeOfExpression(expression.leftExpression, 16777218 /* MemberAccessBaseDefaults */).type);
1549
1613
  let classMemberInfo;
1550
1614
  if ((0, types_1.isClassInstance)(baseType)) {
1551
- classMemberInfo = (0, typeUtils_1.lookUpObjectMember)(baseType, expression.memberName.value, 32 /* DeclaredTypesOnly */);
1615
+ classMemberInfo = (0, typeUtils_1.lookUpObjectMember)(baseType, expression.memberName.value, 64 /* DeclaredTypesOnly */);
1552
1616
  classOrObjectBase = baseType;
1553
1617
  memberAccessClass = classMemberInfo === null || classMemberInfo === void 0 ? void 0 : classMemberInfo.classType;
1554
1618
  // If this is an instance member (e.g. a dataclass field), don't
@@ -1559,7 +1623,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
1559
1623
  useDescriptorSetterType = true;
1560
1624
  }
1561
1625
  else if ((0, types_1.isInstantiableClass)(baseType)) {
1562
- classMemberInfo = (0, typeUtils_1.lookUpClassMember)(baseType, expression.memberName.value, 8 /* SkipInstanceVariables */ | 32 /* DeclaredTypesOnly */);
1626
+ classMemberInfo = (0, typeUtils_1.lookUpClassMember)(baseType, expression.memberName.value, 16 /* SkipInstanceMembers */ | 64 /* DeclaredTypesOnly */);
1563
1627
  classOrObjectBase = baseType;
1564
1628
  memberAccessClass = classMemberInfo === null || classMemberInfo === void 0 ? void 0 : classMemberInfo.classType;
1565
1629
  }
@@ -1668,17 +1732,17 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
1668
1732
  }
1669
1733
  // Validates that the type is an iterator and returns the iterated type
1670
1734
  // (i.e. the type returned from the '__next__' or '__anext__' method).
1671
- function getTypeOfIterator(typeResult, isAsync, errorNode) {
1735
+ function getTypeOfIterator(typeResult, isAsync, errorNode, emitNotIterableError = true) {
1672
1736
  const iterMethodName = isAsync ? '__aiter__' : '__iter__';
1673
1737
  const nextMethodName = isAsync ? '__anext__' : '__next__';
1674
1738
  let isValidIterator = true;
1675
1739
  let type = (0, typeUtils_1.transformPossibleRecursiveTypeAlias)(typeResult.type);
1676
1740
  type = makeTopLevelTypeVarsConcrete(type);
1677
1741
  if ((0, typeUtils_1.isOptionalType)(type)) {
1678
- if (errorNode && !typeResult.isIncomplete) {
1742
+ if (!typeResult.isIncomplete && emitNotIterableError) {
1679
1743
  addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportOptionalIterable, diagnosticRules_1.DiagnosticRule.reportOptionalIterable, localize_1.Localizer.Diagnostic.noneNotIterable(), errorNode);
1680
1744
  }
1681
- type = (0, types_1.removeNoneFromUnion)(type);
1745
+ type = (0, typeUtils_1.removeNoneFromUnion)(type);
1682
1746
  }
1683
1747
  const iterableType = (0, typeUtils_1.mapSubtypes)(type, (subtype) => {
1684
1748
  subtype = makeTopLevelTypeVarsConcrete(subtype);
@@ -1687,33 +1751,23 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
1687
1751
  }
1688
1752
  const diag = new diagnostic_1.DiagnosticAddendum();
1689
1753
  if ((0, types_1.isClass)(subtype)) {
1690
- let iterReturnType;
1691
- if (types_1.TypeBase.isInstance(subtype)) {
1692
- // Handle an empty tuple specially.
1693
- if ((0, typeUtils_1.isTupleClass)(subtype) &&
1694
- subtype.tupleTypeArguments &&
1695
- subtype.tupleTypeArguments.length === 0) {
1696
- return types_1.NeverType.createNever();
1697
- }
1698
- iterReturnType = getSpecializedReturnType(subtype, iterMethodName, [], errorNode);
1699
- }
1700
- else if (types_1.TypeBase.isInstantiable(subtype) &&
1701
- subtype.details.effectiveMetaclass &&
1702
- (0, types_1.isInstantiableClass)(subtype.details.effectiveMetaclass)) {
1703
- iterReturnType = getSpecializedReturnType(types_1.ClassType.cloneAsInstance(subtype.details.effectiveMetaclass), iterMethodName, [], errorNode, subtype);
1704
- }
1754
+ // Handle an empty tuple specially.
1755
+ if (types_1.TypeBase.isInstance(subtype) &&
1756
+ (0, typeUtils_1.isTupleClass)(subtype) &&
1757
+ subtype.tupleTypeArguments &&
1758
+ subtype.tupleTypeArguments.length === 0) {
1759
+ return types_1.NeverType.createNever();
1760
+ }
1761
+ const iterReturnType = getTypeOfMagicMethodCall(subtype, iterMethodName, [], errorNode);
1705
1762
  if (!iterReturnType) {
1706
1763
  // There was no __iter__. See if we can fall back to
1707
1764
  // the __getitem__ method instead.
1708
1765
  if (!isAsync && (0, types_1.isClassInstance)(subtype)) {
1709
- const getItemReturnType = getSpecializedReturnType(subtype, '__getitem__', [
1766
+ const getItemReturnType = getTypeOfMagicMethodCall(subtype, '__getitem__', [
1710
1767
  {
1711
- argumentCategory: 0 /* Simple */,
1712
- typeResult: {
1713
- type: intClassType && (0, types_1.isInstantiableClass)(intClassType)
1714
- ? types_1.ClassType.cloneAsInstance(intClassType)
1715
- : types_1.UnknownType.create(),
1716
- },
1768
+ type: intClassType && (0, types_1.isInstantiableClass)(intClassType)
1769
+ ? types_1.ClassType.cloneAsInstance(intClassType)
1770
+ : types_1.UnknownType.create(),
1717
1771
  },
1718
1772
  ], errorNode);
1719
1773
  if (getItemReturnType) {
@@ -1730,7 +1784,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
1730
1784
  return subtype;
1731
1785
  }
1732
1786
  if ((0, types_1.isClassInstance)(subtype)) {
1733
- let nextReturnType = getSpecializedReturnType(subtype, nextMethodName, [], errorNode);
1787
+ let nextReturnType = getTypeOfMagicMethodCall(subtype, nextMethodName, [], errorNode);
1734
1788
  if (!nextReturnType) {
1735
1789
  iterReturnTypeDiag.addMessage(localize_1.Localizer.Diagnostic.methodNotDefinedOnType().format({
1736
1790
  name: nextMethodName,
@@ -1765,7 +1819,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
1765
1819
  diag.addAddendum(iterReturnTypeDiag);
1766
1820
  }
1767
1821
  }
1768
- if (errorNode && !typeResult.isIncomplete) {
1822
+ if (!typeResult.isIncomplete && emitNotIterableError) {
1769
1823
  addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeNotIterable().format({ type: printType(subtype) }) + diag.getString(), errorNode);
1770
1824
  }
1771
1825
  isValidIterator = false;
@@ -1774,35 +1828,27 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
1774
1828
  return isValidIterator ? { type: iterableType, isIncomplete: typeResult.isIncomplete } : undefined;
1775
1829
  }
1776
1830
  // Validates that the type is an iterable and returns the iterable type argument.
1777
- function getTypeOfIterable(typeResult, isAsync, errorNode) {
1831
+ function getTypeOfIterable(typeResult, isAsync, errorNode, emitNotIterableError = true) {
1778
1832
  const iterMethodName = isAsync ? '__aiter__' : '__iter__';
1779
1833
  let isValidIterable = true;
1780
1834
  let type = makeTopLevelTypeVarsConcrete(typeResult.type);
1781
1835
  if ((0, typeUtils_1.isOptionalType)(type)) {
1782
- if (errorNode && !typeResult.isIncomplete) {
1836
+ if (!typeResult.isIncomplete && emitNotIterableError) {
1783
1837
  addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportOptionalIterable, diagnosticRules_1.DiagnosticRule.reportOptionalIterable, localize_1.Localizer.Diagnostic.noneNotIterable(), errorNode);
1784
1838
  }
1785
- type = (0, types_1.removeNoneFromUnion)(type);
1839
+ type = (0, typeUtils_1.removeNoneFromUnion)(type);
1786
1840
  }
1787
1841
  const iterableType = (0, typeUtils_1.mapSubtypes)(type, (subtype) => {
1788
1842
  if ((0, types_1.isAnyOrUnknown)(subtype)) {
1789
1843
  return subtype;
1790
1844
  }
1791
1845
  if ((0, types_1.isClass)(subtype)) {
1792
- let iterReturnType;
1793
- if (types_1.TypeBase.isInstance(subtype)) {
1794
- iterReturnType = getSpecializedReturnType(subtype, iterMethodName, [], errorNode);
1795
- }
1796
- else if (types_1.TypeBase.isInstantiable(subtype) &&
1797
- subtype.details.effectiveMetaclass &&
1798
- (0, types_1.isInstantiableClass)(subtype.details.effectiveMetaclass)) {
1799
- iterReturnType = getSpecializedReturnType(types_1.ClassType.cloneAsInstance(subtype.details.effectiveMetaclass), iterMethodName, [], errorNode, subtype);
1800
- }
1846
+ const iterReturnType = getTypeOfMagicMethodCall(subtype, iterMethodName, [], errorNode);
1801
1847
  if (iterReturnType) {
1802
1848
  return makeTopLevelTypeVarsConcrete(iterReturnType);
1803
1849
  }
1804
1850
  }
1805
- if (errorNode) {
1851
+ if (emitNotIterableError) {
1806
1852
  addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeNotIterable().format({ type: printType(subtype) }), errorNode);
1807
1853
  }
1808
1854
  isValidIterable = false;
@@ -1828,7 +1874,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
1828
1874
  // Handle the case where the type is synthesized (used for
1829
1875
  // dataclasses).
1830
1876
  if (synthesizedType) {
1831
- isObjectHashable = !(0, types_1.isNoneInstance)(synthesizedType);
1877
+ isObjectHashable = !(0, typeUtils_1.isNoneInstance)(synthesizedType);
1832
1878
  }
1833
1879
  else {
1834
1880
  // Assume that if '__hash__' is declared as a variable, it is
@@ -1851,13 +1897,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
1851
1897
  return isTypeHashable;
1852
1898
  }
1853
1899
  function getTypedDictClassType() {
1854
- return typedDictPrivateClassType;
1900
+ return typedDictPrivateClassType && (0, types_1.isInstantiableClass)(typedDictPrivateClassType)
1901
+ ? typedDictPrivateClassType
1902
+ : undefined;
1855
1903
  }
1856
1904
  function getTupleClassType() {
1857
- return tupleClassType;
1905
+ return tupleClassType && (0, types_1.isInstantiableClass)(tupleClassType) ? tupleClassType : undefined;
1858
1906
  }
1859
1907
  function getObjectType() {
1860
- return objectType;
1908
+ return objectType !== null && objectType !== void 0 ? objectType : types_1.UnknownType.create();
1909
+ }
1910
+ function getNoneType() {
1911
+ return noneType !== null && noneType !== void 0 ? noneType : types_1.UnknownType.create();
1861
1912
  }
1862
1913
  function getTypingType(node, symbolName) {
1863
1914
  var _a;
@@ -1935,9 +1986,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
1935
1986
  const codeFlowResult = analyzer.getTypeFromCodeFlow(flowNode,
1936
1987
  /* reference */ undefined,
1937
1988
  /* targetSymbolId */ undefined,
1938
- /* typeAtStart */ types_1.UnboundType.create(), {
1939
- skipNoReturnCallAnalysis: true,
1940
- });
1989
+ /* typeAtStart */ types_1.UnboundType.create());
1941
1990
  return codeFlowResult.type !== undefined && !(0, types_1.isNever)(codeFlowResult.type);
1942
1991
  }
1943
1992
  // Determines whether there is a code flow path from sourceNode to sinkNode.
@@ -2194,7 +2243,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
2194
2243
  }
2195
2244
  const classTypeInfo = getTypeOfClass(classDef);
2196
2245
  if (classTypeInfo && (0, types_1.isInstantiableClass)(classTypeInfo.classType)) {
2197
- let memberInfo = (0, typeUtils_1.lookUpClassMember)(classTypeInfo.classType, memberName, isInstanceMember ? 0 /* Default */ : 8 /* SkipInstanceVariables */);
2246
+ let memberInfo = (0, typeUtils_1.lookUpClassMember)(classTypeInfo.classType, memberName, isInstanceMember ? 0 /* Default */ : 16 /* SkipInstanceMembers */);
2198
2247
  const memberFields = classTypeInfo.classType.details.fields;
2199
2248
  if (memberInfo) {
2200
2249
  // Are we accessing an existing member on this class, or is
@@ -2212,7 +2261,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
2212
2261
  // Determine whether the assignment corresponds to a descriptor
2213
2262
  // that was assigned as a class variable. If so, then slots will not
2214
2263
  // apply in this case.
2215
- const classMemberDetails = (0, typeUtils_1.lookUpClassMember)(memberClass, memberName, 8 /* SkipInstanceVariables */);
2264
+ const classMemberDetails = (0, typeUtils_1.lookUpClassMember)(memberClass, memberName, 16 /* SkipInstanceMembers */);
2216
2265
  let isPotentiallyDescriptor = false;
2217
2266
  if (classMemberDetails) {
2218
2267
  const classMemberSymbolType = getEffectiveTypeOfSymbol(classMemberDetails.symbol);
@@ -2259,7 +2308,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
2259
2308
  }
2260
2309
  }
2261
2310
  // Look up the member info again, now that we've potentially updated it.
2262
- memberInfo = (0, typeUtils_1.lookUpClassMember)(classTypeInfo.classType, memberName, 32 /* DeclaredTypesOnly */);
2311
+ memberInfo = (0, typeUtils_1.lookUpClassMember)(classTypeInfo.classType, memberName, 64 /* DeclaredTypesOnly */);
2263
2312
  if (!memberInfo && srcExprNode && !isTypeIncomplete) {
2264
2313
  reportPossibleUnknownAssignment(fileInfo.diagnosticRuleSet.reportUnknownMemberType, diagnosticRules_1.DiagnosticRule.reportUnknownMemberType, node.memberName, srcType, node,
2265
2314
  /* ignoreEmptyContainers */ true);
@@ -2390,6 +2439,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
2390
2439
  // TypeVar, only the conditions that match the filter will be included.
2391
2440
  function makeTopLevelTypeVarsConcrete(type, makeParamSpecsConcrete = false, conditionFilter) {
2392
2441
  return (0, typeUtils_1.mapSubtypes)(type, (subtype) => {
2442
+ var _a, _b;
2393
2443
  if ((0, types_1.isParamSpec)(subtype)) {
2394
2444
  if (subtype.paramSpecAccess === 'args') {
2395
2445
  if (tupleClassType &&
@@ -2445,22 +2495,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
2445
2495
  }
2446
2496
  }
2447
2497
  if ((0, types_1.isTypeVar)(subtype) && !subtype.details.recursiveTypeAliasName) {
2448
- if (subtype.details.boundType) {
2449
- const boundType = types_1.TypeBase.isInstantiable(subtype)
2450
- ? (0, typeUtils_1.convertToInstantiable)(subtype.details.boundType)
2451
- : subtype.details.boundType;
2452
- // Handle Self and type[Self] specially.
2453
- if (subtype.details.isSynthesizedSelf && (0, types_1.isClass)(boundType)) {
2454
- return types_1.ClassType.cloneIncludeSubclasses(boundType);
2455
- }
2456
- return (0, typeUtils_1.addConditionToType)(boundType, [
2457
- {
2458
- typeVarName: types_1.TypeVarType.getNameWithScope(subtype),
2459
- constraintIndex: 0,
2460
- isConstrainedTypeVar: false,
2461
- },
2462
- ]);
2463
- }
2464
2498
  // If this is a recursive type alias placeholder
2465
2499
  // that hasn't yet been resolved, return it as is.
2466
2500
  if (subtype.details.recursiveTypeAliasName) {
@@ -2496,32 +2530,20 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
2496
2530
  if (subtype.details.isExemptFromBoundCheck) {
2497
2531
  return types_1.AnyType.create();
2498
2532
  }
2499
- // Convert to an "object" or "type" instance depending on whether
2500
- // it's instantiable.
2501
- if (types_1.TypeBase.isInstantiable(subtype)) {
2502
- if (typeClassType && (0, types_1.isInstantiableClass)(typeClassType)) {
2503
- return subtype.details.isSynthesized
2504
- ? typeClassType
2505
- : (0, typeUtils_1.addConditionToType)(types_1.ClassType.cloneAsInstance(typeClassType), [
2506
- {
2507
- typeVarName: types_1.TypeVarType.getNameWithScope(subtype),
2508
- constraintIndex: 0,
2509
- isConstrainedTypeVar: false,
2510
- },
2511
- ]);
2512
- }
2513
- }
2514
- else if (objectType) {
2515
- return subtype.details.isSynthesized
2516
- ? objectType
2517
- : (0, typeUtils_1.addConditionToType)(objectType, [
2518
- {
2519
- typeVarName: types_1.TypeVarType.getNameWithScope(subtype),
2520
- constraintIndex: 0,
2521
- isConstrainedTypeVar: false,
2522
- },
2523
- ]);
2533
+ // Fall back to a bound of "object" if no bound is provided.
2534
+ let boundType = (_b = (_a = subtype.details.boundType) !== null && _a !== void 0 ? _a : objectType) !== null && _b !== void 0 ? _b : types_1.UnknownType.create();
2535
+ boundType = types_1.TypeBase.isInstantiable(subtype) ? (0, typeUtils_1.convertToInstantiable)(boundType) : boundType;
2536
+ // Handle Self and type[Self] specially.
2537
+ if (subtype.details.isSynthesizedSelf && (0, types_1.isClass)(boundType)) {
2538
+ return types_1.ClassType.cloneIncludeSubclasses(boundType);
2524
2539
  }
2540
+ return (0, typeUtils_1.addConditionToType)(boundType, [
2541
+ {
2542
+ typeVarName: types_1.TypeVarType.getNameWithScope(subtype),
2543
+ constraintIndex: 0,
2544
+ isConstrainedTypeVar: false,
2545
+ },
2546
+ ]);
2525
2547
  return types_1.AnyType.create();
2526
2548
  }
2527
2549
  return subtype;
@@ -2533,7 +2555,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
2533
2555
  // its bound type and a constrained TypeVar is expanded to its individual
2534
2556
  // constrained types). If conditionFilter is specified, conditions that
2535
2557
  // do not match will be ignored.
2536
- function mapSubtypesExpandTypeVars(type, conditionFilter, callback, recursionCount = 0) {
2558
+ function mapSubtypesExpandTypeVars(type, conditionFilter, callback, sortSubtypes = false, recursionCount = 0) {
2537
2559
  const newSubtypes = [];
2538
2560
  let typeChanged = false;
2539
2561
  function expandSubtype(unexpandedType, isLastSubtype) {
@@ -2561,10 +2583,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
2561
2583
  newSubtypes.push(transformedType);
2562
2584
  }
2563
2585
  return undefined;
2564
- });
2586
+ }, sortSubtypes);
2565
2587
  }
2566
2588
  if ((0, types_1.isUnion)(type)) {
2567
- type.subtypes.forEach((subtype, index) => {
2589
+ const subtypes = sortSubtypes ? (0, typeUtils_1.sortTypes)(type.subtypes) : type.subtypes;
2590
+ subtypes.forEach((subtype, index) => {
2568
2591
  expandSubtype(subtype, index === type.subtypes.length - 1);
2569
2592
  });
2570
2593
  }
@@ -2576,7 +2599,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
2576
2599
  }
2577
2600
  const newType = (0, types_1.combineTypes)(newSubtypes);
2578
2601
  // Do our best to retain type aliases.
2579
- if (newType.category === 9 /* Union */) {
2602
+ if (newType.category === 8 /* Union */) {
2580
2603
  types_1.UnionType.addTypeAliasSource(newType, type);
2581
2604
  }
2582
2605
  return newType;
@@ -2606,7 +2629,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
2606
2629
  }
2607
2630
  const filteredTypeArg = mapSubtypesExpandTypeVars(typeArg, conditionFilter, (expandedSubtype) => {
2608
2631
  return expandedSubtype;
2609
- }, recursionCount);
2632
+ },
2633
+ /* sortSubtypes */ undefined, recursionCount);
2610
2634
  if (filteredTypeArg !== typeArg) {
2611
2635
  typeWasTransformed = true;
2612
2636
  }
@@ -2837,41 +2861,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
2837
2861
  fileInfo.accessedSymbolSet.add(symbol.id);
2838
2862
  }
2839
2863
  }
2840
- function getSpecializedReturnType(objType, memberName, argList, errorNode, bindToClass) {
2841
- const classMember = (0, typeUtils_1.lookUpObjectMember)(objType, memberName, 8 /* SkipInstanceVariables */);
2842
- if (!classMember) {
2843
- return undefined;
2844
- }
2845
- const memberTypeResult = getTypeOfMemberInternal(classMember, objType);
2846
- if (!memberTypeResult) {
2847
- return undefined;
2848
- }
2849
- const memberType = memberTypeResult.type;
2850
- if ((0, types_1.isAnyOrUnknown)(memberType)) {
2851
- return memberType;
2852
- }
2853
- if ((0, types_1.isFunction)(memberType) || (0, types_1.isOverloadedFunction)(memberType)) {
2854
- const methodType = bindFunctionToClassOrObjectWithErrors(bindToClass || objType, memberType, classMember && (0, types_1.isInstantiableClass)(classMember.classType) ? classMember.classType : undefined, errorNode,
2855
- /* treatConstructorAsClassMember */ false,
2856
- /* firstParamType */ bindToClass);
2857
- if (methodType) {
2858
- if ((0, types_1.isOverloadedFunction)(methodType)) {
2859
- if (errorNode) {
2860
- const bestOverload = getBestOverloadForArguments(errorNode, { type: methodType, isIncomplete: memberTypeResult.isIncomplete }, argList);
2861
- if (bestOverload) {
2862
- return getFunctionEffectiveReturnType(bestOverload);
2863
- }
2864
- }
2865
- }
2866
- else {
2867
- return getFunctionEffectiveReturnType(methodType);
2868
- }
2869
- }
2870
- }
2871
- return undefined;
2872
- }
2873
2864
  function getTypeOfName(node, flags) {
2874
- var _a;
2875
2865
  const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
2876
2866
  const name = node.value;
2877
2867
  let symbol;
@@ -3019,29 +3009,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3019
3009
  type = types_1.UnknownType.create();
3020
3010
  }
3021
3011
  }
3022
- if ((0, types_1.isTypeVar)(type) &&
3023
- !type.details.isParamSpec &&
3024
- !type.isVariadicInUnion &&
3025
- (flags & 128 /* ExpectingInstantiableType */) === 0 &&
3026
- type.details.name === name) {
3027
- // Handle the special case of a PEP 604 union. These can appear within
3028
- // an implied type alias where we are not expecting a type.
3029
- const isPep604Union = ((_a = node.parent) === null || _a === void 0 ? void 0 : _a.nodeType) === 7 /* BinaryOperation */ &&
3030
- node.parent.operator === 6 /* BitwiseOr */;
3031
- if (!isPep604Union) {
3032
- // A TypeVar in contexts where we're not expecting a type is
3033
- // simply a TypeVar or TypeVarTuple object.
3034
- const typeVarType = type.details.isVariadic
3035
- ? getTypingType(node, 'TypeVarTuple')
3036
- : getTypingType(node, 'TypeVar');
3037
- if (typeVarType && (0, types_1.isInstantiableClass)(typeVarType)) {
3038
- type = types_1.ClassType.cloneAsInstance(typeVarType);
3039
- }
3040
- else {
3041
- type = types_1.UnknownType.create();
3042
- }
3043
- }
3044
- }
3012
+ type = convertTypeVarToRuntimeInstance(node, type, flags);
3045
3013
  if ((flags & 256 /* ExpectingTypeAnnotation */) === 0) {
3046
3014
  reportUseOfTypeCheckOnly(type, node);
3047
3015
  }
@@ -3062,6 +3030,37 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3062
3030
  }
3063
3031
  return { type, isIncomplete };
3064
3032
  }
3033
+ // If the type is a TypeVar and we're not expecting a type, convert
3034
+ // a TypeVar or TypeVarTuple into a runtime type. We don't currently
3035
+ // do this for ParamSpec (although we arguably should) because it's
3036
+ // problematic for handling P.args and P.kwargs.
3037
+ function convertTypeVarToRuntimeInstance(node, type, flags) {
3038
+ var _a;
3039
+ if (node.nodeType === 38 /* Name */ &&
3040
+ (0, types_1.isTypeVar)(type) &&
3041
+ node.value === type.details.name &&
3042
+ !type.isVariadicInUnion &&
3043
+ (flags & 128 /* ExpectingInstantiableType */) === 0) {
3044
+ if ((flags & 33554432 /* SkipConvertParamSpecToRuntimeObject */) !== 0 && type.details.isParamSpec) {
3045
+ return type;
3046
+ }
3047
+ // Handle the special case of a PEP 604 union. These can appear within
3048
+ // an implied type alias where we are not expecting a type.
3049
+ const isPep604Union = ((_a = node.parent) === null || _a === void 0 ? void 0 : _a.nodeType) === 7 /* BinaryOperation */ &&
3050
+ node.parent.operator === 6 /* BitwiseOr */;
3051
+ if (!isPep604Union) {
3052
+ // A TypeVar in contexts where we're not expecting a type is
3053
+ // simply a runtime object.
3054
+ if (type.details.runtimeClass) {
3055
+ type = types_1.ClassType.cloneAsInstance(type.details.runtimeClass);
3056
+ }
3057
+ else {
3058
+ type = types_1.UnknownType.create();
3059
+ }
3060
+ }
3061
+ }
3062
+ return type;
3063
+ }
3065
3064
  // Handles the case where a variable or parameter is defined in an outer
3066
3065
  // scope and captured by an inner scope (either a function or a lambda).
3067
3066
  function getCodeFlowTypeForCapturedVariable(node, symbolWithScope, effectiveType) {
@@ -3387,15 +3386,25 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3387
3386
  return { type, isRescoped: false, foundInterveningClass: false };
3388
3387
  }
3389
3388
  function getTypeOfMemberAccess(node, flags) {
3390
- const baseTypeFlags = 16777218 /* MemberAccessBaseDefaults */ |
3391
- (flags &
3389
+ var _a;
3390
+ // Compute flags specifically for evaluating the left expression.
3391
+ let leftExprFlags = 16777218 /* MemberAccessBaseDefaults */;
3392
+ leftExprFlags |=
3393
+ flags &
3392
3394
  (256 /* ExpectingTypeAnnotation */ |
3393
3395
  32768 /* VariableTypeAnnotation */ |
3394
3396
  4 /* AllowForwardReferences */ |
3395
3397
  524288 /* NotParsedByInterpreter */ |
3396
3398
  2048 /* DisallowTypeVarsWithScopeId */ |
3397
- 8192 /* AssociateTypeVarsWithCurrentScope */));
3398
- const baseTypeResult = getTypeOfExpression(node.leftExpression, baseTypeFlags);
3399
+ 8192 /* AssociateTypeVarsWithCurrentScope */);
3400
+ // Handle special casing for ParamSpec "args" and "kwargs" accesses.
3401
+ if ((flags & 128 /* ExpectingInstantiableType */) !== 0) {
3402
+ const memberName = node.memberName.value;
3403
+ if (memberName === 'args' || memberName === 'kwargs') {
3404
+ leftExprFlags |= 33554432 /* SkipConvertParamSpecToRuntimeObject */;
3405
+ }
3406
+ }
3407
+ const baseTypeResult = getTypeOfExpression(node.leftExpression, leftExprFlags);
3399
3408
  if ((0, typeUtils_1.isTypeAliasPlaceholder)(baseTypeResult.type)) {
3400
3409
  return {
3401
3410
  type: types_1.UnknownType.create(/* isIncomplete */ true),
@@ -3443,12 +3452,28 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3443
3452
  if (baseTypeResult.isIncomplete) {
3444
3453
  typeResult.isIncomplete = true;
3445
3454
  }
3455
+ // See if we need to log an "unknown member access" diagnostic.
3456
+ let skipPartialUnknownCheck = typeResult.isIncomplete;
3457
+ // Don't report an error if the type is a partially-specialized
3458
+ // class being passed as an argument. This comes up frequently in
3459
+ // cases where a type is passed as an argument (e.g. "defaultdict(list)").
3460
+ // It can also come up in cases like "isinstance(x, (list, dict))".
3461
+ if ((0, types_1.isInstantiableClass)(typeResult.type)) {
3462
+ const argNode = ParseTreeUtils.getParentNodeOfType(node, 1 /* Argument */);
3463
+ if (argNode && ((_a = argNode === null || argNode === void 0 ? void 0 : argNode.parent) === null || _a === void 0 ? void 0 : _a.nodeType) === 9 /* Call */) {
3464
+ skipPartialUnknownCheck = true;
3465
+ }
3466
+ }
3467
+ if (!skipPartialUnknownCheck) {
3468
+ reportPossibleUnknownAssignment(AnalyzerNodeInfo.getFileInfo(node).diagnosticRuleSet.reportUnknownMemberType, diagnosticRules_1.DiagnosticRule.reportUnknownMemberType, node.memberName, typeResult.type, node,
3469
+ /* ignoreEmptyContainers */ false);
3470
+ }
3446
3471
  // Cache the type information in the member name node.
3447
3472
  writeTypeCache(node.memberName, typeResult, flags);
3448
3473
  return typeResult;
3449
3474
  }
3450
3475
  function getTypeOfMemberAccessWithBaseType(node, baseTypeResult, usage, flags) {
3451
- var _a, _b, _c;
3476
+ var _a, _b;
3452
3477
  let baseType = baseTypeResult.type;
3453
3478
  const memberName = node.memberName.value;
3454
3479
  let diag = new diagnostic_1.DiagnosticAddendum();
@@ -3471,113 +3496,90 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3471
3496
  baseType = objectType;
3472
3497
  }
3473
3498
  }
3474
- function getTypeOfNoneBase(subtype) {
3475
- if (noneType && (0, types_1.isInstantiableClass)(noneType)) {
3476
- if (types_1.TypeBase.isInstance(subtype)) {
3477
- return getTypeOfObjectMember(node.memberName, types_1.ClassType.cloneAsInstance(noneType), memberName, usage, diag);
3478
- }
3479
- else {
3480
- return getTypeOfClassMember(node.memberName, noneType, memberName, usage, diag);
3481
- }
3482
- }
3483
- return undefined;
3484
- }
3485
3499
  if ((0, types_1.isParamSpec)(baseType) && baseType.paramSpecAccess) {
3486
3500
  baseType = makeTopLevelTypeVarsConcrete(baseType);
3487
3501
  }
3488
3502
  switch (baseType.category) {
3489
3503
  case 2 /* Any */:
3490
3504
  case 1 /* Unknown */:
3491
- case 4 /* Never */: {
3505
+ case 3 /* Never */: {
3492
3506
  type = baseType;
3493
3507
  break;
3494
3508
  }
3495
- case 10 /* TypeVar */: {
3509
+ case 0 /* Unbound */: {
3510
+ break;
3511
+ }
3512
+ case 9 /* TypeVar */: {
3496
3513
  if (baseType.details.isParamSpec) {
3497
- if (memberName === 'args') {
3498
- const paramNode = ParseTreeUtils.getEnclosingParameter(node);
3499
- if (!paramNode || paramNode.category !== 1 /* ArgsList */) {
3500
- addError(localize_1.Localizer.Diagnostic.paramSpecArgsUsage(), node);
3501
- return { type: types_1.UnknownType.create(isIncomplete), isIncomplete };
3502
- }
3503
- return { type: types_1.TypeVarType.cloneForParamSpecAccess(baseType, 'args'), isIncomplete };
3504
- }
3505
- if (memberName === 'kwargs') {
3514
+ // Handle special cases for "P.args" and "P.kwargs".
3515
+ if (memberName === 'args' || memberName === 'kwargs') {
3516
+ const isArgs = memberName === 'args';
3506
3517
  const paramNode = ParseTreeUtils.getEnclosingParameter(node);
3507
- if (!paramNode || paramNode.category !== 2 /* KwargsDict */) {
3508
- addError(localize_1.Localizer.Diagnostic.paramSpecKwargsUsage(), node);
3509
- return { type: types_1.UnknownType.create(isIncomplete), isIncomplete };
3518
+ const expectedCategory = isArgs ? 1 /* ArgsList */ : 2 /* KwargsDict */;
3519
+ if (!paramNode || paramNode.category !== expectedCategory) {
3520
+ const errorMessage = isArgs
3521
+ ? localize_1.Localizer.Diagnostic.paramSpecArgsUsage()
3522
+ : localize_1.Localizer.Diagnostic.paramSpecKwargsUsage();
3523
+ addError(errorMessage, node);
3524
+ type = types_1.UnknownType.create(isIncomplete);
3525
+ break;
3510
3526
  }
3511
- return { type: types_1.TypeVarType.cloneForParamSpecAccess(baseType, 'kwargs'), isIncomplete };
3527
+ type = types_1.TypeVarType.cloneForParamSpecAccess(baseType, memberName);
3528
+ break;
3512
3529
  }
3513
3530
  if (!isIncomplete) {
3514
3531
  addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.paramSpecUnknownMember().format({ name: memberName }), node);
3515
3532
  }
3516
- return { type: types_1.UnknownType.create(isIncomplete), isIncomplete };
3533
+ type = types_1.UnknownType.create(isIncomplete);
3534
+ break;
3517
3535
  }
3536
+ // It's illegal to reference a member from a type variable.
3518
3537
  if ((flags & 256 /* ExpectingTypeAnnotation */) !== 0) {
3519
3538
  if (!isIncomplete) {
3520
- addDiagnostic(AnalyzerNodeInfo.getFileInfo(node).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeVarNoMember().format({
3539
+ addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeVarNoMember().format({
3521
3540
  type: printType(baseType),
3522
3541
  name: memberName,
3523
3542
  }), node.leftExpression);
3524
3543
  }
3525
- return { type: types_1.UnknownType.create(isIncomplete), isIncomplete };
3544
+ type = types_1.UnknownType.create(isIncomplete);
3545
+ break;
3526
3546
  }
3527
3547
  if (baseType.details.recursiveTypeAliasName) {
3528
- return { type: types_1.UnknownType.create(/* isIncomplete */ true), isIncomplete: true };
3548
+ type = types_1.UnknownType.create(/* isIncomplete */ true);
3549
+ isIncomplete = true;
3550
+ break;
3529
3551
  }
3530
- if (!baseType.details.isVariadic) {
3531
- return getTypeOfMemberAccessWithBaseType(node, {
3532
- type: makeTopLevelTypeVarsConcrete(baseType),
3533
- bindToType: baseType,
3534
- isIncomplete,
3535
- }, usage, 0 /* None */);
3552
+ if (baseType.details.isVariadic) {
3553
+ break;
3536
3554
  }
3537
- break;
3555
+ return getTypeOfMemberAccessWithBaseType(node, {
3556
+ type: makeTopLevelTypeVarsConcrete(baseType),
3557
+ bindToSelfType: types_1.TypeBase.isInstantiable(baseType) ? (0, typeUtils_1.convertToInstance)(baseType) : baseType,
3558
+ isIncomplete,
3559
+ }, usage, 0 /* None */);
3538
3560
  }
3539
- case 7 /* Class */: {
3540
- if (types_1.TypeBase.isInstantiable(baseType)) {
3541
- const typeResult = getTypeOfClassMember(node.memberName, baseType, memberName, usage, diag, 0 /* None */, baseTypeResult.bindToType);
3542
- type = typeResult === null || typeResult === void 0 ? void 0 : typeResult.type;
3543
- if (typeResult === null || typeResult === void 0 ? void 0 : typeResult.isIncomplete) {
3544
- isIncomplete = true;
3545
- }
3546
- if (typeResult === null || typeResult === void 0 ? void 0 : typeResult.isAsymmetricAccessor) {
3547
- isAsymmetricAccessor = true;
3548
- }
3549
- if (typeResult === null || typeResult === void 0 ? void 0 : typeResult.memberAccessDeprecationInfo) {
3550
- memberAccessDeprecationInfo = typeResult.memberAccessDeprecationInfo;
3551
- }
3561
+ case 6 /* Class */: {
3562
+ const typeResult = (_a = (0, enums_1.getTypeOfEnumMember)(evaluatorInterface, node, baseType, memberName, isIncomplete)) !== null && _a !== void 0 ? _a : getTypeOfObjectMember(node.memberName, baseType, memberName, usage, diag,
3563
+ /* memberAccessFlags */ undefined, baseTypeResult.bindToSelfType);
3564
+ if (typeResult) {
3565
+ type = (0, typeUtils_1.addConditionToType)(typeResult.type, (0, typeUtils_1.getTypeCondition)(baseType));
3552
3566
  }
3553
- else {
3554
- // Handle the special case of 'name' and 'value' members within an enum.
3555
- const enumMemberResult = (0, enums_1.getTypeOfEnumMember)(evaluatorInterface, node, baseType, memberName, isIncomplete);
3556
- if (enumMemberResult) {
3557
- return enumMemberResult;
3558
- }
3559
- const typeResult = getTypeOfObjectMember(node.memberName, baseType, memberName, usage, diag,
3560
- /* memberAccessFlags */ undefined, baseTypeResult.bindToType);
3561
- if (typeResult) {
3562
- type = (0, typeUtils_1.addConditionToType)(typeResult.type, (0, typeUtils_1.getTypeCondition)(baseType));
3563
- }
3564
- if (typeResult === null || typeResult === void 0 ? void 0 : typeResult.isIncomplete) {
3565
- isIncomplete = true;
3566
- }
3567
- if (typeResult === null || typeResult === void 0 ? void 0 : typeResult.isAsymmetricAccessor) {
3568
- isAsymmetricAccessor = true;
3569
- }
3570
- if (typeResult === null || typeResult === void 0 ? void 0 : typeResult.memberAccessDeprecationInfo) {
3571
- memberAccessDeprecationInfo = typeResult.memberAccessDeprecationInfo;
3572
- }
3567
+ if (typeResult === null || typeResult === void 0 ? void 0 : typeResult.isIncomplete) {
3568
+ isIncomplete = true;
3569
+ }
3570
+ if (typeResult === null || typeResult === void 0 ? void 0 : typeResult.isAsymmetricAccessor) {
3571
+ isAsymmetricAccessor = true;
3572
+ }
3573
+ if (typeResult === null || typeResult === void 0 ? void 0 : typeResult.memberAccessDeprecationInfo) {
3574
+ memberAccessDeprecationInfo = typeResult.memberAccessDeprecationInfo;
3573
3575
  }
3574
3576
  break;
3575
3577
  }
3576
- case 8 /* Module */: {
3578
+ case 7 /* Module */: {
3577
3579
  const symbol = types_1.ModuleType.getField(baseType, memberName);
3578
3580
  if (symbol && !symbol.isExternallyHidden()) {
3579
3581
  if (usage.method === 'get') {
3580
- setSymbolAccessed(AnalyzerNodeInfo.getFileInfo(node), symbol, node.memberName);
3582
+ setSymbolAccessed(fileInfo, symbol, node.memberName);
3581
3583
  }
3582
3584
  type = getEffectiveTypeOfSymbolForUsage(symbol,
3583
3585
  /* usageNode */ undefined,
@@ -3593,12 +3595,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3593
3595
  type = types_1.UnknownType.create(/* isIncomplete */ true);
3594
3596
  }
3595
3597
  if (symbol.isPrivateMember()) {
3596
- addDiagnostic(AnalyzerNodeInfo.getFileInfo(node).diagnosticRuleSet.reportPrivateUsage, diagnosticRules_1.DiagnosticRule.reportPrivateUsage, localize_1.Localizer.Diagnostic.privateUsedOutsideOfModule().format({
3598
+ addDiagnostic(fileInfo.diagnosticRuleSet.reportPrivateUsage, diagnosticRules_1.DiagnosticRule.reportPrivateUsage, localize_1.Localizer.Diagnostic.privateUsedOutsideOfModule().format({
3597
3599
  name: memberName,
3598
3600
  }), node.memberName);
3599
3601
  }
3600
3602
  if (symbol.isPrivatePyTypedImport()) {
3601
- addDiagnostic(AnalyzerNodeInfo.getFileInfo(node).diagnosticRuleSet.reportPrivateImportUsage, diagnosticRules_1.DiagnosticRule.reportPrivateImportUsage, localize_1.Localizer.Diagnostic.privateImportFromPyTypedModule().format({
3603
+ addDiagnostic(fileInfo.diagnosticRuleSet.reportPrivateImportUsage, diagnosticRules_1.DiagnosticRule.reportPrivateImportUsage, localize_1.Localizer.Diagnostic.privateImportFromPyTypedModule().format({
3602
3604
  name: memberName,
3603
3605
  module: baseType.moduleName,
3604
3606
  }), node.memberName);
@@ -3641,10 +3643,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3641
3643
  }
3642
3644
  break;
3643
3645
  }
3644
- case 9 /* Union */: {
3646
+ case 8 /* Union */: {
3645
3647
  type = (0, typeUtils_1.mapSubtypes)(baseType, (subtype) => {
3646
- if ((0, types_1.isNoneInstance)(subtype)) {
3647
- const typeResult = getTypeOfNoneBase(subtype);
3648
+ if ((0, types_1.isUnbound)(subtype)) {
3649
+ // Don't do anything if it's unbound. The error will already
3650
+ // be reported elsewhere.
3651
+ return undefined;
3652
+ }
3653
+ if ((0, typeUtils_1.isNoneInstance)(subtype) && noneType && (0, types_1.isClassInstance)(noneType)) {
3654
+ const typeResult = getTypeOfObjectMember(node.memberName, noneType, memberName, usage, diag);
3648
3655
  if (typeResult) {
3649
3656
  type = (0, typeUtils_1.addConditionToType)(typeResult.type, (0, typeUtils_1.getTypeCondition)(baseType));
3650
3657
  if (typeResult.isIncomplete) {
@@ -3652,42 +3659,28 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3652
3659
  }
3653
3660
  return type;
3654
3661
  }
3655
- else {
3656
- if (!isIncomplete) {
3657
- addDiagnostic(AnalyzerNodeInfo.getFileInfo(node).diagnosticRuleSet.reportOptionalMemberAccess, diagnosticRules_1.DiagnosticRule.reportOptionalMemberAccess, localize_1.Localizer.Diagnostic.noneUnknownMember().format({ name: memberName }), node.memberName);
3658
- }
3659
- return undefined;
3662
+ if (!isIncomplete) {
3663
+ addDiagnostic(fileInfo.diagnosticRuleSet.reportOptionalMemberAccess, diagnosticRules_1.DiagnosticRule.reportOptionalMemberAccess, localize_1.Localizer.Diagnostic.noneUnknownMember().format({ name: memberName }), node.memberName);
3660
3664
  }
3661
- }
3662
- else if ((0, types_1.isUnbound)(subtype)) {
3663
- // Don't do anything if it's unbound. The error will already
3664
- // be reported elsewhere.
3665
3665
  return undefined;
3666
3666
  }
3667
- else {
3668
- const typeResult = getTypeOfMemberAccessWithBaseType(node, {
3669
- type: subtype,
3670
- isIncomplete: baseTypeResult.isIncomplete,
3671
- }, usage, 0 /* None */);
3672
- if (typeResult.isIncomplete) {
3673
- isIncomplete = true;
3674
- }
3675
- if (typeResult === null || typeResult === void 0 ? void 0 : typeResult.memberAccessDeprecationInfo) {
3676
- memberAccessDeprecationInfo = typeResult.memberAccessDeprecationInfo;
3677
- }
3678
- return typeResult.type;
3667
+ const typeResult = getTypeOfMemberAccessWithBaseType(node, {
3668
+ type: subtype,
3669
+ isIncomplete: baseTypeResult.isIncomplete,
3670
+ }, usage, 0 /* None */);
3671
+ if (typeResult.isIncomplete) {
3672
+ isIncomplete = true;
3673
+ }
3674
+ if (typeResult === null || typeResult === void 0 ? void 0 : typeResult.memberAccessDeprecationInfo) {
3675
+ memberAccessDeprecationInfo = typeResult.memberAccessDeprecationInfo;
3679
3676
  }
3677
+ return typeResult.type;
3680
3678
  });
3681
3679
  break;
3682
3680
  }
3683
- case 5 /* Function */:
3684
- case 6 /* OverloadedFunction */: {
3685
- if (memberName === '__defaults__') {
3686
- // The "__defaults__" member is not currently defined in the "function"
3687
- // class, so we'll special-case it here.
3688
- type = types_1.AnyType.create();
3689
- }
3690
- else if (memberName === '__self__') {
3681
+ case 4 /* Function */:
3682
+ case 5 /* OverloadedFunction */: {
3683
+ if (memberName === '__self__') {
3691
3684
  // The "__self__" member is not currently defined in the "function"
3692
3685
  // class, so we'll special-case it here.
3693
3686
  const functionType = (0, types_1.isFunction)(baseType) ? baseType : baseType.overloads[0];
@@ -3697,29 +3690,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3697
3690
  }
3698
3691
  }
3699
3692
  else {
3700
- if (!functionObj) {
3701
- type = types_1.AnyType.create();
3702
- }
3703
- else {
3704
- type = getTypeOfMemberAccessWithBaseType(node, { type: functionObj }, usage, flags).type;
3705
- }
3706
- }
3707
- break;
3708
- }
3709
- case 3 /* None */: {
3710
- const typeResult = getTypeOfNoneBase(baseType);
3711
- if (typeResult) {
3712
- type = (0, typeUtils_1.addConditionToType)(typeResult.type, (0, typeUtils_1.getTypeCondition)(baseType));
3713
- if (typeResult.isIncomplete) {
3714
- isIncomplete = true;
3715
- }
3693
+ type = getTypeOfMemberAccessWithBaseType(node, { type: functionObj !== null && functionObj !== void 0 ? functionObj : types_1.AnyType.create() }, usage, flags).type;
3716
3694
  }
3717
3695
  break;
3718
3696
  }
3719
3697
  default:
3720
- diag.addMessage(localize_1.Localizer.DiagnosticAddendum.typeUnsupported().format({ type: printType(baseType) }));
3721
- break;
3698
+ (0, debug_1.assertNever)(baseType);
3722
3699
  }
3700
+ // If type is undefined, emit a general error message indicating that the
3701
+ // member could not be accessed.
3723
3702
  if (!type) {
3724
3703
  const isFunctionRule = (0, types_1.isFunction)(baseType) ||
3725
3704
  (0, types_1.isOverloadedFunction)(baseType) ||
@@ -3741,7 +3720,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3741
3720
  const [ruleSet, rule] = isFunctionRule
3742
3721
  ? [fileInfo.diagnosticRuleSet.reportFunctionMemberAccess, diagnosticRules_1.DiagnosticRule.reportFunctionMemberAccess]
3743
3722
  : [fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues];
3744
- addDiagnostic(ruleSet, rule, diagMessage.format({ name: memberName, type: printType(baseType) }) + diag.getString(), node.memberName, (_a = diag.getEffectiveTextRange()) !== null && _a !== void 0 ? _a : node.memberName);
3723
+ addDiagnostic(ruleSet, rule, diagMessage.format({ name: memberName, type: printType(baseType) }) + diag.getString(), node.memberName, (_b = diag.getEffectiveTextRange()) !== null && _b !== void 0 ? _b : node.memberName);
3745
3724
  }
3746
3725
  // If this is member access on a function, use "Any" so if the
3747
3726
  // reportFunctionMemberAccess rule is disabled, we don't trigger
@@ -3751,159 +3730,133 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3751
3730
  if ((flags & 256 /* ExpectingTypeAnnotation */) === 0) {
3752
3731
  reportUseOfTypeCheckOnly(type, node.memberName);
3753
3732
  }
3754
- // Should we specialize the class?
3755
- if ((flags & 2 /* DoNotSpecialize */) === 0) {
3756
- if ((0, types_1.isInstantiableClass)(type) && !type.typeArguments) {
3757
- type = (_b = createSpecializedClassType(type, /* typeArgs */ undefined, flags, node)) === null || _b === void 0 ? void 0 : _b.type;
3758
- }
3759
- }
3760
- if (usage.method === 'get') {
3761
- let skipPartialUnknownCheck = isIncomplete;
3762
- // Don't report an error if the type is a partially-specialized
3763
- // class being passed as an argument. This comes up frequently in
3764
- // cases where a type is passed as an argument (e.g. "defaultdict(list)").
3765
- // It can also come up in cases like "isinstance(x, (list, dict))".
3766
- if ((0, types_1.isInstantiableClass)(type)) {
3767
- const argNode = ParseTreeUtils.getParentNodeOfType(node, 1 /* Argument */);
3768
- if (argNode && ((_c = argNode === null || argNode === void 0 ? void 0 : argNode.parent) === null || _c === void 0 ? void 0 : _c.nodeType) === 9 /* Call */) {
3769
- skipPartialUnknownCheck = true;
3770
- }
3771
- }
3772
- if (!skipPartialUnknownCheck) {
3773
- reportPossibleUnknownAssignment(fileInfo.diagnosticRuleSet.reportUnknownMemberType, diagnosticRules_1.DiagnosticRule.reportUnknownMemberType, node.memberName, type, node,
3774
- /* ignoreEmptyContainers */ false);
3775
- }
3776
- }
3777
3733
  return { type, isIncomplete, isAsymmetricAccessor, isRequired, isNotRequired, memberAccessDeprecationInfo };
3778
3734
  }
3779
- function getTypeOfClassMemberName(errorNode, classType, isAccessedThroughObject, memberName, usage, diag, flags, bindToType) {
3780
- var _a, _b;
3781
- let classLookupFlags = 0 /* Default */;
3782
- if (flags & 1 /* AccessClassMembersOnly */) {
3783
- classLookupFlags |= 8 /* SkipInstanceVariables */;
3784
- }
3785
- if (flags & 2 /* AccessInstanceMembersOnly */) {
3786
- classLookupFlags |= 16 /* SkipClassVariables */;
3787
- }
3788
- if (flags & 4 /* SkipBaseClasses */) {
3789
- classLookupFlags |= 2 /* SkipBaseClasses */;
3790
- }
3791
- if (flags & 8 /* SkipObjectBaseClass */) {
3792
- classLookupFlags |= 4 /* SkipObjectBaseClass */;
3793
- }
3794
- if (flags & 512 /* SkipTypeBaseClass */) {
3795
- classLookupFlags |= 64 /* SkipTypeBaseClass */;
3796
- }
3797
- if (flags & 256 /* SkipOriginalClass */) {
3798
- classLookupFlags |= 1 /* SkipOriginalClass */;
3799
- }
3735
+ function getTypeOfClassMemberName(errorNode, classType, memberName, usage, diag, flags, selfType) {
3736
+ var _a, _b, _c, _d;
3737
+ const isAccessedThroughObject = types_1.TypeBase.isInstance(classType);
3800
3738
  // Always look for a member with a declared type first.
3801
- let memberInfo = (0, typeUtils_1.lookUpClassMember)(classType, memberName, classLookupFlags | 32 /* DeclaredTypesOnly */);
3739
+ let memberInfo = (0, typeUtils_1.lookUpClassMember)(classType, memberName, flags | 64 /* DeclaredTypesOnly */);
3802
3740
  // If we couldn't find a symbol with a declared type, use
3803
3741
  // a symbol with an inferred type.
3804
3742
  if (!memberInfo) {
3805
- memberInfo = (0, typeUtils_1.lookUpClassMember)(classType, memberName, classLookupFlags);
3743
+ memberInfo = (0, typeUtils_1.lookUpClassMember)(classType, memberName, flags);
3806
3744
  }
3807
- if (memberInfo) {
3808
- let type;
3809
- let isTypeIncomplete = false;
3810
- if (memberInfo.symbol.isInitVar()) {
3811
- diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.Localizer.DiagnosticAddendum.memberIsInitVar().format({ name: memberName }));
3812
- return undefined;
3745
+ if (!memberInfo) {
3746
+ // No attribute of that name was found. If this is a member access
3747
+ // through an object, see if there's an attribute access override
3748
+ // method ("__getattr__", etc.).
3749
+ if ((flags & 512 /* SkipAttributeAccessOverride */) === 0) {
3750
+ const generalAttrType = applyAttributeAccessOverride(classType, errorNode, usage, memberName, selfType);
3751
+ if (generalAttrType) {
3752
+ return {
3753
+ symbol: undefined,
3754
+ type: generalAttrType.type,
3755
+ isTypeIncomplete: false,
3756
+ isDescriptorError: false,
3757
+ isClassMember: false,
3758
+ isClassVar: false,
3759
+ isAsymmetricAccessor: generalAttrType.isAsymmetricAccessor,
3760
+ };
3761
+ }
3813
3762
  }
3814
- if (usage.method !== 'get') {
3815
- // If the usage indicates a 'set' or 'delete' and the access is within the
3816
- // class definition itself, use only the declared type to avoid circular
3817
- // type evaluation.
3818
- const containingClass = ParseTreeUtils.getEnclosingClass(errorNode);
3819
- if (containingClass) {
3820
- const containingClassType = (_a = getTypeOfClass(containingClass)) === null || _a === void 0 ? void 0 : _a.classType;
3821
- if (containingClassType &&
3822
- (0, types_1.isInstantiableClass)(containingClassType) &&
3823
- types_1.ClassType.isSameGenericClass(containingClassType, classType)) {
3824
- type = (_b = getDeclaredTypeOfSymbol(memberInfo.symbol)) === null || _b === void 0 ? void 0 : _b.type;
3825
- if (type && (0, types_1.isInstantiableClass)(memberInfo.classType)) {
3826
- type = (0, typeUtils_1.partiallySpecializeType)(type, memberInfo.classType);
3827
- }
3828
- // If we're setting a class variable via a write through an object,
3829
- // this is normally considered a type violation. But it is allowed
3830
- // if the class variable is a descriptor object. In this case, we will
3831
- // clear the flag that causes an error to be generated.
3832
- if (usage.method === 'set' && memberInfo.symbol.isClassVar() && isAccessedThroughObject) {
3833
- const selfClass = bindToType || memberName === '__new__' ? undefined : classType;
3834
- const typeResult = getTypeOfMemberInternal(memberInfo, selfClass);
3835
- if (typeResult) {
3836
- if ((0, typeUtils_1.isDescriptorInstance)(typeResult.type, /* requireSetter */ true)) {
3837
- type = typeResult.type;
3838
- flags &= 16 /* DisallowClassVarWrites */;
3839
- }
3763
+ // Report that the member could not be accessed.
3764
+ diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.Localizer.DiagnosticAddendum.memberUnknown().format({ name: memberName }));
3765
+ return undefined;
3766
+ }
3767
+ let type;
3768
+ let isTypeIncomplete = false;
3769
+ if (memberInfo.symbol.isInitVar()) {
3770
+ diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.Localizer.DiagnosticAddendum.memberIsInitVar().format({ name: memberName }));
3771
+ return undefined;
3772
+ }
3773
+ if (usage.method !== 'get') {
3774
+ // If the usage indicates a 'set' or 'delete' and the access is within the
3775
+ // class definition itself, use only the declared type to avoid circular
3776
+ // type evaluation.
3777
+ const containingClass = ParseTreeUtils.getEnclosingClass(errorNode);
3778
+ if (containingClass) {
3779
+ const containingClassType = (_a = getTypeOfClass(containingClass)) === null || _a === void 0 ? void 0 : _a.classType;
3780
+ if (containingClassType &&
3781
+ (0, types_1.isInstantiableClass)(containingClassType) &&
3782
+ types_1.ClassType.isSameGenericClass(containingClassType, classType)) {
3783
+ type = (_b = getDeclaredTypeOfSymbol(memberInfo.symbol)) === null || _b === void 0 ? void 0 : _b.type;
3784
+ if (type && (0, types_1.isInstantiableClass)(memberInfo.classType)) {
3785
+ type = (0, typeUtils_1.partiallySpecializeType)(type, memberInfo.classType);
3786
+ }
3787
+ // If we're setting a class variable via a write through an object,
3788
+ // this is normally considered a type violation. But it is allowed
3789
+ // if the class variable is a descriptor object. In this case, we will
3790
+ // clear the flag that causes an error to be generated.
3791
+ if (usage.method === 'set' && memberInfo.symbol.isClassVar() && isAccessedThroughObject) {
3792
+ const selfClass = (selfType !== null && selfType !== void 0 ? selfType : memberName === '__new__') ? undefined : classType;
3793
+ const typeResult = getTypeOfMemberInternal(memberInfo, selfClass);
3794
+ if (typeResult) {
3795
+ if ((0, typeUtils_1.isDescriptorInstance)(typeResult.type, /* requireSetter */ true)) {
3796
+ type = typeResult.type;
3797
+ flags &= 128 /* DisallowClassVarWrites */;
3840
3798
  }
3841
3799
  }
3842
- if (!type) {
3843
- type = types_1.UnknownType.create();
3844
- }
3845
3800
  }
3846
- }
3847
- }
3848
- if (!type) {
3849
- let selfClass = classType;
3850
- // Determine whether to replace Self variables with a specific
3851
- // class. Avoid doing this if there's a "bindToType" specified
3852
- // because that case is used for super() calls where we want
3853
- // to leave the Self type generic (not specialized). We'll also
3854
- // skip this for __new__ methods because they are not bound
3855
- // to the class but rather assume the type of the cls argument.
3856
- if (bindToType) {
3857
- if ((0, types_1.isTypeVar)(bindToType) && bindToType.details.isSynthesizedSelf) {
3858
- selfClass = bindToType;
3859
- }
3860
- else {
3861
- selfClass = undefined;
3801
+ if (!type) {
3802
+ type = types_1.UnknownType.create();
3862
3803
  }
3863
3804
  }
3864
- else if (memberName === '__new__') {
3865
- selfClass = undefined;
3866
- }
3867
- const typeResult = getTypeOfMemberInternal(memberInfo, selfClass);
3868
- if (typeResult) {
3869
- type = typeResult.type;
3870
- if (typeResult.isIncomplete) {
3871
- isTypeIncomplete = true;
3872
- }
3805
+ }
3806
+ }
3807
+ if (!type) {
3808
+ let selfClass = classType;
3809
+ // Determine whether to replace Self variables with a specific
3810
+ // class. Avoid doing this if there's a "selfType" specified
3811
+ // because that case is used for super() calls where we want
3812
+ // to leave the Self type generic (not specialized). We'll also
3813
+ // skip this for __new__ methods because they are not bound
3814
+ // to the class but rather assume the type of the cls argument.
3815
+ if (selfType) {
3816
+ if ((0, types_1.isTypeVar)(selfType) && selfType.details.isSynthesizedSelf) {
3817
+ selfClass = selfType;
3873
3818
  }
3874
3819
  else {
3875
- type = types_1.UnknownType.create();
3820
+ selfClass = undefined;
3876
3821
  }
3877
3822
  }
3878
- // Don't include variables within typed dict classes.
3879
- if ((0, types_1.isClass)(memberInfo.classType) && types_1.ClassType.isTypedDictClass(memberInfo.classType)) {
3880
- const typedDecls = memberInfo.symbol.getTypedDeclarations();
3881
- if (typedDecls.length > 0 && typedDecls[0].type === 1 /* Variable */) {
3882
- diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.Localizer.DiagnosticAddendum.memberUnknown().format({ name: memberName }));
3883
- return undefined;
3884
- }
3823
+ else if (memberName === '__new__') {
3824
+ selfClass = undefined;
3885
3825
  }
3886
- if (usage.method === 'get') {
3887
- // Mark the member accessed if it's not coming from a parent class.
3888
- if ((0, types_1.isInstantiableClass)(memberInfo.classType) &&
3889
- types_1.ClassType.isSameGenericClass(memberInfo.classType, classType)) {
3890
- setSymbolAccessed(AnalyzerNodeInfo.getFileInfo(errorNode), memberInfo.symbol, errorNode);
3891
- }
3892
- // Special-case `__init_subclass` and `__class_getitem__` because
3893
- // these are always treated as class methods even if they're not
3894
- // decorated as such.
3895
- if (memberName === '__init_subclass__' || memberName === '__class_getitem__') {
3896
- if ((0, types_1.isFunction)(type) && !types_1.FunctionType.isClassMethod(type)) {
3897
- type = types_1.FunctionType.cloneWithNewFlags(type, type.details.flags | 2 /* ClassMethod */);
3898
- }
3899
- }
3826
+ const typeResult = getTypeOfMemberInternal(memberInfo, selfClass);
3827
+ type = (_c = typeResult === null || typeResult === void 0 ? void 0 : typeResult.type) !== null && _c !== void 0 ? _c : types_1.UnknownType.create();
3828
+ if (typeResult === null || typeResult === void 0 ? void 0 : typeResult.isIncomplete) {
3829
+ isTypeIncomplete = true;
3900
3830
  }
3901
- const descriptorResult = applyDescriptorAccessMethod(type, memberInfo, classType, bindToType, isAccessedThroughObject, flags, errorNode, memberName, usage, diag);
3902
- if (!descriptorResult) {
3831
+ }
3832
+ // Don't include variables within typed dict classes.
3833
+ if ((0, types_1.isClass)(memberInfo.classType) && types_1.ClassType.isTypedDictClass(memberInfo.classType)) {
3834
+ const typedDecls = memberInfo.symbol.getTypedDeclarations();
3835
+ if (typedDecls.length > 0 && typedDecls[0].type === 1 /* Variable */) {
3836
+ diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.Localizer.DiagnosticAddendum.memberUnknown().format({ name: memberName }));
3903
3837
  return undefined;
3904
3838
  }
3905
- type = descriptorResult.type;
3906
- let isSetTypeError = false;
3839
+ }
3840
+ if (usage.method === 'get') {
3841
+ // Mark the member accessed if it's not coming from a parent class.
3842
+ if ((0, types_1.isInstantiableClass)(memberInfo.classType) &&
3843
+ types_1.ClassType.isSameGenericClass(memberInfo.classType, classType)) {
3844
+ setSymbolAccessed(AnalyzerNodeInfo.getFileInfo(errorNode), memberInfo.symbol, errorNode);
3845
+ }
3846
+ // Special-case `__init_subclass` and `__class_getitem__` because
3847
+ // these are always treated as class methods even if they're not
3848
+ // decorated as such.
3849
+ if (memberName === '__init_subclass__' || memberName === '__class_getitem__') {
3850
+ if ((0, types_1.isFunction)(type) && !types_1.FunctionType.isClassMethod(type)) {
3851
+ type = types_1.FunctionType.cloneWithNewFlags(type, type.details.flags | 2 /* ClassMethod */);
3852
+ }
3853
+ }
3854
+ }
3855
+ const descResult = applyDescriptorAccessMethod(type, memberInfo, classType, selfType, flags, errorNode, memberName, usage, diag);
3856
+ let isDescriptorError = true;
3857
+ if (descResult) {
3858
+ isDescriptorError = false;
3859
+ type = descResult.type;
3907
3860
  if (usage.method === 'set' && usage.setType) {
3908
3861
  // Verify that the assigned type is compatible.
3909
3862
  if (!assignType(type, usage.setType.type, diag === null || diag === void 0 ? void 0 : diag.createAddendum())) {
@@ -3914,7 +3867,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3914
3867
  classType: printObjectTypeForClass(classType),
3915
3868
  }));
3916
3869
  }
3917
- isSetTypeError = true;
3870
+ isDescriptorError = true;
3918
3871
  }
3919
3872
  if ((0, types_1.isInstantiableClass)(memberInfo.classType) &&
3920
3873
  types_1.ClassType.isFrozenDataClass(memberInfo.classType) &&
@@ -3922,47 +3875,28 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3922
3875
  diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.Localizer.DiagnosticAddendum.dataClassFrozen().format({
3923
3876
  name: printType(types_1.ClassType.cloneAsInstance(memberInfo.classType)),
3924
3877
  }));
3925
- isSetTypeError = true;
3878
+ isDescriptorError = true;
3926
3879
  }
3927
3880
  }
3928
- return {
3929
- symbol: memberInfo.symbol,
3930
- type,
3931
- isTypeIncomplete,
3932
- isSetTypeError,
3933
- isClassMember: !memberInfo.isInstanceMember,
3934
- isClassVar: memberInfo.isClassVar,
3935
- classType: memberInfo.classType,
3936
- isAsymmetricAccessor: descriptorResult.isAsymmetricAccessor,
3937
- memberAccessDeprecationInfo: descriptorResult === null || descriptorResult === void 0 ? void 0 : descriptorResult.memberAccessDeprecationInfo,
3938
- };
3939
- }
3940
- // No attribute of that name was found. If this is a member access
3941
- // through an object, see if there's an attribute access override
3942
- // method ("__getattr__", etc.).
3943
- if ((flags & (1 /* AccessClassMembersOnly */ | 128 /* SkipAttributeAccessOverride */)) ===
3944
- 0) {
3945
- const generalAttrType = applyAttributeAccessOverride(classType, errorNode, usage, memberName);
3946
- if (generalAttrType) {
3947
- return {
3948
- symbol: undefined,
3949
- type: generalAttrType.type,
3950
- isTypeIncomplete: false,
3951
- isSetTypeError: false,
3952
- isClassMember: false,
3953
- isClassVar: false,
3954
- isAsymmetricAccessor: generalAttrType.isAsymmetricAccessor,
3955
- };
3956
- }
3957
3881
  }
3958
- diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.Localizer.DiagnosticAddendum.memberUnknown().format({ name: memberName }));
3959
- return undefined;
3882
+ return {
3883
+ symbol: memberInfo.symbol,
3884
+ type,
3885
+ isTypeIncomplete,
3886
+ isDescriptorError,
3887
+ isClassMember: !memberInfo.isInstanceMember,
3888
+ isClassVar: memberInfo.isClassVar,
3889
+ classType: memberInfo.classType,
3890
+ isAsymmetricAccessor: (_d = descResult === null || descResult === void 0 ? void 0 : descResult.isAsymmetricAccessor) !== null && _d !== void 0 ? _d : false,
3891
+ memberAccessDeprecationInfo: descResult === null || descResult === void 0 ? void 0 : descResult.memberAccessDeprecationInfo,
3892
+ };
3960
3893
  }
3961
3894
  // Applies descriptor access methods "__get__", "__set__", or "__delete__"
3962
3895
  // if they apply. Also binds methods to the class/object through which it
3963
3896
  // is accessed.
3964
- function applyDescriptorAccessMethod(type, memberInfo, baseTypeClass, bindToType, isAccessedThroughObject, flags, errorNode, memberName, usage, diag) {
3965
- const treatConstructorAsClassMember = (flags & 32 /* TreatConstructorAsClassMethod */) !== 0;
3897
+ function applyDescriptorAccessMethod(type, memberInfo, classType, selfType, flags, errorNode, memberName, usage, diag) {
3898
+ const treatConstructorAsClassMember = (flags & 256 /* TreatConstructorAsClassMethod */) !== 0;
3899
+ const isAccessedThroughObject = types_1.TypeBase.isInstance(classType);
3966
3900
  let isTypeValid = true;
3967
3901
  let isAsymmetricAccessor = false;
3968
3902
  let memberAccessDeprecationInfo;
@@ -4004,7 +3938,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4004
3938
  else {
4005
3939
  accessMethodName = '__delete__';
4006
3940
  }
4007
- const accessMethod = (0, typeUtils_1.lookUpClassMember)(lookupClass, accessMethodName, 8 /* SkipInstanceVariables */);
3941
+ const accessMethod = (0, typeUtils_1.lookUpClassMember)(lookupClass, accessMethodName, 16 /* SkipInstanceMembers */);
4008
3942
  // Handle properties specially.
4009
3943
  if (types_1.ClassType.isPropertyClass(lookupClass)) {
4010
3944
  if (usage.method === 'set') {
@@ -4034,10 +3968,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4034
3968
  argumentCategory: 0 /* Simple */,
4035
3969
  typeResult: {
4036
3970
  type: types_1.ClassType.isClassProperty(lookupClass)
4037
- ? baseTypeClass
3971
+ ? classType
4038
3972
  : isAccessedThroughObject
4039
- ? bindToType !== null && bindToType !== void 0 ? bindToType : types_1.ClassType.cloneAsInstance(baseTypeClass)
4040
- : types_1.NoneType.createInstance(),
3973
+ ? selfType !== null && selfType !== void 0 ? selfType : types_1.ClassType.cloneAsInstance(classType)
3974
+ : getNoneType(),
4041
3975
  },
4042
3976
  },
4043
3977
  ];
@@ -4046,7 +3980,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4046
3980
  argList.push({
4047
3981
  argumentCategory: 0 /* Simple */,
4048
3982
  typeResult: {
4049
- type: baseTypeClass,
3983
+ type: isAccessedThroughObject
3984
+ ? types_1.ClassType.cloneAsInstantiable(classType)
3985
+ : classType,
4050
3986
  },
4051
3987
  });
4052
3988
  }
@@ -4075,8 +4011,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4075
4011
  // flag this as an error because a property within a protocol is meant to be
4076
4012
  // interpreted as a read-only attribute rather than a protocol, so accessing
4077
4013
  // it directly from the class has an ambiguous meaning.
4078
- if ((flags & 1 /* AccessClassMembersOnly */) !== 0 &&
4079
- types_1.ClassType.isProtocolClass(baseTypeClass)) {
4014
+ if ((flags & 16 /* SkipInstanceMembers */) !== 0 &&
4015
+ types_1.ClassType.isProtocolClass(classType)) {
4080
4016
  diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.Localizer.DiagnosticAddendum.propertyAccessFromProtocolClass());
4081
4017
  isTypeValid = false;
4082
4018
  }
@@ -4095,12 +4031,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4095
4031
  // because of the way we model the __get__ logic in the property class.
4096
4032
  if (types_1.ClassType.isPropertyClass(concreteSubtype) && !isAccessedThroughMetaclass) {
4097
4033
  if (memberInfo && (0, types_1.isInstantiableClass)(memberInfo.classType)) {
4098
- bindToClass = memberInfo.classType;
4099
- }
4100
- }
4101
- else {
4102
- if ((0, types_1.isInstantiableClass)(accessMethod.classType)) {
4103
- bindToClass = accessMethod.classType;
4034
+ bindToClass = types_1.ClassType.cloneAsInstance(memberInfo.classType);
4104
4035
  }
4105
4036
  }
4106
4037
  let boundMethodType = bindFunctionToClassOrObjectWithErrors(lookupClass, methodType, bindToClass, errorNode,
@@ -4112,7 +4043,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4112
4043
  ((0, types_1.isFunction)(boundMethodType) || (0, types_1.isOverloadedFunction)(boundMethodType))) {
4113
4044
  const typeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(boundMethodType));
4114
4045
  if (bindToClass) {
4115
- const specializedBoundType = (0, typeUtils_1.partiallySpecializeType)(boundMethodType, bindToClass, baseTypeClass);
4046
+ const specializedBoundType = (0, typeUtils_1.partiallySpecializeType)(boundMethodType, bindToClass, classType);
4116
4047
  if (specializedBoundType) {
4117
4048
  if ((0, types_1.isFunction)(specializedBoundType) ||
4118
4049
  (0, types_1.isOverloadedFunction)(specializedBoundType)) {
@@ -4151,6 +4082,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4151
4082
  diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.Localizer.Diagnostic.noOverload().format({ name: accessMethodName }));
4152
4083
  }
4153
4084
  }
4085
+ else {
4086
+ diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.Localizer.DiagnosticAddendum.descriptorAccessCallFailed().format({
4087
+ name: accessMethodName,
4088
+ className: printType((0, typeUtils_1.convertToInstance)(accessMethod.classType)),
4089
+ }));
4090
+ }
4154
4091
  isTypeValid = false;
4155
4092
  return types_1.AnyType.create();
4156
4093
  }
@@ -4159,6 +4096,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4159
4096
  ? (_a = callResult.returnType) !== null && _a !== void 0 ? _a : types_1.UnknownType.create()
4160
4097
  : types_1.AnyType.create();
4161
4098
  }
4099
+ diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.Localizer.DiagnosticAddendum.descriptorAccessBindingFailed().format({
4100
+ name: accessMethodName,
4101
+ className: printType((0, typeUtils_1.convertToInstance)(accessMethod.classType)),
4102
+ }));
4103
+ isTypeValid = false;
4162
4104
  return undefined;
4163
4105
  });
4164
4106
  // Determine if we're calling __set__ on an asymmetric descriptor or property.
@@ -4199,29 +4141,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4199
4141
  // If this function is an instance member (e.g. a lambda that was
4200
4142
  // assigned to an instance variable), don't perform any binding.
4201
4143
  if (!isAccessedThroughObject || (memberInfo && !memberInfo.isInstanceMember)) {
4202
- let effectiveBindToType = bindToType;
4203
- if (bindToType && !(0, typeUtils_1.isInstantiableMetaclass)(baseTypeClass)) {
4204
- // If bindToType is an instantiable class or TypeVar but we're targeting
4205
- // an instance method (in a non-metaclass), we need to convert
4206
- // the bindToType to an instance.
4207
- const targetMethod = (0, types_1.isFunction)(concreteSubtype)
4208
- ? concreteSubtype
4209
- : concreteSubtype.overloads[0];
4210
- if (types_1.FunctionType.isInstanceMethod(targetMethod) && !types_1.TypeBase.isInstance(bindToType)) {
4211
- effectiveBindToType = (0, typeUtils_1.convertToInstance)(bindToType);
4212
- }
4213
- }
4214
- // If the bind-to type is a specific class, add the "includeSubclasses" flag
4215
- // to the type to indicate that it could be a subclass.
4216
- if (effectiveBindToType && (0, types_1.isClass)(effectiveBindToType)) {
4217
- effectiveBindToType = types_1.ClassType.cloneIncludeSubclasses(effectiveBindToType);
4218
- }
4219
- return bindFunctionToClassOrObjectWithErrors(isAccessedThroughObject ? types_1.ClassType.cloneAsInstance(baseTypeClass) : baseTypeClass, concreteSubtype, memberInfo && (0, types_1.isInstantiableClass)(memberInfo.classType) ? memberInfo.classType : undefined, errorNode, treatConstructorAsClassMember, effectiveBindToType);
4144
+ return bindFunctionToClassOrObjectWithErrors(classType, concreteSubtype, memberInfo && (0, types_1.isInstantiableClass)(memberInfo.classType) ? memberInfo.classType : undefined, errorNode, treatConstructorAsClassMember, selfType && (0, types_1.isClass)(selfType) ? types_1.ClassType.cloneIncludeSubclasses(selfType) : selfType);
4220
4145
  }
4221
4146
  }
4222
4147
  if (usage.method === 'set') {
4223
4148
  if (memberInfo === null || memberInfo === void 0 ? void 0 : memberInfo.symbol.isClassVar()) {
4224
- if (flags & 16 /* DisallowClassVarWrites */) {
4149
+ if (flags & 128 /* DisallowClassVarWrites */) {
4225
4150
  diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.Localizer.DiagnosticAddendum.memberSetClassVar().format({ name: memberName }));
4226
4151
  isTypeValid = false;
4227
4152
  return undefined;
@@ -4351,14 +4276,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4351
4276
  return isAsymmetric;
4352
4277
  }
4353
4278
  // Applies the __getattr__, __setattr__ or __delattr__ method if present.
4354
- function applyAttributeAccessOverride(classType, errorNode, usage, memberName) {
4279
+ function applyAttributeAccessOverride(classType, errorNode, usage, memberName, selfType) {
4355
4280
  var _a, _b, _c, _d, _e;
4356
4281
  const getAttributeAccessMember = (name) => {
4357
4282
  var _a;
4358
- // See if the class has a "__getattribute__" or "__getattr__" method.
4359
- // If so, arbitrary members are supported.
4360
- return (_a = getTypeOfClassMember(errorNode, classType, name, { method: 'get' },
4361
- /* diag */ undefined, 8 /* SkipObjectBaseClass */ | 128 /* SkipAttributeAccessOverride */)) === null || _a === void 0 ? void 0 : _a.type;
4283
+ return (_a = getTypeOfObjectMember(errorNode, classType, name, { method: 'get' },
4284
+ /* diag */ undefined, 16 /* SkipInstanceMembers */ |
4285
+ 4 /* SkipObjectBaseClass */ |
4286
+ 512 /* SkipAttributeAccessOverride */, selfType)) === null || _a === void 0 ? void 0 : _a.type;
4362
4287
  };
4363
4288
  let accessMemberType;
4364
4289
  if (usage.method === 'get') {
@@ -4371,51 +4296,47 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4371
4296
  (0, debug_1.assert)(usage.method === 'del');
4372
4297
  accessMemberType = getAttributeAccessMember('__delattr__');
4373
4298
  }
4374
- if (accessMemberType) {
4375
- let nameLiteralType = types_1.AnyType.create();
4376
- if (strClassType && (0, types_1.isInstantiableClass)(strClassType)) {
4377
- nameLiteralType = types_1.ClassType.cloneWithLiteral(types_1.ClassType.cloneAsInstance(strClassType), memberName);
4378
- }
4379
- const argList = [
4380
- {
4381
- // Provide "self" argument.
4382
- argumentCategory: 0 /* Simple */,
4383
- typeResult: { type: types_1.ClassType.cloneAsInstance(classType) },
4384
- },
4385
- {
4386
- // Provide "name" argument.
4387
- argumentCategory: 0 /* Simple */,
4388
- typeResult: { type: nameLiteralType },
4299
+ if (!accessMemberType) {
4300
+ return undefined;
4301
+ }
4302
+ let nameLiteralType = types_1.AnyType.create();
4303
+ if (strClassType && (0, types_1.isInstantiableClass)(strClassType)) {
4304
+ nameLiteralType = types_1.ClassType.cloneWithLiteral(types_1.ClassType.cloneAsInstance(strClassType), memberName);
4305
+ }
4306
+ const argList = [
4307
+ {
4308
+ // Provide "name" argument.
4309
+ argumentCategory: 0 /* Simple */,
4310
+ typeResult: { type: nameLiteralType },
4311
+ },
4312
+ ];
4313
+ if (usage.method === 'set') {
4314
+ argList.push({
4315
+ // Provide "value" argument.
4316
+ argumentCategory: 0 /* Simple */,
4317
+ typeResult: {
4318
+ type: (_c = (_b = usage.setType) === null || _b === void 0 ? void 0 : _b.type) !== null && _c !== void 0 ? _c : types_1.UnknownType.create(),
4319
+ isIncomplete: !!((_d = usage.setType) === null || _d === void 0 ? void 0 : _d.isIncomplete),
4389
4320
  },
4390
- ];
4391
- if (usage.method === 'set') {
4392
- argList.push({
4393
- // Provide "value" argument.
4394
- argumentCategory: 0 /* Simple */,
4395
- typeResult: {
4396
- type: (_c = (_b = usage.setType) === null || _b === void 0 ? void 0 : _b.type) !== null && _c !== void 0 ? _c : types_1.UnknownType.create(),
4397
- isIncomplete: !!((_d = usage.setType) === null || _d === void 0 ? void 0 : _d.isIncomplete),
4398
- },
4399
- });
4400
- }
4401
- if ((0, types_1.isFunction)(accessMemberType) || (0, types_1.isOverloadedFunction)(accessMemberType)) {
4402
- const boundMethodType = bindFunctionToClassOrObjectWithErrors(classType, accessMemberType, classType, errorNode);
4403
- if (boundMethodType && ((0, types_1.isFunction)(boundMethodType) || (0, types_1.isOverloadedFunction)(boundMethodType))) {
4404
- const typeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(boundMethodType));
4405
- const callResult = validateCallArguments(errorNode, argList, { type: boundMethodType }, typeVarContext,
4406
- /* skipUnknownArgCheck */ true);
4407
- let isAsymmetricAccessor = false;
4408
- if (usage.method === 'set') {
4409
- isAsymmetricAccessor = isClassWithAsymmetricAttributeAccessor(classType);
4410
- }
4411
- return {
4412
- type: (_e = callResult.returnType) !== null && _e !== void 0 ? _e : types_1.UnknownType.create(),
4413
- isAsymmetricAccessor,
4414
- };
4415
- }
4416
- }
4321
+ });
4417
4322
  }
4418
- return undefined;
4323
+ if (!(0, types_1.isFunction)(accessMemberType) && !(0, types_1.isOverloadedFunction)(accessMemberType)) {
4324
+ // TODO - need to handle and report this error.
4325
+ return undefined;
4326
+ }
4327
+ const typeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(accessMemberType));
4328
+ const callResult = validateCallArguments(errorNode, argList, { type: accessMemberType }, typeVarContext,
4329
+ /* skipUnknownArgCheck */ true);
4330
+ // TODO - need to handle and report errors when validating call to
4331
+ // attribute access method.
4332
+ let isAsymmetricAccessor = false;
4333
+ if (usage.method === 'set') {
4334
+ isAsymmetricAccessor = isClassWithAsymmetricAttributeAccessor(classType);
4335
+ }
4336
+ return {
4337
+ type: (_e = callResult.returnType) !== null && _e !== void 0 ? _e : types_1.UnknownType.create(),
4338
+ isAsymmetricAccessor,
4339
+ };
4419
4340
  }
4420
4341
  function getTypeOfIndex(node, flags = 0 /* None */) {
4421
4342
  const baseTypeResult = getTypeOfExpression(node.baseExpression, flags | 2 /* IndexBaseDefaults */);
@@ -4771,9 +4692,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4771
4692
  (0, types_1.isInstantiableClass)(concreteSubtype.details.effectiveMetaclass) &&
4772
4693
  !types_1.ClassType.isBuiltIn(concreteSubtype.details.effectiveMetaclass, ['type', '_InitVarMeta']) &&
4773
4694
  (flags & 128 /* ExpectingInstantiableType */) === 0) {
4774
- const itemMethodType = getTypeOfClassMember(node, concreteSubtype, getIndexAccessMagicMethodName(usage),
4695
+ const itemMethodType = getTypeOfObjectMember(node, concreteSubtype, getIndexAccessMagicMethodName(usage),
4775
4696
  /* usage */ undefined,
4776
- /* diag */ undefined, 128 /* SkipAttributeAccessOverride */ | 64 /* ConsiderMetaclassOnly */);
4697
+ /* diag */ undefined, 512 /* SkipAttributeAccessOverride */);
4777
4698
  if ((flags & 256 /* ExpectingTypeAnnotation */) !== 0) {
4778
4699
  // If the class doesn't derive from Generic, a type argument should not be allowed.
4779
4700
  addDiagnostic(AnalyzerNodeInfo.getFileInfo(node).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeArgsExpectingNone().format({
@@ -4861,6 +4782,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4861
4782
  }
4862
4783
  return result.type;
4863
4784
  }
4785
+ if ((0, typeUtils_1.isNoneInstance)(concreteSubtype)) {
4786
+ if (!isIncomplete) {
4787
+ addDiagnostic(AnalyzerNodeInfo.getFileInfo(node).diagnosticRuleSet.reportOptionalSubscript, diagnosticRules_1.DiagnosticRule.reportOptionalSubscript, localize_1.Localizer.Diagnostic.noneNotSubscriptable(), node.baseExpression);
4788
+ }
4789
+ return types_1.UnknownType.create();
4790
+ }
4864
4791
  if ((0, types_1.isClassInstance)(concreteSubtype)) {
4865
4792
  const typeResult = getTypeOfIndexedObjectOrClass(node, concreteSubtype, usage);
4866
4793
  if (typeResult.isIncomplete) {
@@ -4871,10 +4798,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4871
4798
  if ((0, types_1.isNever)(concreteSubtype) || (0, types_1.isUnbound)(concreteSubtype)) {
4872
4799
  return types_1.NeverType.createNever();
4873
4800
  }
4874
- if ((0, types_1.isNoneInstance)(concreteSubtype) && !isIncomplete) {
4875
- addDiagnostic(AnalyzerNodeInfo.getFileInfo(node).diagnosticRuleSet.reportOptionalSubscript, diagnosticRules_1.DiagnosticRule.reportOptionalSubscript, localize_1.Localizer.Diagnostic.noneNotSubscriptable(), node.baseExpression);
4876
- return types_1.UnknownType.create();
4877
- }
4878
4801
  if (!isIncomplete) {
4879
4802
  const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
4880
4803
  addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeNotSubscriptable().format({ type: printType(concreteSubtype) }), node.baseExpression);
@@ -4937,7 +4860,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4937
4860
  });
4938
4861
  }
4939
4862
  (0, typeUtils_1.doForEachSubtype)(type, (subtype) => {
4940
- if (subtype.category === 5 /* Function */) {
4863
+ if (subtype.category === 4 /* Function */) {
4941
4864
  if (subtype.specializedTypes) {
4942
4865
  subtype.specializedTypes.parameterTypes.forEach((paramType) => {
4943
4866
  updateUsageVarianceForType(paramType, 4 /* Contravariant */);
@@ -4948,7 +4871,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4948
4871
  }
4949
4872
  }
4950
4873
  }
4951
- else if (subtype.category === 7 /* Class */) {
4874
+ else if (subtype.category === 6 /* Class */) {
4952
4875
  if (subtype.typeArguments) {
4953
4876
  // If the class includes type parameters that uses auto variance,
4954
4877
  // compute the calculated variance.
@@ -4997,7 +4920,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4997
4920
  }
4998
4921
  }
4999
4922
  function getTypeOfIndexedObjectOrClass(node, baseType, usage) {
5000
- var _a, _b, _c, _d, _e, _f;
4923
+ var _a, _b, _c, _d, _e;
5001
4924
  // Handle index operations for TypedDict classes specially.
5002
4925
  if ((0, types_1.isClassInstance)(baseType) && types_1.ClassType.isTypedDictClass(baseType)) {
5003
4926
  const typeFromTypedDict = (0, typedDicts_1.getTypeOfIndexedTypedDict)(evaluatorInterface, node, baseType, usage);
@@ -5006,13 +4929,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5006
4929
  }
5007
4930
  }
5008
4931
  const magicMethodName = getIndexAccessMagicMethodName(usage);
5009
- const itemMethodType = (0, types_1.isClassInstance)(baseType)
5010
- ? (_a = getTypeOfObjectMember(node, baseType, magicMethodName,
5011
- /* usage */ undefined,
5012
- /* diag */ undefined, 128 /* SkipAttributeAccessOverride */)) === null || _a === void 0 ? void 0 : _a.type
5013
- : (_b = getTypeOfClassMember(node, baseType, magicMethodName,
5014
- /* usage */ undefined,
5015
- /* diag */ undefined, 128 /* SkipAttributeAccessOverride */ | 64 /* ConsiderMetaclassOnly */)) === null || _b === void 0 ? void 0 : _b.type;
4932
+ const itemMethodType = (_a = getTypeOfObjectMember(node, baseType, magicMethodName,
4933
+ /* usage */ undefined,
4934
+ /* diag */ undefined, 512 /* SkipAttributeAccessOverride */)) === null || _a === void 0 ? void 0 : _a.type;
5016
4935
  if (!itemMethodType) {
5017
4936
  const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
5018
4937
  addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.methodNotDefinedOnType().format({
@@ -5141,7 +5060,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5141
5060
  },
5142
5061
  ];
5143
5062
  if (usage.method === 'set') {
5144
- let setType = (_d = (_c = usage.setType) === null || _c === void 0 ? void 0 : _c.type) !== null && _d !== void 0 ? _d : types_1.AnyType.create();
5063
+ let setType = (_c = (_b = usage.setType) === null || _b === void 0 ? void 0 : _b.type) !== null && _c !== void 0 ? _c : types_1.AnyType.create();
5145
5064
  // Expand constrained type variables.
5146
5065
  if ((0, types_1.isTypeVar)(setType) && setType.details.constraints.length > 0) {
5147
5066
  const conditionFilter = (0, types_1.isClassInstance)(baseType) ? baseType.condition : undefined;
@@ -5152,7 +5071,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5152
5071
  argumentCategory: 0 /* Simple */,
5153
5072
  typeResult: {
5154
5073
  type: setType,
5155
- isIncomplete: !!((_e = usage.setType) === null || _e === void 0 ? void 0 : _e.isIncomplete),
5074
+ isIncomplete: !!((_d = usage.setType) === null || _d === void 0 ? void 0 : _d.isIncomplete),
5156
5075
  },
5157
5076
  });
5158
5077
  }
@@ -5202,7 +5121,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5202
5121
  }
5203
5122
  callResult = validateCallArguments(node, argList, { type: itemMethodType });
5204
5123
  return {
5205
- type: (_f = callResult.returnType) !== null && _f !== void 0 ? _f : types_1.UnknownType.create(),
5124
+ type: (_e = callResult.returnType) !== null && _e !== void 0 ? _e : types_1.UnknownType.create(),
5206
5125
  isIncomplete: !!callResult.isTypeIncomplete,
5207
5126
  };
5208
5127
  }
@@ -5531,7 +5450,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5531
5450
  else {
5532
5451
  baseTypeResult = getTypeOfExpression(node.leftExpression, 16777218 /* CallBaseDefaults */ | (flags & 4 /* AllowForwardReferences */));
5533
5452
  }
5534
- const argList = node.arguments.map((arg) => {
5453
+ const argList = ParseTreeUtils.getArgumentsByRuntimeOrder(node).map((arg) => {
5535
5454
  const functionArg = {
5536
5455
  valueExpression: arg.valueExpression,
5537
5456
  argumentCategory: arg.argumentCategory,
@@ -5792,7 +5711,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5792
5711
  else {
5793
5712
  addInformation(localize_1.Localizer.Diagnostic.revealLocalsNone(), node);
5794
5713
  }
5795
- return types_1.NoneType.createInstance();
5714
+ return getNoneType();
5796
5715
  }
5797
5716
  function getTypeOfSuperCall(node) {
5798
5717
  var _a;
@@ -5817,6 +5736,16 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5817
5736
  else {
5818
5737
  if (enclosingClassType) {
5819
5738
  targetClassType = enclosingClassType !== null && enclosingClassType !== void 0 ? enclosingClassType : types_1.UnknownType.create();
5739
+ // Zero-argument forms of super are not allowed within static methods.
5740
+ // This results in a runtime exception.
5741
+ if (enclosingFunction) {
5742
+ const functionInfo = (0, decorators_1.getFunctionInfoFromDecorators)(evaluatorInterface, enclosingFunction,
5743
+ /* isInClass */ true);
5744
+ if (((functionInfo === null || functionInfo === void 0 ? void 0 : functionInfo.flags) & 4 /* StaticMethod */) !== 0) {
5745
+ addError(localize_1.Localizer.Diagnostic.superCallZeroArgFormStaticMethod(), node.leftExpression);
5746
+ targetClassType = types_1.UnknownType.create();
5747
+ }
5748
+ }
5820
5749
  }
5821
5750
  else {
5822
5751
  addError(localize_1.Localizer.Diagnostic.superCallZeroArgForm(), node.leftExpression);
@@ -5912,8 +5841,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5912
5841
  type: resultIsInstance
5913
5842
  ? types_1.ClassType.cloneAsInstance(lookupResults.classType)
5914
5843
  : lookupResults.classType,
5915
- bindToType: bindToType
5916
- ? types_1.TypeBase.cloneForCondition((0, typeUtils_1.synthesizeTypeVarForSelfCls)(bindToType, !resultIsInstance), bindToType.condition)
5844
+ bindToSelfType: bindToType
5845
+ ? types_1.TypeBase.cloneForCondition((0, typeUtils_1.synthesizeTypeVarForSelfCls)(bindToType, /* isClsParam */ false), bindToType.condition)
5917
5846
  : undefined,
5918
5847
  };
5919
5848
  }
@@ -6403,7 +6332,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6403
6332
  }, {
6404
6333
  allowDiagnostics: true,
6405
6334
  });
6406
- });
6335
+ },
6336
+ /* sortSubtypes */ true);
6407
6337
  // If we ended up with a "Never" type because all code paths returned
6408
6338
  // undefined due to argument errors, transform the result into an Unknown
6409
6339
  // to avoid subsequent false positives.
@@ -6420,7 +6350,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6420
6350
  }
6421
6351
  function validateCallArgumentsForSubtype(errorNode, argList, expandedCallType, unexpandedCallType, isCallTypeIncomplete, typeVarContext, skipUnknownArgCheck, inferenceContext, recursionCount) {
6422
6352
  switch (expandedCallType.category) {
6423
- case 4 /* Never */:
6353
+ case 3 /* Never */:
6424
6354
  case 1 /* Unknown */:
6425
6355
  case 2 /* Any */: {
6426
6356
  // Touch all of the args so they're marked accessed. Don't bother
@@ -6435,37 +6365,30 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6435
6365
  }
6436
6366
  return { returnType: expandedCallType };
6437
6367
  }
6438
- case 5 /* Function */: {
6368
+ case 4 /* Function */: {
6439
6369
  return validateCallForFunction(errorNode, argList, expandedCallType, isCallTypeIncomplete, typeVarContext, skipUnknownArgCheck, inferenceContext);
6440
6370
  }
6441
- case 6 /* OverloadedFunction */: {
6371
+ case 5 /* OverloadedFunction */: {
6442
6372
  return validateCallForOverloadedFunction(errorNode, argList, expandedCallType, isCallTypeIncomplete, typeVarContext, skipUnknownArgCheck, inferenceContext);
6443
6373
  }
6444
- case 7 /* Class */: {
6374
+ case 6 /* Class */: {
6375
+ if ((0, typeUtils_1.isNoneInstance)(expandedCallType)) {
6376
+ addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportOptionalCall, diagnosticRules_1.DiagnosticRule.reportOptionalCall, localize_1.Localizer.Diagnostic.noneNotCallable(), errorNode);
6377
+ return { argumentErrors: true };
6378
+ }
6445
6379
  if (types_1.TypeBase.isInstantiable(expandedCallType)) {
6446
6380
  return validateCallForInstantiableClass(errorNode, argList, expandedCallType, unexpandedCallType, skipUnknownArgCheck, inferenceContext);
6447
6381
  }
6448
6382
  return validateCallForClassInstance(errorNode, argList, expandedCallType, unexpandedCallType, typeVarContext, skipUnknownArgCheck, inferenceContext, recursionCount);
6449
6383
  }
6450
- case 3 /* None */: {
6451
- if (types_1.TypeBase.isInstantiable(expandedCallType)) {
6452
- if (noneType && (0, types_1.isInstantiableClass)(noneType)) {
6453
- const callResult = validateCallForInstantiableClass(errorNode, argList, noneType, noneType, skipUnknownArgCheck, inferenceContext);
6454
- return { ...callResult, returnType: types_1.NoneType.createInstance() };
6455
- }
6456
- return { returnType: types_1.NoneType.createInstance() };
6457
- }
6458
- addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportOptionalCall, diagnosticRules_1.DiagnosticRule.reportOptionalCall, localize_1.Localizer.Diagnostic.noneNotCallable(), errorNode);
6459
- return { argumentErrors: true };
6460
- }
6461
6384
  // TypeVars should have been expanded in most cases,
6462
6385
  // but we still need to handle the case of Type[T] where
6463
6386
  // T is a constrained type that contains a union. We also
6464
6387
  // need to handle recursive type aliases.
6465
- case 10 /* TypeVar */: {
6388
+ case 9 /* TypeVar */: {
6466
6389
  return validateCallArguments(errorNode, argList, { type: (0, typeUtils_1.transformPossibleRecursiveTypeAlias)(expandedCallType), isIncomplete: isCallTypeIncomplete }, typeVarContext, skipUnknownArgCheck, inferenceContext, recursionCount);
6467
6390
  }
6468
- case 8 /* Module */: {
6391
+ case 7 /* Module */: {
6469
6392
  addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.moduleNotCallable(), errorNode);
6470
6393
  return { argumentErrors: true };
6471
6394
  }
@@ -6596,7 +6519,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6596
6519
  const returnType = (0, typeUtils_1.mapSubtypes)(argType, (subtype) => {
6597
6520
  if ((0, types_1.isClassInstance)(subtype) ||
6598
6521
  ((0, types_1.isTypeVar)(subtype) && types_1.TypeBase.isInstance(subtype)) ||
6599
- (0, types_1.isNoneInstance)(subtype)) {
6522
+ (0, typeUtils_1.isNoneInstance)(subtype)) {
6600
6523
  return (0, typeUtils_1.convertToInstantiable)(stripLiteralValue(subtype));
6601
6524
  }
6602
6525
  else if ((0, types_1.isFunction)(subtype) && types_1.TypeBase.isInstance(subtype)) {
@@ -6744,7 +6667,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6744
6667
  var _a, _b;
6745
6668
  const memberType = (_a = getTypeOfObjectMember(errorNode, expandedCallType, '__call__',
6746
6669
  /* usage */ undefined,
6747
- /* diag */ undefined, 128 /* SkipAttributeAccessOverride */ | 1 /* AccessClassMembersOnly */)) === null || _a === void 0 ? void 0 : _a.type;
6670
+ /* diag */ undefined, 512 /* SkipAttributeAccessOverride */ | 16 /* SkipInstanceMembers */)) === null || _a === void 0 ? void 0 : _a.type;
6748
6671
  if (!memberType) {
6749
6672
  addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.objectNotCallable().format({
6750
6673
  type: printType(expandedCallType),
@@ -7006,8 +6929,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
7006
6929
  // with a ParamSpec and a Concatenate operator. PEP 612 indicates that
7007
6930
  // all positional parameters specified in the Concatenate must be
7008
6931
  // filled explicitly.
7009
- if (typeResult.type.details.paramSpec && paramIndex < positionParamLimitIndex) {
7010
- if ((0, types_1.isTypeVar)(argTypeResult.type) && argTypeResult.type.paramSpecAccess === 'args') {
6932
+ if (paramIndex < positionParamLimitIndex) {
6933
+ if ((0, types_1.isTypeVar)(argTypeResult.type) &&
6934
+ argTypeResult.type.paramSpecAccess === 'args' &&
6935
+ paramDetails.params[paramIndex].param.category !== 1 /* ArgsList */) {
7011
6936
  if (!isDiagnosticSuppressedForNode(errorNode)) {
7012
6937
  addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, positionParamLimitIndex === 1
7013
6938
  ? localize_1.Localizer.Diagnostic.argPositionalExpectedOne()
@@ -8125,6 +8050,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8125
8050
  return { argumentErrors: paramSpecArgResult.argumentErrors, typeVarContexts };
8126
8051
  }
8127
8052
  function validateFunctionArgumentsForParamSpecSignature(errorNode, argList, paramSpec, typeVarContext, signatureTracker) {
8053
+ var _a;
8128
8054
  let paramSpecType = typeVarContext.getParamSpecType(paramSpec);
8129
8055
  if (!paramSpecType) {
8130
8056
  paramSpecType = (0, typeUtils_1.convertTypeToParamSpecValue)(paramSpec);
@@ -8147,8 +8073,37 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8147
8073
  if (functionType.details.paramSpec &&
8148
8074
  functionType.details.parameters.length === 0 &&
8149
8075
  (0, types_1.isTypeSame)(functionType.details.paramSpec, paramSpec)) {
8150
- // TODO - need to perform additional validation here.
8151
- return { argumentErrors: false, typeVarContexts: [srcTypeVarContext] };
8076
+ // If there are any arguments other than *args: P.args or **kwargs: P.kwargs,
8077
+ // report an error.
8078
+ let sawArgs = false;
8079
+ let sawKwargs = false;
8080
+ let argumentErrors = false;
8081
+ let argErrorNode;
8082
+ for (const arg of argList) {
8083
+ const argType = (_a = getTypeOfArgument(arg)) === null || _a === void 0 ? void 0 : _a.type;
8084
+ const isArgTypeCompatible = argType && ((0, types_1.isTypeSame)(argType, paramSpec) || (0, types_1.isAnyOrUnknown)(argType));
8085
+ if (arg.argumentCategory === 1 /* UnpackedList */ && !sawArgs && isArgTypeCompatible) {
8086
+ sawArgs = true;
8087
+ }
8088
+ else if (arg.argumentCategory === 2 /* UnpackedDictionary */ &&
8089
+ !sawKwargs &&
8090
+ isArgTypeCompatible) {
8091
+ sawKwargs = true;
8092
+ }
8093
+ else {
8094
+ argErrorNode = argErrorNode !== null && argErrorNode !== void 0 ? argErrorNode : arg.valueExpression;
8095
+ argumentErrors = true;
8096
+ }
8097
+ }
8098
+ if (!sawArgs || !sawKwargs) {
8099
+ argumentErrors = true;
8100
+ }
8101
+ if (argumentErrors) {
8102
+ addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.paramSpecArgsMissing().format({
8103
+ type: printType(functionType.details.paramSpec),
8104
+ }), argErrorNode !== null && argErrorNode !== void 0 ? argErrorNode : errorNode);
8105
+ }
8106
+ return { argumentErrors, typeVarContexts: [srcTypeVarContext] };
8152
8107
  }
8153
8108
  const result = validateFunctionArgumentTypes(errorNode, matchResults, srcTypeVarContext, signatureTracker);
8154
8109
  return { argumentErrors: !!result.argumentErrors, typeVarContexts: [srcTypeVarContext] };
@@ -8312,7 +8267,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8312
8267
  }
8313
8268
  }
8314
8269
  if ((0, types_1.isClassInstance)(argType)) {
8315
- const callMember = (0, typeUtils_1.lookUpObjectMember)(argType, '__call__', 8 /* SkipInstanceVariables */);
8270
+ const callMember = (0, typeUtils_1.lookUpObjectMember)(argType, '__call__', 16 /* SkipInstanceMembers */);
8316
8271
  if (callMember) {
8317
8272
  const memberType = getTypeOfMember(callMember);
8318
8273
  if ((0, types_1.isOverloadedFunction)(memberType)) {
@@ -8455,7 +8410,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8455
8410
  else {
8456
8411
  addError(localize_1.Localizer.Diagnostic.typeVarFirstArg(), firstArg.valueExpression || errorNode);
8457
8412
  }
8458
- const typeVar = types_1.TypeVarType.createInstantiable(typeVarName, /* isParamSpec */ false);
8413
+ const typeVar = types_1.TypeVarType.createInstantiable(typeVarName, /* isParamSpec */ false, classType);
8459
8414
  // Parse the remaining parameters.
8460
8415
  const paramNameMap = new Map();
8461
8416
  for (let i = 1; i < argList.length; i++) {
@@ -8581,7 +8536,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8581
8536
  else {
8582
8537
  addError(localize_1.Localizer.Diagnostic.typeVarFirstArg(), firstArg.valueExpression || errorNode);
8583
8538
  }
8584
- const typeVar = types_1.TypeVarType.createInstantiable(typeVarName, /* isParamSpec */ false);
8539
+ const typeVar = types_1.TypeVarType.createInstantiable(typeVarName, /* isParamSpec */ false, classType);
8585
8540
  typeVar.details.isVariadic = true;
8586
8541
  // Parse the remaining parameters.
8587
8542
  for (let i = 1; i < argList.length; i++) {
@@ -8633,7 +8588,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8633
8588
  else {
8634
8589
  addError(localize_1.Localizer.Diagnostic.paramSpecFirstArg(), firstArg.valueExpression || errorNode);
8635
8590
  }
8636
- const paramSpec = types_1.TypeVarType.createInstantiable(paramSpecName, /* isParamSpec */ true);
8591
+ const paramSpec = types_1.TypeVarType.createInstantiable(paramSpecName, /* isParamSpec */ true, classType);
8637
8592
  // Parse the remaining parameters.
8638
8593
  for (let i = 1; i < argList.length; i++) {
8639
8594
  const paramNameNode = argList[i].name;
@@ -8876,7 +8831,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8876
8831
  type: types_1.ClassType.cloneAsInstance(baseClass),
8877
8832
  hasDeclaredType: true,
8878
8833
  });
8879
- initType.details.declaredReturnType = types_1.NoneType.createInstance();
8834
+ initType.details.declaredReturnType = getNoneType();
8880
8835
  classType.details.fields.set('__init__', symbol_1.Symbol.createWithType(4 /* ClassMember */, initType));
8881
8836
  // Synthesize a trivial __new__ method.
8882
8837
  const newType = types_1.FunctionType.createSynthesizedInstance('__new__', 1 /* ConstructorMethod */);
@@ -8923,10 +8878,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8923
8878
  function getTypeOfConstant(node, flags) {
8924
8879
  let type;
8925
8880
  if (node.constType === 26 /* None */) {
8926
- type =
8927
- (flags & 128 /* ExpectingInstantiableType */) !== 0
8928
- ? types_1.NoneType.createType()
8929
- : types_1.NoneType.createInstance();
8881
+ type = (flags & 128 /* ExpectingInstantiableType */) !== 0 ? noneClassType : noneType;
8930
8882
  }
8931
8883
  else if (node.constType === 33 /* True */ ||
8932
8884
  node.constType === 15 /* False */ ||
@@ -8943,30 +8895,22 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8943
8895
  }
8944
8896
  }
8945
8897
  }
8946
- if (!type) {
8947
- return undefined;
8948
- }
8949
- return { type };
8898
+ return { type: type !== null && type !== void 0 ? type : types_1.UnknownType.create() };
8950
8899
  }
8951
- function getTypeOfMagicMethodReturn(objType, args, magicMethodName, errorNode, inferenceContext) {
8900
+ function getTypeOfMagicMethodCall(objType, methodName, argList, errorNode, inferenceContext) {
8952
8901
  let magicMethodSupported = true;
8953
8902
  // Create a helper lambda for object subtypes.
8954
8903
  const handleSubtype = (subtype) => {
8955
- var _a, _b;
8904
+ var _a;
8956
8905
  let magicMethodType;
8957
8906
  const concreteSubtype = makeTopLevelTypeVarsConcrete(subtype);
8958
- if ((0, types_1.isClassInstance)(concreteSubtype)) {
8959
- magicMethodType = (_a = getTypeOfObjectMember(errorNode, concreteSubtype, magicMethodName,
8960
- /* usage */ undefined,
8961
- /* diag */ undefined, 128 /* SkipAttributeAccessOverride */ | 1 /* AccessClassMembersOnly */)) === null || _a === void 0 ? void 0 : _a.type;
8962
- }
8963
- else if ((0, types_1.isInstantiableClass)(concreteSubtype)) {
8964
- magicMethodType = (_b = getTypeOfClassMember(errorNode, concreteSubtype, magicMethodName,
8907
+ if ((0, types_1.isClass)(concreteSubtype)) {
8908
+ magicMethodType = (_a = getTypeOfObjectMember(errorNode, concreteSubtype, methodName,
8965
8909
  /* usage */ undefined,
8966
- /* diag */ undefined, 128 /* SkipAttributeAccessOverride */ | 64 /* ConsiderMetaclassOnly */)) === null || _b === void 0 ? void 0 : _b.type;
8910
+ /* diag */ undefined, 16 /* SkipInstanceMembers */ | 512 /* SkipAttributeAccessOverride */)) === null || _a === void 0 ? void 0 : _a.type;
8967
8911
  }
8968
8912
  if (magicMethodType) {
8969
- const functionArgs = args.map((arg) => {
8913
+ const functionArgs = argList.map((arg) => {
8970
8914
  return {
8971
8915
  argumentCategory: 0 /* Simple */,
8972
8916
  typeResult: arg,
@@ -9002,13 +8946,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9002
8946
  if ((0, types_1.isClassInstance)(subtype) || (0, types_1.isInstantiableClass)(subtype) || (0, types_1.isTypeVar)(subtype)) {
9003
8947
  return handleSubtype(subtype);
9004
8948
  }
9005
- if ((0, types_1.isNoneInstance)(subtype)) {
8949
+ if ((0, typeUtils_1.isNoneInstance)(subtype)) {
9006
8950
  if (objectType && (0, types_1.isClassInstance)(objectType)) {
9007
8951
  // Use 'object' for 'None'.
9008
8952
  return handleSubtype(objectType);
9009
8953
  }
9010
8954
  }
9011
- if ((0, types_1.isNoneTypeClass)(subtype)) {
8955
+ if ((0, typeUtils_1.isNoneTypeClass)(subtype)) {
9012
8956
  if (typeClassType && (0, types_1.isInstantiableClass)(typeClassType)) {
9013
8957
  // Use 'type' for 'type[None]'.
9014
8958
  return handleSubtype(types_1.ClassType.cloneAsInstance(typeClassType));
@@ -9618,12 +9562,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9618
9562
  return { type: returnedType };
9619
9563
  }
9620
9564
  function getTypeOfLambda(node, inferenceContext) {
9621
- let isIncomplete = !!(inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.isTypeIncomplete);
9622
- const functionType = types_1.FunctionType.createInstance('', '', '', 131072 /* PartiallyEvaluated */);
9623
- functionType.details.typeVarScopeId = ParseTreeUtils.getScopeIdForNode(node);
9624
- // Pre-cache the incomplete function type in case the evaluation of the
9625
- // lambda depends on itself.
9626
- writeTypeCache(node, { type: functionType, isIncomplete: true }, 0 /* None */);
9627
9565
  let expectedFunctionTypes = [];
9628
9566
  if (inferenceContext) {
9629
9567
  (0, typeUtils_1.mapSubtypes)(inferenceContext.expectedType, (subtype) => {
@@ -9638,30 +9576,54 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9638
9576
  }
9639
9577
  return undefined;
9640
9578
  });
9641
- // Determine the minimum number of parameters that are required to
9642
- // satisfy the lambda.
9643
- const minLambdaParamCount = node.parameters.filter((param) => param.category === 0 /* Simple */ && !!param.name && param.defaultValue === undefined).length;
9644
- const maxLambdaParamCount = node.parameters.filter((param) => param.category === 0 /* Simple */ && !!param.name).length;
9645
- // Remove any expected subtypes that don't satisfy the minimum
9646
- // parameter count requirement.
9647
- expectedFunctionTypes = expectedFunctionTypes.filter((functionType) => {
9648
- const functionParamCount = functionType.details.parameters.filter((param) => !!param.name && !param.hasDefault).length;
9649
- const hasVarArgs = functionType.details.parameters.some((param) => !!param.name && param.category !== 0 /* Simple */);
9650
- const hasParamSpec = !!functionType.details.paramSpec;
9651
- return (hasVarArgs ||
9652
- hasParamSpec ||
9653
- (functionParamCount >= minLambdaParamCount && functionParamCount <= maxLambdaParamCount));
9654
- });
9655
9579
  }
9656
- // For now, use only the first expected type.
9657
- const expectedFunctionType = expectedFunctionTypes.length > 0 ? expectedFunctionTypes[0] : undefined;
9580
+ if (expectedFunctionTypes.length <= 1) {
9581
+ return getTypeOfLambdaWithExpectedType(node, expectedFunctionTypes.length > 0 ? expectedFunctionTypes[0] : undefined, inferenceContext,
9582
+ /* forceSpeculative */ false);
9583
+ }
9584
+ // Sort the expected types for deterministic results.
9585
+ expectedFunctionTypes = (0, typeUtils_1.sortTypes)(expectedFunctionTypes);
9586
+ // If there's more than one type, try each in turn until we find one that works.
9587
+ for (const expectedFunctionType of expectedFunctionTypes) {
9588
+ const result = getTypeOfLambdaWithExpectedType(node, expectedFunctionType, inferenceContext,
9589
+ /* forceSpeculative */ true);
9590
+ if (!result.typeErrors) {
9591
+ return getTypeOfLambdaWithExpectedType(node, expectedFunctionType, inferenceContext,
9592
+ /* forceSpeculative */ false);
9593
+ }
9594
+ }
9595
+ return getTypeOfLambdaWithExpectedType(node, expectedFunctionTypes[0], inferenceContext,
9596
+ /* forceSpeculative */ true);
9597
+ }
9598
+ function getTypeOfLambdaWithExpectedType(node, expectedType, inferenceContext, forceSpeculative) {
9599
+ let isIncomplete = !!(inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.isTypeIncomplete);
9658
9600
  let paramsArePositionOnly = true;
9659
- const expectedParamDetails = expectedFunctionType ? (0, parameterUtils_1.getParameterListDetails)(expectedFunctionType) : undefined;
9601
+ const expectedParamDetails = expectedType ? (0, parameterUtils_1.getParameterListDetails)(expectedType) : undefined;
9602
+ const functionType = types_1.FunctionType.createInstance('', '', '', 131072 /* PartiallyEvaluated */);
9603
+ functionType.details.typeVarScopeId = ParseTreeUtils.getScopeIdForNode(node);
9604
+ // Pre-cache the incomplete function type in case the evaluation of the
9605
+ // lambda depends on itself.
9606
+ writeTypeCache(node, { type: functionType, isIncomplete: true }, 0 /* None */);
9607
+ // We assume for simplicity that the parameter signature of the lambda is
9608
+ // the same as the expected type. If this isn't the case, we'll use
9609
+ // object for any lambda parameters that don't match. We could make this
9610
+ // more sophisticated in the future, but it becomes very complex to handle
9611
+ // all of the permutations.
9612
+ let sawParamMismatch = false;
9660
9613
  node.parameters.forEach((param, index) => {
9661
9614
  let paramType;
9662
- if (expectedParamDetails) {
9615
+ if (expectedParamDetails && !sawParamMismatch) {
9663
9616
  if (index < expectedParamDetails.params.length) {
9664
- paramType = expectedParamDetails.params[index].type;
9617
+ const expectedParam = expectedParamDetails.params[index];
9618
+ // If the parameter category matches and both of the parameters are
9619
+ // either separators (/ or *) or not separators, copy the type
9620
+ // from the expected parameter.
9621
+ if (expectedParam.param.category === param.category && !param.name === !expectedParam.param.name) {
9622
+ paramType = expectedParam.type;
9623
+ }
9624
+ else {
9625
+ sawParamMismatch = true;
9626
+ }
9665
9627
  }
9666
9628
  else if (param.defaultValue) {
9667
9629
  // If the lambda param has a default value but there is no associated
@@ -9721,9 +9683,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9721
9683
  type: types_1.UnknownType.create(),
9722
9684
  });
9723
9685
  }
9724
- const expectedReturnType = expectedFunctionType
9725
- ? getFunctionEffectiveReturnType(expectedFunctionType)
9726
- : undefined;
9686
+ const expectedReturnType = expectedType ? getFunctionEffectiveReturnType(expectedType) : undefined;
9687
+ let typeErrors = false;
9727
9688
  // If we're speculatively evaluating the lambda, create another speculative
9728
9689
  // evaluation scope for the return expression and do not allow retention
9729
9690
  // of the cached types.
@@ -9731,19 +9692,28 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9731
9692
  // cache the type of the lambda return expression because it depends on
9732
9693
  // the parameter types that we set above, and the speculative type cache
9733
9694
  // doesn't know about that context.
9734
- useSpeculativeMode(isSpeculativeModeInUse(node) || (inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.isTypeIncomplete) ? node.expression : undefined, () => {
9695
+ useSpeculativeMode(forceSpeculative || isSpeculativeModeInUse(node) || (inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.isTypeIncomplete)
9696
+ ? node.expression
9697
+ : undefined, () => {
9735
9698
  const returnTypeResult = getTypeOfExpression(node.expression,
9736
9699
  /* flags */ undefined, (0, typeUtils_1.makeInferenceContext)(expectedReturnType));
9737
9700
  functionType.inferredReturnType = returnTypeResult.type;
9738
9701
  if (returnTypeResult.isIncomplete) {
9739
9702
  isIncomplete = true;
9740
9703
  }
9704
+ if (returnTypeResult.typeErrors) {
9705
+ typeErrors = true;
9706
+ }
9741
9707
  }, {
9742
- dependentType: inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.expectedType,
9708
+ dependentType: expectedType,
9743
9709
  });
9744
9710
  // Mark the function type as no longer being evaluated.
9745
9711
  functionType.details.flags &= ~131072 /* PartiallyEvaluated */;
9746
- return { type: functionType, isIncomplete };
9712
+ // Is the resulting function compatible with the expected type?
9713
+ if (expectedType && !assignType(expectedType, functionType)) {
9714
+ typeErrors = true;
9715
+ }
9716
+ return { type: functionType, isIncomplete, typeErrors };
9747
9717
  }
9748
9718
  function getTypeOfListComprehension(node, inferenceContext) {
9749
9719
  var _a;
@@ -9763,8 +9733,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9763
9733
  }
9764
9734
  let expectedElementType;
9765
9735
  if (inferenceContext) {
9766
- expectedElementType = (_a = getTypeOfIterator({ type: inferenceContext.expectedType }, isAsync,
9767
- /* errorNode */ undefined)) === null || _a === void 0 ? void 0 : _a.type;
9736
+ expectedElementType = (_a = getTypeOfIterator({ type: inferenceContext.expectedType }, isAsync, node,
9737
+ /* emitNotIterableError */ false)) === null || _a === void 0 ? void 0 : _a.type;
9768
9738
  }
9769
9739
  const elementTypeResult = getElementTypeFromListComprehension(node, expectedElementType);
9770
9740
  if (elementTypeResult.isIncomplete) {
@@ -9784,9 +9754,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9784
9754
  }
9785
9755
  const builtInIteratorType = getTypingType(node, isAsync ? 'AsyncGenerator' : 'Generator');
9786
9756
  if (builtInIteratorType && (0, types_1.isInstantiableClass)(builtInIteratorType)) {
9787
- type = types_1.ClassType.cloneAsInstance(types_1.ClassType.cloneForSpecialization(builtInIteratorType, isAsync
9788
- ? [elementType, types_1.NoneType.createInstance()]
9789
- : [elementType, types_1.NoneType.createInstance(), types_1.NoneType.createInstance()],
9757
+ type = types_1.ClassType.cloneAsInstance(types_1.ClassType.cloneForSpecialization(builtInIteratorType, isAsync ? [elementType, getNoneType()] : [elementType, getNoneType(), getNoneType()],
9790
9758
  /* isTypeArgumentExplicit */ true));
9791
9759
  }
9792
9760
  return { type, isIncomplete, typeErrors };
@@ -10115,7 +10083,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10115
10083
  addExpectedClassDiagnostic(typeArg0Type, typeArgs[0].node);
10116
10084
  typeArg0Type = types_1.UnknownType.create();
10117
10085
  }
10118
- const optionalType = (0, types_1.combineTypes)([typeArg0Type, types_1.NoneType.createType()]);
10086
+ const optionalType = (0, types_1.combineTypes)([typeArg0Type, noneClassType !== null && noneClassType !== void 0 ? noneClassType : types_1.UnknownType.create()]);
10119
10087
  if ((0, types_1.isUnion)(optionalType)) {
10120
10088
  types_1.TypeBase.setSpecialForm(optionalType);
10121
10089
  }
@@ -10178,7 +10146,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10178
10146
  type = cloneBuiltinClassWithLiteral(node, 'bool', false);
10179
10147
  }
10180
10148
  else if (itemExpr.constType === 26 /* None */) {
10181
- type = types_1.NoneType.createType();
10149
+ type = noneClassType !== null && noneClassType !== void 0 ? noneClassType : types_1.UnknownType.create();
10182
10150
  }
10183
10151
  }
10184
10152
  else if (itemExpr.nodeType === 55 /* UnaryOperation */ &&
@@ -10202,7 +10170,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10202
10170
  let isLiteralType = true;
10203
10171
  (0, typeUtils_1.doForEachSubtype)(exprType.type, (subtype) => {
10204
10172
  if (!(0, types_1.isInstantiableClass)(subtype) || subtype.literalValue === undefined) {
10205
- if (!(0, types_1.isNoneTypeClass)(subtype)) {
10173
+ if (!(0, typeUtils_1.isNoneTypeClass)(subtype)) {
10206
10174
  isLiteralType = false;
10207
10175
  }
10208
10176
  }
@@ -10613,7 +10581,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10613
10581
  // is allowed if it's an unpacked variadic type var or tuple. None is also allowed
10614
10582
  // since it is used to define NoReturn in typeshed stubs).
10615
10583
  if (types.length === 1) {
10616
- if (!(0, types_1.isVariadicTypeVar)(types[0]) && !(0, types_1.isUnpacked)(types[0]) && !(0, types_1.isNoneInstance)(types[0])) {
10584
+ if (!(0, types_1.isVariadicTypeVar)(types[0]) && !(0, types_1.isUnpacked)(types[0]) && !(0, typeUtils_1.isNoneInstance)(types[0])) {
10617
10585
  addError(localize_1.Localizer.Diagnostic.unionTypeArgCount(), errorNode);
10618
10586
  }
10619
10587
  }
@@ -10962,8 +10930,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10962
10930
  if (typeAliasNameNode) {
10963
10931
  // If this was a speculative type alias, it becomes a real type alias
10964
10932
  // only if the evaluated type is an instantiable type.
10965
- if (!isSpeculativeTypeAlias ||
10966
- (types_1.TypeBase.isInstantiable(rightHandType) && !(0, types_1.isUnknown)(rightHandType))) {
10933
+ if (!isSpeculativeTypeAlias || isLegalImplicitTypeAliasType(rightHandType)) {
10967
10934
  // If this is a type alias, record its name based on the assignment target.
10968
10935
  rightHandType = transformTypeForTypeAlias(rightHandType, typeAliasNameNode, node.rightExpression,
10969
10936
  /* isPep695Syntax */ false);
@@ -11089,6 +11056,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
11089
11056
  return `__type_of_${paramName}`;
11090
11057
  }
11091
11058
  function getTypeOfClass(node) {
11059
+ initializedBasicTypes(node);
11092
11060
  // Is this type already cached?
11093
11061
  const cachedClassType = readTypeCache(node.name, 0 /* None */);
11094
11062
  if (cachedClassType) {
@@ -11109,7 +11077,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
11109
11077
  if ((scope === null || scope === void 0 ? void 0 : scope.type) === 4 /* Builtin */ ||
11110
11078
  fileInfo.isTypingStubFile ||
11111
11079
  fileInfo.isTypingExtensionsStubFile ||
11112
- fileInfo.isBuiltInStubFile) {
11080
+ fileInfo.isBuiltInStubFile ||
11081
+ fileInfo.isTypeshedStubFile) {
11113
11082
  classFlags |= 1 /* BuiltInClass */;
11114
11083
  if (fileInfo.isTypingExtensionsStubFile) {
11115
11084
  classFlags |= 65536 /* TypingExtensionClass */;
@@ -11328,8 +11297,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
11328
11297
  metaclassNode = arg.valueExpression;
11329
11298
  }
11330
11299
  }
11331
- else if (types_1.ClassType.isTypedDictClass(classType) &&
11332
- (arg.name.value === 'total' || arg.name.value === 'readonly')) {
11300
+ else if (types_1.ClassType.isTypedDictClass(classType) && arg.name.value === 'total') {
11333
11301
  // The "total" and "readonly" parameters apply only for TypedDict classes.
11334
11302
  // PEP 589 specifies that the parameter must be either True or False.
11335
11303
  const constArgValue = (0, staticExpressions_1.evaluateStaticBoolExpression)(arg.valueExpression, fileInfo.executionEnvironment, fileInfo.definedConstants);
@@ -11339,9 +11307,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
11339
11307
  else if (arg.name.value === 'total' && !constArgValue) {
11340
11308
  classType.details.flags |= 256 /* CanOmitDictValues */;
11341
11309
  }
11342
- else if (arg.name.value === 'readonly' && constArgValue) {
11343
- classType.details.flags |= 512 /* DictValuesReadOnly */;
11344
- }
11345
11310
  }
11346
11311
  else {
11347
11312
  // Collect arguments that will be passed to the `__init_subclass__`
@@ -11421,7 +11386,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
11421
11386
  classType.details.fields.set('__hash__', symbol_1.Symbol.createWithType(4 /* ClassMember */ |
11422
11387
  128 /* ClassVar */ |
11423
11388
  64 /* IgnoredForProtocolMatch */ |
11424
- 4096 /* IgnoredForOverrideChecks */, types_1.NoneType.createInstance()));
11389
+ 4096 /* IgnoredForOverrideChecks */, getNoneType()));
11425
11390
  }
11426
11391
  // Determine whether the class's instance variables are constrained
11427
11392
  // to those defined by __slots__. We need to do this prior to dataclass
@@ -11867,22 +11832,22 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
11867
11832
  }
11868
11833
  });
11869
11834
  const errorNode = argList.length > 0 ? argList[0].node.name : node.name;
11870
- const initSubclassMethodInfo = getTypeOfClassMemberName(errorNode, classType,
11871
- /* isAccessedThroughObject */ false, '__init_subclass__', { method: 'get' },
11872
- /* diag */ undefined, 1 /* AccessClassMembersOnly */ |
11873
- 8 /* SkipObjectBaseClass */ |
11874
- 256 /* SkipOriginalClass */, classType);
11835
+ const initSubclassMethodInfo = getTypeOfObjectMember(errorNode, classType, '__init_subclass__', { method: 'get' },
11836
+ /* diag */ undefined, 32 /* SkipClassMembers */ |
11837
+ 4 /* SkipObjectBaseClass */ |
11838
+ 1 /* SkipOriginalClass */ |
11839
+ 512 /* SkipAttributeAccessOverride */);
11875
11840
  if (initSubclassMethodInfo) {
11876
11841
  const initSubclassMethodType = initSubclassMethodInfo.type;
11877
11842
  if (initSubclassMethodType) {
11878
11843
  validateCallArguments(errorNode, argList, { type: initSubclassMethodType },
11879
11844
  /* typeVarContext */ undefined,
11880
- /* skipUnknownArgCheck */ false, (0, typeUtils_1.makeInferenceContext)(types_1.NoneType.createInstance()));
11845
+ /* skipUnknownArgCheck */ false, (0, typeUtils_1.makeInferenceContext)(getNoneType()));
11881
11846
  }
11882
11847
  }
11883
11848
  else if (classType.details.effectiveMetaclass && (0, types_1.isClass)(classType.details.effectiveMetaclass)) {
11884
11849
  // See if the metaclass has a `__new__` method that accepts keyword parameters.
11885
- const newMethodMember = (0, typeUtils_1.lookUpClassMember)(classType.details.effectiveMetaclass, '__new__', 64 /* SkipTypeBaseClass */);
11850
+ const newMethodMember = (0, typeUtils_1.lookUpClassMember)(classType.details.effectiveMetaclass, '__new__', 8 /* SkipTypeBaseClass */);
11886
11851
  if (newMethodMember) {
11887
11852
  const newMethodType = getTypeOfMember(newMethodMember);
11888
11853
  if ((0, types_1.isFunction)(newMethodType)) {
@@ -11945,6 +11910,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
11945
11910
  });
11946
11911
  }
11947
11912
  function getTypeOfFunction(node) {
11913
+ initializedBasicTypes(node);
11948
11914
  // Is this predecorated function type cached?
11949
11915
  let functionType = readTypeCache(node.name, 0 /* None */);
11950
11916
  if (functionType) {
@@ -12307,7 +12273,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
12307
12273
  // Special-case the __init__ method, which is commonly left without
12308
12274
  // an annotated return type, but we can assume it returns None.
12309
12275
  if (node.name.value === '__init__') {
12310
- functionType.details.declaredReturnType = types_1.NoneType.createInstance();
12276
+ functionType.details.declaredReturnType = getNoneType();
12311
12277
  }
12312
12278
  else {
12313
12279
  functionType.details.declaredReturnType = types_1.UnknownType.create();
@@ -12400,7 +12366,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
12400
12366
  param.defaultValue.constType === 26 /* None */ &&
12401
12367
  !(0, typeUtils_1.isOptionalType)(type) &&
12402
12368
  !AnalyzerNodeInfo.getFileInfo(param).diagnosticRuleSet.strictParameterNoneValue) {
12403
- return (0, types_1.combineTypes)([type, types_1.NoneType.createInstance()]);
12369
+ return (0, types_1.combineTypes)([type, getNoneType()]);
12404
12370
  }
12405
12371
  return type;
12406
12372
  }
@@ -12460,7 +12426,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
12460
12426
  // whose name starts with an underscore)? If so, we will assume that the
12461
12427
  // value is a singleton sentinel. The actual supported type is going to be
12462
12428
  // a union of this type and Unknown.
12463
- if ((0, types_1.isNoneInstance)(defaultValueType) ||
12429
+ if ((0, typeUtils_1.isNoneInstance)(defaultValueType) ||
12464
12430
  ((0, types_1.isClassInstance)(defaultValueType) && (0, symbolNameUtils_1.isPrivateOrProtectedName)(defaultValueType.details.name))) {
12465
12431
  inferredParamType = (0, types_1.combineTypes)([defaultValueType, types_1.UnknownType.create()]);
12466
12432
  }
@@ -12658,13 +12624,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
12658
12624
  inferredReturnTypes.push(returnType);
12659
12625
  }
12660
12626
  else {
12661
- inferredReturnTypes.push(types_1.NoneType.createInstance());
12627
+ inferredReturnTypes.push(getNoneType());
12662
12628
  }
12663
12629
  }
12664
12630
  });
12665
12631
  }
12666
12632
  if (!functionNeverReturns && implicitlyReturnsNone) {
12667
- inferredReturnTypes.push(types_1.NoneType.createInstance());
12633
+ inferredReturnTypes.push(getNoneType());
12668
12634
  }
12669
12635
  inferredReturnType = (0, types_1.combineTypes)(inferredReturnTypes);
12670
12636
  // Remove any unbound values since those would generate an exception
@@ -12706,14 +12672,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
12706
12672
  inferredYieldTypes.push(yieldType !== null && yieldType !== void 0 ? yieldType : types_1.UnknownType.create());
12707
12673
  }
12708
12674
  else {
12709
- inferredYieldTypes.push(types_1.NoneType.createInstance());
12675
+ inferredYieldTypes.push(getNoneType());
12710
12676
  }
12711
12677
  }
12712
12678
  }
12713
12679
  });
12714
12680
  }
12715
12681
  if (inferredYieldTypes.length === 0) {
12716
- inferredYieldTypes.push(types_1.NoneType.createInstance());
12682
+ inferredYieldTypes.push(getNoneType());
12717
12683
  }
12718
12684
  const inferredYieldType = (0, types_1.combineTypes)(inferredYieldTypes);
12719
12685
  // Inferred yield types need to be wrapped in a Generator or
@@ -12727,7 +12693,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
12727
12693
  // This eliminates any "partially unknown" errors in strict mode
12728
12694
  // in the common case.
12729
12695
  const sendType = isYieldResultUsed ? types_1.UnknownType.create() : types_1.AnyType.create();
12730
- typeArgs.push(inferredYieldType, sendType, (0, types_1.isNever)(inferredReturnType) ? types_1.NoneType.createInstance() : inferredReturnType);
12696
+ typeArgs.push(inferredYieldType, sendType, (0, types_1.isNever)(inferredReturnType) ? getNoneType() : inferredReturnType);
12731
12697
  if (useAwaitableGenerator) {
12732
12698
  typeArgs.push(types_1.AnyType.create());
12733
12699
  }
@@ -12844,7 +12810,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
12844
12810
  if ((0, typeUtils_1.isOptionalType)(exprType)) {
12845
12811
  const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
12846
12812
  addDiagnostic(fileInfo.diagnosticRuleSet.reportOptionalContextManager, diagnosticRules_1.DiagnosticRule.reportOptionalContextManager, localize_1.Localizer.Diagnostic.noneNotUsableWith(), node.expression);
12847
- exprType = (0, types_1.removeNoneFromUnion)(exprType);
12813
+ exprType = (0, typeUtils_1.removeNoneFromUnion)(exprType);
12848
12814
  }
12849
12815
  // Verify that the target has an __enter__ or __aenter__ method defined.
12850
12816
  const enterMethodName = isAsync ? '__aenter__' : '__enter__';
@@ -12855,7 +12821,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
12855
12821
  }
12856
12822
  const additionalHelp = new diagnostic_1.DiagnosticAddendum();
12857
12823
  if ((0, types_1.isClass)(subtype)) {
12858
- let enterType = getTypeOfMagicMethodReturn(subtype, [], enterMethodName, node.expression,
12824
+ let enterType = getTypeOfMagicMethodCall(subtype, enterMethodName, [], node.expression,
12859
12825
  /* inferenceContext */ undefined);
12860
12826
  if (enterType) {
12861
12827
  // For "async while", an implicit "await" is performed.
@@ -12865,7 +12831,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
12865
12831
  return enterType;
12866
12832
  }
12867
12833
  if (!isAsync) {
12868
- if (getTypeOfMagicMethodReturn(subtype, [], '__aenter__', node.expression,
12834
+ if (getTypeOfMagicMethodCall(subtype, '__aenter__', [], node.expression,
12869
12835
  /* inferenceContext */ undefined)) {
12870
12836
  additionalHelp.addMessage(localize_1.Localizer.DiagnosticAddendum.asyncHelp());
12871
12837
  }
@@ -12885,7 +12851,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
12885
12851
  }
12886
12852
  if ((0, types_1.isClass)(subtype)) {
12887
12853
  const anyArg = { type: types_1.AnyType.create() };
12888
- const exitType = getTypeOfMagicMethodReturn(subtype, [anyArg, anyArg, anyArg], exitMethodName, node.expression,
12854
+ const exitType = getTypeOfMagicMethodCall(subtype, exitMethodName, [anyArg, anyArg, anyArg], node.expression,
12889
12855
  /* inferenceContext */ undefined);
12890
12856
  if (exitType) {
12891
12857
  return;
@@ -14234,6 +14200,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
14234
14200
  .getDeclarations()
14235
14201
  .find((decl) => decl.type === 2 /* Parameter */);
14236
14202
  }
14203
+ const parameterDetails = (0, parameterUtils_1.getParameterListDetails)(type);
14204
+ if (parameterDetails.unpackedKwargsTypedDictType) {
14205
+ const lookupResults = (0, typeUtils_1.lookUpClassMember)(parameterDetails.unpackedKwargsTypedDictType, paramName);
14206
+ if (lookupResults) {
14207
+ return lookupResults.symbol
14208
+ .getDeclarations()
14209
+ .find((decl) => decl.type === 1 /* Variable */);
14210
+ }
14211
+ }
14237
14212
  }
14238
14213
  }
14239
14214
  }
@@ -14314,7 +14289,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
14314
14289
  if ((0, types_1.isInstantiableClass)(subtype)) {
14315
14290
  // Try to find a member that has a declared type. If so, that
14316
14291
  // overrides any inferred types.
14317
- let member = (0, typeUtils_1.lookUpClassMember)(subtype, memberName, 32 /* DeclaredTypesOnly */);
14292
+ let member = (0, typeUtils_1.lookUpClassMember)(subtype, memberName, 64 /* DeclaredTypesOnly */);
14318
14293
  if (!member) {
14319
14294
  member = (0, typeUtils_1.lookUpClassMember)(subtype, memberName);
14320
14295
  }
@@ -14331,7 +14306,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
14331
14306
  else if ((0, types_1.isClassInstance)(subtype)) {
14332
14307
  // Try to find a member that has a declared type. If so, that
14333
14308
  // overrides any inferred types.
14334
- let member = (0, typeUtils_1.lookUpObjectMember)(subtype, memberName, 32 /* DeclaredTypesOnly */);
14309
+ let member = (0, typeUtils_1.lookUpObjectMember)(subtype, memberName, 64 /* DeclaredTypesOnly */);
14335
14310
  if (!member) {
14336
14311
  member = (0, typeUtils_1.lookUpObjectMember)(subtype, memberName);
14337
14312
  }
@@ -14397,7 +14372,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
14397
14372
  }
14398
14373
  else if ((0, types_1.isInstantiableClass)(baseType)) {
14399
14374
  const initMethodType = (_b = getTypeOfObjectMember(argNode.parent.leftExpression, types_1.ClassType.cloneAsInstance(baseType), '__init__', { method: 'get' },
14400
- /* diag */ undefined, 8 /* SkipObjectBaseClass */)) === null || _b === void 0 ? void 0 : _b.type;
14375
+ /* diag */ undefined, 4 /* SkipObjectBaseClass */)) === null || _b === void 0 ? void 0 : _b.type;
14401
14376
  if (initMethodType && (0, types_1.isFunction)(initMethodType)) {
14402
14377
  const paramDecl = getDeclarationFromFunctionNamedParameter(initMethodType, paramName);
14403
14378
  if (paramDecl) {
@@ -14461,7 +14436,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
14461
14436
  return { type: strType };
14462
14437
  }
14463
14438
  if (declaration.intrinsicType === 'str | None') {
14464
- return { type: (0, types_1.combineTypes)([strType, types_1.NoneType.createInstance()]) };
14439
+ return { type: (0, types_1.combineTypes)([strType, getNoneType()]) };
14465
14440
  }
14466
14441
  if (declaration.intrinsicType === 'int') {
14467
14442
  return { type: intType };
@@ -14574,13 +14549,19 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
14574
14549
  if (cachedTypeVarType && (0, types_1.isTypeVar)(cachedTypeVarType)) {
14575
14550
  return cachedTypeVarType;
14576
14551
  }
14577
- let typeVar = types_1.TypeVarType.createInstantiable(node.name.value);
14578
- typeVar.details.isTypeParamSyntax = true;
14552
+ let runtimeClassName = 'TypeVar';
14579
14553
  if (node.typeParamCategory === parseNodes_1.TypeParameterCategory.TypeVarTuple) {
14580
- typeVar.details.isVariadic = true;
14554
+ runtimeClassName = 'TypeVarTuple';
14581
14555
  }
14582
14556
  else if (node.typeParamCategory === parseNodes_1.TypeParameterCategory.ParamSpec) {
14583
- typeVar.details.isParamSpec = true;
14557
+ runtimeClassName = 'ParamSpec';
14558
+ }
14559
+ const runtimeType = getTypingType(node, runtimeClassName);
14560
+ const runtimeClass = runtimeType && (0, types_1.isInstantiableClass)(runtimeType) ? runtimeType : undefined;
14561
+ let typeVar = types_1.TypeVarType.createInstantiable(node.name.value, node.typeParamCategory === parseNodes_1.TypeParameterCategory.ParamSpec, runtimeClass);
14562
+ typeVar.details.isTypeParamSyntax = true;
14563
+ if (node.typeParamCategory === parseNodes_1.TypeParameterCategory.TypeVarTuple) {
14564
+ typeVar.details.isVariadic = true;
14584
14565
  }
14585
14566
  // Cache the value before we evaluate the bound or the default type in
14586
14567
  // case it refers to itself in a circular manner.
@@ -14803,9 +14784,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
14803
14784
  // If this was a speculative type alias, it becomes a real type alias only
14804
14785
  // in the event that its inferred type is instantiable or explicitly Any
14805
14786
  // (but not an ellipsis).
14806
- if (types_1.TypeBase.isInstantiable(inferredType) &&
14807
- !(0, types_1.isUnknown)(inferredType) &&
14808
- !(0, typeUtils_1.isEllipsisType)(inferredType)) {
14787
+ if (isLegalImplicitTypeAliasType(inferredType)) {
14809
14788
  inferredType = transformTypeForTypeAlias(inferredType, resolvedDecl.typeAliasName, resolvedDecl.node,
14810
14789
  /* isPep695Syntax */ false);
14811
14790
  isUnambiguousType = true;
@@ -14909,7 +14888,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
14909
14888
  return getEffectiveTypeOfSymbolForUsage(symbol).type;
14910
14889
  }
14911
14890
  function getEffectiveTypeOfSymbolForUsage(symbol, usageNode, useLastDecl = false) {
14912
- var _a, _b;
14913
14891
  let declaredTypeInfo;
14914
14892
  // If there's a declared type, it takes precedence over inferred types.
14915
14893
  if (symbol.hasTypedDeclarations()) {
@@ -14942,13 +14920,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
14942
14920
  let cacheEntries = effectiveTypeCache.get(symbol.id);
14943
14921
  const usageNodeId = usageNode ? usageNode.id : undefined;
14944
14922
  const effectiveTypeCacheKey = `${usageNodeId === undefined ? '.' : usageNodeId.toString()}${useLastDecl ? '*' : ''}`;
14945
- if (cacheEntries) {
14946
- const result = cacheEntries.get(effectiveTypeCacheKey);
14947
- if (result) {
14948
- if (!result.isIncomplete) {
14949
- return result;
14950
- }
14951
- }
14923
+ const cacheEntry = cacheEntries === null || cacheEntries === void 0 ? void 0 : cacheEntries.get(effectiveTypeCacheKey);
14924
+ if (cacheEntry && !cacheEntry.isIncomplete) {
14925
+ return cacheEntry;
14952
14926
  }
14953
14927
  // Infer the type.
14954
14928
  const decls = symbol.getDeclarations();
@@ -15037,8 +15011,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
15037
15011
  }
15038
15012
  declsToConsider.push(resolvedDecl);
15039
15013
  });
15040
- const evaluationAttempts = ((_b = (_a = cacheEntries === null || cacheEntries === void 0 ? void 0 : cacheEntries.get(effectiveTypeCacheKey)) === null || _a === void 0 ? void 0 : _a.evaluationAttempts) !== null && _b !== void 0 ? _b : 0) + 1;
15041
- const result = getTypeOfSymbolForDecls(symbol, declsToConsider, evaluationAttempts);
15014
+ const result = getTypeOfSymbolForDecls(symbol, declsToConsider, effectiveTypeCacheKey);
15042
15015
  // Add the result to the effective type cache if it doesn't include speculative results.
15043
15016
  if (!result.includesSpeculativeResult) {
15044
15017
  addToEffectiveTypeCache(result);
@@ -15054,7 +15027,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
15054
15027
  }
15055
15028
  }
15056
15029
  // Returns the type of a symbol based on a subset of its declarations.
15057
- function getTypeOfSymbolForDecls(symbol, decls, evaluationAttempts) {
15030
+ function getTypeOfSymbolForDecls(symbol, decls, typeCacheKey) {
15031
+ var _a, _b;
15058
15032
  const typesToCombine = [];
15059
15033
  let isIncomplete = false;
15060
15034
  let sawPendingEvaluation = false;
@@ -15122,6 +15096,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
15122
15096
  sawPendingEvaluation = true;
15123
15097
  }
15124
15098
  });
15099
+ // How many times have we already attempted to evaluate this declaration already?
15100
+ const cacheEntries = effectiveTypeCache.get(symbol.id);
15101
+ const evaluationAttempts = ((_b = (_a = cacheEntries === null || cacheEntries === void 0 ? void 0 : cacheEntries.get(typeCacheKey)) === null || _a === void 0 ? void 0 : _a.evaluationAttempts) !== null && _b !== void 0 ? _b : 0) + 1;
15125
15102
  let type;
15126
15103
  if (typesToCombine.length > 0) {
15127
15104
  // Ignore the pending evaluation flag if we've already attempted the
@@ -15276,7 +15253,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
15276
15253
  // Don't bother inferring the return type of __init__ because it's
15277
15254
  // always None.
15278
15255
  if (types_1.FunctionType.isInstanceMethod(type) && type.details.name === '__init__') {
15279
- returnType = types_1.NoneType.createInstance();
15256
+ returnType = getNoneType();
15280
15257
  }
15281
15258
  else if (type.details.declaration) {
15282
15259
  const functionNode = type.details.declaration.node;
@@ -16239,23 +16216,23 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
16239
16216
  if ((0, types_1.isUnion)(destType)) {
16240
16217
  return assignToUnionType(destType, srcType, diag, destTypeVarContext, srcTypeVarContext, originalFlags, recursionCount);
16241
16218
  }
16242
- if ((0, types_1.isNoneInstance)(destType)) {
16243
- if ((0, types_1.isNoneInstance)(srcType)) {
16219
+ if ((0, typeUtils_1.isNoneInstance)(destType)) {
16220
+ if ((0, typeUtils_1.isNoneInstance)(srcType)) {
16244
16221
  return true;
16245
16222
  }
16246
16223
  if ((0, types_1.isClassInstance)(srcType) && types_1.ClassType.isBuiltIn(srcType, 'NoneType')) {
16247
16224
  return true;
16248
16225
  }
16249
16226
  }
16250
- if ((0, types_1.isNoneTypeClass)(destType)) {
16251
- if ((0, types_1.isNoneTypeClass)(srcType)) {
16227
+ if ((0, typeUtils_1.isNoneTypeClass)(destType)) {
16228
+ if ((0, typeUtils_1.isNoneTypeClass)(srcType)) {
16252
16229
  return true;
16253
16230
  }
16254
16231
  if ((0, types_1.isInstantiableClass)(srcType) && types_1.ClassType.isBuiltIn(srcType, 'NoneType')) {
16255
16232
  return true;
16256
16233
  }
16257
16234
  }
16258
- // Is the src a specialized "Type" object?
16235
+ // Is the src a specialized "type" object?
16259
16236
  if ((0, types_1.isClassInstance)(expandedSrcType) && types_1.ClassType.isBuiltIn(expandedSrcType, 'type')) {
16260
16237
  const srcTypeArgs = expandedSrcType.typeArguments;
16261
16238
  let typeTypeArg;
@@ -16307,6 +16284,19 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
16307
16284
  diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.Localizer.DiagnosticAddendum.typeAssignmentMismatch().format(printSrcDestTypes(srcType, destType)));
16308
16285
  return false;
16309
16286
  }
16287
+ else if ((0, types_1.isClassInstance)(expandedSrcType) && (0, typeUtils_1.isMetaclassInstance)(expandedSrcType)) {
16288
+ // If the source is a metaclass instance, verify that it's compatible with
16289
+ // the metaclass of the instantiable dest type.
16290
+ const destMetaclass = destType.details.effectiveMetaclass;
16291
+ if (destMetaclass && (0, types_1.isInstantiableClass)(destMetaclass)) {
16292
+ if (assignClass(types_1.ClassType.cloneAsInstance(destMetaclass), expandedSrcType, diag, destTypeVarContext, srcTypeVarContext, flags, recursionCount,
16293
+ /* reportErrorsUsingObjType */ false)) {
16294
+ return true;
16295
+ }
16296
+ diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.Localizer.DiagnosticAddendum.typeAssignmentMismatch().format(printSrcDestTypes(srcType, destType)));
16297
+ return false;
16298
+ }
16299
+ }
16310
16300
  }
16311
16301
  if ((0, types_1.isClassInstance)(destType)) {
16312
16302
  // Is the dest a specialized "Type" object?
@@ -16525,13 +16515,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
16525
16515
  }
16526
16516
  }
16527
16517
  // Are we trying to assign None to a protocol?
16528
- if ((0, types_1.isNoneInstance)(srcType) && (0, types_1.isClassInstance)(destType) && types_1.ClassType.isProtocolClass(destType)) {
16529
- if (noneType && (0, types_1.isInstantiableClass)(noneType)) {
16530
- return (0, protocols_1.assignClassToProtocol)(evaluatorInterface, types_1.ClassType.cloneAsInstantiable(destType), noneType, diag, destTypeVarContext, srcTypeVarContext, flags,
16518
+ if ((0, typeUtils_1.isNoneInstance)(srcType) && (0, types_1.isClassInstance)(destType) && types_1.ClassType.isProtocolClass(destType)) {
16519
+ if (noneClassType && (0, types_1.isInstantiableClass)(noneClassType)) {
16520
+ return (0, protocols_1.assignClassToProtocol)(evaluatorInterface, types_1.ClassType.cloneAsInstantiable(destType), noneClassType, diag, destTypeVarContext, srcTypeVarContext, flags,
16531
16521
  /* treatSourceAsInstantiable */ false, recursionCount);
16532
16522
  }
16533
16523
  }
16534
- if ((0, types_1.isNoneInstance)(destType)) {
16524
+ if ((0, typeUtils_1.isNoneInstance)(destType)) {
16535
16525
  diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.Localizer.DiagnosticAddendum.assignToNone());
16536
16526
  return false;
16537
16527
  }
@@ -16788,7 +16778,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
16788
16778
  // match we find because we may need to match TypeVars in other
16789
16779
  // subtypes. We special-case "None" so we can handle Optional[T]
16790
16780
  // without matching the None to the type var.
16791
- if ((0, types_1.isNoneInstance)(srcType) && (0, typeUtils_1.isOptionalType)(destType)) {
16781
+ if ((0, typeUtils_1.isNoneInstance)(srcType) && (0, typeUtils_1.isOptionalType)(destType)) {
16792
16782
  foundMatch = true;
16793
16783
  }
16794
16784
  else {
@@ -17071,15 +17061,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
17071
17061
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
17072
17062
  let canAssign = true;
17073
17063
  const checkReturnType = (flags & 32 /* SkipFunctionReturnTypeCheck */) === 0;
17064
+ const reverseMatching = (flags & 2 /* ReverseTypeVarMatching */) !== 0;
17074
17065
  flags &= ~32 /* SkipFunctionReturnTypeCheck */;
17075
17066
  destType = (0, typeUtils_1.removeParamSpecVariadicsFromFunction)(destType);
17076
17067
  srcType = (0, typeUtils_1.removeParamSpecVariadicsFromFunction)(srcType);
17077
17068
  const destParamDetails = (0, parameterUtils_1.getParameterListDetails)(destType);
17078
17069
  const srcParamDetails = (0, parameterUtils_1.getParameterListDetails)(srcType);
17079
- adjustSourceParamDetailsForDestVariadic(srcParamDetails, destParamDetails);
17080
- const targetIncludesParamSpec = (flags & 2 /* ReverseTypeVarMatching */) !== 0
17081
- ? !!srcType.details.paramSpec
17082
- : !!destType.details.paramSpec;
17070
+ adjustSourceParamDetailsForDestVariadic(reverseMatching ? destParamDetails : srcParamDetails, reverseMatching ? srcParamDetails : destParamDetails);
17071
+ const targetIncludesParamSpec = reverseMatching ? !!srcType.details.paramSpec : !!destType.details.paramSpec;
17083
17072
  const destPositionalCount = (_b = (_a = destParamDetails.argsIndex) !== null && _a !== void 0 ? _a : destParamDetails.firstKeywordOnlyIndex) !== null && _b !== void 0 ? _b : destParamDetails.params.length;
17084
17073
  const srcPositionalCount = (_d = (_c = srcParamDetails.argsIndex) !== null && _c !== void 0 ? _c : srcParamDetails.firstKeywordOnlyIndex) !== null && _d !== void 0 ? _d : srcParamDetails.params.length;
17085
17074
  const positionalsToMatch = Math.min(destPositionalCount, srcPositionalCount);
@@ -17352,7 +17341,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
17352
17341
  canAssign = false;
17353
17342
  }
17354
17343
  }
17355
- const effectiveSrcTypeVarContext = (flags & 2 /* ReverseTypeVarMatching */) === 0 ? srcTypeVarContext : destTypeVarContext;
17344
+ const effectiveSrcTypeVarContext = reverseMatching ? destTypeVarContext : srcTypeVarContext;
17356
17345
  // If the target function was generic and we solved some of the type variables
17357
17346
  // in that generic type, assign them back to the destination typeVar.
17358
17347
  const typeVarSignatureContext = effectiveSrcTypeVarContext.getPrimarySignature();
@@ -17362,8 +17351,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
17362
17351
  });
17363
17352
  // Are we assigning to a function with a ParamSpec?
17364
17353
  if (targetIncludesParamSpec) {
17365
- const effectiveDestType = (flags & 2 /* ReverseTypeVarMatching */) === 0 ? destType : srcType;
17366
- const effectiveSrcType = (flags & 2 /* ReverseTypeVarMatching */) === 0 ? srcType : destType;
17354
+ const effectiveDestType = reverseMatching ? srcType : destType;
17355
+ const effectiveSrcType = reverseMatching ? destType : srcType;
17367
17356
  if (effectiveDestType.details.paramSpec) {
17368
17357
  const requiredMatchParamCount = effectiveDestType.details.parameters.filter((p) => {
17369
17358
  if (!p.name) {
@@ -17416,9 +17405,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
17416
17405
  remainingParams.forEach((param) => {
17417
17406
  types_1.FunctionType.addParameter(remainingFunction, param);
17418
17407
  });
17419
- remainingFunction.details.paramSpec = srcParamSpec
17420
- ? (0, typeUtils_1.convertToInstance)(srcParamSpec)
17421
- : undefined;
17408
+ remainingFunction.details.paramSpec = srcParamSpec ? (0, typeUtils_1.convertToInstance)(srcParamSpec) : undefined;
17422
17409
  if (!assignType(destParamSpec, remainingFunction,
17423
17410
  /* diag */ undefined, destTypeVarContext, srcTypeVarContext, flags)) {
17424
17411
  // If we couldn't assign the function to the ParamSpec, see if we can
@@ -17677,9 +17664,16 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
17677
17664
  previousMatchIndex = matchIndex;
17678
17665
  }
17679
17666
  if (previousMatchIndex < baseOverloads.length - 1) {
17680
- // We didn't find matches for all of the base overloads.
17681
- diag.addMessage(localize_1.Localizer.DiagnosticAddendum.overrideOverloadNoMatch());
17682
- return false;
17667
+ const unmatchedOverloads = baseOverloads.slice(previousMatchIndex + 1);
17668
+ // See if all of the remaining overrides are nonapplicable.
17669
+ if (!baseClass ||
17670
+ unmatchedOverloads.some((overload) => {
17671
+ return isOverrideMethodApplicable(overload, baseClass);
17672
+ })) {
17673
+ // We didn't find matches for all of the base overloads.
17674
+ diag.addMessage(localize_1.Localizer.DiagnosticAddendum.overrideOverloadNoMatch());
17675
+ return false;
17676
+ }
17683
17677
  }
17684
17678
  return true;
17685
17679
  }
@@ -18115,9 +18109,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
18115
18109
  });
18116
18110
  return methodList;
18117
18111
  }
18118
- function bindFunctionToClassOrObjectWithErrors(baseType, memberType, memberClass, errorNode, treatConstructorAsClassMember = false, firstParamType) {
18112
+ function bindFunctionToClassOrObjectWithErrors(baseType, memberType, memberClass, errorNode, treatConstructorAsClassMember = false, selfType) {
18119
18113
  const diag = errorNode ? new diagnostic_1.DiagnosticAddendum() : undefined;
18120
- const result = bindFunctionToClassOrObject(baseType, memberType, memberClass, treatConstructorAsClassMember, firstParamType, diag);
18114
+ const result = bindFunctionToClassOrObject(baseType, memberType, memberClass, treatConstructorAsClassMember, selfType, diag);
18121
18115
  if (!result && errorNode && diag) {
18122
18116
  addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, diag.getString(), errorNode);
18123
18117
  }
@@ -18125,75 +18119,43 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
18125
18119
  }
18126
18120
  // If the memberType is an instance or class method, creates a new
18127
18121
  // version of the function that has the "self" or "cls" parameter bound
18128
- // to it. If treatAsClassMethod is true, the function is treated like a
18129
- // class method even if it's not marked as such. That's needed to
18130
- // special-case the __new__ magic method when it's invoked as a
18131
- // constructor (as opposed to by name).
18132
- function bindFunctionToClassOrObject(baseType, memberType, memberClass, treatConstructorAsClassMember = false, firstParamType, diag, recursionCount = 0) {
18133
- if ((0, types_1.isFunction)(memberType)) {
18122
+ // to it. If treatConstructorAsClassMember is true, the function is
18123
+ // treated like a class method even if it's not marked as such. That's
18124
+ // needed to special-case the __new__ magic method when it's invoked as
18125
+ // a constructor (as opposed to by name).
18126
+ function bindFunctionToClassOrObject(baseType, memberType, memberClass, treatConstructorAsClassMember = false, selfType, diag, recursionCount = 0) {
18127
+ return (0, typeUtils_1.mapSignatures)(memberType, (functionType) => {
18134
18128
  // If the caller specified no base type, always strip the
18135
18129
  // first parameter. This is used in cases like constructors.
18136
18130
  if (!baseType) {
18137
- return types_1.FunctionType.clone(memberType, /* stripFirstParam */ true);
18131
+ return types_1.FunctionType.clone(functionType, /* stripFirstParam */ true);
18138
18132
  }
18139
- if (types_1.FunctionType.isInstanceMethod(memberType)) {
18133
+ if (types_1.FunctionType.isInstanceMethod(functionType)) {
18140
18134
  // If the baseType is a metaclass, don't specialize the function.
18141
18135
  if ((0, typeUtils_1.isInstantiableMetaclass)(baseType)) {
18142
- return memberType;
18136
+ return functionType;
18143
18137
  }
18144
- const baseObj = (0, types_1.isClassInstance)(baseType)
18145
- ? baseType
18146
- : types_1.ClassType.cloneAsInstance((0, typeUtils_1.specializeClassType)(baseType));
18147
- return partiallySpecializeFunctionForBoundClassOrObject(baseType, memberType, memberClass || types_1.ClassType.cloneAsInstantiable(baseObj), diag, recursionCount, firstParamType || baseObj,
18138
+ const baseObj = (0, types_1.isInstantiableClass)(baseType)
18139
+ ? types_1.ClassType.cloneAsInstance((0, typeUtils_1.specializeClassType)(baseType))
18140
+ : baseType;
18141
+ return partiallySpecializeFunctionForBoundClassOrObject(baseType, functionType, memberClass !== null && memberClass !== void 0 ? memberClass : types_1.ClassType.cloneAsInstantiable(baseObj), diag, recursionCount, selfType !== null && selfType !== void 0 ? selfType : baseObj,
18148
18142
  /* stripFirstParam */ (0, types_1.isClassInstance)(baseType));
18149
18143
  }
18150
- if (types_1.FunctionType.isClassMethod(memberType) ||
18151
- (treatConstructorAsClassMember && types_1.FunctionType.isConstructorMethod(memberType))) {
18144
+ if (types_1.FunctionType.isClassMethod(functionType) ||
18145
+ (treatConstructorAsClassMember && types_1.FunctionType.isConstructorMethod(functionType))) {
18152
18146
  const baseClass = (0, types_1.isInstantiableClass)(baseType) ? baseType : types_1.ClassType.cloneAsInstantiable(baseType);
18153
- // If the caller passed an object as the base type, we need to also
18154
- // convert the firstParamType to an instantiable.
18155
- const effectiveFirstParamType = firstParamType
18156
- ? (0, types_1.isInstantiableClass)(baseType)
18157
- ? firstParamType
18158
- : (0, typeUtils_1.convertToInstantiable)(firstParamType)
18159
- : baseClass;
18160
- return partiallySpecializeFunctionForBoundClassOrObject(types_1.TypeBase.isInstance(baseType) ? types_1.ClassType.cloneAsInstantiable(baseType) : baseType, memberType, memberClass || baseClass, diag, recursionCount, effectiveFirstParamType,
18147
+ const clsType = selfType ? (0, typeUtils_1.convertToInstantiable)(selfType) : undefined;
18148
+ return partiallySpecializeFunctionForBoundClassOrObject(baseClass, functionType, memberClass !== null && memberClass !== void 0 ? memberClass : baseClass, diag, recursionCount, clsType !== null && clsType !== void 0 ? clsType : baseClass,
18161
18149
  /* stripFirstParam */ true);
18162
18150
  }
18163
- if (types_1.FunctionType.isStaticMethod(memberType)) {
18151
+ if (types_1.FunctionType.isStaticMethod(functionType)) {
18164
18152
  const baseClass = (0, types_1.isInstantiableClass)(baseType) ? baseType : types_1.ClassType.cloneAsInstantiable(baseType);
18165
- return partiallySpecializeFunctionForBoundClassOrObject(types_1.TypeBase.isInstance(baseType) ? types_1.ClassType.cloneAsInstantiable(baseType) : baseType, memberType, memberClass || baseClass, diag, recursionCount,
18166
- /* effectiveFirstParamType */ undefined,
18153
+ return partiallySpecializeFunctionForBoundClassOrObject(baseClass, functionType, memberClass !== null && memberClass !== void 0 ? memberClass : baseClass, diag, recursionCount,
18154
+ /* firstParamType */ undefined,
18167
18155
  /* stripFirstParam */ false);
18168
18156
  }
18169
- }
18170
- else if ((0, types_1.isOverloadedFunction)(memberType)) {
18171
- const newOverloadType = types_1.OverloadedFunctionType.create([]);
18172
- // Don't bother binding the implementation.
18173
- types_1.OverloadedFunctionType.getOverloads(memberType).forEach((overload) => {
18174
- const boundMethod = bindFunctionToClassOrObject(baseType, overload, memberClass, treatConstructorAsClassMember, firstParamType,
18175
- /* diag */ undefined, recursionCount);
18176
- if (boundMethod) {
18177
- types_1.OverloadedFunctionType.addOverload(newOverloadType, boundMethod);
18178
- }
18179
- });
18180
- const newOverloads = types_1.OverloadedFunctionType.getOverloads(newOverloadType);
18181
- if (newOverloads.length === 0) {
18182
- // No overloads matched, so rebind with the diag
18183
- // to report the error(s) to the user.
18184
- if (diag) {
18185
- memberType.overloads.forEach((overload) => {
18186
- bindFunctionToClassOrObject(baseType, overload, memberClass, treatConstructorAsClassMember, firstParamType, diag, recursionCount);
18187
- });
18188
- }
18189
- return undefined;
18190
- }
18191
- if (newOverloads.length === 1) {
18192
- return newOverloads[0];
18193
- }
18194
- return newOverloadType;
18195
- }
18196
- return memberType;
18157
+ return functionType;
18158
+ });
18197
18159
  }
18198
18160
  // Specializes the specified function for the specified class,
18199
18161
  // optionally stripping the first first parameter (the "self" or "cls")
@@ -18298,6 +18260,22 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
18298
18260
  }
18299
18261
  return true;
18300
18262
  }
18263
+ function isLegalImplicitTypeAliasType(type) {
18264
+ // We explicitly exclude "Unknown" and "...".
18265
+ if ((0, types_1.isUnknown)(type) || (0, typeUtils_1.isEllipsisType)(type)) {
18266
+ return false;
18267
+ }
18268
+ // Look at the subtypes within the union. If any of them are not
18269
+ // instantiable (other than "None" which is special-cased), it is
18270
+ // not a legal type alias type.
18271
+ let isLegal = true;
18272
+ (0, typeUtils_1.doForEachSubtype)(type, (subtype) => {
18273
+ if (!types_1.TypeBase.isInstantiable(subtype) && !(0, typeUtils_1.isNoneInstance)(subtype)) {
18274
+ isLegal = false;
18275
+ }
18276
+ });
18277
+ return isLegal;
18278
+ }
18301
18279
  function printObjectTypeForClass(type) {
18302
18280
  return TypePrinter.printObjectTypeForClass(type, evaluatorOptions.printTypeFlags, getFunctionEffectiveReturnType);
18303
18281
  }
@@ -18467,9 +18445,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
18467
18445
  getBuiltInType,
18468
18446
  getTypeOfMember,
18469
18447
  getTypeOfObjectMember,
18470
- getTypeOfClassMemberName,
18471
18448
  getBoundMethod,
18472
- getTypeOfMagicMethodReturn,
18449
+ getTypeOfMagicMethodCall,
18473
18450
  bindFunctionToClassOrObject,
18474
18451
  getCallSignatureInfo,
18475
18452
  getAbstractMethods,
@@ -18483,6 +18460,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
18483
18460
  getTypedDictClassType,
18484
18461
  getTupleClassType,
18485
18462
  getObjectType,
18463
+ getNoneType,
18486
18464
  getBuiltInObject,
18487
18465
  getTypingType,
18488
18466
  assignTypeArguments,