@zzzen/pyright-internal 1.2.0-dev.20240825 → 1.2.0-dev.20240901

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 (39) hide show
  1. package/dist/analyzer/checker.js +2 -4
  2. package/dist/analyzer/checker.js.map +1 -1
  3. package/dist/analyzer/constraintSolution.js.map +1 -1
  4. package/dist/analyzer/constraintSolver.js +1 -0
  5. package/dist/analyzer/constraintSolver.js.map +1 -1
  6. package/dist/analyzer/constructors.js +1 -1
  7. package/dist/analyzer/constructors.js.map +1 -1
  8. package/dist/analyzer/operations.js +58 -53
  9. package/dist/analyzer/operations.js.map +1 -1
  10. package/dist/analyzer/typeEvaluator.js +267 -187
  11. package/dist/analyzer/typeEvaluator.js.map +1 -1
  12. package/dist/analyzer/typePrinter.js +121 -116
  13. package/dist/analyzer/typePrinter.js.map +1 -1
  14. package/dist/analyzer/typeUtils.d.ts +2 -2
  15. package/dist/analyzer/typeUtils.js +17 -16
  16. package/dist/analyzer/typeUtils.js.map +1 -1
  17. package/dist/analyzer/types.d.ts +1 -1
  18. package/dist/analyzer/types.js.map +1 -1
  19. package/dist/common/configOptions.js +3 -3
  20. package/dist/common/configOptions.js.map +1 -1
  21. package/dist/localization/package.nls.cs.json +4 -4
  22. package/dist/localization/package.nls.de.json +4 -4
  23. package/dist/localization/package.nls.en-us.json +25 -25
  24. package/dist/localization/package.nls.es.json +4 -4
  25. package/dist/localization/package.nls.fr.json +4 -4
  26. package/dist/localization/package.nls.it.json +4 -4
  27. package/dist/localization/package.nls.ja.json +4 -4
  28. package/dist/localization/package.nls.ko.json +4 -4
  29. package/dist/localization/package.nls.pl.json +4 -4
  30. package/dist/localization/package.nls.pt-br.json +4 -4
  31. package/dist/localization/package.nls.ru.json +4 -4
  32. package/dist/localization/package.nls.tr.json +4 -4
  33. package/dist/localization/package.nls.zh-cn.json +4 -4
  34. package/dist/localization/package.nls.zh-tw.json +4 -4
  35. package/dist/tests/typeEvaluator2.test.js +2 -2
  36. package/dist/tests/typeEvaluator4.test.js +1 -1
  37. package/dist/tests/typeEvaluator6.test.js +34 -36
  38. package/dist/tests/typeEvaluator6.test.js.map +1 -1
  39. package/package.json +5 -5
@@ -891,89 +891,99 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
891
891
  function getTypeOfStringList(node, flags) {
892
892
  let typeResult;
893
893
  if ((flags & 8 /* EvalFlags.StrLiteralAsType */) !== 0) {
894
- let updatedFlags = flags | 4 /* EvalFlags.ForwardRefs */ | 128 /* EvalFlags.InstantiableType */;
895
- // In most cases, annotations within a string are not parsed by the interpreter.
896
- // There are a few exceptions (e.g. the "bound" value for a TypeVar constructor).
897
- if ((flags & 8388608 /* EvalFlags.ParsesStringLiteral */) === 0) {
898
- updatedFlags |= 524288 /* EvalFlags.NotParsed */;
899
- }
900
- if (node.d.annotation) {
901
- typeResult = getTypeOfExpression(node.d.annotation, updatedFlags);
902
- }
903
- else if (node.d.strings.length === 1) {
904
- const tokenFlags = node.d.strings[0].d.token.flags;
905
- if (tokenFlags & 32 /* StringTokenFlags.Bytes */) {
906
- addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.annotationBytesString(), node);
907
- typeResult = { type: types_1.UnknownType.create() };
908
- }
909
- else if (tokenFlags & 8 /* StringTokenFlags.Raw */) {
910
- addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.annotationRawString(), node);
911
- typeResult = { type: types_1.UnknownType.create() };
894
+ return getTypeOfStringListAsType(node, flags);
895
+ }
896
+ let isLiteralString = true;
897
+ let isIncomplete = false;
898
+ node.d.strings.forEach((expr) => {
899
+ const typeResult = getTypeOfString(expr);
900
+ if (typeResult.isIncomplete) {
901
+ isIncomplete = true;
902
+ }
903
+ let isExprLiteralString = false;
904
+ if ((0, types_1.isClassInstance)(typeResult.type)) {
905
+ if (types_1.ClassType.isBuiltIn(typeResult.type, 'str') && typeResult.type.priv.literalValue !== undefined) {
906
+ isExprLiteralString = true;
912
907
  }
913
- else if (tokenFlags & 64 /* StringTokenFlags.Format */) {
914
- addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.annotationFormatString(), node);
915
- typeResult = { type: types_1.UnknownType.create() };
908
+ else if (types_1.ClassType.isBuiltIn(typeResult === null || typeResult === void 0 ? void 0 : typeResult.type, 'LiteralString')) {
909
+ isExprLiteralString = true;
916
910
  }
917
- else {
918
- // We didn't know at parse time that this string node was going
919
- // to be evaluated as a forward-referenced type. We need
920
- // to re-invoke the parser at this stage.
921
- const expr = parseStringAsTypeAnnotation(node);
922
- if (expr) {
923
- typeResult = getTypeOfExpression(expr, updatedFlags);
924
- }
911
+ }
912
+ if (!isExprLiteralString) {
913
+ isLiteralString = false;
914
+ }
915
+ });
916
+ const isBytes = (node.d.strings[0].d.token.flags & 32 /* StringTokenFlags.Bytes */) !== 0;
917
+ // Don't create a literal type if it's an f-string.
918
+ if (node.d.strings.some((str) => str.nodeType === 30 /* ParseNodeType.FormatString */)) {
919
+ if (isLiteralString) {
920
+ const literalStringType = getTypingType(node, 'LiteralString');
921
+ if (literalStringType && (0, types_1.isInstantiableClass)(literalStringType)) {
922
+ typeResult = { type: types_1.ClassType.cloneAsInstance(literalStringType) };
925
923
  }
926
924
  }
927
925
  if (!typeResult) {
928
- addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.expectedTypeNotString(), node);
929
- typeResult = { type: types_1.UnknownType.create() };
926
+ typeResult = {
927
+ type: getBuiltInObject(node, isBytes ? 'bytes' : 'str'),
928
+ isIncomplete,
929
+ };
930
930
  }
931
931
  }
932
932
  else {
933
- // Evaluate the format string expressions in this context.
934
- let isLiteralString = true;
935
- let isIncomplete = false;
936
- node.d.strings.forEach((expr) => {
937
- const typeResult = getTypeOfString(expr);
938
- if (typeResult.isIncomplete) {
939
- isIncomplete = true;
940
- }
941
- let isExprLiteralString = false;
942
- if ((0, types_1.isClassInstance)(typeResult.type)) {
943
- if (types_1.ClassType.isBuiltIn(typeResult.type, 'str') &&
944
- typeResult.type.priv.literalValue !== undefined) {
945
- isExprLiteralString = true;
946
- }
947
- else if (types_1.ClassType.isBuiltIn(typeResult === null || typeResult === void 0 ? void 0 : typeResult.type, 'LiteralString')) {
948
- isExprLiteralString = true;
949
- }
950
- }
951
- if (!isExprLiteralString) {
952
- isLiteralString = false;
933
+ typeResult = {
934
+ type: cloneBuiltinObjectWithLiteral(node, isBytes ? 'bytes' : 'str', node.d.strings.map((s) => s.d.value).join('')),
935
+ isIncomplete,
936
+ };
937
+ }
938
+ return typeResult;
939
+ }
940
+ function getTypeOfStringListAsType(node, flags) {
941
+ const reportTypeErrors = (flags & 8 /* EvalFlags.StrLiteralAsType */) !== 0;
942
+ let updatedFlags = flags | 4 /* EvalFlags.ForwardRefs */ | 128 /* EvalFlags.InstantiableType */;
943
+ let typeResult;
944
+ // In most cases, annotations within a string are not parsed by the interpreter.
945
+ // There are a few exceptions (e.g. the "bound" value for a TypeVar constructor).
946
+ if ((flags & 8388608 /* EvalFlags.ParsesStringLiteral */) === 0) {
947
+ updatedFlags |= 524288 /* EvalFlags.NotParsed */;
948
+ }
949
+ if (node.d.annotation) {
950
+ return getTypeOfExpression(node.d.annotation, updatedFlags);
951
+ }
952
+ if (node.d.strings.length === 1) {
953
+ const tokenFlags = node.d.strings[0].d.token.flags;
954
+ if (tokenFlags & 32 /* StringTokenFlags.Bytes */) {
955
+ if (reportTypeErrors) {
956
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.annotationBytesString(), node);
953
957
  }
954
- });
955
- const isBytes = (node.d.strings[0].d.token.flags & 32 /* StringTokenFlags.Bytes */) !== 0;
956
- // Don't create a literal type if it's an f-string.
957
- if (node.d.strings.some((str) => str.nodeType === 30 /* ParseNodeType.FormatString */)) {
958
- if (isLiteralString) {
959
- const literalStringType = getTypingType(node, 'LiteralString');
960
- if (literalStringType && (0, types_1.isInstantiableClass)(literalStringType)) {
961
- typeResult = { type: types_1.ClassType.cloneAsInstance(literalStringType) };
962
- }
958
+ return { type: types_1.UnknownType.create() };
959
+ }
960
+ if (tokenFlags & 8 /* StringTokenFlags.Raw */) {
961
+ if (reportTypeErrors) {
962
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.annotationRawString(), node);
963
963
  }
964
- if (!typeResult) {
965
- typeResult = {
966
- type: getBuiltInObject(node, isBytes ? 'bytes' : 'str'),
967
- isIncomplete,
968
- };
964
+ return { type: types_1.UnknownType.create() };
965
+ }
966
+ if (tokenFlags & 64 /* StringTokenFlags.Format */) {
967
+ if (reportTypeErrors) {
968
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.annotationFormatString(), node);
969
969
  }
970
+ return { type: types_1.UnknownType.create() };
970
971
  }
971
- else {
972
- typeResult = {
973
- type: cloneBuiltinObjectWithLiteral(node, isBytes ? 'bytes' : 'str', node.d.strings.map((s) => s.d.value).join('')),
974
- isIncomplete,
975
- };
972
+ // We didn't know at parse time that this string node was going
973
+ // to be evaluated as a forward-referenced type. We need
974
+ // to re-invoke the parser at this stage.
975
+ const expr = parseStringAsTypeAnnotation(node, reportTypeErrors);
976
+ if (expr) {
977
+ typeResult = useSpeculativeMode(reportTypeErrors ? undefined : node, () => {
978
+ return getTypeOfExpression(expr, updatedFlags);
979
+ });
980
+ }
981
+ }
982
+ if (!typeResult) {
983
+ if (reportTypeErrors) {
984
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.expectedTypeNotString(), node);
976
985
  }
986
+ typeResult = { type: types_1.UnknownType.create() };
977
987
  }
978
988
  return typeResult;
979
989
  }
@@ -1045,7 +1055,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
1045
1055
  var _a;
1046
1056
  if ((0, types_1.isClass)(subtype)) {
1047
1057
  if (subtype.priv.literalValue !== undefined) {
1048
- return types_1.ClassType.cloneWithLiteral(subtype, /* value */ undefined);
1058
+ subtype = types_1.ClassType.cloneWithLiteral(subtype, /* value */ undefined);
1049
1059
  }
1050
1060
  if (types_1.ClassType.isBuiltIn(subtype, 'LiteralString')) {
1051
1061
  // Handle "LiteralString" specially.
@@ -3123,7 +3133,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
3123
3133
  // evaluated in a value expression context, convert it from its special
3124
3134
  // meaning to its runtime value.
3125
3135
  function convertSpecialFormToRuntimeValue(type, flags) {
3126
- var _a, _b, _c;
3136
+ var _a, _b;
3127
3137
  const exemptFlags = 256 /* EvalFlags.TypeExpression */ | 128 /* EvalFlags.InstantiableType */ | 33554432 /* EvalFlags.NoConvertSpecialForm */;
3128
3138
  if ((flags & exemptFlags) !== 0) {
3129
3139
  return type;
@@ -3135,7 +3145,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
3135
3145
  return type;
3136
3146
  }
3137
3147
  }
3138
- return (_c = (_b = type.props) === null || _b === void 0 ? void 0 : _b.specialForm) !== null && _c !== void 0 ? _c : type;
3148
+ if (!((_b = type.props) === null || _b === void 0 ? void 0 : _b.specialForm)) {
3149
+ return type;
3150
+ }
3151
+ return type.props.specialForm;
3139
3152
  }
3140
3153
  // Handles the case where a variable or parameter is defined in an outer
3141
3154
  // scope and captured by an inner scope (a function, lambda, or comprehension).
@@ -3352,7 +3365,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
3352
3365
  // type arguments. If so, it fills in these type arguments with Unknown
3353
3366
  // and optionally reports an error.
3354
3367
  function reportMissingTypeArgs(node, type, flags) {
3355
- var _a, _b, _c;
3368
+ var _a, _b;
3356
3369
  if ((flags & 2 /* EvalFlags.NoSpecialize */) !== 0) {
3357
3370
  return type;
3358
3371
  }
@@ -3372,49 +3385,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
3372
3385
  }
3373
3386
  }
3374
3387
  // Is this a generic type alias that needs to be specialized?
3375
- const aliasInfo = (_c = type.props) === null || _c === void 0 ? void 0 : _c.typeAliasInfo;
3376
- if ((flags & 128 /* EvalFlags.InstantiableType */) !== 0 &&
3377
- aliasInfo &&
3378
- aliasInfo.typeParams &&
3379
- aliasInfo.typeParams.length > 0 &&
3380
- !aliasInfo.typeArgs) {
3381
- let reportMissingTypeArgs = false;
3382
- const defaultTypeArgs = [];
3383
- const constraints = new constraintTracker_1.ConstraintTracker();
3384
- aliasInfo.typeParams.forEach((param) => {
3385
- if (!param.shared.isDefaultExplicit) {
3386
- reportMissingTypeArgs = true;
3387
- }
3388
- let defaultType;
3389
- if (param.shared.isDefaultExplicit || (0, types_1.isParamSpec)(param)) {
3390
- defaultType = solveAndApplyConstraints(param, constraints, {
3391
- replaceUnsolved: {
3392
- scopeIds: [aliasInfo.typeVarScopeId],
3393
- tupleClassType: getTupleClassType(),
3394
- },
3395
- });
3396
- }
3397
- else if ((0, types_1.isTypeVarTuple)(param) && tupleClass && (0, types_1.isInstantiableClass)(tupleClass)) {
3398
- defaultType = makeTupleObject([{ type: types_1.UnknownType.create(), isUnbounded: true }],
3399
- /* isUnpacked */ true);
3400
- }
3401
- else {
3402
- defaultType = types_1.UnknownType.create();
3403
- }
3404
- defaultTypeArgs.push(defaultType);
3405
- constraints.setBounds(param, defaultType);
3406
- });
3407
- if (reportMissingTypeArgs) {
3408
- addDiagnostic(diagnosticRules_1.DiagnosticRule.reportMissingTypeArgument, localize_1.LocMessage.typeArgsMissingForAlias().format({
3409
- name: aliasInfo.name,
3410
- }), node);
3411
- }
3412
- type = types_1.TypeBase.cloneForTypeAlias(solveAndApplyConstraints(type, constraints, {
3413
- replaceUnsolved: {
3414
- scopeIds: [aliasInfo.typeVarScopeId],
3415
- tupleClassType: getTupleClassType(),
3416
- },
3417
- }), { ...aliasInfo, typeArgs: defaultTypeArgs });
3388
+ if ((flags & 128 /* EvalFlags.InstantiableType */) !== 0) {
3389
+ type = specializeTypeAliasWithDefaults(type, node);
3418
3390
  }
3419
3391
  return type;
3420
3392
  }
@@ -4701,6 +4673,59 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
4701
4673
  }
4702
4674
  return true;
4703
4675
  }
4676
+ // If the type is a generic type alias that is not specialized, provides
4677
+ // default type arguments for the type alias. It optionally logs diagnostics
4678
+ // for missing type arguments.
4679
+ function specializeTypeAliasWithDefaults(type, errorNode) {
4680
+ var _a;
4681
+ // Is this a type alias?
4682
+ const aliasInfo = (_a = type.props) === null || _a === void 0 ? void 0 : _a.typeAliasInfo;
4683
+ if (!aliasInfo) {
4684
+ return type;
4685
+ }
4686
+ // Is this a generic type alias that needs specializing?
4687
+ if (!aliasInfo.typeParams || aliasInfo.typeParams.length === 0 || aliasInfo.typeArgs) {
4688
+ return type;
4689
+ }
4690
+ let reportDiag = false;
4691
+ const defaultTypeArgs = [];
4692
+ const constraints = new constraintTracker_1.ConstraintTracker();
4693
+ aliasInfo.typeParams.forEach((param) => {
4694
+ if (!param.shared.isDefaultExplicit) {
4695
+ reportDiag = true;
4696
+ }
4697
+ let defaultType;
4698
+ if (param.shared.isDefaultExplicit || (0, types_1.isParamSpec)(param)) {
4699
+ defaultType = solveAndApplyConstraints(param, constraints, {
4700
+ replaceUnsolved: {
4701
+ scopeIds: [aliasInfo.typeVarScopeId],
4702
+ tupleClassType: getTupleClassType(),
4703
+ },
4704
+ });
4705
+ }
4706
+ else if ((0, types_1.isTypeVarTuple)(param) && tupleClass && (0, types_1.isInstantiableClass)(tupleClass)) {
4707
+ defaultType = makeTupleObject([{ type: types_1.UnknownType.create(), isUnbounded: true }],
4708
+ /* isUnpacked */ true);
4709
+ }
4710
+ else {
4711
+ defaultType = types_1.UnknownType.create();
4712
+ }
4713
+ defaultTypeArgs.push(defaultType);
4714
+ constraints.setBounds(param, defaultType);
4715
+ });
4716
+ if (reportDiag && errorNode) {
4717
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportMissingTypeArgument, localize_1.LocMessage.typeArgsMissingForAlias().format({
4718
+ name: aliasInfo.name,
4719
+ }), errorNode);
4720
+ }
4721
+ type = types_1.TypeBase.cloneForTypeAlias(solveAndApplyConstraints(type, constraints, {
4722
+ replaceUnsolved: {
4723
+ scopeIds: [aliasInfo.typeVarScopeId],
4724
+ tupleClassType: getTupleClassType(),
4725
+ },
4726
+ }), { ...aliasInfo, typeArgs: defaultTypeArgs });
4727
+ return type;
4728
+ }
4704
4729
  // Handles index expressions that are providing type arguments for a
4705
4730
  // generic type alias.
4706
4731
  function createSpecializedTypeAlias(node, baseType, flags) {
@@ -9114,7 +9139,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
9114
9139
  }
9115
9140
  // Specifically disallow Annotated.
9116
9141
  if (((_d = baseClass.props) === null || _d === void 0 ? void 0 : _d.specialForm) &&
9117
- (0, types_1.isInstantiableClass)(baseClass.props.specialForm) &&
9142
+ (0, types_1.isClassInstance)(baseClass.props.specialForm) &&
9118
9143
  types_1.ClassType.isBuiltIn(baseClass.props.specialForm, 'Annotated')) {
9119
9144
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.newTypeNotAClass(), argList[1].node || errorNode);
9120
9145
  return undefined;
@@ -10336,7 +10361,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10336
10361
  function createCallableType(classType, typeArgs, errorNode) {
10337
10362
  const functionType = types_1.FunctionType.createInstantiable(0 /* FunctionTypeFlags.None */);
10338
10363
  let paramSpec;
10339
- types_1.TypeBase.setSpecialForm(functionType, classType);
10364
+ types_1.TypeBase.setSpecialForm(functionType, types_1.ClassType.cloneAsInstance(classType));
10340
10365
  functionType.shared.declaredReturnType = types_1.UnknownType.create();
10341
10366
  functionType.shared.typeVarScopeId = ParseTreeUtils.getScopeIdForNode(errorNode);
10342
10367
  if (typeArgs && typeArgs.length > 0) {
@@ -10608,6 +10633,27 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10608
10633
  }
10609
10634
  return type;
10610
10635
  }
10636
+ function createTypeFormType(classType, errorNode, typeArgs) {
10637
+ // If no type arguments are provided, the resulting type
10638
+ // depends on whether we're evaluating a type annotation or
10639
+ // we're in some other context.
10640
+ if (!typeArgs) {
10641
+ return classType;
10642
+ }
10643
+ if (typeArgs.length > 1) {
10644
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgsTooMany().format({
10645
+ name: classType.priv.aliasName || classType.shared.name,
10646
+ expected: 1,
10647
+ received: typeArgs.length,
10648
+ }), typeArgs[1].node);
10649
+ return types_1.UnknownType.create();
10650
+ }
10651
+ const convertedTypeArgs = typeArgs.map((typeArg) => {
10652
+ return (0, typeUtils_1.convertToInstance)(validateTypeArg(typeArg) ? typeArg.type : types_1.UnknownType.create());
10653
+ });
10654
+ const resultType = types_1.ClassType.specialize(classType, convertedTypeArgs);
10655
+ return resultType;
10656
+ }
10611
10657
  // Creates a "TypeGuard" and "TypeIs" type.
10612
10658
  function createTypeGuardType(classType, errorNode, typeArgs, flags) {
10613
10659
  // If no type arguments are provided, the resulting type
@@ -10626,7 +10672,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10626
10672
  const convertedTypeArgs = typeArgs.map((typeArg) => {
10627
10673
  return (0, typeUtils_1.convertToInstance)(validateTypeArg(typeArg) ? typeArg.type : types_1.UnknownType.create());
10628
10674
  });
10629
- return types_1.ClassType.specialize(classType, convertedTypeArgs);
10675
+ const resultType = types_1.ClassType.specialize(classType, convertedTypeArgs);
10676
+ return resultType;
10630
10677
  }
10631
10678
  function createSelfType(classType, errorNode, typeArgs, flags) {
10632
10679
  var _a;
@@ -10701,11 +10748,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10701
10748
  return { type: classType };
10702
10749
  }
10703
10750
  if (!typeArgs || typeArgs.length !== 1) {
10704
- addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, classType.shared.name === 'ReadOnly'
10705
- ? localize_1.LocMessage.readOnlyArgCount()
10706
- : classType.shared.name === 'Required'
10707
- ? localize_1.LocMessage.requiredArgCount()
10708
- : localize_1.LocMessage.notRequiredArgCount(), errorNode);
10751
+ if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0) {
10752
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, classType.shared.name === 'ReadOnly'
10753
+ ? localize_1.LocMessage.readOnlyArgCount()
10754
+ : classType.shared.name === 'Required'
10755
+ ? localize_1.LocMessage.requiredArgCount()
10756
+ : localize_1.LocMessage.notRequiredArgCount(), errorNode);
10757
+ }
10709
10758
  return { type: classType };
10710
10759
  }
10711
10760
  const typeArgType = typeArgs[0].type;
@@ -10743,25 +10792,23 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10743
10792
  isNotRequired = classType.shared.name === 'NotRequired';
10744
10793
  }
10745
10794
  if (!isUsageLegal) {
10746
- addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, classType.shared.name === 'ReadOnly'
10747
- ? localize_1.LocMessage.readOnlyNotInTypedDict()
10748
- : classType.shared.name === 'Required'
10749
- ? localize_1.LocMessage.requiredNotInTypedDict()
10750
- : localize_1.LocMessage.notRequiredNotInTypedDict(), errorNode);
10751
- return { type: types_1.ClassType.specialize(classType, [(0, typeUtils_1.convertToInstance)(typeArgType)], !!typeArgs) };
10795
+ if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0) {
10796
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, classType.shared.name === 'ReadOnly'
10797
+ ? localize_1.LocMessage.readOnlyNotInTypedDict()
10798
+ : classType.shared.name === 'Required'
10799
+ ? localize_1.LocMessage.requiredNotInTypedDict()
10800
+ : localize_1.LocMessage.notRequiredNotInTypedDict(), errorNode);
10801
+ }
10802
+ return { type: classType };
10752
10803
  }
10753
10804
  return { type: typeArgType, isReadOnly, isRequired, isNotRequired };
10754
10805
  }
10755
10806
  function createUnpackType(classType, errorNode, typeArgs, flags) {
10756
- // If no type arguments are provided, the resulting type
10757
- // depends on whether we're evaluating a type annotation or
10758
- // we're in some other context.
10759
- if (!typeArgs && (flags & 256 /* EvalFlags.TypeExpression */) === 0) {
10760
- return classType;
10761
- }
10762
10807
  if (!typeArgs || typeArgs.length !== 1) {
10763
- addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.unpackArgCount(), errorNode);
10764
- return types_1.UnknownType.create();
10808
+ if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0) {
10809
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.unpackArgCount(), errorNode);
10810
+ }
10811
+ return classType;
10765
10812
  }
10766
10813
  let typeArgType = typeArgs[0].type;
10767
10814
  if ((0, types_1.isUnion)(typeArgType) && typeArgType.priv.subtypes.length === 1) {
@@ -10774,6 +10821,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10774
10821
  if ((0, types_1.isTypeVarTuple)(typeArgType) && !typeArgType.priv.isUnpacked) {
10775
10822
  return types_1.TypeVarType.cloneForUnpacked(typeArgType);
10776
10823
  }
10824
+ if ((flags & 256 /* EvalFlags.TypeExpression */) === 0) {
10825
+ return classType;
10826
+ }
10777
10827
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.unpackExpectedTypeVarTuple(), errorNode);
10778
10828
  return types_1.UnknownType.create();
10779
10829
  }
@@ -10781,19 +10831,27 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10781
10831
  if ((0, types_1.isInstantiableClass)(typeArgType) && types_1.ClassType.isTypedDictClass(typeArgType)) {
10782
10832
  return types_1.ClassType.cloneForUnpacked(typeArgType);
10783
10833
  }
10834
+ if ((flags & 256 /* EvalFlags.TypeExpression */) === 0) {
10835
+ return classType;
10836
+ }
10784
10837
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.unpackExpectedTypedDict(), errorNode);
10785
10838
  return types_1.UnknownType.create();
10786
10839
  }
10840
+ if ((flags & 256 /* EvalFlags.TypeExpression */) === 0) {
10841
+ return classType;
10842
+ }
10787
10843
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.unpackNotAllowed(), errorNode);
10788
10844
  return types_1.UnknownType.create();
10789
10845
  }
10790
10846
  // Creates a "Final" type.
10791
10847
  function createFinalType(classType, errorNode, typeArgs, flags) {
10792
10848
  if (flags & 16 /* EvalFlags.NoFinal */) {
10793
- addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.finalContext(), errorNode);
10849
+ if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0) {
10850
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.finalContext(), errorNode);
10851
+ }
10794
10852
  return classType;
10795
10853
  }
10796
- if (!typeArgs || typeArgs.length === 0) {
10854
+ if ((flags & 256 /* EvalFlags.TypeExpression */) === 0 || !typeArgs || typeArgs.length === 0) {
10797
10855
  return classType;
10798
10856
  }
10799
10857
  if (typeArgs.length > 1) {
@@ -10803,8 +10861,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10803
10861
  }
10804
10862
  function createConcatenateType(classType, errorNode, typeArgs, flags) {
10805
10863
  if ((flags & 134217728 /* EvalFlags.AllowConcatenate */) === 0) {
10806
- addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.concatenateContext(), errorNode);
10807
- return types_1.AnyType.create();
10864
+ if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0) {
10865
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.concatenateContext(), errorNode);
10866
+ }
10867
+ return classType;
10808
10868
  }
10809
10869
  if (!typeArgs || typeArgs.length === 0) {
10810
10870
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.concatenateTypeArgsMissing(), errorNode);
@@ -10832,11 +10892,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10832
10892
  return createSpecialType(classType, typeArgs, /* paramLimit */ undefined, /* allowParamSpec */ true);
10833
10893
  }
10834
10894
  function createAnnotatedType(classType, errorNode, typeArgs, flags) {
10895
+ let type;
10835
10896
  const typeExprFlags = 256 /* EvalFlags.TypeExpression */ | 33554432 /* EvalFlags.NoConvertSpecialForm */;
10836
10897
  if ((flags & typeExprFlags) === 0) {
10837
- return { type: classType };
10898
+ type = types_1.ClassType.cloneAsInstance(classType);
10899
+ return { type };
10838
10900
  }
10839
- let type;
10840
10901
  if (typeArgs && typeArgs.length > 0) {
10841
10902
  type = typeArgs[0].type;
10842
10903
  if (typeArgs.length < 2) {
@@ -10853,14 +10914,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10853
10914
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgListNotAllowed(), typeArgs[0].node);
10854
10915
  }
10855
10916
  return {
10856
- type: types_1.TypeBase.cloneAsSpecialForm(type, classType),
10917
+ type: types_1.TypeBase.cloneAsSpecialForm(type, types_1.ClassType.cloneAsInstance(classType)),
10857
10918
  isReadOnly: typeArgs[0].isReadOnly,
10858
10919
  isRequired: typeArgs[0].isRequired,
10859
10920
  isNotRequired: typeArgs[0].isNotRequired,
10860
10921
  };
10861
10922
  }
10862
- // Enforces metadata consistency as specified in PEP 746 and associates
10863
- // refinement type predicates with the base type.
10923
+ // Enforces metadata consistency as specified in PEP 746.
10864
10924
  function validateAnnotatedMetadata(errorNode, baseType, metaArgs) {
10865
10925
  for (const metaArg of metaArgs) {
10866
10926
  validateTypeMetadata(errorNode, baseType, metaArg);
@@ -11309,6 +11369,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
11309
11369
  typeParamVariance: 2 /* Variance.Invariant */,
11310
11370
  },
11311
11371
  ],
11372
+ ['TypeForm', { alias: '', module: 'builtins', isSpecialForm: true, typeParamVariance: 3 /* Variance.Covariant */ }],
11312
11373
  ]);
11313
11374
  const aliasMapEntry = specialTypes.get(assignedName);
11314
11375
  if (aliasMapEntry) {
@@ -14261,17 +14322,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14261
14322
  case 'Callable': {
14262
14323
  return { type: createCallableType(classType, typeArgs, errorNode) };
14263
14324
  }
14264
- case 'Never': {
14265
- if (typeArgs && typeArgs.length > 0) {
14266
- addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgsExpectingNone().format({ name: 'Never' }), typeArgs[0].node);
14267
- }
14268
- return { type: types_1.NeverType.createNever() };
14269
- }
14325
+ case 'Never':
14270
14326
  case 'NoReturn': {
14271
14327
  if (typeArgs && typeArgs.length > 0) {
14272
- addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgsExpectingNone().format({ name: 'NoReturn' }), typeArgs[0].node);
14328
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgsExpectingNone().format({ name: aliasedName }), typeArgs[0].node);
14273
14329
  }
14274
- return { type: types_1.NeverType.createNoReturn() };
14330
+ const resultType = aliasedName === 'Never' ? types_1.NeverType.createNever() : types_1.NeverType.createNoReturn();
14331
+ return { type: resultType };
14275
14332
  }
14276
14333
  case 'Optional': {
14277
14334
  return { type: createOptionalType(classType, errorNode, typeArgs, flags) };
@@ -14358,6 +14415,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14358
14415
  case 'LiteralString': {
14359
14416
  return { type: createSpecialType(classType, typeArgs, 0) };
14360
14417
  }
14418
+ case 'TypeForm': {
14419
+ return { type: createTypeFormType(classType, errorNode, typeArgs) };
14420
+ }
14361
14421
  }
14362
14422
  }
14363
14423
  const fileInfo = AnalyzerNodeInfo.getFileInfo(errorNode);
@@ -14388,12 +14448,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14388
14448
  // Handle "tuple" specially, since it needs to act like "Tuple"
14389
14449
  // in Python 3.9 and newer.
14390
14450
  if ((0, typeUtils_1.isTupleClass)(classType)) {
14391
- return {
14392
- type: createSpecialType(classType, typeArgs,
14393
- /* paramLimit */ undefined,
14394
- /* allowParamSpec */ undefined,
14395
- /* isSpecialForm */ false),
14396
- };
14451
+ const specializedClass = createSpecialType(classType, typeArgs,
14452
+ /* paramLimit */ undefined,
14453
+ /* allowParamSpec */ undefined,
14454
+ /* isSpecialForm */ false);
14455
+ return { type: specializedClass };
14397
14456
  }
14398
14457
  }
14399
14458
  let typeArgCount = typeArgs ? typeArgs.length : 0;
@@ -16534,18 +16593,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
16534
16593
  inferVarianceForClass(destType);
16535
16594
  // If we're enforcing invariance, literal types must match.
16536
16595
  if ((flags & 1 /* AssignTypeFlags.Invariant */) !== 0) {
16537
- const srcIsLiteral = srcType.priv.literalValue !== undefined;
16538
- const destIsLiteral = destType.priv.literalValue !== undefined;
16596
+ const srcIsLiteral = (0, typeUtils_1.isLiteralLikeType)(srcType);
16597
+ const destIsLiteral = (0, typeUtils_1.isLiteralLikeType)(destType);
16539
16598
  if (srcIsLiteral !== destIsLiteral) {
16540
16599
  return false;
16541
16600
  }
16542
16601
  }
16543
- else {
16544
- // If the dest is an 'object', it's assignable.
16545
- if (types_1.ClassType.isBuiltIn(destType, 'object')) {
16546
- return true;
16547
- }
16548
- }
16549
16602
  for (let ancestorIndex = inheritanceChain.length - 1; ancestorIndex >= 0; ancestorIndex--) {
16550
16603
  const ancestorType = inheritanceChain[ancestorIndex];
16551
16604
  // If we've hit an "unknown", all bets are off, and we need to assume
@@ -17067,7 +17120,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
17067
17120
  assignType(destType, concreteSrcType.priv.literalValue.itemType)) {
17068
17121
  return true;
17069
17122
  }
17070
- if (destType.priv.literalValue !== undefined) {
17123
+ if (destType.priv.literalValue !== undefined &&
17124
+ types_1.ClassType.isSameGenericClass(destType, concreteSrcType)) {
17071
17125
  const srcLiteral = concreteSrcType.priv.literalValue;
17072
17126
  if (srcLiteral === undefined || !types_1.ClassType.isLiteralValueSame(concreteSrcType, destType)) {
17073
17127
  diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.LocAddendum.literalAssignmentMismatch().format({
@@ -17918,11 +17972,21 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
17918
17972
  }
17919
17973
  }
17920
17974
  }
17921
- if (destParam.defaultType && !srcParam.defaultType && paramIndex !== srcParamDetails.argsIndex) {
17922
- diag === null || diag === void 0 ? void 0 : diag.createAddendum().addMessage(localize_1.LocAddendum.functionParamDefaultMissing().format({
17923
- name: srcParamName,
17924
- }));
17925
- canAssign = false;
17975
+ if (destParam.defaultType) {
17976
+ if (!srcParam.defaultType && paramIndex !== srcParamDetails.argsIndex) {
17977
+ diag === null || diag === void 0 ? void 0 : diag.createAddendum().addMessage(localize_1.LocAddendum.functionParamDefaultMissing().format({
17978
+ name: srcParamName,
17979
+ }));
17980
+ canAssign = false;
17981
+ }
17982
+ // If we're performing a partial overload match and both the source
17983
+ // and dest parameters provide defaults, assume that there could
17984
+ // be a match.
17985
+ if ((flags & 32 /* AssignTypeFlags.PartialOverloadOverlap */) !== 0) {
17986
+ if (srcParam.defaultType) {
17987
+ continue;
17988
+ }
17989
+ }
17926
17990
  }
17927
17991
  // Handle the special case of an overloaded __init__ method whose self
17928
17992
  // parameter is annotated.
@@ -17993,7 +18057,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
17993
18057
  // fixed.
17994
18058
  if (defaultArgType &&
17995
18059
  !assignType(paramInfo.type, defaultArgType, diag === null || diag === void 0 ? void 0 : diag.createAddendum(), constraints, flags, recursionCount)) {
17996
- canAssign = false;
18060
+ if ((flags & 32 /* AssignTypeFlags.PartialOverloadOverlap */) === 0) {
18061
+ canAssign = false;
18062
+ }
17997
18063
  }
17998
18064
  continue;
17999
18065
  }
@@ -18051,6 +18117,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
18051
18117
  if (destParamDetails.argsIndex !== undefined && destParamDetails.argsIndex < destPositionalCount) {
18052
18118
  adjDestPositionalCount--;
18053
18119
  }
18120
+ // If we're doing a partial overload overlap check, ignore dest positional
18121
+ // params with default values.
18122
+ if ((flags & 32 /* AssignTypeFlags.PartialOverloadOverlap */) !== 0) {
18123
+ while (adjDestPositionalCount > 0 &&
18124
+ destParamDetails.params[adjDestPositionalCount - 1].defaultType) {
18125
+ adjDestPositionalCount--;
18126
+ }
18127
+ }
18054
18128
  if (srcPositionalCount < adjDestPositionalCount) {
18055
18129
  diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.LocAddendum.functionTooManyParams().format({
18056
18130
  expected: srcPositionalCount,
@@ -18141,7 +18215,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
18141
18215
  const defaultArgType = (_a = srcParamInfo.defaultType) !== null && _a !== void 0 ? _a : srcParamInfo.defaultType;
18142
18216
  if (defaultArgType &&
18143
18217
  !assignType(srcParamInfo.type, defaultArgType, diag === null || diag === void 0 ? void 0 : diag.createAddendum(), constraints, flags, recursionCount)) {
18144
- canAssign = false;
18218
+ if ((flags & 32 /* AssignTypeFlags.PartialOverloadOverlap */) === 0) {
18219
+ canAssign = false;
18220
+ }
18145
18221
  }
18146
18222
  }
18147
18223
  }
@@ -18998,7 +19074,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
18998
19074
  }
18999
19075
  const baseObj = (0, types_1.isClassInstance)(baseType)
19000
19076
  ? baseType
19001
- : types_1.ClassType.cloneAsInstance((0, typeUtils_1.specializeClassType)(baseType));
19077
+ : types_1.ClassType.cloneAsInstance((0, typeUtils_1.specializeWithDefaultTypeArgs)(baseType));
19002
19078
  let stripFirstParam = false;
19003
19079
  if ((0, types_1.isClassInstance)(baseType)) {
19004
19080
  stripFirstParam = true;
@@ -19248,7 +19324,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
19248
19324
  // call produces an expression tree that is not attached to the main parse
19249
19325
  // expression tree because we don't want to mutate the latter; the
19250
19326
  // expression tree created by this function is therefore used only temporarily.
19251
- function parseStringAsTypeAnnotation(node) {
19327
+ function parseStringAsTypeAnnotation(node, reportErrors) {
19252
19328
  const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
19253
19329
  const parser = new parser_1.Parser();
19254
19330
  const textValue = node.d.strings[0].d.value;
@@ -19269,14 +19345,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
19269
19345
  const parseResults = parser.parseTextExpression(dummyFileContents, valueOffset, textValue.length, parseOptions, 0 /* ParseTextMode.Expression */,
19270
19346
  /* initialParenDepth */ undefined, fileInfo.typingSymbolAliases);
19271
19347
  if (parseResults.parseTree) {
19272
- const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
19273
- parseResults.diagnostics.forEach((diag) => {
19274
- fileInfo.diagnosticSink.addDiagnosticWithTextRange('error', diag.message, node);
19275
- });
19348
+ if (reportErrors) {
19349
+ const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
19350
+ parseResults.diagnostics.forEach((diag) => {
19351
+ fileInfo.diagnosticSink.addDiagnosticWithTextRange('error', diag.message, node);
19352
+ });
19353
+ }
19276
19354
  parseResults.parseTree.parent = node;
19277
- // Add the new subtree to the parse tree so it can participate in
19278
- // language server operations like find and replace.
19279
- node.d.annotation = parseResults.parseTree;
19355
+ // Optionally add the new subtree to the parse tree so it can
19356
+ // participate in language server operations like find and replace.
19357
+ if (reportErrors) {
19358
+ node.d.annotation = parseResults.parseTree;
19359
+ }
19280
19360
  return parseResults.parseTree;
19281
19361
  }
19282
19362
  return undefined;