@zzzen/pyright-internal 1.2.0-dev.20220703 → 1.2.0-dev.20220710

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 (75) hide show
  1. package/README.md +82 -1
  2. package/dist/analyzer/analyzerFileInfo.d.ts +2 -1
  3. package/dist/analyzer/analyzerFileInfo.js.map +1 -1
  4. package/dist/analyzer/binder.js +1 -1
  5. package/dist/analyzer/binder.js.map +1 -1
  6. package/dist/analyzer/checker.js +11 -8
  7. package/dist/analyzer/checker.js.map +1 -1
  8. package/dist/analyzer/codeFlowEngine.js +6 -1
  9. package/dist/analyzer/codeFlowEngine.js.map +1 -1
  10. package/dist/analyzer/constraintSolver.js +5 -5
  11. package/dist/analyzer/constraintSolver.js.map +1 -1
  12. package/dist/analyzer/functionTransform.js +2 -1
  13. package/dist/analyzer/functionTransform.js.map +1 -1
  14. package/dist/analyzer/parseTreeUtils.d.ts +1 -1
  15. package/dist/analyzer/parseTreeUtils.js +12 -7
  16. package/dist/analyzer/parseTreeUtils.js.map +1 -1
  17. package/dist/analyzer/patternMatching.js +1 -1
  18. package/dist/analyzer/patternMatching.js.map +1 -1
  19. package/dist/analyzer/program.d.ts +2 -2
  20. package/dist/analyzer/program.js +1 -1
  21. package/dist/analyzer/program.js.map +1 -1
  22. package/dist/analyzer/service.d.ts +3 -2
  23. package/dist/analyzer/service.js +3 -2
  24. package/dist/analyzer/service.js.map +1 -1
  25. package/dist/analyzer/sourceFile.d.ts +6 -1
  26. package/dist/analyzer/sourceFile.js +17 -5
  27. package/dist/analyzer/sourceFile.js.map +1 -1
  28. package/dist/analyzer/typeEvaluator.js +179 -128
  29. package/dist/analyzer/typeEvaluator.js.map +1 -1
  30. package/dist/analyzer/typeEvaluatorTypes.d.ts +3 -3
  31. package/dist/analyzer/typeGuards.js +5 -0
  32. package/dist/analyzer/typeGuards.js.map +1 -1
  33. package/dist/analyzer/typeUtils.d.ts +1 -1
  34. package/dist/analyzer/typeUtils.js +4 -4
  35. package/dist/analyzer/typeUtils.js.map +1 -1
  36. package/dist/analyzer/typedDicts.js +3 -1
  37. package/dist/analyzer/typedDicts.js.map +1 -1
  38. package/dist/analyzer/types.d.ts +6 -5
  39. package/dist/analyzer/types.js +17 -9
  40. package/dist/analyzer/types.js.map +1 -1
  41. package/dist/languageServerBase.d.ts +3 -2
  42. package/dist/languageServerBase.js +3 -2
  43. package/dist/languageServerBase.js.map +1 -1
  44. package/dist/languageService/indentationUtils.js +3 -2
  45. package/dist/languageService/indentationUtils.js.map +1 -1
  46. package/dist/languageService/insertionPointUtils.d.ts +9 -0
  47. package/dist/languageService/insertionPointUtils.js +110 -0
  48. package/dist/languageService/insertionPointUtils.js.map +1 -0
  49. package/dist/languageService/signatureHelpProvider.js +4 -2
  50. package/dist/languageService/signatureHelpProvider.js.map +1 -1
  51. package/dist/parser/parser.d.ts +2 -1
  52. package/dist/parser/parser.js +2 -1
  53. package/dist/parser/parser.js.map +1 -1
  54. package/dist/parser/tokenizer.d.ts +2 -1
  55. package/dist/parser/tokenizer.js +3 -2
  56. package/dist/parser/tokenizer.js.map +1 -1
  57. package/dist/tests/chainedSourceFiles.test.js +2 -1
  58. package/dist/tests/chainedSourceFiles.test.js.map +1 -1
  59. package/dist/tests/fourslash/import.multipart.fourslash.d.ts +1 -0
  60. package/dist/tests/fourslash/import.multipart.fourslash.js +18 -0
  61. package/dist/tests/fourslash/import.multipart.fourslash.js.map +1 -0
  62. package/dist/tests/fourslash/signature.simple.fourslash.js +16 -0
  63. package/dist/tests/fourslash/signature.simple.fourslash.js.map +1 -1
  64. package/dist/tests/insertionPointUtils.test.d.ts +1 -0
  65. package/dist/tests/insertionPointUtils.test.js +74 -0
  66. package/dist/tests/insertionPointUtils.test.js.map +1 -0
  67. package/dist/tests/testUtils.js +2 -1
  68. package/dist/tests/testUtils.js.map +1 -1
  69. package/dist/tests/typeEvaluator3.test.js +8 -0
  70. package/dist/tests/typeEvaluator3.test.js.map +1 -1
  71. package/dist/tests/typeEvaluator4.test.js +10 -0
  72. package/dist/tests/typeEvaluator4.test.js.map +1 -1
  73. package/dist/tests/typeEvaluator5.test.js +6 -0
  74. package/dist/tests/typeEvaluator5.test.js.map +1 -1
  75. package/package.json +3 -2
@@ -915,7 +915,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
915
915
  const argList = [
916
916
  {
917
917
  argumentCategory: 0 /* Simple */,
918
- type: functionOrClassType,
918
+ typeResult: { type: functionOrClassType },
919
919
  },
920
920
  ];
921
921
  const returnType = validateCallArguments(node.expression, argList, decoratorTypeResult,
@@ -1230,7 +1230,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
1230
1230
  function addFakeArg() {
1231
1231
  argList.push({
1232
1232
  argumentCategory: previousCategory,
1233
- type: types_1.UnknownType.create(),
1233
+ typeResult: { type: types_1.UnknownType.create() },
1234
1234
  active: true,
1235
1235
  });
1236
1236
  }
@@ -3569,25 +3569,27 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3569
3569
  {
3570
3570
  // Provide "obj" argument.
3571
3571
  argumentCategory: 0 /* Simple */,
3572
- type: types_1.ClassType.isClassProperty(lookupClass)
3573
- ? baseTypeClass
3574
- : isAccessedThroughObject
3575
- ? bindToType || types_1.ClassType.cloneAsInstance(baseTypeClass)
3576
- : types_1.NoneType.createInstance(),
3572
+ typeResult: {
3573
+ type: types_1.ClassType.isClassProperty(lookupClass)
3574
+ ? baseTypeClass
3575
+ : isAccessedThroughObject
3576
+ ? bindToType || types_1.ClassType.cloneAsInstance(baseTypeClass)
3577
+ : types_1.NoneType.createInstance(),
3578
+ },
3577
3579
  },
3578
3580
  ];
3579
3581
  if (usage.method === 'get') {
3580
3582
  // Provide "objtype" argument.
3581
3583
  argList.push({
3582
3584
  argumentCategory: 0 /* Simple */,
3583
- type: baseTypeClass,
3585
+ typeResult: { type: baseTypeClass },
3584
3586
  });
3585
3587
  }
3586
3588
  else if (usage.method === 'set') {
3587
3589
  // Provide "value" argument.
3588
3590
  argList.push({
3589
3591
  argumentCategory: 0 /* Simple */,
3590
- type: (_a = usage.setType) !== null && _a !== void 0 ? _a : types_1.UnknownType.create(),
3592
+ typeResult: { type: (_a = usage.setType) !== null && _a !== void 0 ? _a : types_1.UnknownType.create() },
3591
3593
  });
3592
3594
  }
3593
3595
  if (types_1.ClassType.isPropertyClass(lookupClass) &&
@@ -3821,55 +3823,57 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
3821
3823
  }
3822
3824
  // Applies the __getattr__, __setattr__ or __delattr__ method if present.
3823
3825
  function applyAttributeAccessOverride(classType, errorNode, usage, memberName) {
3824
- var _a, _b, _c, _d;
3825
- if (usage.method === 'get') {
3826
+ var _a, _b, _c;
3827
+ const getAttributeAccessMember = (name) => {
3828
+ var _a;
3826
3829
  // See if the class has a "__getattribute__" or "__getattr__" method.
3827
3830
  // If so, arbitrary members are supported.
3828
- let getAttrType = (_a = getTypeOfClassMember(errorNode, classType, '__getattribute__', { method: 'get' },
3831
+ return (_a = getTypeOfClassMember(errorNode, classType, name, { method: 'get' },
3829
3832
  /* diag */ undefined, 4 /* SkipObjectBaseClass */ | 64 /* SkipAttributeAccessOverride */)) === null || _a === void 0 ? void 0 : _a.type;
3830
- if (!getAttrType) {
3831
- getAttrType = (_b = getTypeOfClassMember(errorNode, classType, '__getattr__', { method: 'get' },
3832
- /* diag */ undefined, 4 /* SkipObjectBaseClass */ | 64 /* SkipAttributeAccessOverride */)) === null || _b === void 0 ? void 0 : _b.type;
3833
- }
3834
- // If it's an overload, it might be based on the member name. Create
3835
- // a literal str type based on the member name and find the best overload.
3836
- if (getAttrType && (0, types_1.isOverloadedFunction)(getAttrType)) {
3837
- let nameLiteralType = types_1.AnyType.create();
3838
- if (strClassType && (0, types_1.isInstantiableClass)(strClassType)) {
3839
- nameLiteralType = types_1.ClassType.cloneWithLiteral(types_1.ClassType.cloneAsInstance(strClassType), memberName);
3840
- }
3841
- getAttrType = getBestOverloadForArguments(errorNode, getAttrType, [
3842
- {
3843
- argumentCategory: 0 /* Simple */,
3844
- type: types_1.AnyType.create(),
3845
- },
3846
- {
3847
- argumentCategory: 0 /* Simple */,
3848
- type: nameLiteralType,
3849
- },
3850
- ]);
3851
- }
3852
- if (getAttrType && (0, types_1.isFunction)(getAttrType)) {
3853
- return getFunctionEffectiveReturnType(getAttrType);
3854
- }
3833
+ };
3834
+ let accessMemberType;
3835
+ if (usage.method === 'get') {
3836
+ accessMemberType = (_a = getAttributeAccessMember('__getattribute__')) !== null && _a !== void 0 ? _a : getAttributeAccessMember('__getattr__');
3855
3837
  }
3856
3838
  else if (usage.method === 'set') {
3857
- const setAttrType = (_c = getTypeOfClassMember(errorNode, classType, '__setattr__', { method: 'get' },
3858
- /* diag */ undefined, 4 /* SkipObjectBaseClass */ | 64 /* SkipAttributeAccessOverride */)) === null || _c === void 0 ? void 0 : _c.type;
3859
- if (setAttrType) {
3860
- // The type doesn't matter for a set usage. We just need
3861
- // to return a defined type.
3862
- return types_1.AnyType.create();
3863
- }
3839
+ accessMemberType = getAttributeAccessMember('__setattr__');
3864
3840
  }
3865
3841
  else {
3866
3842
  (0, debug_1.assert)(usage.method === 'del');
3867
- const delAttrType = (_d = getTypeOfClassMember(errorNode, classType, '__detattr__', { method: 'get' },
3868
- /* diag */ undefined, 4 /* SkipObjectBaseClass */ | 64 /* SkipAttributeAccessOverride */)) === null || _d === void 0 ? void 0 : _d.type;
3869
- if (delAttrType) {
3870
- // The type doesn't matter for a delete usage. We just need
3871
- // to return a defined type.
3872
- return types_1.AnyType.create();
3843
+ accessMemberType = getAttributeAccessMember('__delattr__');
3844
+ }
3845
+ if (accessMemberType) {
3846
+ let nameLiteralType = types_1.AnyType.create();
3847
+ if (strClassType && (0, types_1.isInstantiableClass)(strClassType)) {
3848
+ nameLiteralType = types_1.ClassType.cloneWithLiteral(types_1.ClassType.cloneAsInstance(strClassType), memberName);
3849
+ }
3850
+ const argList = [
3851
+ {
3852
+ // Provide "self" argument.
3853
+ argumentCategory: 0 /* Simple */,
3854
+ typeResult: { type: types_1.ClassType.cloneAsInstance(classType) },
3855
+ },
3856
+ {
3857
+ // Provide "name" argument.
3858
+ argumentCategory: 0 /* Simple */,
3859
+ typeResult: { type: nameLiteralType },
3860
+ },
3861
+ ];
3862
+ if (usage.method === 'set') {
3863
+ argList.push({
3864
+ // Provide "value" argument.
3865
+ argumentCategory: 0 /* Simple */,
3866
+ typeResult: { type: (_b = usage.setType) !== null && _b !== void 0 ? _b : types_1.UnknownType.create() },
3867
+ });
3868
+ }
3869
+ if ((0, types_1.isFunction)(accessMemberType) || (0, types_1.isOverloadedFunction)(accessMemberType)) {
3870
+ const boundMethodType = bindFunctionToClassOrObject(classType, accessMemberType, classType, errorNode);
3871
+ if (boundMethodType && ((0, types_1.isFunction)(boundMethodType) || (0, types_1.isOverloadedFunction)(boundMethodType))) {
3872
+ const typeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(boundMethodType));
3873
+ const callResult = validateCallArguments(errorNode, argList, { type: boundMethodType }, typeVarContext,
3874
+ /* skipUnknownArgCheck */ true);
3875
+ return (_c = callResult.returnType) !== null && _c !== void 0 ? _c : types_1.UnknownType.create();
3876
+ }
3873
3877
  }
3874
3878
  }
3875
3879
  return undefined;
@@ -4408,9 +4412,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4408
4412
  const keywordArgs = node.items.filter((item) => item.argumentCategory === 0 /* Simple */ && !!item.name);
4409
4413
  const unpackedDictArgs = node.items.filter((item) => item.argumentCategory === 2 /* UnpackedDictionary */);
4410
4414
  let positionalIndexType;
4415
+ let isPositionalIndexTypeIncomplete = false;
4411
4416
  if (positionalArgs.length === 1 && unpackedListArgs.length === 0 && !node.trailingComma) {
4412
4417
  // Handle the common case where there is a single positional argument.
4413
- positionalIndexType = getTypeOfExpression(positionalArgs[0].valueExpression).type;
4418
+ const typeResult = getTypeOfExpression(positionalArgs[0].valueExpression);
4419
+ positionalIndexType = typeResult.type;
4420
+ if (typeResult.isIncomplete) {
4421
+ isPositionalIndexTypeIncomplete = true;
4422
+ }
4414
4423
  }
4415
4424
  else if (positionalArgs.length === 0 && unpackedListArgs.length === 0) {
4416
4425
  // Handle the case where there are no positionals provided but there are keywords.
@@ -4423,10 +4432,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4423
4432
  // Package up all of the positionals into a tuple.
4424
4433
  const tupleEntries = [];
4425
4434
  positionalArgs.forEach((arg) => {
4426
- tupleEntries.push(getTypeOfExpression(arg.valueExpression).type);
4435
+ const typeResult = getTypeOfExpression(arg.valueExpression);
4436
+ tupleEntries.push(typeResult.type);
4437
+ if (typeResult.isIncomplete) {
4438
+ isPositionalIndexTypeIncomplete = true;
4439
+ }
4427
4440
  });
4428
4441
  unpackedListArgs.forEach((arg) => {
4429
- const exprType = getTypeOfExpression(arg.valueExpression).type;
4442
+ const typeResult = getTypeOfExpression(arg.valueExpression);
4443
+ const exprType = typeResult.type;
4444
+ if (typeResult.isIncomplete) {
4445
+ isPositionalIndexTypeIncomplete = true;
4446
+ }
4430
4447
  const iterableType = getTypeOfIterator(exprType, /* isAsync */ false, arg) || types_1.UnknownType.create();
4431
4448
  tupleEntries.push(iterableType);
4432
4449
  });
@@ -4435,7 +4452,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4435
4452
  let argList = [
4436
4453
  {
4437
4454
  argumentCategory: 0 /* Simple */,
4438
- type: positionalIndexType,
4455
+ typeResult: { type: positionalIndexType, isIncomplete: isPositionalIndexTypeIncomplete },
4439
4456
  },
4440
4457
  ];
4441
4458
  if (usage.method === 'set') {
@@ -4447,7 +4464,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4447
4464
  }
4448
4465
  argList.push({
4449
4466
  argumentCategory: 0 /* Simple */,
4450
- type: setType,
4467
+ typeResult: { type: setType, isIncomplete: isPositionalIndexTypeIncomplete },
4451
4468
  });
4452
4469
  }
4453
4470
  keywordArgs.forEach((arg) => {
@@ -4469,29 +4486,31 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
4469
4486
  // Speculatively attempt the call. We may need to replace the index
4470
4487
  // type with 'int', and we don't want to emit errors before we know
4471
4488
  // which type to use.
4472
- useSpeculativeMode(node, () => {
4473
- callResult = validateCallArguments(node, argList, { type: itemMethodType });
4474
- if (callResult.argumentErrors) {
4475
- // If the object supports "__index__" magic method, convert
4476
- // the index it to an int and try again.
4477
- if ((0, types_1.isClassInstance)(positionalIndexType) && keywordArgs.length === 0 && unpackedDictArgs.length === 0) {
4478
- const altArgList = [...argList];
4479
- altArgList[0] = { ...altArgList[0] };
4480
- const indexMethod = getTypeOfObjectMember(node, positionalIndexType, '__index__');
4481
- if (indexMethod) {
4482
- const intType = getBuiltInObject(node, 'int');
4483
- if ((0, types_1.isClassInstance)(intType)) {
4484
- altArgList[0].type = intType;
4489
+ if (keywordArgs.length === 0 && unpackedDictArgs.length === 0 && positionalArgs.length === 1) {
4490
+ useSpeculativeMode(node, () => {
4491
+ callResult = validateCallArguments(node, argList, { type: itemMethodType });
4492
+ if (callResult.argumentErrors) {
4493
+ // If the object supports "__index__" magic method, convert
4494
+ // the index to an int and try again.
4495
+ if ((0, types_1.isClassInstance)(positionalIndexType)) {
4496
+ const altArgList = [...argList];
4497
+ altArgList[0] = { ...altArgList[0] };
4498
+ const indexMethod = getTypeOfObjectMember(node, positionalIndexType, '__index__');
4499
+ if (indexMethod) {
4500
+ const intType = getBuiltInObject(node, 'int');
4501
+ if ((0, types_1.isClassInstance)(intType)) {
4502
+ altArgList[0].typeResult = { type: intType };
4503
+ }
4504
+ }
4505
+ callResult = validateCallArguments(node, altArgList, { type: itemMethodType });
4506
+ // We were successful, so replace the arg list.
4507
+ if (!callResult.argumentErrors) {
4508
+ argList = altArgList;
4485
4509
  }
4486
- }
4487
- callResult = validateCallArguments(node, altArgList, { type: itemMethodType });
4488
- // We were successful, so replace the arg list.
4489
- if (!callResult.argumentErrors) {
4490
- argList = altArgList;
4491
4510
  }
4492
4511
  }
4493
- }
4494
- });
4512
+ });
4513
+ }
4495
4514
  callResult = validateCallArguments(node, argList, { type: itemMethodType });
4496
4515
  return {
4497
4516
  node,
@@ -5301,8 +5320,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
5301
5320
  // the expectedType. We'll use this to determine whether we need to do
5302
5321
  // union expansion.
5303
5322
  contextFreeArgTypes = argList.map((arg) => {
5304
- if (arg.type) {
5305
- return arg.type;
5323
+ if (arg.typeResult) {
5324
+ return arg.typeResult.type;
5306
5325
  }
5307
5326
  if (arg.valueExpression) {
5308
5327
  const valueExpressionNode = arg.valueExpression;
@@ -6204,7 +6223,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6204
6223
  const funcArg = listElementType
6205
6224
  ? {
6206
6225
  argumentCategory: 0 /* Simple */,
6207
- type: listElementType,
6226
+ typeResult: { type: listElementType, isIncomplete: argTypeResult.isIncomplete },
6208
6227
  }
6209
6228
  : undefined;
6210
6229
  if (funcArg && argTypeResult.isIncomplete) {
@@ -6369,7 +6388,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6369
6388
  requiresTypeVarMatching: (0, typeUtils_1.requiresSpecialization)(paramType),
6370
6389
  argument: {
6371
6390
  argumentCategory: 0 /* Simple */,
6372
- type: entry.valueType,
6391
+ typeResult: { type: entry.valueType },
6373
6392
  },
6374
6393
  errorNode: argList[argIndex].valueExpression || errorNode,
6375
6394
  paramName: name,
@@ -6384,7 +6403,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6384
6403
  requiresTypeVarMatching: (0, typeUtils_1.requiresSpecialization)(paramType),
6385
6404
  argument: {
6386
6405
  argumentCategory: 0 /* Simple */,
6387
- type: entry.valueType,
6406
+ typeResult: { type: entry.valueType },
6388
6407
  },
6389
6408
  errorNode: argList[argIndex].valueExpression || errorNode,
6390
6409
  paramName: name,
@@ -6574,7 +6593,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6574
6593
  requiresTypeVarMatching: (0, typeUtils_1.requiresSpecialization)(paramType),
6575
6594
  argument: {
6576
6595
  argumentCategory: 0 /* Simple */,
6577
- type: unpackedDictionaryArgType,
6596
+ typeResult: { type: unpackedDictionaryArgType },
6578
6597
  },
6579
6598
  errorNode: (_b = (_a = argList.find((arg) => arg.argumentCategory === 2 /* UnpackedDictionary */)) === null || _a === void 0 ? void 0 : _a.valueExpression) !== null && _b !== void 0 ? _b : errorNode,
6580
6599
  paramName: param.name,
@@ -6621,7 +6640,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6621
6640
  requiresTypeVarMatching: true,
6622
6641
  argument: {
6623
6642
  argumentCategory: 0 /* Simple */,
6624
- type: defaultArgType,
6643
+ typeResult: { type: defaultArgType },
6625
6644
  },
6626
6645
  errorNode: errorNode,
6627
6646
  paramName: param.name,
@@ -6675,7 +6694,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
6675
6694
  paramCategory: 1 /* VarArgList */,
6676
6695
  paramType,
6677
6696
  requiresTypeVarMatching: true,
6678
- argument: { argumentCategory: 0 /* Simple */, type: specializedTuple },
6697
+ argument: {
6698
+ argumentCategory: 0 /* Simple */,
6699
+ typeResult: { type: specializedTuple },
6700
+ },
6679
6701
  errorNode,
6680
6702
  paramName: paramDetails.params[paramDetails.argsIndex].param.name,
6681
6703
  isParamNameSynthesized: paramDetails.params[paramDetails.argsIndex].param.isNameSynthesized,
@@ -7177,7 +7199,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
7177
7199
  if (argParam.argType) {
7178
7200
  argType = argParam.argType;
7179
7201
  }
7180
- else if (argParam.expectingType && !argParam.argument.type && argParam.argument.valueExpression) {
7202
+ else if (argParam.expectingType && !argParam.argument.typeResult && argParam.argument.valueExpression) {
7181
7203
  const argTypeResult = getTypeOfExpression(argParam.argument.valueExpression, 8 /* EvaluateStringLiteralAsType */ |
7182
7204
  32 /* ParamSpecDisallowed */ |
7183
7205
  128 /* TypeVarTupleDisallowed */);
@@ -7356,7 +7378,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
7356
7378
  return { isCompatible, argType, isTypeIncomplete, condition };
7357
7379
  }
7358
7380
  function createTypeVarType(errorNode, argList) {
7359
- var _a, _b, _c;
7381
+ var _a, _b, _c, _d, _e;
7360
7382
  let typeVarName = '';
7361
7383
  let firstConstraintArg;
7362
7384
  if (argList.length === 0) {
@@ -7385,7 +7407,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
7385
7407
  addError(localize_1.Localizer.Diagnostic.typeVarBoundAndConstrained(), argList[i].valueExpression || errorNode);
7386
7408
  }
7387
7409
  else {
7388
- const argType = (_a = argList[i].type) !== null && _a !== void 0 ? _a : getTypeOfExpressionExpectingType(argList[i].valueExpression,
7410
+ const argType = (_b = (_a = argList[i].typeResult) === null || _a === void 0 ? void 0 : _a.type) !== null && _b !== void 0 ? _b : getTypeOfExpressionExpectingType(argList[i].valueExpression,
7389
7411
  /* allowFinal */ undefined,
7390
7412
  /* allowRequired */ undefined).type;
7391
7413
  if ((0, typeUtils_1.requiresSpecialization)(argType, /* ignorePseudoGeneric */ true)) {
@@ -7396,26 +7418,26 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
7396
7418
  }
7397
7419
  else if (paramName === 'covariant') {
7398
7420
  if (argList[i].valueExpression && getBooleanValue(argList[i].valueExpression)) {
7399
- if (typeVar.details.declaredVariance === 3 /* Contravariant */) {
7421
+ if (typeVar.details.declaredVariance === 4 /* Contravariant */) {
7400
7422
  addError(localize_1.Localizer.Diagnostic.typeVarVariance(), argList[i].valueExpression);
7401
7423
  }
7402
7424
  else {
7403
- typeVar.details.declaredVariance = 2 /* Covariant */;
7425
+ typeVar.details.declaredVariance = 3 /* Covariant */;
7404
7426
  }
7405
7427
  }
7406
7428
  }
7407
7429
  else if (paramName === 'contravariant') {
7408
7430
  if (argList[i].valueExpression && getBooleanValue(argList[i].valueExpression)) {
7409
- if (typeVar.details.declaredVariance === 2 /* Covariant */) {
7431
+ if (typeVar.details.declaredVariance === 3 /* Covariant */) {
7410
7432
  addError(localize_1.Localizer.Diagnostic.typeVarVariance(), argList[i].valueExpression);
7411
7433
  }
7412
7434
  else {
7413
- typeVar.details.declaredVariance = 3 /* Contravariant */;
7435
+ typeVar.details.declaredVariance = 4 /* Contravariant */;
7414
7436
  }
7415
7437
  }
7416
7438
  }
7417
7439
  else {
7418
- addError(localize_1.Localizer.Diagnostic.typeVarUnknownParam().format({ name: paramName }), ((_b = argList[i].node) === null || _b === void 0 ? void 0 : _b.name) || argList[i].valueExpression || errorNode);
7440
+ addError(localize_1.Localizer.Diagnostic.typeVarUnknownParam().format({ name: paramName }), ((_c = argList[i].node) === null || _c === void 0 ? void 0 : _c.name) || argList[i].valueExpression || errorNode);
7419
7441
  }
7420
7442
  paramNameMap.set(paramName, paramName);
7421
7443
  }
@@ -7424,7 +7446,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
7424
7446
  addError(localize_1.Localizer.Diagnostic.typeVarBoundAndConstrained(), argList[i].valueExpression || errorNode);
7425
7447
  }
7426
7448
  else {
7427
- const argType = (_c = argList[i].type) !== null && _c !== void 0 ? _c : getTypeOfExpressionExpectingType(argList[i].valueExpression,
7449
+ const argType = (_e = (_d = argList[i].typeResult) === null || _d === void 0 ? void 0 : _d.type) !== null && _e !== void 0 ? _e : getTypeOfExpressionExpectingType(argList[i].valueExpression,
7428
7450
  /* allowFinal */ undefined,
7429
7451
  /* allowRequired */ undefined).type;
7430
7452
  if ((0, typeUtils_1.requiresSpecialization)(argType, /* ignorePseudoGeneric */ true)) {
@@ -7919,7 +7941,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
7919
7941
  // incomplete because we may be evaluating types within a loop,
7920
7942
  // so the literal values may change each time.
7921
7943
  const isLiteralMathAllowed = !ParseTreeUtils.isWithinLoop(node);
7922
- let type = validateBinaryOperation(node.operator, leftType, rightType, node, expectedType, diag, isLiteralMathAllowed);
7944
+ let type = validateBinaryOperation(node.operator, { type: leftType, isIncomplete: leftTypeResult.isIncomplete }, { type: rightType, isIncomplete: rightTypeResult.isIncomplete }, node, expectedType, diag, isLiteralMathAllowed);
7923
7945
  if (!diag.isEmpty() || !type) {
7924
7946
  if (!isIncomplete) {
7925
7947
  const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
@@ -8005,14 +8027,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8005
8027
  return (0, typeUtils_1.preserveUnknown)(leftSubtypeUnexpanded, rightSubtypeUnexpanded);
8006
8028
  }
8007
8029
  const magicMethodName = operatorMap[node.operator][0];
8008
- let returnType = getTypeOfMagicMethodReturn(leftSubtypeUnexpanded, [rightSubtypeUnexpanded], magicMethodName, node, expectedType);
8030
+ let returnType = getTypeOfMagicMethodReturn(leftSubtypeUnexpanded, [{ type: rightSubtypeUnexpanded, isIncomplete: rightTypeResult.isIncomplete }], magicMethodName, node, expectedType);
8009
8031
  if (!returnType && leftSubtypeUnexpanded !== leftSubtypeExpanded) {
8010
8032
  // Try with the expanded left type.
8011
- returnType = getTypeOfMagicMethodReturn(leftSubtypeExpanded, [rightSubtypeUnexpanded], magicMethodName, node, expectedType);
8033
+ returnType = getTypeOfMagicMethodReturn(leftSubtypeExpanded, [{ type: rightSubtypeUnexpanded, isIncomplete: rightTypeResult.isIncomplete }], magicMethodName, node, expectedType);
8012
8034
  }
8013
8035
  if (!returnType && rightSubtypeUnexpanded !== rightSubtypeExpanded) {
8014
8036
  // Try with the expanded left and right type.
8015
- returnType = getTypeOfMagicMethodReturn(leftSubtypeExpanded, [rightSubtypeExpanded], magicMethodName, node, expectedType);
8037
+ returnType = getTypeOfMagicMethodReturn(leftSubtypeExpanded, [{ type: rightSubtypeExpanded, isIncomplete: rightTypeResult.isIncomplete }], magicMethodName, node, expectedType);
8016
8038
  }
8017
8039
  if (!returnType) {
8018
8040
  // If the LHS class didn't support the magic method for augmented
@@ -8025,7 +8047,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8025
8047
  !rightTypeResult.isIncomplete &&
8026
8048
  (0, typeUtils_1.getUnionSubtypeCount)(leftType) * (0, typeUtils_1.getUnionSubtypeCount)(rightType) <
8027
8049
  maxLiteralMathSubtypeCount;
8028
- returnType = validateBinaryOperation(binaryOperator, leftSubtypeUnexpanded, rightSubtypeUnexpanded, node, expectedType, diag, isLiteralMathAllowed);
8050
+ returnType = validateBinaryOperation(binaryOperator, { type: leftSubtypeUnexpanded, isIncomplete: leftTypeResult.isIncomplete }, { type: rightSubtypeUnexpanded, isIncomplete: rightTypeResult.isIncomplete }, node, expectedType, diag, isLiteralMathAllowed);
8029
8051
  }
8030
8052
  return returnType;
8031
8053
  });
@@ -8048,7 +8070,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8048
8070
  assignTypeToExpression(node.destExpression, typeResult.type, !!typeResult.isIncomplete, node.rightExpression);
8049
8071
  return typeResult;
8050
8072
  }
8051
- function validateBinaryOperation(operator, leftType, rightType, errorNode, expectedType, diag, isLiteralMathAllowed) {
8073
+ function validateBinaryOperation(operator, leftTypeResult, rightTypeResult, errorNode, expectedType, diag, isLiteralMathAllowed) {
8074
+ const leftType = leftTypeResult.type;
8075
+ const rightType = rightTypeResult.type;
8052
8076
  let type;
8053
8077
  let concreteLeftType = makeTopLevelTypeVarsConcrete(leftType);
8054
8078
  if (booleanOperatorMap[operator] !== undefined) {
@@ -8098,7 +8122,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8098
8122
  if ((0, types_1.isAnyOrUnknown)(leftSubtype) || (0, types_1.isAnyOrUnknown)(rightSubtypeUnexpanded)) {
8099
8123
  return (0, typeUtils_1.preserveUnknown)(leftSubtype, rightSubtypeExpanded);
8100
8124
  }
8101
- let returnType = getTypeOfMagicMethodReturn(rightSubtypeExpanded, [leftSubtype], '__contains__', errorNode,
8125
+ let returnType = getTypeOfMagicMethodReturn(rightSubtypeExpanded, [{ type: leftSubtype, isIncomplete: leftTypeResult.isIncomplete }], '__contains__', errorNode,
8102
8126
  /* expectedType */ undefined);
8103
8127
  if (!returnType) {
8104
8128
  // If __contains__ was not supported, fall back
@@ -8255,26 +8279,31 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8255
8279
  ]));
8256
8280
  }
8257
8281
  const magicMethodName = binaryOperatorMap[operator][0];
8258
- let resultType = getTypeOfMagicMethodReturn(convertFunctionToObject(leftSubtypeUnexpanded), [rightSubtypeUnexpanded], magicMethodName, errorNode, expectedType);
8282
+ let resultType = getTypeOfMagicMethodReturn(convertFunctionToObject(leftSubtypeUnexpanded), [{ type: rightSubtypeUnexpanded, isIncomplete: rightTypeResult.isIncomplete }], magicMethodName, errorNode, expectedType);
8259
8283
  if (!resultType && leftSubtypeUnexpanded !== leftSubtypeExpanded) {
8260
8284
  // Try the expanded left type.
8261
- resultType = getTypeOfMagicMethodReturn(convertFunctionToObject(leftSubtypeExpanded), [rightSubtypeUnexpanded], magicMethodName, errorNode, expectedType);
8285
+ resultType = getTypeOfMagicMethodReturn(convertFunctionToObject(leftSubtypeExpanded), [{ type: rightSubtypeUnexpanded, isIncomplete: rightTypeResult.isIncomplete }], magicMethodName, errorNode, expectedType);
8262
8286
  }
8263
8287
  if (!resultType && rightSubtypeUnexpanded !== rightSubtypeExpanded) {
8264
8288
  // Try the expanded left and right type.
8265
- resultType = getTypeOfMagicMethodReturn(convertFunctionToObject(leftSubtypeExpanded), [rightSubtypeExpanded], magicMethodName, errorNode, expectedType);
8289
+ resultType = getTypeOfMagicMethodReturn(convertFunctionToObject(leftSubtypeExpanded), [{ type: rightSubtypeExpanded, isIncomplete: rightTypeResult.isIncomplete }], magicMethodName, errorNode, expectedType);
8266
8290
  }
8267
8291
  if (!resultType) {
8268
8292
  // Try the alternate form (swapping right and left).
8269
8293
  const altMagicMethodName = binaryOperatorMap[operator][1];
8270
- resultType = getTypeOfMagicMethodReturn(convertFunctionToObject(rightSubtypeUnexpanded), [leftSubtypeUnexpanded], altMagicMethodName, errorNode, expectedType);
8294
+ resultType = getTypeOfMagicMethodReturn(convertFunctionToObject(rightSubtypeUnexpanded), [{ type: leftSubtypeUnexpanded, isIncomplete: leftTypeResult.isIncomplete }], altMagicMethodName, errorNode, expectedType);
8271
8295
  if (!resultType && rightSubtypeUnexpanded !== rightSubtypeExpanded) {
8272
8296
  // Try the expanded right type.
8273
- resultType = getTypeOfMagicMethodReturn(convertFunctionToObject(rightSubtypeExpanded), [leftSubtypeUnexpanded], altMagicMethodName, errorNode, expectedType);
8297
+ resultType = getTypeOfMagicMethodReturn(convertFunctionToObject(rightSubtypeExpanded), [
8298
+ {
8299
+ type: leftSubtypeUnexpanded,
8300
+ isIncomplete: leftTypeResult.isIncomplete,
8301
+ },
8302
+ ], altMagicMethodName, errorNode, expectedType);
8274
8303
  }
8275
8304
  if (!resultType && leftSubtypeUnexpanded !== leftSubtypeExpanded) {
8276
8305
  // Try the expanded right and left type.
8277
- resultType = getTypeOfMagicMethodReturn(convertFunctionToObject(rightSubtypeExpanded), [leftSubtypeExpanded], altMagicMethodName, errorNode, expectedType);
8306
+ resultType = getTypeOfMagicMethodReturn(convertFunctionToObject(rightSubtypeExpanded), [{ type: leftSubtypeExpanded, isIncomplete: leftTypeResult.isIncomplete }], altMagicMethodName, errorNode, expectedType);
8278
8307
  }
8279
8308
  }
8280
8309
  if (!resultType) {
@@ -8322,7 +8351,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
8322
8351
  const functionArgs = args.map((arg) => {
8323
8352
  return {
8324
8353
  argumentCategory: 0 /* Simple */,
8325
- type: arg,
8354
+ typeResult: arg,
8326
8355
  };
8327
8356
  });
8328
8357
  let callResult;
@@ -9118,15 +9147,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
9118
9147
  return { type, node, isIncomplete };
9119
9148
  }
9120
9149
  function getTypeOfSlice(node) {
9121
- // Evaluate the expressions to report errors and record symbol references.
9122
- if (node.startValue) {
9123
- getTypeOfExpression(node.startValue);
9124
- }
9125
- if (node.endValue) {
9126
- getTypeOfExpression(node.endValue);
9127
- }
9128
- if (node.stepValue) {
9129
- getTypeOfExpression(node.stepValue);
9150
+ // Evaluate the expressions to report errors and record symbol
9151
+ // references. We can skip this if we're executing speculatively.
9152
+ if (!speculativeTypeTracker.isSpeculative(node)) {
9153
+ if (node.startValue) {
9154
+ getTypeOfExpression(node.startValue);
9155
+ }
9156
+ if (node.endValue) {
9157
+ getTypeOfExpression(node.endValue);
9158
+ }
9159
+ if (node.stepValue) {
9160
+ getTypeOfExpression(node.stepValue);
9161
+ }
9130
9162
  }
9131
9163
  return { type: getBuiltInObject(node, 'slice'), node };
9132
9164
  }
@@ -10622,6 +10654,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10622
10654
  }
10623
10655
  }
10624
10656
  const effectiveMetaclass = computeEffectiveMetaclass(classType, node.name);
10657
+ // Clear the "partially constructed" flag.
10658
+ classType.details.flags &= ~131072 /* PartiallyEvaluated */;
10625
10659
  // Now determine the decorated type of the class.
10626
10660
  let decoratedType = classType;
10627
10661
  let foundUnknown = false;
@@ -10658,8 +10692,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10658
10692
  (0, dataClasses_1.applyDataClassDefaultBehaviors)(classType, dataClassBehaviors);
10659
10693
  (0, dataClasses_1.applyDataClassClassBehaviorOverrides)(evaluatorInterface, classType, initSubclassArgs);
10660
10694
  }
10661
- // Clear the "partially constructed" flag.
10662
- classType.details.flags &= ~131072 /* PartiallyEvaluated */;
10663
10695
  // Run any class hooks that depend on this class.
10664
10696
  runClassTypeHooks(classType);
10665
10697
  // Synthesize TypedDict methods.
@@ -10742,6 +10774,16 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10742
10774
  if (!objectType || !(0, types_1.isClassInstance)(objectType)) {
10743
10775
  return;
10744
10776
  }
10777
+ // Presumptively mark the variance inference as complete. This
10778
+ // prevents potential recursion.
10779
+ classType.details.requiresVarianceInference = false;
10780
+ // Presumptively mark the computed variance to "in progress". We'll
10781
+ // replace this below once the variance has been inferred.
10782
+ classType.details.typeParameters.forEach((param) => {
10783
+ if (param.details.declaredVariance === 0 /* Auto */) {
10784
+ param.computedVariance = 1 /* Unknown */;
10785
+ }
10786
+ });
10745
10787
  // Replace all of the type parameters with invariant TypeVars.
10746
10788
  const updatedTypeParams = classType.details.typeParameters.map((typeParam) => types_1.TypeVarType.cloneAsInvariant(typeParam));
10747
10789
  const updatedClassType = types_1.ClassType.cloneWithNewTypeParameters(classType, updatedTypeParams);
@@ -10775,15 +10817,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10775
10817
  const isDestSubtypeOfSrc = assignClassToSelf(srcType, destType);
10776
10818
  let inferredVariance;
10777
10819
  if (isDestSubtypeOfSrc) {
10778
- inferredVariance = 2 /* Covariant */;
10820
+ inferredVariance = 3 /* Covariant */;
10779
10821
  }
10780
10822
  else {
10781
10823
  const isSrcSubtypeOfDest = assignClassToSelf(destType, srcType);
10782
10824
  if (isSrcSubtypeOfDest) {
10783
- inferredVariance = 3 /* Contravariant */;
10825
+ inferredVariance = 4 /* Contravariant */;
10784
10826
  }
10785
10827
  else {
10786
- inferredVariance = 1 /* Invariant */;
10828
+ inferredVariance = 2 /* Invariant */;
10787
10829
  }
10788
10830
  }
10789
10831
  // We assume here that we don't need to clone the type var object
@@ -10791,8 +10833,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
10791
10833
  // class scope.
10792
10834
  classType.details.typeParameters[paramIndex].computedVariance = inferredVariance;
10793
10835
  });
10794
- // Note that variance inference is complete.
10795
- classType.details.requiresVarianceInference = false;
10796
10836
  }
10797
10837
  function evaluateTypeParameterList(node) {
10798
10838
  const paramTypes = [];
@@ -13061,8 +13101,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
13061
13101
  return specializedClass;
13062
13102
  }
13063
13103
  function getTypeOfArgument(arg) {
13064
- if (arg.type) {
13065
- return { type: arg.type };
13104
+ if (arg.typeResult) {
13105
+ return { type: arg.typeResult.type, isIncomplete: arg.typeResult.isIncomplete };
13066
13106
  }
13067
13107
  if (!arg.valueExpression) {
13068
13108
  // We shouldn't ever get here, but just in case.
@@ -13077,8 +13117,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
13077
13117
  // and therefore follows the normal rules of types (e.g. they
13078
13118
  // can be forward-declared in stubs, etc.).
13079
13119
  function getTypeOfArgumentExpectingType(arg) {
13080
- if (arg.type) {
13081
- return { type: arg.type };
13120
+ if (arg.typeResult) {
13121
+ return { type: arg.typeResult.type, isIncomplete: arg.typeResult.isIncomplete };
13082
13122
  }
13083
13123
  // If there was no defined type provided, there should always
13084
13124
  // be a value expression from which we can retrieve the type.
@@ -14702,7 +14742,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
14702
14742
  const destTypeArg = destArgIndex >= 0 ? destTypeArgs[destArgIndex] : types_1.UnknownType.create();
14703
14743
  const destTypeParam = destArgIndex < destTypeParams.length ? destTypeParams[destArgIndex] : undefined;
14704
14744
  const assignmentDiag = new diagnostic_1.DiagnosticAddendum();
14705
- if (!destTypeParam || types_1.TypeVarType.getVariance(destTypeParam) === 2 /* Covariant */) {
14745
+ if (!destTypeParam || types_1.TypeVarType.getVariance(destTypeParam) === 3 /* Covariant */) {
14706
14746
  if (!assignType(destTypeArg, srcTypeArg, assignmentDiag, destTypeVarContext, srcTypeVarContext, flags | 128 /* RetainLiteralsForTypeVar */, recursionCount)) {
14707
14747
  if (diag) {
14708
14748
  if (destTypeParam) {
@@ -14719,7 +14759,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
14719
14759
  return false;
14720
14760
  }
14721
14761
  }
14722
- else if (types_1.TypeVarType.getVariance(destTypeParam) === 3 /* Contravariant */) {
14762
+ else if (types_1.TypeVarType.getVariance(destTypeParam) === 4 /* Contravariant */) {
14723
14763
  if (!assignType(srcTypeArg, destTypeArg, assignmentDiag, srcTypeVarContext, destTypeVarContext, (flags ^ 2 /* ReverseTypeVarMatching */) | 128 /* RetainLiteralsForTypeVar */, recursionCount)) {
14724
14764
  if (diag) {
14725
14765
  const childDiag = diag.createAddendum();
@@ -15402,6 +15442,17 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
15402
15442
  // matches for types like `tuple[Any]` and `tuple[int]` from being considered
15403
15443
  // proper subtypes of each other.
15404
15444
  function isProperSubtype(destType, srcType, recursionCount) {
15445
+ // Shortcut the check if either type is Any or Unknown.
15446
+ if ((0, types_1.isAnyOrUnknown)(destType) || (0, types_1.isAnyOrUnknown)(srcType)) {
15447
+ return true;
15448
+ }
15449
+ // Shortcut the check if either type is a class whose hierarchy contains an unknown type.
15450
+ if ((0, types_1.isClass)(destType) && destType.details.mro.some((mro) => (0, types_1.isAnyOrUnknown)(mro))) {
15451
+ return true;
15452
+ }
15453
+ if ((0, types_1.isClass)(srcType) && srcType.details.mro.some((mro) => (0, types_1.isAnyOrUnknown)(mro))) {
15454
+ return true;
15455
+ }
15405
15456
  return (assignType(destType, srcType,
15406
15457
  /* diag */ undefined,
15407
15458
  /* destTypeVarContext */ undefined,