@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
@@ -358,6 +358,7 @@ class Checker extends parseTreeWalker_1.ParseTreeWalker {
358
358
  // Verify TypeGuard functions.
359
359
  this._validateTypeGuardFunction(node, functionTypeResult.functionType, containingClassNode !== undefined);
360
360
  this._validateFunctionTypeVarUsage(node, functionTypeResult);
361
+ this._validateGeneratorReturnType(node, functionTypeResult.functionType);
361
362
  }
362
363
  // If we're at the module level within a stub file, report a diagnostic
363
364
  // if there is a '__getattr__' function defined when in strict mode.
@@ -723,7 +724,10 @@ class Checker extends parseTreeWalker_1.ParseTreeWalker {
723
724
  const baseType = this._evaluator.getType(node.baseExpression);
724
725
  if (baseType) {
725
726
  (0, typeUtils_1.doForEachSubtype)(baseType, (subtype) => {
726
- if ((0, types_1.isClassInstance)(subtype) && subtype.tupleTypeArguments && !(0, typeUtils_1.isUnboundedTupleClass)(subtype)) {
727
+ if ((0, types_1.isClassInstance)(subtype) &&
728
+ subtype.tupleTypeArguments &&
729
+ !(0, typeUtils_1.isUnboundedTupleClass)(subtype) &&
730
+ !this._evaluator.isTypeSubsumedByOtherType(subtype, baseType, /* allowAnyToSubsume */ false)) {
727
731
  const tupleLength = subtype.tupleTypeArguments.length;
728
732
  if (node.items.length === 1 &&
729
733
  !node.trailingComma &&
@@ -1412,6 +1416,49 @@ class Checker extends parseTreeWalker_1.ParseTreeWalker {
1412
1416
  }
1413
1417
  return true;
1414
1418
  }
1419
+ // If the function is a generator, validates that its annotated return type
1420
+ // is appropriate for a generator.
1421
+ _validateGeneratorReturnType(node, functionType) {
1422
+ var _a;
1423
+ if (!types_1.FunctionType.isGenerator(functionType)) {
1424
+ return;
1425
+ }
1426
+ const declaredReturnType = functionType.details.declaredReturnType;
1427
+ if (!declaredReturnType) {
1428
+ return;
1429
+ }
1430
+ if ((0, types_1.isNever)(declaredReturnType)) {
1431
+ return;
1432
+ }
1433
+ const functionDecl = functionType.details.declaration;
1434
+ if (!functionDecl || !functionDecl.yieldStatements || functionDecl.yieldStatements.length === 0) {
1435
+ return;
1436
+ }
1437
+ let generatorType;
1438
+ if (!node.isAsync &&
1439
+ (0, types_1.isClassInstance)(declaredReturnType) &&
1440
+ types_1.ClassType.isBuiltIn(declaredReturnType, 'AwaitableGenerator')) {
1441
+ // Handle the old-style (pre-await) generator case
1442
+ // if the return type explicitly uses AwaitableGenerator.
1443
+ generatorType = this._evaluator.getTypingType(node, 'AwaitableGenerator');
1444
+ }
1445
+ else {
1446
+ generatorType = this._evaluator.getTypingType(node, node.isAsync ? 'AsyncGenerator' : 'Generator');
1447
+ }
1448
+ if (!generatorType || !(0, types_1.isInstantiableClass)(generatorType)) {
1449
+ return;
1450
+ }
1451
+ const specializedGenerator = types_1.ClassType.cloneAsInstance(types_1.ClassType.cloneForSpecialization(generatorType, [types_1.AnyType.create(), types_1.AnyType.create(), types_1.AnyType.create()],
1452
+ /* isTypeArgumentExplicit */ true));
1453
+ const diagAddendum = new diagnostic_1.DiagnosticAddendum();
1454
+ if (!this._evaluator.assignType(declaredReturnType, specializedGenerator, diagAddendum)) {
1455
+ const errorMessage = node.isAsync
1456
+ ? localize_1.Localizer.Diagnostic.generatorAsyncReturnType()
1457
+ : localize_1.Localizer.Diagnostic.generatorSyncReturnType();
1458
+ this._evaluator.addDiagnostic(this._fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, errorMessage.format({ yieldType: this._evaluator.printType(types_1.AnyType.create()) }) +
1459
+ diagAddendum.getString(), (_a = node.returnTypeAnnotation) !== null && _a !== void 0 ? _a : node.name);
1460
+ }
1461
+ }
1415
1462
  // Determines whether the specified type is one that should trigger
1416
1463
  // an "unused" value diagnostic.
1417
1464
  _isTypeValidForUnusedValueTest(type) {
@@ -1897,14 +1944,6 @@ class Checker extends parseTreeWalker_1.ParseTreeWalker {
1897
1944
  // There should never be a single overload.
1898
1945
  this._evaluator.addDiagnostic(this._fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.singleOverload().format({ name }), primaryDecl.node.name);
1899
1946
  }
1900
- overloadedFunctions.forEach((overload) => {
1901
- if (overload.details.declaration &&
1902
- !ParseTreeUtils.isFunctionSuiteEmpty(overload.details.declaration.node)) {
1903
- const diag = new diagnostic_1.DiagnosticAddendum();
1904
- diag.addMessage(localize_1.Localizer.DiagnosticAddendum.overloadWithImplementation());
1905
- this._evaluator.addDiagnostic(this._fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.overloadWithImplementation().format({ name }) + diag.getString(), overload.details.declaration.node.name);
1906
- }
1907
- });
1908
1947
  // If the file is not a stub and this is the first overload,
1909
1948
  // verify that there is an implementation.
1910
1949
  if (!this._fileInfo.isStubFile && overloadedFunctions.length > 0) {
@@ -2383,6 +2422,7 @@ class Checker extends parseTreeWalker_1.ParseTreeWalker {
2383
2422
  arg0Type = (0, typeUtils_1.mapSubtypes)(arg0Type, (subtype) => {
2384
2423
  return (0, typeUtils_1.transformPossibleRecursiveTypeAlias)(subtype);
2385
2424
  });
2425
+ arg0Type = this._evaluator.expandPromotionTypes(node, arg0Type);
2386
2426
  const arg1Type = this._evaluator.getType(node.arguments[1].valueExpression);
2387
2427
  if (!arg1Type) {
2388
2428
  return;
@@ -2499,7 +2539,7 @@ class Checker extends parseTreeWalker_1.ParseTreeWalker {
2499
2539
  const filteredTypes = [];
2500
2540
  for (const filterType of classTypeList) {
2501
2541
  const filterIsSuperclass = (0, typeGuards_1.isIsinstanceFilterSuperclass)(this._evaluator, varType, filterType, filterType, isInstanceCheck);
2502
- const filterIsSubclass = (0, typeGuards_1.isIsinstanceFilterSubclass)(this._evaluator, varType, filterType, filterType, isInstanceCheck);
2542
+ const filterIsSubclass = (0, typeGuards_1.isIsinstanceFilterSubclass)(this._evaluator, varType, filterType, isInstanceCheck);
2503
2543
  // Normally, a class should never be both a subclass and a
2504
2544
  // superclass. However, this can happen if one of the classes
2505
2545
  // derives from an unknown type. In this case, we'll add an
@@ -2662,27 +2702,34 @@ class Checker extends parseTreeWalker_1.ParseTreeWalker {
2662
2702
  // we can determine which overload(s) were used to satisfy
2663
2703
  // the call expression and determine whether any of them
2664
2704
  // are deprecated.
2705
+ let callTypeResult;
2665
2706
  const callNode = ParseTreeUtils.getCallForName(node);
2666
2707
  if (callNode) {
2667
- const callTypeResult = evaluator.getTypeResult(callNode);
2668
- if (callTypeResult &&
2669
- callTypeResult.overloadsUsedForCall &&
2670
- callTypeResult.overloadsUsedForCall.length > 0) {
2671
- callTypeResult.overloadsUsedForCall.forEach((overload) => {
2672
- if (overload.details.deprecatedMessage !== undefined) {
2673
- if (node.value === overload.details.name) {
2674
- deprecatedMessage = overload.details.deprecatedMessage;
2675
- errorMessage = getDeprecatedMessageForFunction(overload);
2676
- }
2677
- else if ((0, types_1.isInstantiableClass)(type) && overload.details.name === '__init__') {
2678
- deprecatedMessage = overload.details.deprecatedMessage;
2679
- errorMessage = localize_1.Localizer.Diagnostic.deprecatedConstructor().format({
2680
- name: type.details.name,
2681
- });
2682
- }
2708
+ callTypeResult = evaluator.getTypeResult(callNode);
2709
+ }
2710
+ else {
2711
+ const decoratorNode = ParseTreeUtils.getDecoratorForName(node);
2712
+ if (decoratorNode) {
2713
+ callTypeResult = evaluator.getTypeResultForDecorator(decoratorNode);
2714
+ }
2715
+ }
2716
+ if (callTypeResult &&
2717
+ callTypeResult.overloadsUsedForCall &&
2718
+ callTypeResult.overloadsUsedForCall.length > 0) {
2719
+ callTypeResult.overloadsUsedForCall.forEach((overload) => {
2720
+ if (overload.details.deprecatedMessage !== undefined) {
2721
+ if (node.value === overload.details.name) {
2722
+ deprecatedMessage = overload.details.deprecatedMessage;
2723
+ errorMessage = getDeprecatedMessageForFunction(overload);
2683
2724
  }
2684
- });
2685
- }
2725
+ else if ((0, types_1.isInstantiableClass)(type) && overload.details.name === '__init__') {
2726
+ deprecatedMessage = overload.details.deprecatedMessage;
2727
+ errorMessage = localize_1.Localizer.Diagnostic.deprecatedConstructor().format({
2728
+ name: type.details.name,
2729
+ });
2730
+ }
2731
+ }
2732
+ });
2686
2733
  }
2687
2734
  }
2688
2735
  (0, typeUtils_1.doForEachSubtype)(type, (subtype) => {
@@ -2699,9 +2746,11 @@ class Checker extends parseTreeWalker_1.ParseTreeWalker {
2699
2746
  }
2700
2747
  }
2701
2748
  else if ((0, types_1.isFunction)(subtype)) {
2702
- if (subtype.details.deprecatedMessage !== undefined && node.value === subtype.details.name) {
2703
- deprecatedMessage = subtype.details.deprecatedMessage;
2704
- errorMessage = getDeprecatedMessageForFunction(subtype);
2749
+ if (subtype.details.deprecatedMessage !== undefined) {
2750
+ if (!subtype.details.name || node.value === subtype.details.name) {
2751
+ deprecatedMessage = subtype.details.deprecatedMessage;
2752
+ errorMessage = getDeprecatedMessageForFunction(subtype);
2753
+ }
2705
2754
  }
2706
2755
  }
2707
2756
  else if ((0, types_1.isOverloadedFunction)(subtype)) {
@@ -4122,10 +4171,31 @@ class Checker extends parseTreeWalker_1.ParseTreeWalker {
4122
4171
  const decls = overrideSymbol.getDeclarations();
4123
4172
  if (decls.length > 0) {
4124
4173
  const lastDecl = decls[decls.length - 1];
4174
+ const primaryDecl = decls[0];
4125
4175
  // Verify that the override type is assignable to (same or narrower than)
4126
4176
  // the declared type of the base symbol.
4127
- const diagAddendum = new diagnostic_1.DiagnosticAddendum();
4128
- if (!this._evaluator.assignType(baseType, overrideType, diagAddendum)) {
4177
+ let isInvariant = (primaryDecl === null || primaryDecl === void 0 ? void 0 : primaryDecl.type) === 1 /* Variable */ && !primaryDecl.isFinal;
4178
+ // If the entry is a member of a frozen dataclass, it is immutable,
4179
+ // so it does not need to be invariant.
4180
+ if (types_1.ClassType.isFrozenDataClass(baseClassAndSymbol.classType) &&
4181
+ baseClassAndSymbol.classType.details.dataClassEntries) {
4182
+ const dataclassEntry = baseClassAndSymbol.classType.details.dataClassEntries.find((entry) => entry.name === memberName);
4183
+ if (dataclassEntry) {
4184
+ isInvariant = false;
4185
+ }
4186
+ }
4187
+ let diagAddendum = new diagnostic_1.DiagnosticAddendum();
4188
+ if (!this._evaluator.assignType(baseType, overrideType, diagAddendum,
4189
+ /* destTypeVarContext */ undefined,
4190
+ /* srcTypeVarContext */ undefined, isInvariant ? 1 /* EnforceInvariance */ : 0 /* Default */)) {
4191
+ if (isInvariant) {
4192
+ diagAddendum = new diagnostic_1.DiagnosticAddendum();
4193
+ diagAddendum.addMessage(localize_1.Localizer.DiagnosticAddendum.overrideIsInvariant());
4194
+ diagAddendum.createAddendum().addMessage(localize_1.Localizer.DiagnosticAddendum.overrideInvariantMismatch().format({
4195
+ overrideType: this._evaluator.printType(overrideType),
4196
+ baseType: this._evaluator.printType(baseType),
4197
+ }));
4198
+ }
4129
4199
  const diag = this._evaluator.addDiagnostic(this._fileInfo.diagnosticRuleSet.reportIncompatibleVariableOverride, diagnosticRules_1.DiagnosticRule.reportIncompatibleVariableOverride, localize_1.Localizer.Diagnostic.symbolOverridden().format({
4130
4200
  name: memberName,
4131
4201
  className: baseClassAndSymbol.classType.details.name,