@zzzen/pyright-internal 1.2.0-dev.20230910 → 1.2.0-dev.20230924

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 (92) hide show
  1. package/dist/analyzer/binder.d.ts +1 -0
  2. package/dist/analyzer/binder.js +8 -4
  3. package/dist/analyzer/binder.js.map +1 -1
  4. package/dist/analyzer/checker.d.ts +1 -0
  5. package/dist/analyzer/checker.js +103 -33
  6. package/dist/analyzer/checker.js.map +1 -1
  7. package/dist/analyzer/codeFlowEngine.js +1 -1
  8. package/dist/analyzer/codeFlowEngine.js.map +1 -1
  9. package/dist/analyzer/constraintSolver.js +1 -0
  10. package/dist/analyzer/constraintSolver.js.map +1 -1
  11. package/dist/analyzer/constructors.js +6 -0
  12. package/dist/analyzer/constructors.js.map +1 -1
  13. package/dist/analyzer/dataClasses.js +1 -1
  14. package/dist/analyzer/dataClasses.js.map +1 -1
  15. package/dist/analyzer/declarationUtils.js +12 -2
  16. package/dist/analyzer/declarationUtils.js.map +1 -1
  17. package/dist/analyzer/decorators.js +11 -4
  18. package/dist/analyzer/decorators.js.map +1 -1
  19. package/dist/analyzer/namedTuples.js +2 -2
  20. package/dist/analyzer/namedTuples.js.map +1 -1
  21. package/dist/analyzer/parameterUtils.d.ts +2 -0
  22. package/dist/analyzer/parameterUtils.js +53 -1
  23. package/dist/analyzer/parameterUtils.js.map +1 -1
  24. package/dist/analyzer/parseTreeUtils.d.ts +1 -0
  25. package/dist/analyzer/parseTreeUtils.js +19 -2
  26. package/dist/analyzer/parseTreeUtils.js.map +1 -1
  27. package/dist/analyzer/patternMatching.js +41 -23
  28. package/dist/analyzer/patternMatching.js.map +1 -1
  29. package/dist/analyzer/protocols.js +7 -5
  30. package/dist/analyzer/protocols.js.map +1 -1
  31. package/dist/analyzer/sourceMapper.d.ts +1 -0
  32. package/dist/analyzer/sourceMapper.js +9 -4
  33. package/dist/analyzer/sourceMapper.js.map +1 -1
  34. package/dist/analyzer/typeEvaluator.js +124 -78
  35. package/dist/analyzer/typeEvaluator.js.map +1 -1
  36. package/dist/analyzer/typeEvaluatorTypes.d.ts +6 -3
  37. package/dist/analyzer/typeEvaluatorTypes.js.map +1 -1
  38. package/dist/analyzer/typeGuards.d.ts +10 -1
  39. package/dist/analyzer/typeGuards.js +26 -4
  40. package/dist/analyzer/typeGuards.js.map +1 -1
  41. package/dist/analyzer/typePrinter.js +3 -2
  42. package/dist/analyzer/typePrinter.js.map +1 -1
  43. package/dist/analyzer/typeUtils.js +17 -4
  44. package/dist/analyzer/typeUtils.js.map +1 -1
  45. package/dist/analyzer/typedDicts.js +25 -21
  46. package/dist/analyzer/typedDicts.js.map +1 -1
  47. package/dist/analyzer/types.d.ts +2 -0
  48. package/dist/analyzer/types.js +10 -1
  49. package/dist/analyzer/types.js.map +1 -1
  50. package/dist/backgroundAnalysisBase.js +1 -1
  51. package/dist/backgroundAnalysisBase.js.map +1 -1
  52. package/dist/common/pathUtils.js +1 -1
  53. package/dist/common/pathUtils.js.map +1 -1
  54. package/dist/common/realFileSystem.js +8 -3
  55. package/dist/common/realFileSystem.js.map +1 -1
  56. package/dist/common/serviceProvider.d.ts +1 -1
  57. package/dist/common/serviceProvider.js +6 -1
  58. package/dist/common/serviceProvider.js.map +1 -1
  59. package/dist/languageService/autoImporter.d.ts +4 -1
  60. package/dist/languageService/autoImporter.js +12 -1
  61. package/dist/languageService/autoImporter.js.map +1 -1
  62. package/dist/localization/localize.d.ts +8 -4
  63. package/dist/localization/localize.js +3 -2
  64. package/dist/localization/localize.js.map +1 -1
  65. package/dist/localization/package.nls.cs.json +6 -7
  66. package/dist/localization/package.nls.de.json +9 -10
  67. package/dist/localization/package.nls.en-us.json +3 -2
  68. package/dist/localization/package.nls.es.json +6 -7
  69. package/dist/localization/package.nls.fr.json +6 -7
  70. package/dist/localization/package.nls.it.json +6 -7
  71. package/dist/localization/package.nls.ja.json +6 -7
  72. package/dist/localization/package.nls.ko.json +6 -7
  73. package/dist/localization/package.nls.pl.json +5 -6
  74. package/dist/localization/package.nls.pt-br.json +6 -7
  75. package/dist/localization/package.nls.qps-ploc.json +4 -5
  76. package/dist/localization/package.nls.ru.json +5 -6
  77. package/dist/localization/package.nls.tr.json +6 -7
  78. package/dist/localization/package.nls.zh-cn.json +6 -7
  79. package/dist/localization/package.nls.zh-tw.json +6 -7
  80. package/dist/tests/checker.test.js +2 -2
  81. package/dist/tests/checker.test.js.map +1 -1
  82. package/dist/tests/pathUtils.test.js +25 -3
  83. package/dist/tests/pathUtils.test.js.map +1 -1
  84. package/dist/tests/typeEvaluator1.test.js +2 -2
  85. package/dist/tests/typeEvaluator1.test.js.map +1 -1
  86. package/dist/tests/typeEvaluator2.test.js +1 -1
  87. package/dist/tests/typeEvaluator3.test.js +3 -3
  88. package/dist/tests/typeEvaluator3.test.js.map +1 -1
  89. package/dist/tests/typeEvaluator4.test.js +8 -4
  90. package/dist/tests/typeEvaluator4.test.js.map +1 -1
  91. package/dist/tests/typeEvaluator5.test.js +1 -1
  92. package/package.json +1 -1
@@ -103,7 +103,6 @@ const nonSubscriptableBuiltinTypes = new Map([
103
103
  const typePromotions = new Map([
104
104
  ['builtins.float', ['builtins.int']],
105
105
  ['builtins.complex', ['builtins.float', 'builtins.int']],
106
- ['builtins.bytes', ['builtins.bytearray', 'builtins.memoryview']],
107
106
  ]);
108
107
  // How many levels deep should we attempt to infer return
109
108
  // types based on call-site argument types? The deeper we go,
@@ -294,8 +293,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
294
293
  }
295
294
  }
296
295
  }
297
- function setTypeForNode(node, type = types_1.UnknownType.create(), flags = 0 /* None */) {
298
- writeTypeCache(node, { type }, flags);
296
+ function setTypeResultForNode(node, typeResult, flags = 0 /* None */) {
297
+ writeTypeCache(node, typeResult, flags);
299
298
  }
300
299
  function setAsymmetricDescriptorAssignment(node) {
301
300
  if (isSpeculativeModeInUse(/* node */ undefined)) {
@@ -399,6 +398,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
399
398
  evaluateTypesForExpressionInContext(node);
400
399
  });
401
400
  }
401
+ function getTypeResultForDecorator(node) {
402
+ return evaluateTypeForSubnode(node, () => {
403
+ evaluateTypesForExpressionInContext(node.expression);
404
+ });
405
+ }
402
406
  // Reads the type of the node from the cache.
403
407
  function getCachedType(node) {
404
408
  return readTypeCache(node, 0 /* None */);
@@ -2321,6 +2325,32 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
2321
2325
  });
2322
2326
  writeTypeCache(target, { type, isIncomplete: isTypeIncomplete }, 0 /* None */);
2323
2327
  }
2328
+ // If the type includes promotion types, expand these to their constituent types.
2329
+ function expandPromotionTypes(node, type) {
2330
+ return (0, typeUtils_1.mapSubtypes)(type, (subtype) => {
2331
+ if (!(0, types_1.isClass)(subtype) || !subtype.includePromotions) {
2332
+ return subtype;
2333
+ }
2334
+ const typesToCombine = [types_1.ClassType.cloneForPromotionType(subtype, /* includePromotions */ false)];
2335
+ const promotionTypeNames = typePromotions.get(subtype.details.fullName);
2336
+ if (promotionTypeNames) {
2337
+ for (const promotionTypeName of promotionTypeNames) {
2338
+ const nameSplit = promotionTypeName.split('.');
2339
+ let promotionSubtype = getBuiltInType(node, nameSplit[nameSplit.length - 1]);
2340
+ if (promotionSubtype && (0, types_1.isInstantiableClass)(promotionSubtype)) {
2341
+ promotionSubtype = types_1.ClassType.cloneForPromotionType(promotionSubtype,
2342
+ /* includePromotions */ false);
2343
+ if ((0, types_1.isClassInstance)(subtype)) {
2344
+ promotionSubtype = types_1.ClassType.cloneAsInstance(promotionSubtype);
2345
+ }
2346
+ promotionSubtype = (0, typeUtils_1.addConditionToType)(promotionSubtype, subtype.condition);
2347
+ typesToCombine.push(promotionSubtype);
2348
+ }
2349
+ }
2350
+ }
2351
+ return (0, types_1.combineTypes)(typesToCombine);
2352
+ });
2353
+ }
2324
2354
  // Replaces all of the top-level TypeVars (as opposed to TypeVars
2325
2355
  // used as type arguments in other types) with their concrete form.
2326
2356
  // If conditionFilter is specified and the TypeVar is a constrained
@@ -3405,7 +3435,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3405
3435
  const isNotRequired = false;
3406
3436
  // If the base type was incomplete and unbound, don't proceed
3407
3437
  // because false positive errors will be generated.
3408
- if (baseTypeResult.isIncomplete && (0, types_1.isUnbound)(baseTypeResult.type)) {
3438
+ if (baseTypeResult.isIncomplete && (0, types_1.isUnbound)(baseType)) {
3409
3439
  return { type: types_1.UnknownType.create(/* isIncomplete */ true), isIncomplete: true };
3410
3440
  }
3411
3441
  // Handle the special case where the expression is an actual
@@ -3429,6 +3459,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3429
3459
  if ((0, types_1.isParamSpec)(baseType) && baseType.paramSpecAccess) {
3430
3460
  baseType = makeTopLevelTypeVarsConcrete(baseType);
3431
3461
  }
3462
+ // Do union expansion for promotion types.
3463
+ baseType = expandPromotionTypes(node, baseType);
3432
3464
  switch (baseType.category) {
3433
3465
  case 2 /* Any */:
3434
3466
  case 1 /* Unknown */:
@@ -5184,7 +5216,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5184
5216
  typeArgs.push(getTypeArgTypeResult(item, index));
5185
5217
  });
5186
5218
  // Set the node's type so it isn't reevaluated later.
5187
- setTypeForNode(node.items[0].valueExpression, types_1.UnknownType.create());
5219
+ setTypeResultForNode(node.items[0].valueExpression, { type: types_1.UnknownType.create() });
5188
5220
  }
5189
5221
  else {
5190
5222
  node.items.forEach((arg, index) => {
@@ -5228,7 +5260,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5228
5260
  node,
5229
5261
  };
5230
5262
  // Set the node's type so it isn't reevaluated later.
5231
- setTypeForNode(node, types_1.UnknownType.create());
5263
+ setTypeResultForNode(node, { type: types_1.UnknownType.create() });
5232
5264
  }
5233
5265
  else if (node.nodeType === 15 /* Dictionary */ && supportsDictExpression) {
5234
5266
  const inlinedTypeDict = typedDictClassType && (0, types_1.isInstantiableClass)(typedDictClassType)
@@ -6104,6 +6136,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6104
6136
  function validateOverloadedFunctionArguments(errorNode, argList, typeResult, typeVarContext, skipUnknownArgCheck, inferenceContext) {
6105
6137
  let filteredMatchResults = [];
6106
6138
  let contextFreeArgTypes;
6139
+ let isTypeIncomplete = !!typeResult.isIncomplete;
6107
6140
  // Start by evaluating the types of the arguments without any expected
6108
6141
  // type. Also, filter the list of overloads based on the number of
6109
6142
  // positional and named arguments that are present. We do all of this
@@ -6117,6 +6150,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6117
6150
  // intended behavior according to PEP 484.
6118
6151
  const matchResults = matchFunctionArgumentsToParameters(errorNode, argList, { type: overload, isIncomplete: typeResult.isIncomplete }, overloadIndex);
6119
6152
  if (!matchResults.argumentErrors) {
6153
+ if (inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.expectedType) {
6154
+ const returnType = getFunctionEffectiveReturnType(matchResults.overload);
6155
+ if (!assignType(inferenceContext.expectedType, returnType,
6156
+ /* diag */ undefined,
6157
+ /* destTypeVarContext */ undefined,
6158
+ /* srcTypeVarContext */ undefined, 8 /* SkipSolveTypeVars */)) {
6159
+ matchResults.relevance += -0.5;
6160
+ }
6161
+ }
6120
6162
  filteredMatchResults.push(matchResults);
6121
6163
  }
6122
6164
  overloadIndex++;
@@ -6144,7 +6186,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6144
6186
  diagAddendum.addMessage(localize_1.Localizer.DiagnosticAddendum.argumentTypes().format({ types: argTypes.join(', ') }));
6145
6187
  addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.noOverload().format({ name: functionName }) + diagAddendum.getString(), errorNode);
6146
6188
  }
6147
- return { argumentErrors: true, isTypeIncomplete: false, overloadsUsedForCall: [] };
6189
+ return { argumentErrors: true, isTypeIncomplete, overloadsUsedForCall: [] };
6148
6190
  }
6149
6191
  // Create a helper function that evaluates the overload that best
6150
6192
  // matches the arg/param lists.
@@ -6170,7 +6212,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6170
6212
  return evaluateUsingBestMatchingOverload(/* skipUnknownArgCheck */ false);
6171
6213
  }
6172
6214
  let expandedArgTypes = [argList.map((arg) => undefined)];
6173
- let isTypeIncomplete = !!typeResult.isIncomplete;
6174
6215
  while (true) {
6175
6216
  const callResult = validateOverloadsWithExpandedTypes(errorNode, expandedArgTypes, filteredMatchResults, typeVarContext, skipUnknownArgCheck, inferenceContext);
6176
6217
  if (callResult.isTypeIncomplete) {
@@ -6216,7 +6257,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6216
6257
  result.returnType = types_1.UnknownType.create();
6217
6258
  return { ...result, argumentErrors: true };
6218
6259
  }
6219
- return { argumentErrors: true, isTypeIncomplete: false, overloadsUsedForCall: [] };
6260
+ return { argumentErrors: true, isTypeIncomplete, overloadsUsedForCall: [] };
6220
6261
  }
6221
6262
  // Replaces each item in the expandedArgTypes with n items where n is
6222
6263
  // the number of subtypes in a union. The contextFreeArgTypes parameter
@@ -6900,14 +6941,16 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6900
6941
  // all positional parameters specified in the Concatenate must be
6901
6942
  // filled explicitly.
6902
6943
  if (typeResult.type.details.paramSpec && paramIndex < positionParamLimitIndex) {
6903
- if (!isDiagnosticSuppressedForNode(errorNode)) {
6904
- addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, positionParamLimitIndex === 1
6905
- ? localize_1.Localizer.Diagnostic.argPositionalExpectedOne()
6906
- : localize_1.Localizer.Diagnostic.argPositionalExpectedCount().format({
6907
- expected: positionParamLimitIndex,
6908
- }), (_c = argList[argIndex].valueExpression) !== null && _c !== void 0 ? _c : errorNode);
6944
+ if ((0, types_1.isTypeVar)(argTypeResult.type) && argTypeResult.type.paramSpecAccess === 'args') {
6945
+ if (!isDiagnosticSuppressedForNode(errorNode)) {
6946
+ addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, positionParamLimitIndex === 1
6947
+ ? localize_1.Localizer.Diagnostic.argPositionalExpectedOne()
6948
+ : localize_1.Localizer.Diagnostic.argPositionalExpectedCount().format({
6949
+ expected: positionParamLimitIndex,
6950
+ }), (_c = argList[argIndex].valueExpression) !== null && _c !== void 0 ? _c : errorNode);
6951
+ }
6952
+ reportedArgError = true;
6909
6953
  }
6910
- reportedArgError = true;
6911
6954
  }
6912
6955
  const argType = argTypeResult.type;
6913
6956
  if (isParamVariadic && (0, types_1.isUnpackedVariadicTypeVar)(argType)) {
@@ -7193,17 +7236,17 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
7193
7236
  reportedArgError = true;
7194
7237
  }
7195
7238
  }
7196
- else if ((0, types_1.isParamSpec)(argType) && argType.paramSpecAccess === 'kwargs') {
7239
+ else if (typeResult.type.details.paramSpec &&
7240
+ (0, parameterUtils_1.isParamSpecKwargsArgument)(typeResult.type.details.paramSpec, argType)) {
7197
7241
  unpackedDictionaryArgType = types_1.AnyType.create();
7198
- if (typeResult.type.details.paramSpec) {
7199
- validateArgTypeParams.push({
7200
- paramCategory: 2 /* KwargsDict */,
7201
- paramType: typeResult.type.details.paramSpec,
7202
- requiresTypeVarMatching: false,
7203
- argument: argList[argIndex],
7204
- errorNode: argList[argIndex].valueExpression || errorNode,
7205
- });
7206
- }
7242
+ validateArgTypeParams.push({
7243
+ paramCategory: 2 /* KwargsDict */,
7244
+ paramType: typeResult.type.details.paramSpec,
7245
+ requiresTypeVarMatching: false,
7246
+ argument: argList[argIndex],
7247
+ argType: (0, types_1.isParamSpec)(argType) ? undefined : types_1.AnyType.create(),
7248
+ errorNode: argList[argIndex].valueExpression || errorNode,
7249
+ });
7207
7250
  }
7208
7251
  else {
7209
7252
  const strObjType = getBuiltInObject(errorNode, 'str');
@@ -7331,20 +7374,21 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
7331
7374
  }
7332
7375
  }
7333
7376
  else if (argList[argIndex].argumentCategory === 1 /* UnpackedList */) {
7334
- // Handle the case where a *args: P.args is passed as an argument to
7335
- // a function that accepts a ParamSpec.
7377
+ // Handle the case where a *args: P.args (or *args: Any) is passed as an
7378
+ // argument to a function that accepts a ParamSpec.
7336
7379
  if (typeResult.type.details.paramSpec) {
7337
7380
  const argTypeResult = getTypeOfArgument(argList[argIndex]);
7338
7381
  const argType = argTypeResult.type;
7339
7382
  if (argTypeResult.isIncomplete) {
7340
7383
  isTypeIncomplete = true;
7341
7384
  }
7342
- if ((0, types_1.isParamSpec)(argType) && argType.paramSpecAccess === 'args') {
7385
+ if ((0, parameterUtils_1.isParamSpecArgsArgument)(typeResult.type.details.paramSpec, argType)) {
7343
7386
  validateArgTypeParams.push({
7344
7387
  paramCategory: 1 /* ArgsList */,
7345
7388
  paramType: typeResult.type.details.paramSpec,
7346
7389
  requiresTypeVarMatching: false,
7347
7390
  argument: argList[argIndex],
7391
+ argType: (0, types_1.isParamSpec)(argType) ? undefined : types_1.AnyType.create(),
7348
7392
  errorNode: (_j = argList[argIndex].valueExpression) !== null && _j !== void 0 ? _j : errorNode,
7349
7393
  });
7350
7394
  }
@@ -7764,12 +7808,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
7764
7808
  }
7765
7809
  if (type.details.paramSpec) {
7766
7810
  if (argParam.argument.argumentCategory === 1 /* UnpackedList */) {
7767
- if ((0, types_1.isParamSpec)(argResult.argType) && argResult.argType.paramSpecAccess === 'args') {
7811
+ if ((0, parameterUtils_1.isParamSpecArgsArgument)(type.details.paramSpec, argResult.argType)) {
7768
7812
  sawParamSpecArgs = true;
7769
7813
  }
7770
7814
  }
7771
7815
  if (argParam.argument.argumentCategory === 2 /* UnpackedDictionary */) {
7772
- if ((0, types_1.isParamSpec)(argResult.argType) && argResult.argType.paramSpecAccess === 'kwargs') {
7816
+ if ((0, parameterUtils_1.isParamSpecKwargsArgument)(type.details.paramSpec, argResult.argType)) {
7773
7817
  sawParamSpecKwargs = true;
7774
7818
  }
7775
7819
  }
@@ -10685,6 +10729,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10685
10729
  }
10686
10730
  return (0, typeUtils_1.convertToInstance)(typeVar);
10687
10731
  });
10732
+ const typeAliasScopeId = ParseTreeUtils.getScopeIdForNode(name);
10688
10733
  // Validate the default types for all type parameters.
10689
10734
  typeParameters.forEach((typeParam, index) => {
10690
10735
  var _a;
@@ -10692,7 +10737,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10692
10737
  if (typeParamNodes && index < typeParamNodes.length) {
10693
10738
  bestErrorNode = (_a = typeParamNodes[index].defaultExpression) !== null && _a !== void 0 ? _a : typeParamNodes[index].name;
10694
10739
  }
10695
- validateTypeParameterDefault(bestErrorNode, typeParam, typeParameters.slice(0, index));
10740
+ validateTypeParameterDefault(bestErrorNode, typeParam, typeParameters.slice(0, index), typeAliasScopeId);
10696
10741
  });
10697
10742
  // Verify that we have at most one variadic type variable.
10698
10743
  const variadics = typeParameters.filter((param) => (0, types_1.isVariadicTypeVar)(param));
@@ -10702,7 +10747,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10702
10747
  }), errorNode);
10703
10748
  }
10704
10749
  const fileInfo = AnalyzerNodeInfo.getFileInfo(name);
10705
- const typeAliasScopeId = ParseTreeUtils.getScopeIdForNode(name);
10706
10750
  const boundTypeVars = typeParameters.filter((typeVar) => typeVar.scopeId !== typeAliasScopeId && typeVar.scopeType === 0 /* Class */);
10707
10751
  if (boundTypeVars.length > 0) {
10708
10752
  addError(localize_1.Localizer.Diagnostic.genericTypeAliasBoundTypeVar().format({
@@ -11137,6 +11181,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
11137
11181
  /* declaredMetaclass */ undefined,
11138
11182
  /* effectiveMetaclass */ undefined, ParseTreeUtils.getDocString(node.suite.statements));
11139
11183
  classType.details.typeVarScopeId = ParseTreeUtils.getScopeIdForNode(node);
11184
+ // Is this a special type that supports type promotions according to PEP 484?
11185
+ if (typePromotions.has(classType.details.fullName)) {
11186
+ classType.includePromotions = true;
11187
+ }
11140
11188
  // Some classes refer to themselves within type arguments used within
11141
11189
  // base classes. We'll register the partially-constructed class type
11142
11190
  // to allow these to be resolved.
@@ -11244,14 +11292,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
11244
11292
  if (types_1.ClassType.isBuiltIn(argType, 'TypedDict') || types_1.ClassType.isTypedDictClass(argType)) {
11245
11293
  classType.details.flags |= 128 /* TypedDictClass */;
11246
11294
  }
11247
- else if (types_1.ClassType.isTypedDictClass(classType) && !types_1.ClassType.isTypedDictClass(argType)) {
11248
- // Exempt Generic from this test. As of Python 3.11, generic TypedDict
11249
- // classes are supported.
11250
- if (!(0, types_1.isInstantiableClass)(argType) || !types_1.ClassType.isBuiltIn(argType, 'Generic')) {
11251
- // TypedDict classes must derive only from other TypedDict classes.
11252
- addError(localize_1.Localizer.Diagnostic.typedDictBaseClass(), arg);
11253
- }
11254
- }
11255
11295
  // Validate that the class isn't deriving from itself, creating a
11256
11296
  // circular dependency.
11257
11297
  if ((0, typeUtils_1.derivesFromClassRecursive)(argType, classType, /* ignoreUnknown */ true)) {
@@ -11412,7 +11452,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
11412
11452
  const typeParamNode = node.typeParameters.parameters[index];
11413
11453
  bestErrorNode = (_a = typeParamNode.defaultExpression) !== null && _a !== void 0 ? _a : typeParamNode.name;
11414
11454
  }
11415
- validateTypeParameterDefault(bestErrorNode, typeParam, classType.details.typeParameters.slice(0, index));
11455
+ validateTypeParameterDefault(bestErrorNode, typeParam, classType.details.typeParameters.slice(0, index), classType.details.typeVarScopeId);
11416
11456
  });
11417
11457
  if (!(0, typeUtils_1.computeMroLinearization)(classType)) {
11418
11458
  addError(localize_1.Localizer.Diagnostic.methodOrdering(), node.name);
@@ -11548,6 +11588,20 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
11548
11588
  registerDeferredClassCompletion(node, /* dependsUpon */ undefined);
11549
11589
  // Synthesize TypedDict methods.
11550
11590
  if (types_1.ClassType.isTypedDictClass(classType)) {
11591
+ // TypedDict classes must derive only from other TypedDict classes.
11592
+ let foundInvalidBaseClass = false;
11593
+ const diag = new diagnostic_1.DiagnosticAddendum();
11594
+ classType.details.baseClasses.forEach((baseClass) => {
11595
+ if ((0, types_1.isClass)(baseClass) &&
11596
+ !types_1.ClassType.isTypedDictClass(baseClass) &&
11597
+ !types_1.ClassType.isBuiltIn(baseClass, ['_TypedDict', 'Generic'])) {
11598
+ foundInvalidBaseClass = true;
11599
+ diag.addMessage(localize_1.Localizer.DiagnosticAddendum.typedDictBaseClass().format({ type: baseClass.details.name }));
11600
+ }
11601
+ });
11602
+ if (foundInvalidBaseClass) {
11603
+ addError(localize_1.Localizer.Diagnostic.typedDictBaseClass() + diag.getString(), node.name);
11604
+ }
11551
11605
  (0, typedDicts_1.synthesizeTypedDictClassMethods)(evaluatorInterface, node, classType, (0, types_1.isClass)(decoratedType) && types_1.ClassType.isFinal(decoratedType));
11552
11606
  }
11553
11607
  // Synthesize dataclass methods.
@@ -11564,7 +11618,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
11564
11618
  }
11565
11619
  let skipSynthesizeHash = false;
11566
11620
  const hashSymbol = (0, typeUtils_1.lookUpClassMember)(classType, '__hash__', 2 /* SkipBaseClasses */);
11567
- if (hashSymbol) {
11621
+ // If there is a hash symbol defined in the class (i.e. one that we didn't
11622
+ // synthesize above), then we shouldn't synthesize a new one for the dataclass.
11623
+ if (hashSymbol && !hashSymbol.symbol.getSynthesizedType()) {
11568
11624
  skipSynthesizeHash = true;
11569
11625
  }
11570
11626
  (0, dataClasses_1.synthesizeDataClassMethods)(evaluatorInterface, node, classType, skipSynthesizedInit, hasExistingInitMethod, skipSynthesizeHash);
@@ -11614,11 +11670,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
11614
11670
  // type parameter. If so, validates that it is in the list of "live" type
11615
11671
  // parameters and updates the scope of the type parameter referred to in the
11616
11672
  // default type expression.
11617
- function validateTypeParameterDefault(errorNode, typeParam, otherLiveTypeParams) {
11673
+ function validateTypeParameterDefault(errorNode, typeParam, otherLiveTypeParams, scopeId) {
11618
11674
  if (!typeParam.details.defaultType &&
11619
11675
  !typeParam.details.isSynthesized &&
11620
11676
  !typeParam.details.isSynthesizedSelf) {
11621
- const typeVarWithDefault = otherLiveTypeParams.find((param) => param.details.defaultType);
11677
+ const typeVarWithDefault = otherLiveTypeParams.find((param) => param.details.defaultType && param.scopeId === scopeId);
11622
11678
  if (typeVarWithDefault) {
11623
11679
  addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeVarWithoutDefault().format({
11624
11680
  name: typeParam.details.name,
@@ -12311,7 +12367,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
12311
12367
  const typeParamNode = node.typeParameters.parameters[index];
12312
12368
  bestErrorNode = (_a = typeParamNode.defaultExpression) !== null && _a !== void 0 ? _a : typeParamNode.name;
12313
12369
  }
12314
- validateTypeParameterDefault(bestErrorNode, typeParam, functionType.details.typeParameters.slice(0, index));
12370
+ validateTypeParameterDefault(bestErrorNode, typeParam, functionType.details.typeParameters.slice(0, index), functionType.details.typeVarScopeId);
12315
12371
  });
12316
12372
  // Clear the "partially evaluated" flag to indicate that the functionType
12317
12373
  // is fully evaluated.
@@ -14680,25 +14736,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
14680
14736
  if (resolvedDecl.type === 8 /* Alias */) {
14681
14737
  // Build a module type that corresponds to the declaration and
14682
14738
  // its associated loader actions.
14683
- let moduleName = resolvedDecl.moduleName;
14684
- if (decl.type === 8 /* Alias */) {
14685
- if (decl.symbolName) {
14686
- moduleName += '.' + decl.symbolName;
14687
- }
14688
- // If the module name is relative to the current file, use that
14689
- // file's module name as a reference.
14690
- if (moduleName.startsWith('.')) {
14691
- const fileInfo = AnalyzerNodeInfo.getFileInfo(decl.node);
14692
- const nameParts = fileInfo.moduleName.split('.');
14693
- moduleName = moduleName.substr(1);
14694
- while (moduleName.startsWith('.') && nameParts.length > 0) {
14695
- moduleName = moduleName.substr(1);
14696
- nameParts.pop();
14697
- }
14698
- moduleName = nameParts.join('.') + '.' + moduleName;
14699
- }
14700
- }
14701
- const moduleType = types_1.ModuleType.create(moduleName, resolvedDecl.path);
14739
+ const moduleType = types_1.ModuleType.create(resolvedDecl.moduleName, resolvedDecl.path);
14702
14740
  if (resolvedDecl.symbolName && resolvedDecl.submoduleFallback) {
14703
14741
  return applyLoaderActionsToModuleType(moduleType, resolvedDecl.submoduleFallback, importLookup);
14704
14742
  }
@@ -15528,11 +15566,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
15528
15566
  return true;
15529
15567
  }
15530
15568
  // Handle special-case type promotions.
15531
- const promotionList = typePromotions.get(destType.details.fullName);
15532
- if (promotionList &&
15533
- promotionList.some((srcName) => srcType.details.mro.some((mroClass) => (0, types_1.isClass)(mroClass) && srcName === mroClass.details.fullName))) {
15534
- if ((flags & 1 /* EnforceInvariance */) === 0) {
15535
- return true;
15569
+ if (destType.includePromotions) {
15570
+ const promotionList = typePromotions.get(destType.details.fullName);
15571
+ if (promotionList &&
15572
+ promotionList.some((srcName) => srcType.details.mro.some((mroClass) => (0, types_1.isClass)(mroClass) && srcName === mroClass.details.fullName))) {
15573
+ if ((flags & 1 /* EnforceInvariance */) === 0) {
15574
+ return true;
15575
+ }
15536
15576
  }
15537
15577
  }
15538
15578
  // Is it a structural type (i.e. a protocol)? If so, we need to
@@ -16312,7 +16352,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
16312
16352
  // Handle LiteralString special form.
16313
16353
  if (types_1.ClassType.isBuiltIn(destType, 'LiteralString')) {
16314
16354
  if (types_1.ClassType.isBuiltIn(concreteSrcType, 'str') && concreteSrcType.literalValue !== undefined) {
16315
- return true;
16355
+ return (flags & 1 /* EnforceInvariance */) === 0;
16316
16356
  }
16317
16357
  else if (types_1.ClassType.isBuiltIn(concreteSrcType, 'LiteralString')) {
16318
16358
  return true;
@@ -16320,7 +16360,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
16320
16360
  }
16321
16361
  else if (types_1.ClassType.isBuiltIn(concreteSrcType, 'LiteralString') &&
16322
16362
  strClassType &&
16323
- (0, types_1.isInstantiableClass)(strClassType)) {
16363
+ (0, types_1.isInstantiableClass)(strClassType) &&
16364
+ (flags & 1 /* EnforceInvariance */) === 0) {
16324
16365
  concreteSrcType = types_1.ClassType.cloneAsInstance(strClassType);
16325
16366
  }
16326
16367
  if (!assignClass(types_1.ClassType.cloneAsInstantiable(destType), types_1.ClassType.cloneAsInstantiable(concreteSrcType), diag, destTypeVarContext, srcTypeVarContext, flags, recursionCount,
@@ -16572,7 +16613,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
16572
16613
  // If we have no src subtypes remaining but not all dest types have been subsumed
16573
16614
  // by other dest types, then the types are not compatible if we're enforcing invariance.
16574
16615
  if (remainingSrcSubtypes.length === 0) {
16575
- return remainingDestSubtypes.every((destSubtype) => isTypeSubsumedByOtherType(destSubtype, destType.subtypes,
16616
+ return remainingDestSubtypes.every((destSubtype) => isTypeSubsumedByOtherType(destSubtype, destType,
16576
16617
  /* allowAnyToSubsume */ true, recursionCount));
16577
16618
  }
16578
16619
  }
@@ -16629,7 +16670,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
16629
16670
  /* diag */ undefined, destTypeVarContext, srcTypeVarContext, flags, recursionCount)) {
16630
16671
  // Determine if the current subtype is subsumed by another subtype
16631
16672
  // in the same union. If so, we can ignore this.
16632
- const isSubtypeSubsumed = isTypeSubsumedByOtherType(subtype, srcType.subtypes,
16673
+ const isSubtypeSubsumed = isTypeSubsumedByOtherType(subtype, srcType,
16633
16674
  /* allowAnyToSubsume */ false, recursionCount);
16634
16675
  // Try again with a concrete version of the subtype.
16635
16676
  if (!isSubtypeSubsumed &&
@@ -16645,9 +16686,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
16645
16686
  return true;
16646
16687
  }
16647
16688
  // Determines whether a type is "subsumed by" (i.e. is a proper subtype of) one
16648
- // of the other types in a list.
16649
- function isTypeSubsumedByOtherType(type, otherTypes, allowAnyToSubsume, recursionCount = 0) {
16689
+ // of the other type.
16690
+ function isTypeSubsumedByOtherType(type, otherType, allowAnyToSubsume, recursionCount = 0) {
16650
16691
  const concreteType = makeTopLevelTypeVarsConcrete(type);
16692
+ const otherTypes = (0, types_1.isUnion)(otherType) ? otherType.subtypes : [otherType];
16651
16693
  for (const otherType of otherTypes) {
16652
16694
  if ((0, types_1.isTypeSame)(otherType, type)) {
16653
16695
  continue;
@@ -17373,6 +17415,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
17373
17415
  !srcParamSpec ||
17374
17416
  !(0, types_1.isTypeSame)(srcParamSpec, destParamSpec, { ignoreTypeFlags: true })) {
17375
17417
  const remainingFunction = types_1.FunctionType.createInstance('', '', '', effectiveSrcType.details.flags | 64 /* SynthesizedMethod */, effectiveSrcType.details.docString);
17418
+ remainingFunction.details.deprecatedMessage = effectiveSrcType.details.deprecatedMessage;
17376
17419
  remainingFunction.details.typeVarScopeId = effectiveSrcType.details.typeVarScopeId;
17377
17420
  remainingFunction.details.constructorTypeVarScopeId =
17378
17421
  effectiveSrcType.details.constructorTypeVarScopeId;
@@ -18379,6 +18422,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
18379
18422
  runWithCancellationToken,
18380
18423
  getType,
18381
18424
  getTypeResult,
18425
+ getTypeResultForDecorator,
18382
18426
  getCachedType,
18383
18427
  getTypeOfExpression,
18384
18428
  getTypeOfAnnotation,
@@ -18402,7 +18446,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
18402
18446
  validateInitSubclassArgs,
18403
18447
  isAfterNodeReachable,
18404
18448
  isNodeReachable,
18405
- isAsymmetricDescriptorAssignment: isAsymmetricAccessorAssignment,
18449
+ isAsymmetricAccessorAssignment,
18406
18450
  suppressDiagnostics,
18407
18451
  getDeclarationsForStringNode,
18408
18452
  getDeclarationsForNameNode,
@@ -18414,8 +18458,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
18414
18458
  getGetterTypeFromProperty,
18415
18459
  getTypeOfArgument,
18416
18460
  markNamesAccessed,
18461
+ expandPromotionTypes,
18417
18462
  makeTopLevelTypeVarsConcrete,
18418
18463
  mapSubtypesExpandTypeVars,
18464
+ isTypeSubsumedByOtherType,
18419
18465
  lookUpSymbolRecursive,
18420
18466
  getDeclaredTypeOfSymbol,
18421
18467
  getEffectiveTypeOfSymbol,
@@ -18468,7 +18514,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
18468
18514
  disposeEvaluator,
18469
18515
  useSpeculativeMode,
18470
18516
  isSpeculativeModeInUse,
18471
- setTypeForNode,
18517
+ setTypeResultForNode,
18472
18518
  checkForCancellation,
18473
18519
  printControlFlowGraph,
18474
18520
  printTypeVarContext,