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

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 (97) hide show
  1. package/dist/analyzer/binder.js +14 -8
  2. package/dist/analyzer/binder.js.map +1 -1
  3. package/dist/analyzer/checker.js +25 -45
  4. package/dist/analyzer/checker.js.map +1 -1
  5. package/dist/analyzer/codeFlowEngine.js +7 -2
  6. package/dist/analyzer/codeFlowEngine.js.map +1 -1
  7. package/dist/analyzer/constraintSolution.js.map +1 -1
  8. package/dist/analyzer/constraintSolver.js +3 -2
  9. package/dist/analyzer/constraintSolver.js.map +1 -1
  10. package/dist/analyzer/constructors.js +8 -2
  11. package/dist/analyzer/constructors.js.map +1 -1
  12. package/dist/analyzer/dataClasses.js +6 -1
  13. package/dist/analyzer/dataClasses.js.map +1 -1
  14. package/dist/analyzer/namedTuples.js +4 -1
  15. package/dist/analyzer/namedTuples.js.map +1 -1
  16. package/dist/analyzer/operations.js +63 -53
  17. package/dist/analyzer/operations.js.map +1 -1
  18. package/dist/analyzer/parseTreeWalker.js +1 -1
  19. package/dist/analyzer/parseTreeWalker.js.map +1 -1
  20. package/dist/analyzer/protocols.js +5 -8
  21. package/dist/analyzer/protocols.js.map +1 -1
  22. package/dist/analyzer/sourceMapper.js +2 -1
  23. package/dist/analyzer/sourceMapper.js.map +1 -1
  24. package/dist/analyzer/typeEvaluator.js +783 -322
  25. package/dist/analyzer/typeEvaluator.js.map +1 -1
  26. package/dist/analyzer/typeEvaluatorTypes.d.ts +11 -5
  27. package/dist/analyzer/typeEvaluatorTypes.js +4 -1
  28. package/dist/analyzer/typeEvaluatorTypes.js.map +1 -1
  29. package/dist/analyzer/typeGuards.js +11 -8
  30. package/dist/analyzer/typeGuards.js.map +1 -1
  31. package/dist/analyzer/typePrinter.d.ts +2 -1
  32. package/dist/analyzer/typePrinter.js +144 -119
  33. package/dist/analyzer/typePrinter.js.map +1 -1
  34. package/dist/analyzer/typeUtils.d.ts +6 -2
  35. package/dist/analyzer/typeUtils.js +71 -17
  36. package/dist/analyzer/typeUtils.js.map +1 -1
  37. package/dist/analyzer/types.d.ts +9 -5
  38. package/dist/analyzer/types.js +68 -26
  39. package/dist/analyzer/types.js.map +1 -1
  40. package/dist/commands/dumpFileDebugInfoCommand.js +0 -1
  41. package/dist/commands/dumpFileDebugInfoCommand.js.map +1 -1
  42. package/dist/common/configOptions.js +3 -3
  43. package/dist/common/configOptions.js.map +1 -1
  44. package/dist/languageService/callHierarchyProvider.js +2 -1
  45. package/dist/languageService/callHierarchyProvider.js.map +1 -1
  46. package/dist/languageService/completionProvider.js +12 -12
  47. package/dist/languageService/completionProvider.js.map +1 -1
  48. package/dist/languageService/definitionProvider.js +6 -6
  49. package/dist/languageService/definitionProvider.js.map +1 -1
  50. package/dist/languageService/documentSymbolCollector.js +9 -9
  51. package/dist/languageService/documentSymbolCollector.js.map +1 -1
  52. package/dist/languageService/hoverProvider.d.ts +1 -0
  53. package/dist/languageService/hoverProvider.js +30 -3
  54. package/dist/languageService/hoverProvider.js.map +1 -1
  55. package/dist/languageService/signatureHelpProvider.js +2 -2
  56. package/dist/languageService/signatureHelpProvider.js.map +1 -1
  57. package/dist/languageService/tooltipUtils.js +2 -2
  58. package/dist/languageService/tooltipUtils.js.map +1 -1
  59. package/dist/localization/localize.d.ts +2 -0
  60. package/dist/localization/localize.js +2 -0
  61. package/dist/localization/localize.js.map +1 -1
  62. package/dist/localization/package.nls.cs.json +4 -4
  63. package/dist/localization/package.nls.de.json +4 -4
  64. package/dist/localization/package.nls.en-us.json +27 -25
  65. package/dist/localization/package.nls.es.json +4 -4
  66. package/dist/localization/package.nls.fr.json +4 -4
  67. package/dist/localization/package.nls.it.json +4 -4
  68. package/dist/localization/package.nls.ja.json +4 -4
  69. package/dist/localization/package.nls.ko.json +4 -4
  70. package/dist/localization/package.nls.pl.json +4 -4
  71. package/dist/localization/package.nls.pt-br.json +4 -4
  72. package/dist/localization/package.nls.ru.json +4 -4
  73. package/dist/localization/package.nls.tr.json +4 -4
  74. package/dist/localization/package.nls.zh-cn.json +4 -4
  75. package/dist/localization/package.nls.zh-tw.json +4 -4
  76. package/dist/parser/parseNodes.d.ts +2 -3
  77. package/dist/parser/parseNodes.js.map +1 -1
  78. package/dist/parser/parser.js +6 -20
  79. package/dist/parser/parser.js.map +1 -1
  80. package/dist/tests/checker.test.js +5 -1
  81. package/dist/tests/checker.test.js.map +1 -1
  82. package/dist/tests/importStatementUtils.test.js +1 -1
  83. package/dist/tests/importStatementUtils.test.js.map +1 -1
  84. package/dist/tests/typeEvaluator1.test.js +1 -1
  85. package/dist/tests/typeEvaluator2.test.js +15 -3
  86. package/dist/tests/typeEvaluator2.test.js.map +1 -1
  87. package/dist/tests/typeEvaluator3.test.js +1 -1
  88. package/dist/tests/typeEvaluator4.test.js +1 -1
  89. package/dist/tests/typeEvaluator5.test.js +6 -0
  90. package/dist/tests/typeEvaluator5.test.js.map +1 -1
  91. package/dist/tests/typeEvaluator6.test.js +34 -36
  92. package/dist/tests/typeEvaluator6.test.js.map +1 -1
  93. package/dist/tests/typeEvaluator8.test.js +40 -0
  94. package/dist/tests/typeEvaluator8.test.js.map +1 -1
  95. package/dist/workspaceFactory.js +7 -7
  96. package/dist/workspaceFactory.js.map +1 -1
  97. package/package.json +5 -5
@@ -156,6 +156,11 @@ const maxInferFunctionReturnRecursionCount = 12;
156
156
  // (tuples, lists, sets, or dicts) which can lead to infinite type analysis.
157
157
  // This limits the depth.
158
158
  const maxInferredContainerDepth = 8;
159
+ // If a tuple expression with no declared type contains a large number
160
+ // of elements, it can cause performance issues. This value limits the
161
+ // number of elements that will be included in the tuple type before
162
+ // we default to tuple[Unknown, ...].
163
+ const maxInferredTupleEntryCount = 256;
159
164
  // Maximum recursion amount when comparing two recursive type aliases.
160
165
  // Increasing this can greatly increase the time required to evaluate
161
166
  // two recursive type aliases that have the same definition. Decreasing
@@ -429,6 +434,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
429
434
  // context. For example, if it's a subexpression of an argument expression,
430
435
  // the associated parameter type might inform the expected type.
431
436
  function getExpectedType(node) {
437
+ // This is a primary entry point called by language server providers,
438
+ // and it might be called before any other type evaluation has occurred.
439
+ // Use this opportunity to do some initialization.
440
+ initializePrefetchedTypes(node);
432
441
  // Scan up the parse tree to find the top-most expression node
433
442
  // so we can evaluate the entire expression.
434
443
  let topExpression = node;
@@ -500,6 +509,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
500
509
  const anySpecialForm = types_1.AnyType.createSpecialForm();
501
510
  if ((0, types_1.isAny)(anySpecialForm)) {
502
511
  types_1.TypeBase.setSpecialForm(anySpecialForm, anyClass);
512
+ if (isTypeFormSupported(node)) {
513
+ types_1.TypeBase.setTypeForm(anySpecialForm, (0, typeUtils_1.convertToInstance)(anySpecialForm));
514
+ }
503
515
  }
504
516
  }
505
517
  }
@@ -588,7 +600,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
588
600
  }
589
601
  // This is a helper function that implements the core of getTypeOfExpression.
590
602
  function getTypeOfExpressionCore(node, flags = 0 /* EvalFlags.None */, inferenceContext) {
591
- var _a;
603
+ var _a, _b;
592
604
  let typeResult;
593
605
  let expectingInstantiable = (flags & 128 /* EvalFlags.InstantiableType */) !== 0;
594
606
  switch (node.nodeType) {
@@ -745,8 +757,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
745
757
  }
746
758
  // If this is a PEP 695 type alias, remove the special form so the type
747
759
  // printer prints it as its aliased type rather than TypeAliasType.
748
- if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0) {
749
- const specialForm = (_a = typeResult.type.props) === null || _a === void 0 ? void 0 : _a.specialForm;
760
+ if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0 && ((_a = typeResult.type.props) === null || _a === void 0 ? void 0 : _a.typeForm) === undefined) {
761
+ const specialForm = (_b = typeResult.type.props) === null || _b === void 0 ? void 0 : _b.specialForm;
750
762
  if (specialForm && types_1.ClassType.isBuiltIn(specialForm, 'TypeAliasType')) {
751
763
  typeResult.type = types_1.TypeBase.cloneAsSpecialForm(typeResult.type, undefined);
752
764
  }
@@ -889,92 +901,132 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
889
901
  return typeResult;
890
902
  }
891
903
  function getTypeOfStringList(node, flags) {
904
+ var _a;
892
905
  let typeResult;
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() };
906
+ if ((flags & 8 /* EvalFlags.StrLiteralAsType */) !== 0 && (flags & 1073741824 /* EvalFlags.TypeFormArg */) === 0) {
907
+ return getTypeOfStringListAsType(node, flags);
908
+ }
909
+ const isBytesNode = (node) => (node.d.token.flags & 32 /* StringTokenFlags.Bytes */) !== 0;
910
+ // Check for mixing of bytes and str, which is not allowed.
911
+ const firstStrIndex = node.d.strings.findIndex((str) => !isBytesNode(str));
912
+ const firstBytesIndex = node.d.strings.findIndex((str) => isBytesNode(str));
913
+ if (firstStrIndex >= 0 && firstBytesIndex >= 0) {
914
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.mixingBytesAndStr(), node.d.strings[Math.max(firstBytesIndex, firstStrIndex)]);
915
+ return { type: types_1.UnknownType.create() };
916
+ }
917
+ const isBytes = firstBytesIndex >= 0;
918
+ let isLiteralString = true;
919
+ let isIncomplete = false;
920
+ node.d.strings.forEach((expr) => {
921
+ const typeResult = getTypeOfString(expr);
922
+ if (typeResult.isIncomplete) {
923
+ isIncomplete = true;
924
+ }
925
+ let isExprLiteralString = false;
926
+ if ((0, types_1.isClassInstance)(typeResult.type)) {
927
+ if (types_1.ClassType.isBuiltIn(typeResult.type, 'str') && typeResult.type.priv.literalValue !== undefined) {
928
+ isExprLiteralString = true;
912
929
  }
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() };
930
+ else if (types_1.ClassType.isBuiltIn(typeResult === null || typeResult === void 0 ? void 0 : typeResult.type, 'LiteralString')) {
931
+ isExprLiteralString = true;
916
932
  }
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
- }
933
+ }
934
+ if (!isExprLiteralString) {
935
+ isLiteralString = false;
936
+ }
937
+ });
938
+ // Don't create a literal type if it's an f-string.
939
+ if (node.d.strings.some((str) => str.nodeType === 30 /* ParseNodeType.FormatString */)) {
940
+ if (isLiteralString) {
941
+ const literalStringType = getTypingType(node, 'LiteralString');
942
+ if (literalStringType && (0, types_1.isInstantiableClass)(literalStringType)) {
943
+ typeResult = { type: types_1.ClassType.cloneAsInstance(literalStringType) };
925
944
  }
926
945
  }
927
946
  if (!typeResult) {
928
- addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.expectedTypeNotString(), node);
929
- typeResult = { type: types_1.UnknownType.create() };
947
+ typeResult = {
948
+ type: getBuiltInObject(node, isBytes ? 'bytes' : 'str'),
949
+ isIncomplete,
950
+ };
930
951
  }
931
952
  }
932
953
  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;
954
+ typeResult = {
955
+ type: cloneBuiltinObjectWithLiteral(node, isBytes ? 'bytes' : 'str', node.d.strings.map((s) => s.d.value).join('')),
956
+ isIncomplete,
957
+ };
958
+ }
959
+ if (node.d.strings.length !== 1 ||
960
+ node.d.strings[0].nodeType !== 49 /* ParseNodeType.String */ ||
961
+ !isTypeFormSupported(node)) {
962
+ return typeResult;
963
+ }
964
+ // For performance reasons, do not attempt to treat the string literal
965
+ // as a TypeForm if it's going to fail anyway or is unlikely to be a
966
+ // TypeForm (really long, triple-quoted, etc.).
967
+ const stringNode = node.d.strings[0];
968
+ const tokenFlags = stringNode.d.token.flags;
969
+ const disallowedTokenFlags = 32 /* StringTokenFlags.Bytes */ | 8 /* StringTokenFlags.Raw */ | 64 /* StringTokenFlags.Format */ | 4 /* StringTokenFlags.Triplicate */;
970
+ const maxTypeFormStringLength = 256;
971
+ if ((tokenFlags & disallowedTokenFlags) !== 0 ||
972
+ stringNode.d.token.escapedValue.length >= maxTypeFormStringLength) {
973
+ return typeResult;
974
+ }
975
+ const typeFormResult = getTypeOfStringListAsType(node, flags);
976
+ if ((_a = typeFormResult.type.props) === null || _a === void 0 ? void 0 : _a.typeForm) {
977
+ typeResult.type = types_1.TypeBase.cloneWithTypeForm(typeResult.type, typeFormResult.type.props.typeForm);
978
+ }
979
+ return typeResult;
980
+ }
981
+ function getTypeOfStringListAsType(node, flags) {
982
+ const reportTypeErrors = (flags & 8 /* EvalFlags.StrLiteralAsType */) !== 0;
983
+ let updatedFlags = flags | 4 /* EvalFlags.ForwardRefs */ | 128 /* EvalFlags.InstantiableType */;
984
+ let typeResult;
985
+ // In most cases, annotations within a string are not parsed by the interpreter.
986
+ // There are a few exceptions (e.g. the "bound" value for a TypeVar constructor).
987
+ if ((flags & 8388608 /* EvalFlags.ParsesStringLiteral */) === 0) {
988
+ updatedFlags |= 524288 /* EvalFlags.NotParsed */;
989
+ }
990
+ updatedFlags &= ~1073741824 /* EvalFlags.TypeFormArg */;
991
+ if (node.d.annotation) {
992
+ return getTypeOfExpression(node.d.annotation, updatedFlags);
993
+ }
994
+ if (node.d.strings.length === 1) {
995
+ const tokenFlags = node.d.strings[0].d.token.flags;
996
+ if (tokenFlags & 32 /* StringTokenFlags.Bytes */) {
997
+ if (reportTypeErrors) {
998
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.annotationBytesString(), node);
953
999
  }
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
- }
1000
+ return { type: types_1.UnknownType.create() };
1001
+ }
1002
+ if (tokenFlags & 8 /* StringTokenFlags.Raw */) {
1003
+ if (reportTypeErrors) {
1004
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.annotationRawString(), node);
963
1005
  }
964
- if (!typeResult) {
965
- typeResult = {
966
- type: getBuiltInObject(node, isBytes ? 'bytes' : 'str'),
967
- isIncomplete,
968
- };
1006
+ return { type: types_1.UnknownType.create() };
1007
+ }
1008
+ if (tokenFlags & 64 /* StringTokenFlags.Format */) {
1009
+ if (reportTypeErrors) {
1010
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.annotationFormatString(), node);
969
1011
  }
1012
+ return { type: types_1.UnknownType.create() };
970
1013
  }
971
- else {
972
- typeResult = {
973
- type: cloneBuiltinObjectWithLiteral(node, isBytes ? 'bytes' : 'str', node.d.strings.map((s) => s.d.value).join('')),
974
- isIncomplete,
975
- };
1014
+ // We didn't know at parse time that this string node was going
1015
+ // to be evaluated as a forward-referenced type. We need
1016
+ // to re-invoke the parser at this stage.
1017
+ const expr = parseStringAsTypeAnnotation(node, reportTypeErrors);
1018
+ if (expr) {
1019
+ typeResult = useSpeculativeMode(reportTypeErrors ? undefined : node, () => {
1020
+ return getTypeOfExpression(expr, updatedFlags);
1021
+ });
976
1022
  }
977
1023
  }
1024
+ if (!typeResult) {
1025
+ if (reportTypeErrors) {
1026
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.expectedTypeNotString(), node);
1027
+ }
1028
+ typeResult = { type: types_1.UnknownType.create() };
1029
+ }
978
1030
  return typeResult;
979
1031
  }
980
1032
  function getTypeOfString(node) {
@@ -1045,7 +1097,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
1045
1097
  var _a;
1046
1098
  if ((0, types_1.isClass)(subtype)) {
1047
1099
  if (subtype.priv.literalValue !== undefined) {
1048
- return types_1.ClassType.cloneWithLiteral(subtype, /* value */ undefined);
1100
+ subtype = types_1.ClassType.cloneWithLiteral(subtype, /* value */ undefined);
1049
1101
  }
1050
1102
  if (types_1.ClassType.isBuiltIn(subtype, 'LiteralString')) {
1051
1103
  // Handle "LiteralString" specially.
@@ -2250,13 +2302,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
2250
2302
  // If this is a member name (within a class scope) and the member name
2251
2303
  // appears to be a constant, use the strict source type. If it's a member
2252
2304
  // variable that can be overridden by a child class, use the more general
2253
- // version by stripping off the literal.
2305
+ // version by stripping off the literal and TypeForm.
2254
2306
  const scope = ScopeUtils.getScopeForNode(nameNode);
2255
2307
  if ((scope === null || scope === void 0 ? void 0 : scope.type) === 3 /* ScopeType.Class */) {
2256
2308
  if (types_1.TypeBase.isInstance(destType) &&
2257
2309
  !(0, symbolNameUtils_1.isConstantName)(nameValue) &&
2258
2310
  !isFinalVariable(symbolWithScope.symbol)) {
2259
- destType = stripLiteralValue(destType);
2311
+ destType = (0, typeUtils_1.stripTypeForm)(stripLiteralValue(destType));
2260
2312
  }
2261
2313
  }
2262
2314
  }
@@ -2557,6 +2609,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
2557
2609
  // If conditionFilter is specified and the TypeVar is a constrained
2558
2610
  // TypeVar, only the conditions that match the filter will be included.
2559
2611
  function makeTopLevelTypeVarsConcrete(type, makeParamSpecsConcrete = false, conditionFilter) {
2612
+ type = (0, typeUtils_1.transformPossibleRecursiveTypeAlias)(type);
2560
2613
  return (0, typeUtils_1.mapSubtypes)(type, (subtype) => {
2561
2614
  var _a;
2562
2615
  if ((0, types_1.isParamSpec)(subtype)) {
@@ -2859,55 +2912,53 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
2859
2912
  }
2860
2913
  function verifyRaiseExceptionType(node) {
2861
2914
  const baseExceptionType = getBuiltInType(node, 'BaseException');
2862
- if (node.d.typeExpression) {
2863
- const exceptionType = getTypeOfExpression(node.d.typeExpression).type;
2864
- // Validate that the argument of "raise" is an exception object or class.
2865
- // If it is a class, validate that the class's constructor accepts zero
2866
- // arguments.
2867
- if (exceptionType && baseExceptionType && (0, types_1.isInstantiableClass)(baseExceptionType)) {
2868
- const diagAddendum = new diagnostic_1.DiagnosticAddendum();
2869
- (0, typeUtils_1.doForEachSubtype)(exceptionType, (subtype) => {
2870
- const concreteSubtype = makeTopLevelTypeVarsConcrete(subtype);
2871
- if (!(0, types_1.isAnyOrUnknown)(concreteSubtype)) {
2872
- if ((0, types_1.isInstantiableClass)(concreteSubtype) && concreteSubtype.priv.literalValue === undefined) {
2873
- if (!(0, typeUtils_1.derivesFromClassRecursive)(concreteSubtype, baseExceptionType,
2874
- /* ignoreUnknown */ false)) {
2875
- diagAddendum.addMessage(localize_1.LocMessage.exceptionTypeIncorrect().format({
2876
- type: printType(subtype),
2877
- }));
2878
- }
2879
- else {
2880
- let callResult;
2881
- suppressDiagnostics(node.d.typeExpression, () => {
2882
- callResult = (0, constructors_1.validateConstructorArgs)(evaluatorInterface, node.d.typeExpression, [], concreteSubtype,
2883
- /* skipUnknownArgCheck */ false,
2884
- /* inferenceContext */ undefined);
2885
- });
2886
- if (callResult && callResult.argumentErrors) {
2887
- diagAddendum.addMessage(localize_1.LocMessage.exceptionTypeNotInstantiable().format({
2888
- type: printType(subtype),
2889
- }));
2890
- }
2891
- }
2892
- }
2893
- else if ((0, types_1.isClassInstance)(concreteSubtype)) {
2894
- if (!(0, typeUtils_1.derivesFromClassRecursive)(types_1.ClassType.cloneAsInstantiable(concreteSubtype), baseExceptionType,
2895
- /* ignoreUnknown */ false)) {
2896
- diagAddendum.addMessage(localize_1.LocMessage.exceptionTypeIncorrect().format({
2897
- type: printType(subtype),
2898
- }));
2899
- }
2900
- }
2901
- else {
2902
- diagAddendum.addMessage(localize_1.LocMessage.exceptionTypeIncorrect().format({
2915
+ const exceptionType = getTypeOfExpression(node).type;
2916
+ // Validate that the argument of "raise" is an exception object or class.
2917
+ // If it is a class, validate that the class's constructor accepts zero
2918
+ // arguments.
2919
+ if (exceptionType && baseExceptionType && (0, types_1.isInstantiableClass)(baseExceptionType)) {
2920
+ const diag = new diagnostic_1.DiagnosticAddendum();
2921
+ (0, typeUtils_1.doForEachSubtype)(exceptionType, (subtype) => {
2922
+ const concreteSubtype = makeTopLevelTypeVarsConcrete(subtype);
2923
+ if ((0, types_1.isAnyOrUnknown)(concreteSubtype) || (0, types_1.isNever)(concreteSubtype) || (0, typeUtils_1.isNoneInstance)(concreteSubtype)) {
2924
+ return;
2925
+ }
2926
+ if ((0, types_1.isInstantiableClass)(concreteSubtype) && concreteSubtype.priv.literalValue === undefined) {
2927
+ if (!(0, typeUtils_1.derivesFromClassRecursive)(concreteSubtype, baseExceptionType, /* ignoreUnknown */ false)) {
2928
+ diag.addMessage(localize_1.LocMessage.exceptionTypeIncorrect().format({
2929
+ type: printType(subtype),
2930
+ }));
2931
+ }
2932
+ else {
2933
+ let callResult;
2934
+ suppressDiagnostics(node, () => {
2935
+ callResult = (0, constructors_1.validateConstructorArgs)(evaluatorInterface, node, [], concreteSubtype,
2936
+ /* skipUnknownArgCheck */ false,
2937
+ /* inferenceContext */ undefined);
2938
+ });
2939
+ if (callResult && callResult.argumentErrors) {
2940
+ diag.addMessage(localize_1.LocMessage.exceptionTypeNotInstantiable().format({
2903
2941
  type: printType(subtype),
2904
2942
  }));
2905
2943
  }
2906
2944
  }
2907
- });
2908
- if (!diagAddendum.isEmpty()) {
2909
- addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.expectedExceptionClass() + diagAddendum.getString(), node.d.typeExpression);
2910
2945
  }
2946
+ else if ((0, types_1.isClassInstance)(concreteSubtype)) {
2947
+ if (!(0, typeUtils_1.derivesFromClassRecursive)(types_1.ClassType.cloneAsInstantiable(concreteSubtype), baseExceptionType,
2948
+ /* ignoreUnknown */ false)) {
2949
+ diag.addMessage(localize_1.LocMessage.exceptionTypeIncorrect().format({
2950
+ type: printType(subtype),
2951
+ }));
2952
+ }
2953
+ }
2954
+ else {
2955
+ diag.addMessage(localize_1.LocMessage.exceptionTypeIncorrect().format({
2956
+ type: printType(subtype),
2957
+ }));
2958
+ }
2959
+ });
2960
+ if (!diag.isEmpty()) {
2961
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.expectedExceptionClass() + diag.getString(), node);
2911
2962
  }
2912
2963
  }
2913
2964
  }
@@ -3063,6 +3114,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
3063
3114
  if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0) {
3064
3115
  type = validateSymbolIsTypeExpression(node, type, !!effectiveTypeInfo.includesVariableDecl);
3065
3116
  }
3117
+ // Add TypeForm details if appropriate.
3118
+ type = addTypeFormForSymbol(node, type, flags, !!effectiveTypeInfo.includesVariableDecl);
3066
3119
  }
3067
3120
  else {
3068
3121
  // Handle the special case of "reveal_type" and "reveal_locals".
@@ -3096,19 +3149,67 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
3096
3149
  }
3097
3150
  return { type, isIncomplete };
3098
3151
  }
3099
- // Reports diagnostics if type isn't valid within a type expression.
3100
- function validateSymbolIsTypeExpression(node, type, includesVariableDecl) {
3101
- var _a;
3102
- // Verify that the name does not refer to a (non type alias) variable.
3103
- if (!includesVariableDecl || ((_a = type.props) === null || _a === void 0 ? void 0 : _a.typeAliasInfo)) {
3152
+ function addTypeFormForSymbol(node, type, flags, includesVarDecl) {
3153
+ var _a, _b;
3154
+ if (!isTypeFormSupported(node)) {
3104
3155
  return type;
3105
3156
  }
3106
- if ((0, types_1.isTypeVar)(type) || (0, typeUtils_1.isTypeAliasPlaceholder)(type)) {
3157
+ const isValid = isSymbolValidTypeExpression(type, includesVarDecl);
3158
+ // If the type already has type information associated with it, don't replace.
3159
+ if ((_a = type.props) === null || _a === void 0 ? void 0 : _a.typeForm) {
3160
+ // If the NoConvertSpecialForm flag is set, we are evaluating in
3161
+ // the interior of a type expression, so variables are not allowed.
3162
+ // Clear any existing type form type for this symbol in this case.
3163
+ if ((flags & 33554432 /* EvalFlags.NoConvertSpecialForm */) !== 0 && !isValid) {
3164
+ type = types_1.TypeBase.cloneWithTypeForm(type, undefined);
3165
+ }
3166
+ return type;
3167
+ }
3168
+ // If the symbol is not valid for a type expression (e.g. it's a variable),
3169
+ // don't add TypeForm info.
3170
+ if (!isValid) {
3107
3171
  return type;
3108
3172
  }
3109
- // Exempts class types that are created by calling
3110
- // NewType, NamedTuple, etc.
3173
+ if ((0, types_1.isTypeVar)(type) && type.priv.scopeId && !type.shared.isSynthesized) {
3174
+ if (!(0, types_1.isTypeVarTuple)(type) || !type.priv.isInUnion) {
3175
+ const liveScopeIds = ParseTreeUtils.getTypeVarScopesForNode(node);
3176
+ type = types_1.TypeBase.cloneWithTypeForm(type, (0, typeUtils_1.convertToInstance)((0, typeUtils_1.makeTypeVarsBound)(type, liveScopeIds)));
3177
+ }
3178
+ }
3179
+ else if ((0, types_1.isInstantiableClass)(type) && !type.priv.includeSubclasses && !types_1.ClassType.isSpecialBuiltIn(type)) {
3180
+ if (types_1.ClassType.isBuiltIn(type, 'Any')) {
3181
+ type = types_1.TypeBase.cloneWithTypeForm(type, types_1.AnyType.create());
3182
+ }
3183
+ else {
3184
+ type = types_1.TypeBase.cloneWithTypeForm(type, types_1.ClassType.cloneAsInstance((0, typeUtils_1.specializeWithDefaultTypeArgs)(type)));
3185
+ }
3186
+ }
3187
+ if (((_b = type.props) === null || _b === void 0 ? void 0 : _b.typeAliasInfo) && types_1.TypeBase.isInstantiable(type)) {
3188
+ type = types_1.TypeBase.cloneWithTypeForm(type, (0, typeUtils_1.convertToInstance)(specializeTypeAliasWithDefaults(type, /* errorNode */ undefined)));
3189
+ }
3190
+ return type;
3191
+ }
3192
+ function isSymbolValidTypeExpression(type, includesVarDecl) {
3193
+ var _a;
3194
+ // Verify that the name does not refer to a (non type alias) variable.
3195
+ if (!includesVarDecl || ((_a = type.props) === null || _a === void 0 ? void 0 : _a.typeAliasInfo)) {
3196
+ return true;
3197
+ }
3198
+ if ((0, typeUtils_1.isTypeAliasPlaceholder)(type)) {
3199
+ return true;
3200
+ }
3201
+ if ((0, types_1.isTypeVar)(type) && !type.priv.scopeId) {
3202
+ return true;
3203
+ }
3204
+ // Exempts class types that are created by calling NewType, NamedTuple, etc.
3111
3205
  if ((0, types_1.isClass)(type) && !type.priv.includeSubclasses && types_1.ClassType.isValidTypeAliasClass(type)) {
3206
+ return true;
3207
+ }
3208
+ return false;
3209
+ }
3210
+ // Reports diagnostics if type isn't valid within a type expression.
3211
+ function validateSymbolIsTypeExpression(node, type, includesVarDecl) {
3212
+ if (isSymbolValidTypeExpression(type, includesVarDecl)) {
3112
3213
  return type;
3113
3214
  }
3114
3215
  // Disable for assignments in the typings.pyi file, since it defines special forms.
@@ -3123,7 +3224,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
3123
3224
  // evaluated in a value expression context, convert it from its special
3124
3225
  // meaning to its runtime value.
3125
3226
  function convertSpecialFormToRuntimeValue(type, flags) {
3126
- var _a, _b, _c;
3227
+ var _a, _b, _c, _d;
3127
3228
  const exemptFlags = 256 /* EvalFlags.TypeExpression */ | 128 /* EvalFlags.InstantiableType */ | 33554432 /* EvalFlags.NoConvertSpecialForm */;
3128
3229
  if ((flags & exemptFlags) !== 0) {
3129
3230
  return type;
@@ -3135,7 +3236,20 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
3135
3236
  return type;
3136
3237
  }
3137
3238
  }
3138
- return (_c = (_b = type.props) === null || _b === void 0 ? void 0 : _b.specialForm) !== null && _c !== void 0 ? _c : type;
3239
+ if (!((_b = type.props) === null || _b === void 0 ? void 0 : _b.specialForm)) {
3240
+ return type;
3241
+ }
3242
+ // If this is a type alias and we are not supposed to specialize it, return it as is.
3243
+ if ((flags & 2 /* EvalFlags.NoSpecialize */) !== 0 && ((_c = type.props) === null || _c === void 0 ? void 0 : _c.typeAliasInfo)) {
3244
+ // Special-case TypeAliasType which should be converted in this case.
3245
+ if (!types_1.ClassType.isBuiltIn(type.props.specialForm, 'TypeAliasType')) {
3246
+ return type;
3247
+ }
3248
+ }
3249
+ if ((_d = type.props) === null || _d === void 0 ? void 0 : _d.typeForm) {
3250
+ return types_1.TypeBase.cloneWithTypeForm(type.props.specialForm, type.props.typeForm);
3251
+ }
3252
+ return type.props.specialForm;
3139
3253
  }
3140
3254
  // Handles the case where a variable or parameter is defined in an outer
3141
3255
  // scope and captured by an inner scope (a function, lambda, or comprehension).
@@ -3244,7 +3358,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
3244
3358
  if ((0, types_1.isUnpackedTypeVarTuple)(type)) {
3245
3359
  type = types_1.TypeVarType.cloneForPacked(type);
3246
3360
  }
3247
- if ((flags & 1073741824 /* EvalFlags.EnforceClassTypeVarScope */) !== 0 && !enforceClassTypeVarScope(node, type)) {
3361
+ if ((flags & -2147483648 /* EvalFlags.EnforceClassTypeVarScope */) !== 0 && !enforceClassTypeVarScope(node, type)) {
3248
3362
  return types_1.UnknownType.create();
3249
3363
  }
3250
3364
  return type;
@@ -3352,7 +3466,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
3352
3466
  // type arguments. If so, it fills in these type arguments with Unknown
3353
3467
  // and optionally reports an error.
3354
3468
  function reportMissingTypeArgs(node, type, flags) {
3355
- var _a, _b, _c;
3469
+ var _a, _b;
3356
3470
  if ((flags & 2 /* EvalFlags.NoSpecialize */) !== 0) {
3357
3471
  return type;
3358
3472
  }
@@ -3372,49 +3486,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
3372
3486
  }
3373
3487
  }
3374
3488
  // 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 });
3489
+ if ((flags & 128 /* EvalFlags.InstantiableType */) !== 0) {
3490
+ type = specializeTypeAliasWithDefaults(type, node);
3418
3491
  }
3419
3492
  return type;
3420
3493
  }
@@ -3748,6 +3821,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
3748
3821
  if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0) {
3749
3822
  type = validateSymbolIsTypeExpression(node, type, !!typeResult.includesVariableDecl);
3750
3823
  }
3824
+ // Add TypeForm details if appropriate.
3825
+ type = addTypeFormForSymbol(node, type, flags, !!typeResult.includesVariableDecl);
3751
3826
  if ((0, types_1.isTypeVar)(type)) {
3752
3827
  type = validateTypeVarUsage(node, type, flags);
3753
3828
  }
@@ -4111,9 +4186,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
4111
4186
  }
4112
4187
  // Check for an attempt to overwrite or delete an instance variable that is
4113
4188
  // read-only (e.g. in a named tuple).
4114
- if ((memberInfo === null || memberInfo === void 0 ? void 0 : memberInfo.isInstanceMember) &&
4115
- (0, types_1.isClass)(memberInfo.classType) &&
4116
- types_1.ClassType.isReadOnlyInstanceVariables(memberInfo.classType)) {
4189
+ if ((memberInfo === null || memberInfo === void 0 ? void 0 : memberInfo.isInstanceMember) && (0, types_1.isClass)(memberInfo.classType) && memberInfo.isReadOnly) {
4117
4190
  diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.LocAddendum.readOnlyAttribute().format({ name: memberName }));
4118
4191
  isDescriptorError = true;
4119
4192
  }
@@ -4701,6 +4774,59 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
4701
4774
  }
4702
4775
  return true;
4703
4776
  }
4777
+ // If the type is a generic type alias that is not specialized, provides
4778
+ // default type arguments for the type alias. It optionally logs diagnostics
4779
+ // for missing type arguments.
4780
+ function specializeTypeAliasWithDefaults(type, errorNode) {
4781
+ var _a;
4782
+ // Is this a type alias?
4783
+ const aliasInfo = (_a = type.props) === null || _a === void 0 ? void 0 : _a.typeAliasInfo;
4784
+ if (!aliasInfo) {
4785
+ return type;
4786
+ }
4787
+ // Is this a generic type alias that needs specializing?
4788
+ if (!aliasInfo.typeParams || aliasInfo.typeParams.length === 0 || aliasInfo.typeArgs) {
4789
+ return type;
4790
+ }
4791
+ let reportDiag = false;
4792
+ const defaultTypeArgs = [];
4793
+ const constraints = new constraintTracker_1.ConstraintTracker();
4794
+ aliasInfo.typeParams.forEach((param) => {
4795
+ if (!param.shared.isDefaultExplicit) {
4796
+ reportDiag = true;
4797
+ }
4798
+ let defaultType;
4799
+ if (param.shared.isDefaultExplicit || (0, types_1.isParamSpec)(param)) {
4800
+ defaultType = solveAndApplyConstraints(param, constraints, {
4801
+ replaceUnsolved: {
4802
+ scopeIds: [aliasInfo.typeVarScopeId],
4803
+ tupleClassType: getTupleClassType(),
4804
+ },
4805
+ });
4806
+ }
4807
+ else if ((0, types_1.isTypeVarTuple)(param) && tupleClass && (0, types_1.isInstantiableClass)(tupleClass)) {
4808
+ defaultType = makeTupleObject([{ type: types_1.UnknownType.create(), isUnbounded: true }],
4809
+ /* isUnpacked */ true);
4810
+ }
4811
+ else {
4812
+ defaultType = types_1.UnknownType.create();
4813
+ }
4814
+ defaultTypeArgs.push(defaultType);
4815
+ constraints.setBounds(param, defaultType);
4816
+ });
4817
+ if (reportDiag && errorNode) {
4818
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportMissingTypeArgument, localize_1.LocMessage.typeArgsMissingForAlias().format({
4819
+ name: aliasInfo.name,
4820
+ }), errorNode);
4821
+ }
4822
+ type = types_1.TypeBase.cloneForTypeAlias(solveAndApplyConstraints(type, constraints, {
4823
+ replaceUnsolved: {
4824
+ scopeIds: [aliasInfo.typeVarScopeId],
4825
+ tupleClassType: getTupleClassType(),
4826
+ },
4827
+ }), { ...aliasInfo, typeArgs: defaultTypeArgs });
4828
+ return type;
4829
+ }
4704
4830
  // Handles index expressions that are providing type arguments for a
4705
4831
  // generic type alias.
4706
4832
  function createSpecializedTypeAlias(node, baseType, flags) {
@@ -4719,6 +4845,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
4719
4845
  }
4720
4846
  const typeParams = aliasInfo.typeParams;
4721
4847
  let typeArgs = adjustTypeArgsForTypeVarTuple(getTypeArgs(node, flags), typeParams, node);
4848
+ let reportedError = false;
4722
4849
  // PEP 612 says that if the class has only one type parameter consisting
4723
4850
  // of a ParamSpec, the list of arguments does not need to be enclosed in
4724
4851
  // a list. We'll handle that case specially here. Presumably this applies to
@@ -4745,6 +4872,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
4745
4872
  expected: typeParams.length,
4746
4873
  received: typeArgs.length,
4747
4874
  }), typeArgs[typeParams.length].node);
4875
+ reportedError = true;
4748
4876
  }
4749
4877
  else if (typeArgs.length < minTypeArgCount) {
4750
4878
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgsTooFew().format({
@@ -4752,6 +4880,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
4752
4880
  expected: typeParams.length,
4753
4881
  received: typeArgs.length,
4754
4882
  }), node.d.items[node.d.items.length - 1]);
4883
+ reportedError = true;
4755
4884
  }
4756
4885
  // Handle the mypy_extensions.FlexibleAlias type specially.
4757
4886
  if ((0, types_1.isInstantiableClass)(baseType) &&
@@ -4807,11 +4936,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
4807
4936
  }
4808
4937
  else {
4809
4938
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgListExpected(), typeArgs[index].node);
4939
+ reportedError = true;
4810
4940
  }
4811
4941
  }
4812
4942
  else {
4813
4943
  if (index < typeArgs.length && typeArgs[index].typeList) {
4814
4944
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgListNotAllowed(), typeArgs[index].node);
4945
+ reportedError = true;
4815
4946
  }
4816
4947
  let typeArgType;
4817
4948
  if (index < typeArgs.length) {
@@ -4853,6 +4984,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
4853
4984
  });
4854
4985
  if (!diag.isEmpty()) {
4855
4986
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeNotSpecializable().format({ type: printType(baseType) }) + diag.getString(), node, (_b = diag.getEffectiveTextRange()) !== null && _b !== void 0 ? _b : node);
4987
+ reportedError = true;
4856
4988
  }
4857
4989
  const solutionSet = (0, constraintSolver_1.solveConstraints)(evaluatorInterface, constraints).getMainSolutionSet();
4858
4990
  const aliasTypeArgs = [];
@@ -4865,18 +4997,43 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
4865
4997
  }
4866
4998
  aliasTypeArgs.push(typeVarType);
4867
4999
  });
4868
- const type = types_1.TypeBase.cloneForTypeAlias(solveAndApplyConstraints(baseType, constraints), {
5000
+ let type = types_1.TypeBase.cloneForTypeAlias(solveAndApplyConstraints(baseType, constraints), {
4869
5001
  ...aliasInfo,
4870
5002
  typeArgs: aliasTypeArgs,
4871
5003
  });
5004
+ if (isTypeFormSupported(node)) {
5005
+ type = types_1.TypeBase.cloneWithTypeForm(type, reportedError ? undefined : (0, typeUtils_1.convertToInstance)(type));
5006
+ }
4872
5007
  return { type, node };
4873
5008
  }
4874
5009
  function getTypeOfIndexWithBaseType(node, baseTypeResult, usage, flags) {
5010
+ var _a, _b, _c;
4875
5011
  // Handle the case where we're specializing a generic type alias.
4876
5012
  const typeAliasResult = createSpecializedTypeAlias(node, baseTypeResult.type, flags);
4877
5013
  if (typeAliasResult) {
4878
5014
  return typeAliasResult;
4879
5015
  }
5016
+ // Handle the case where Never or NoReturn are being specialized.
5017
+ if ((0, types_1.isNever)(baseTypeResult.type) && ((_a = baseTypeResult.type.props) === null || _a === void 0 ? void 0 : _a.specialForm)) {
5018
+ // Swap in the special form type, which is the Never or NoReturn class.
5019
+ baseTypeResult = { ...baseTypeResult, type: baseTypeResult.type.props.specialForm };
5020
+ }
5021
+ // Handle the case where a TypeAliasType symbol is being specialized
5022
+ // in a value expression.
5023
+ if ((0, types_1.isClassInstance)(baseTypeResult.type) &&
5024
+ types_1.ClassType.isBuiltIn(baseTypeResult.type, 'TypeAliasType') &&
5025
+ ((_b = baseTypeResult.type.props) === null || _b === void 0 ? void 0 : _b.typeForm)) {
5026
+ const typeAliasInfo = (_c = baseTypeResult.type.props.typeForm.props) === null || _c === void 0 ? void 0 : _c.typeAliasInfo;
5027
+ if (typeAliasInfo && typeAliasInfo.typeParams) {
5028
+ const origTypeAlias = types_1.TypeBase.cloneForTypeAlias((0, typeUtils_1.convertToInstantiable)(baseTypeResult.type.props.typeForm), { ...typeAliasInfo, typeArgs: undefined });
5029
+ const typeFormType = createSpecializedTypeAlias(node, origTypeAlias, flags);
5030
+ if (typeFormType) {
5031
+ return {
5032
+ type: types_1.TypeBase.cloneWithTypeForm(baseTypeResult.type, (0, typeUtils_1.convertToInstance)(typeFormType.type)),
5033
+ };
5034
+ }
5035
+ }
5036
+ }
4880
5037
  if ((0, types_1.isTypeVar)(baseTypeResult.type) && (0, typeUtils_1.isTypeAliasPlaceholder)(baseTypeResult.type)) {
4881
5038
  const typeArgTypes = getTypeArgs(node, flags).map((t) => (0, typeUtils_1.convertToInstance)(t.type));
4882
5039
  const type = types_1.TypeBase.cloneForTypeAlias(baseTypeResult.type, {
@@ -5299,6 +5456,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5299
5456
  function getTypeArgs(node, flags, options) {
5300
5457
  const typeArgs = [];
5301
5458
  let adjFlags = flags | 33554432 /* EvalFlags.NoConvertSpecialForm */;
5459
+ adjFlags &= ~1073741824 /* EvalFlags.TypeFormArg */;
5302
5460
  const allowFinalClassVar = () => {
5303
5461
  // If the annotation is a variable within the body of a dataclass, a
5304
5462
  // Final is allowed with a ClassVar annotation. In all other cases,
@@ -5533,7 +5691,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5533
5691
  }
5534
5692
  const entryTypeResults = node.d.items.map((expr, index) => getTypeOfExpression(expr, flags | 268435456 /* EvalFlags.StripTupleLiterals */, (0, typeUtils_1.makeInferenceContext)(index < expectedTypes.length ? expectedTypes[index] : undefined, inferenceContext.isTypeIncomplete)));
5535
5693
  const isIncomplete = entryTypeResults.some((result) => result.isIncomplete);
5536
- const type = makeTupleObject(buildTupleTypesList(entryTypeResults, /* stripLiterals */ false));
5537
5694
  // Copy any expected type diag addenda for precision error reporting.
5538
5695
  let expectedTypeDiagAddendum;
5539
5696
  if (entryTypeResults.some((result) => result.expectedTypeDiagAddendum)) {
@@ -5544,11 +5701,27 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5544
5701
  }
5545
5702
  });
5546
5703
  }
5704
+ // If the tuple contains a very large number of entries, it's probably
5705
+ // generated code. If we encounter type errors, don't bother building
5706
+ // the full tuple type.
5707
+ let type;
5708
+ if (node.d.items.length > maxInferredTupleEntryCount && entryTypeResults.some((result) => result.typeErrors)) {
5709
+ type = makeTupleObject([{ type: types_1.UnknownType.create(), isUnbounded: true }]);
5710
+ }
5711
+ else {
5712
+ type = makeTupleObject(buildTupleTypesList(entryTypeResults, /* stripLiterals */ false));
5713
+ }
5547
5714
  return { type, expectedTypeDiagAddendum, isIncomplete };
5548
5715
  }
5549
5716
  function getTypeOfTupleInferred(node, flags) {
5550
5717
  const entryTypeResults = node.d.items.map((expr) => getTypeOfExpression(expr, flags | 268435456 /* EvalFlags.StripTupleLiterals */));
5551
5718
  const isIncomplete = entryTypeResults.some((result) => result.isIncomplete);
5719
+ // If the tuple contains a very large number of entries, it's probably
5720
+ // generated code. Rather than taking the time to evaluate every entry,
5721
+ // simply return an unknown type in this case.
5722
+ if (node.d.items.length > maxInferredTupleEntryCount) {
5723
+ return { type: makeTupleObject([{ type: types_1.UnknownType.create(), isUnbounded: true }]) };
5724
+ }
5552
5725
  const type = makeTupleObject(buildTupleTypesList(entryTypeResults, (flags & 268435456 /* EvalFlags.StripTupleLiterals */) !== 0));
5553
5726
  if (isIncomplete) {
5554
5727
  if ((0, typeUtils_1.getContainerDepth)(type) > maxInferredContainerDepth) {
@@ -5586,7 +5759,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5586
5759
  entryTypes.push({ type: types_1.UnknownType.create(/* isIncomplete */ true), isUnbounded: false });
5587
5760
  }
5588
5761
  else {
5589
- const entryType = stripLiterals ? stripLiteralValue(typeResult.type) : typeResult.type;
5762
+ let entryType = convertSpecialFormToRuntimeValue(typeResult.type, 0 /* EvalFlags.None */);
5763
+ entryType = stripLiterals ? (0, typeUtils_1.stripTypeForm)(stripLiteralValue(entryType)) : entryType;
5590
5764
  entryTypes.push({ type: entryType, isUnbounded: !!typeResult.unpackedType });
5591
5765
  }
5592
5766
  }
@@ -5650,6 +5824,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5650
5824
  // Handle the "typing.assert_type" call.
5651
5825
  typeResult = getTypeOfAssertType(node, inferenceContext);
5652
5826
  }
5827
+ else if ((0, types_1.isClass)(baseTypeResult.type) && types_1.ClassType.isBuiltIn(baseTypeResult.type, 'TypeForm')) {
5828
+ // Handle the "typing.TypeForm" call.
5829
+ typeResult = getTypeOfTypeForm(node);
5830
+ }
5653
5831
  else if ((0, types_1.isAnyOrUnknown)(baseTypeResult.type) &&
5654
5832
  node.d.leftExpr.nodeType === 38 /* ParseNodeType.Name */ &&
5655
5833
  node.d.leftExpr.d.value === 'reveal_locals') {
@@ -5753,7 +5931,21 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5753
5931
  }
5754
5932
  return typeResult;
5755
5933
  }
5934
+ function getTypeOfTypeForm(node) {
5935
+ if (node.d.args.length !== 1 ||
5936
+ node.d.args[0].d.argCategory !== 0 /* ArgCategory.Simple */ ||
5937
+ node.d.args[0].d.name !== undefined) {
5938
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportCallIssue, localize_1.LocMessage.typeFormArgs(), node);
5939
+ return { type: types_1.UnknownType.create() };
5940
+ }
5941
+ return getTypeOfArgExpectingType((0, typeUtils_1.convertNodeToArg)(node.d.args[0]), {
5942
+ typeFormArg: isTypeFormSupported(node),
5943
+ noNonTypeSpecialForms: true,
5944
+ typeExpression: true,
5945
+ });
5946
+ }
5756
5947
  function getTypeOfAssertType(node, inferenceContext) {
5948
+ var _a;
5757
5949
  if (node.d.args.length !== 2 ||
5758
5950
  node.d.args[0].d.argCategory !== 0 /* ArgCategory.Simple */ ||
5759
5951
  node.d.args[0].d.name !== undefined ||
@@ -5773,7 +5965,24 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5773
5965
  // The spec is unclear on whether this is the correct behavior, but it seems to be
5774
5966
  // what mypy does -- and what various library authors expect.
5775
5967
  const arg0Type = stripTypeGuard(arg0TypeResult.type);
5776
- if (!(0, types_1.isTypeSame)(assertedType, arg0Type, { treatAnySameAsUnknown: true, ignorePseudoGeneric: true })) {
5968
+ let assertSuccess = (0, types_1.isTypeSame)(assertedType, arg0Type, {
5969
+ treatAnySameAsUnknown: true,
5970
+ ignorePseudoGeneric: true,
5971
+ });
5972
+ // Handle TypeForm types specially.
5973
+ if (!assertSuccess &&
5974
+ ((_a = arg0Type.props) === null || _a === void 0 ? void 0 : _a.typeForm) &&
5975
+ (0, types_1.isClassInstance)(assertedType) &&
5976
+ types_1.ClassType.isBuiltIn(assertedType, 'TypeForm')) {
5977
+ const typeFormType = assertedType.priv.typeArgs && assertedType.priv.typeArgs.length >= 1
5978
+ ? assertedType.priv.typeArgs[0]
5979
+ : types_1.UnknownType.create();
5980
+ assertSuccess = (0, types_1.isTypeSame)(arg0Type.props.typeForm, typeFormType, {
5981
+ treatAnySameAsUnknown: true,
5982
+ ignorePseudoGeneric: true,
5983
+ });
5984
+ }
5985
+ if (!assertSuccess) {
5777
5986
  const srcDestTypes = printSrcDestTypes(arg0TypeResult.type, assertedType, { expandTypeAlias: true });
5778
5987
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportAssertTypeFailure, localize_1.LocMessage.assertTypeTypeMismatch().format({
5779
5988
  expected: srcDestTypes.destType,
@@ -5919,30 +6128,33 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5919
6128
  }
5920
6129
  const concreteTargetClassType = makeTopLevelTypeVarsConcrete(targetClassType);
5921
6130
  // Determine whether to further narrow the type.
6131
+ let secondArgType;
5922
6132
  let bindToType;
5923
6133
  if (node.d.args.length > 1) {
5924
- const secondArgType = makeTopLevelTypeVarsConcrete(getTypeOfExpression(node.d.args[1].d.valueExpr).type);
6134
+ secondArgType = getTypeOfExpression(node.d.args[1].d.valueExpr).type;
6135
+ const secondArgConcreteType = makeTopLevelTypeVarsConcrete(secondArgType);
5925
6136
  let reportError = false;
5926
- if ((0, types_1.isAnyOrUnknown)(secondArgType)) {
6137
+ if ((0, types_1.isAnyOrUnknown)(secondArgConcreteType)) {
5927
6138
  // Ignore unknown or any types.
5928
6139
  }
5929
- else if ((0, types_1.isClassInstance)(secondArgType)) {
6140
+ else if ((0, types_1.isClassInstance)(secondArgConcreteType)) {
5930
6141
  if ((0, types_1.isInstantiableClass)(concreteTargetClassType)) {
5931
- if (!(0, typeUtils_1.derivesFromClassRecursive)(types_1.ClassType.cloneAsInstantiable(secondArgType), concreteTargetClassType,
6142
+ if (!(0, typeUtils_1.derivesFromClassRecursive)(types_1.ClassType.cloneAsInstantiable(secondArgConcreteType), concreteTargetClassType,
5932
6143
  /* ignoreUnknown */ true)) {
5933
6144
  reportError = true;
5934
6145
  }
5935
6146
  }
5936
- bindToType = secondArgType;
6147
+ bindToType = secondArgConcreteType;
5937
6148
  }
5938
- else if ((0, types_1.isInstantiableClass)(secondArgType)) {
6149
+ else if ((0, types_1.isInstantiableClass)(secondArgConcreteType)) {
5939
6150
  if ((0, types_1.isInstantiableClass)(concreteTargetClassType)) {
5940
6151
  if (!types_1.ClassType.isBuiltIn(concreteTargetClassType, 'type') &&
5941
- !(0, typeUtils_1.derivesFromClassRecursive)(secondArgType, concreteTargetClassType, /* ignoreUnknown */ true)) {
6152
+ !(0, typeUtils_1.derivesFromClassRecursive)(secondArgConcreteType, concreteTargetClassType,
6153
+ /* ignoreUnknown */ true)) {
5942
6154
  reportError = true;
5943
6155
  }
5944
6156
  }
5945
- bindToType = secondArgType;
6157
+ bindToType = secondArgConcreteType;
5946
6158
  }
5947
6159
  else {
5948
6160
  reportError = true;
@@ -6044,10 +6256,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
6044
6256
  }
6045
6257
  let bindToSelfType;
6046
6258
  if (bindToType) {
6047
- if (node.d.args.length > 1) {
6048
- // If this is a two-argument form of super(), use the
6049
- // bindToType evaluated from the arguments.
6050
- bindToSelfType = (0, typeUtils_1.convertToInstance)(bindToType);
6259
+ if (secondArgType) {
6260
+ // If a TypeVar was passed as the second argument, use it
6261
+ // to derive the the self type.
6262
+ if ((0, types_1.isTypeVar)(secondArgType)) {
6263
+ bindToSelfType = (0, typeUtils_1.convertToInstance)(secondArgType);
6264
+ }
6051
6265
  }
6052
6266
  else {
6053
6267
  // If this is a zero-argument form of super(), synthesize
@@ -7120,7 +7334,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
7120
7334
  // validation is left to the caller.
7121
7335
  // This logic is based on PEP 3102: https://www.python.org/dev/peps/pep-3102/
7122
7336
  function matchArgsToParams(errorNode, argList, typeResult, overloadIndex) {
7123
- var _a, _b, _c, _d, _e, _f, _g, _h;
7337
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
7124
7338
  const overload = typeResult.type;
7125
7339
  const paramDetails = (0, parameterUtils_1.getParamListDetails)(overload);
7126
7340
  const paramSpec = types_1.FunctionType.getParamSpecFromArgsKwargs(overload);
@@ -7537,9 +7751,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
7537
7751
  else if ((0, types_1.isClassInstance)(argType) && types_1.ClassType.isTypedDictClass(argType)) {
7538
7752
  // Handle the special case where it is a TypedDict and we know which
7539
7753
  // keys are present.
7540
- const typedDictEntries = (0, typedDicts_1.getTypedDictMembersForClass)(evaluatorInterface, argType);
7754
+ const tdEntries = (0, typedDicts_1.getTypedDictMembersForClass)(evaluatorInterface, argType);
7541
7755
  const diag = new diagnostic_1.DiagnosticAddendum();
7542
- typedDictEntries.knownItems.forEach((entry, name) => {
7756
+ tdEntries.knownItems.forEach((entry, name) => {
7757
+ var _a, _b;
7543
7758
  const paramEntry = paramMap.get(name);
7544
7759
  if (paramEntry && !paramEntry.isPositionalOnly) {
7545
7760
  if (paramEntry.argsReceived > 0) {
@@ -7558,7 +7773,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
7558
7773
  argCategory: 0 /* ArgCategory.Simple */,
7559
7774
  typeResult: { type: entry.valueType },
7560
7775
  },
7561
- errorNode: argList[argIndex].valueExpression || errorNode,
7776
+ errorNode: (_a = argList[argIndex].valueExpression) !== null && _a !== void 0 ? _a : errorNode,
7562
7777
  paramName: name,
7563
7778
  });
7564
7779
  }
@@ -7573,7 +7788,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
7573
7788
  argCategory: 0 /* ArgCategory.Simple */,
7574
7789
  typeResult: { type: entry.valueType },
7575
7790
  },
7576
- errorNode: argList[argIndex].valueExpression || errorNode,
7791
+ errorNode: (_b = argList[argIndex].valueExpression) !== null && _b !== void 0 ? _b : errorNode,
7577
7792
  paramName: name,
7578
7793
  });
7579
7794
  // Remember that this parameter has already received a value.
@@ -7593,6 +7808,23 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
7593
7808
  }
7594
7809
  }
7595
7810
  });
7811
+ const extraItemsType = (_g = (_f = tdEntries.extraItems) === null || _f === void 0 ? void 0 : _f.valueType) !== null && _g !== void 0 ? _g : getObjectType();
7812
+ if (!(0, types_1.isNever)(extraItemsType)) {
7813
+ if (paramDetails.kwargsIndex !== undefined) {
7814
+ const kwargsParam = paramDetails.params[paramDetails.kwargsIndex];
7815
+ validateArgTypeParams.push({
7816
+ paramCategory: 2 /* ParamCategory.KwargsDict */,
7817
+ paramType: kwargsParam.declaredType,
7818
+ requiresTypeVarMatching: (0, typeUtils_1.requiresSpecialization)(kwargsParam.declaredType),
7819
+ argument: {
7820
+ argCategory: 2 /* ArgCategory.UnpackedDictionary */,
7821
+ typeResult: { type: extraItemsType },
7822
+ },
7823
+ errorNode: (_h = argList[argIndex].valueExpression) !== null && _h !== void 0 ? _h : errorNode,
7824
+ paramName: kwargsParam.param.name,
7825
+ });
7826
+ }
7827
+ }
7596
7828
  if (!diag.isEmpty()) {
7597
7829
  if (!canSkipDiagnosticForNode(errorNode) && !isTypeIncomplete) {
7598
7830
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportCallIssue, localize_1.LocMessage.unpackedTypedDictArgument() + diag.getString(), argList[argIndex].valueExpression || errorNode);
@@ -7699,7 +7931,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
7699
7931
  paramType,
7700
7932
  requiresTypeVarMatching: (0, typeUtils_1.requiresSpecialization)(paramType),
7701
7933
  argument: argList[argIndex],
7702
- errorNode: (_f = argList[argIndex].valueExpression) !== null && _f !== void 0 ? _f : errorNode,
7934
+ errorNode: (_j = argList[argIndex].valueExpression) !== null && _j !== void 0 ? _j : errorNode,
7703
7935
  paramName: paramNameValue,
7704
7936
  });
7705
7937
  trySetActive(argList[argIndex], paramDetails.params[paramInfoIndex].param);
@@ -7722,7 +7954,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
7722
7954
  paramType,
7723
7955
  requiresTypeVarMatching: (0, typeUtils_1.requiresSpecialization)(paramType),
7724
7956
  argument: argList[argIndex],
7725
- errorNode: (_g = argList[argIndex].valueExpression) !== null && _g !== void 0 ? _g : errorNode,
7957
+ errorNode: (_k = argList[argIndex].valueExpression) !== null && _k !== void 0 ? _k : errorNode,
7726
7958
  paramName: paramNameValue,
7727
7959
  });
7728
7960
  // Remember that this parameter has already received a value.
@@ -7773,7 +8005,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
7773
8005
  requiresTypeVarMatching: false,
7774
8006
  argument: argList[argIndex],
7775
8007
  argType: (0, types_1.isParamSpec)(argType) ? undefined : types_1.AnyType.create(),
7776
- errorNode: (_h = argList[argIndex].valueExpression) !== null && _h !== void 0 ? _h : errorNode,
8008
+ errorNode: (_l = argList[argIndex].valueExpression) !== null && _l !== void 0 ? _l : errorNode,
7777
8009
  });
7778
8010
  }
7779
8011
  }
@@ -9114,7 +9346,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
9114
9346
  }
9115
9347
  // Specifically disallow Annotated.
9116
9348
  if (((_d = baseClass.props) === null || _d === void 0 ? void 0 : _d.specialForm) &&
9117
- (0, types_1.isInstantiableClass)(baseClass.props.specialForm) &&
9349
+ (0, types_1.isClassInstance)(baseClass.props.specialForm) &&
9118
9350
  types_1.ClassType.isBuiltIn(baseClass.props.specialForm, 'Annotated')) {
9119
9351
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.newTypeNotAClass(), argList[1].node || errorNode);
9120
9352
  return undefined;
@@ -9183,6 +9415,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
9183
9415
  if (node.d.constType === 26 /* KeywordType.None */) {
9184
9416
  if (noneTypeClass) {
9185
9417
  type = (flags & 128 /* EvalFlags.InstantiableType */) !== 0 ? noneTypeClass : (0, typeUtils_1.convertToInstance)(noneTypeClass);
9418
+ if (isTypeFormSupported(node)) {
9419
+ type = types_1.TypeBase.cloneWithTypeForm(type, (0, typeUtils_1.convertToInstance)(type));
9420
+ }
9186
9421
  }
9187
9422
  }
9188
9423
  else if (node.d.constType === 33 /* KeywordType.True */ ||
@@ -9447,9 +9682,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
9447
9682
  if (keyValueResult.typeErrors) {
9448
9683
  typeErrors = true;
9449
9684
  }
9450
- // Strip any literal values.
9451
- const keyTypes = keyTypeResults.map((t) => stripLiteralValue(t.type));
9452
- const valueTypes = valueTypeResults.map((t) => stripLiteralValue(t.type));
9685
+ // Strip any literal values and TypeForm types.
9686
+ const keyTypes = keyTypeResults.map((t) => (0, typeUtils_1.stripTypeForm)(convertSpecialFormToRuntimeValue(stripLiteralValue(t.type), flags)));
9687
+ const valueTypes = valueTypeResults.map((t) => (0, typeUtils_1.stripTypeForm)(convertSpecialFormToRuntimeValue(stripLiteralValue(t.type), flags)));
9453
9688
  keyType = keyTypes.length > 0 ? (0, types_1.combineTypes)(keyTypes) : fallbackType;
9454
9689
  // If the value type differs and we're not using "strict inference mode",
9455
9690
  // we need to back off because we can't properly represent the mappings
@@ -9800,6 +10035,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
9800
10035
  else {
9801
10036
  entryTypeResult = getTypeOfExpression(entry, flags | 268435456 /* EvalFlags.StripTupleLiterals */);
9802
10037
  }
10038
+ entryTypeResult.type = (0, typeUtils_1.stripTypeForm)(convertSpecialFormToRuntimeValue(entryTypeResult.type, flags));
9803
10039
  if (entryTypeResult.isIncomplete) {
9804
10040
  isIncomplete = true;
9805
10041
  }
@@ -10334,9 +10570,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10334
10570
  // a ParamSpec, a Concatenate, or a list of positional parameter types.
10335
10571
  // The second argument, if present, should specify the return type.
10336
10572
  function createCallableType(classType, typeArgs, errorNode) {
10337
- const functionType = types_1.FunctionType.createInstantiable(0 /* FunctionTypeFlags.None */);
10573
+ let functionType = types_1.FunctionType.createInstantiable(0 /* FunctionTypeFlags.None */);
10338
10574
  let paramSpec;
10339
- types_1.TypeBase.setSpecialForm(functionType, classType);
10575
+ let isValidTypeForm = true;
10576
+ types_1.TypeBase.setSpecialForm(functionType, types_1.ClassType.cloneAsInstance(classType));
10340
10577
  functionType.shared.declaredReturnType = types_1.UnknownType.create();
10341
10578
  functionType.shared.typeVarScopeId = ParseTreeUtils.getScopeIdForNode(errorNode);
10342
10579
  if (typeArgs && typeArgs.length > 0) {
@@ -10351,6 +10588,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10351
10588
  if (!reportedUnpackedError) {
10352
10589
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.variadicTypeArgsTooMany(), entry.node);
10353
10590
  reportedUnpackedError = true;
10591
+ isValidTypeForm = false;
10354
10592
  }
10355
10593
  }
10356
10594
  sawUnpacked = true;
@@ -10412,6 +10650,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10412
10650
  }
10413
10651
  else {
10414
10652
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.callableFirstArg(), typeArgs[0].node);
10653
+ isValidTypeForm = false;
10415
10654
  }
10416
10655
  }
10417
10656
  if (typeArgs.length > 1) {
@@ -10424,22 +10663,31 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10424
10663
  else {
10425
10664
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportMissingTypeArgument, localize_1.LocMessage.callableSecondArg(), errorNode);
10426
10665
  functionType.shared.declaredReturnType = types_1.UnknownType.create();
10666
+ isValidTypeForm = false;
10427
10667
  }
10428
10668
  if (typeArgs.length > 2) {
10429
10669
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.callableExtraArgs(), typeArgs[2].node);
10670
+ isValidTypeForm = false;
10430
10671
  }
10431
10672
  }
10432
10673
  else {
10433
10674
  types_1.FunctionType.addDefaultParams(functionType, /* useUnknown */ true);
10434
10675
  functionType.shared.flags |= 32768 /* FunctionTypeFlags.GradualCallableForm */;
10676
+ if (typeArgs && typeArgs.length === 0) {
10677
+ isValidTypeForm = false;
10678
+ }
10435
10679
  }
10436
10680
  if (paramSpec) {
10437
10681
  types_1.FunctionType.addParamSpecVariadics(functionType, (0, typeUtils_1.convertToInstance)(paramSpec));
10438
10682
  }
10683
+ if (isTypeFormSupported(errorNode) && isValidTypeForm) {
10684
+ functionType = types_1.TypeBase.cloneWithTypeForm(functionType, (0, typeUtils_1.convertToInstance)(functionType));
10685
+ }
10439
10686
  return functionType;
10440
10687
  }
10441
10688
  // Creates an Optional[X] type.
10442
10689
  function createOptionalType(classType, errorNode, typeArgs, flags) {
10690
+ var _a;
10443
10691
  if (!typeArgs) {
10444
10692
  // If no type arguments are provided, the resulting type
10445
10693
  // depends on whether we're evaluating a type annotation or
@@ -10462,6 +10710,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10462
10710
  if (unionTypeClass && (0, types_1.isInstantiableClass)(unionTypeClass)) {
10463
10711
  optionalType = types_1.TypeBase.cloneAsSpecialForm(optionalType, types_1.ClassType.cloneAsInstance(unionTypeClass));
10464
10712
  }
10713
+ if ((_a = typeArg0Type.props) === null || _a === void 0 ? void 0 : _a.typeForm) {
10714
+ const typeFormType = (0, types_1.combineTypes)([
10715
+ typeArg0Type.props.typeForm,
10716
+ (0, typeUtils_1.convertToInstance)(noneTypeClass !== null && noneTypeClass !== void 0 ? noneTypeClass : types_1.UnknownType.create()),
10717
+ ]);
10718
+ optionalType = types_1.TypeBase.cloneWithTypeForm(optionalType, typeFormType);
10719
+ }
10465
10720
  return optionalType;
10466
10721
  }
10467
10722
  function cloneBuiltinObjectWithLiteral(node, builtInName, value) {
@@ -10580,6 +10835,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10580
10835
  if ((0, types_1.isUnion)(result) && unionTypeClass && (0, types_1.isInstantiableClass)(unionTypeClass)) {
10581
10836
  result = types_1.TypeBase.cloneAsSpecialForm(result, types_1.ClassType.cloneAsInstance(unionTypeClass));
10582
10837
  }
10838
+ if (isTypeFormSupported(node)) {
10839
+ result = types_1.TypeBase.cloneWithTypeForm(result, (0, typeUtils_1.convertToInstance)(result));
10840
+ }
10583
10841
  return result;
10584
10842
  }
10585
10843
  // Creates a ClassVar type.
@@ -10608,6 +10866,27 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10608
10866
  }
10609
10867
  return type;
10610
10868
  }
10869
+ function createTypeFormType(classType, errorNode, typeArgs) {
10870
+ if (!typeArgs || typeArgs.length === 0) {
10871
+ return types_1.ClassType.specialize(classType, [types_1.UnknownType.create()]);
10872
+ }
10873
+ if (typeArgs.length > 1) {
10874
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgsTooMany().format({
10875
+ name: classType.priv.aliasName || classType.shared.name,
10876
+ expected: 1,
10877
+ received: typeArgs.length,
10878
+ }), typeArgs[1].node);
10879
+ return types_1.UnknownType.create();
10880
+ }
10881
+ const convertedTypeArgs = typeArgs.map((typeArg) => {
10882
+ return (0, typeUtils_1.convertToInstance)(validateTypeArg(typeArg) ? typeArg.type : types_1.UnknownType.create());
10883
+ });
10884
+ let resultType = types_1.ClassType.specialize(classType, convertedTypeArgs);
10885
+ if (isTypeFormSupported(errorNode)) {
10886
+ resultType = types_1.TypeBase.cloneWithTypeForm(resultType, (0, typeUtils_1.convertToInstance)(resultType));
10887
+ }
10888
+ return resultType;
10889
+ }
10611
10890
  // Creates a "TypeGuard" and "TypeIs" type.
10612
10891
  function createTypeGuardType(classType, errorNode, typeArgs, flags) {
10613
10892
  // If no type arguments are provided, the resulting type
@@ -10626,7 +10905,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10626
10905
  const convertedTypeArgs = typeArgs.map((typeArg) => {
10627
10906
  return (0, typeUtils_1.convertToInstance)(validateTypeArg(typeArg) ? typeArg.type : types_1.UnknownType.create());
10628
10907
  });
10629
- return types_1.ClassType.specialize(classType, convertedTypeArgs);
10908
+ let resultType = types_1.ClassType.specialize(classType, convertedTypeArgs);
10909
+ if (isTypeFormSupported(errorNode)) {
10910
+ resultType = types_1.TypeBase.cloneWithTypeForm(resultType, (0, typeUtils_1.convertToInstance)(resultType));
10911
+ }
10912
+ return resultType;
10630
10913
  }
10631
10914
  function createSelfType(classType, errorNode, typeArgs, flags) {
10632
10915
  var _a;
@@ -10701,11 +10984,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10701
10984
  return { type: classType };
10702
10985
  }
10703
10986
  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);
10987
+ if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0) {
10988
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, classType.shared.name === 'ReadOnly'
10989
+ ? localize_1.LocMessage.readOnlyArgCount()
10990
+ : classType.shared.name === 'Required'
10991
+ ? localize_1.LocMessage.requiredArgCount()
10992
+ : localize_1.LocMessage.notRequiredArgCount(), errorNode);
10993
+ }
10709
10994
  return { type: classType };
10710
10995
  }
10711
10996
  const typeArgType = typeArgs[0].type;
@@ -10743,25 +11028,23 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10743
11028
  isNotRequired = classType.shared.name === 'NotRequired';
10744
11029
  }
10745
11030
  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) };
11031
+ if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0) {
11032
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, classType.shared.name === 'ReadOnly'
11033
+ ? localize_1.LocMessage.readOnlyNotInTypedDict()
11034
+ : classType.shared.name === 'Required'
11035
+ ? localize_1.LocMessage.requiredNotInTypedDict()
11036
+ : localize_1.LocMessage.notRequiredNotInTypedDict(), errorNode);
11037
+ }
11038
+ return { type: classType };
10752
11039
  }
10753
11040
  return { type: typeArgType, isReadOnly, isRequired, isNotRequired };
10754
11041
  }
10755
11042
  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
11043
  if (!typeArgs || typeArgs.length !== 1) {
10763
- addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.unpackArgCount(), errorNode);
10764
- return types_1.UnknownType.create();
11044
+ if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0) {
11045
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.unpackArgCount(), errorNode);
11046
+ }
11047
+ return classType;
10765
11048
  }
10766
11049
  let typeArgType = typeArgs[0].type;
10767
11050
  if ((0, types_1.isUnion)(typeArgType) && typeArgType.priv.subtypes.length === 1) {
@@ -10774,6 +11057,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10774
11057
  if ((0, types_1.isTypeVarTuple)(typeArgType) && !typeArgType.priv.isUnpacked) {
10775
11058
  return types_1.TypeVarType.cloneForUnpacked(typeArgType);
10776
11059
  }
11060
+ if ((flags & 256 /* EvalFlags.TypeExpression */) === 0) {
11061
+ return classType;
11062
+ }
10777
11063
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.unpackExpectedTypeVarTuple(), errorNode);
10778
11064
  return types_1.UnknownType.create();
10779
11065
  }
@@ -10781,19 +11067,27 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10781
11067
  if ((0, types_1.isInstantiableClass)(typeArgType) && types_1.ClassType.isTypedDictClass(typeArgType)) {
10782
11068
  return types_1.ClassType.cloneForUnpacked(typeArgType);
10783
11069
  }
11070
+ if ((flags & 256 /* EvalFlags.TypeExpression */) === 0) {
11071
+ return classType;
11072
+ }
10784
11073
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.unpackExpectedTypedDict(), errorNode);
10785
11074
  return types_1.UnknownType.create();
10786
11075
  }
11076
+ if ((flags & 256 /* EvalFlags.TypeExpression */) === 0) {
11077
+ return classType;
11078
+ }
10787
11079
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.unpackNotAllowed(), errorNode);
10788
11080
  return types_1.UnknownType.create();
10789
11081
  }
10790
11082
  // Creates a "Final" type.
10791
11083
  function createFinalType(classType, errorNode, typeArgs, flags) {
10792
11084
  if (flags & 16 /* EvalFlags.NoFinal */) {
10793
- addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.finalContext(), errorNode);
11085
+ if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0) {
11086
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.finalContext(), errorNode);
11087
+ }
10794
11088
  return classType;
10795
11089
  }
10796
- if (!typeArgs || typeArgs.length === 0) {
11090
+ if ((flags & 256 /* EvalFlags.TypeExpression */) === 0 || !typeArgs || typeArgs.length === 0) {
10797
11091
  return classType;
10798
11092
  }
10799
11093
  if (typeArgs.length > 1) {
@@ -10803,8 +11097,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10803
11097
  }
10804
11098
  function createConcatenateType(classType, errorNode, typeArgs, flags) {
10805
11099
  if ((flags & 134217728 /* EvalFlags.AllowConcatenate */) === 0) {
10806
- addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.concatenateContext(), errorNode);
10807
- return types_1.AnyType.create();
11100
+ if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0) {
11101
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.concatenateContext(), errorNode);
11102
+ }
11103
+ return classType;
10808
11104
  }
10809
11105
  if (!typeArgs || typeArgs.length === 0) {
10810
11106
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.concatenateTypeArgsMissing(), errorNode);
@@ -10832,11 +11128,16 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10832
11128
  return createSpecialType(classType, typeArgs, /* paramLimit */ undefined, /* allowParamSpec */ true);
10833
11129
  }
10834
11130
  function createAnnotatedType(classType, errorNode, typeArgs, flags) {
11131
+ var _a;
11132
+ let type;
10835
11133
  const typeExprFlags = 256 /* EvalFlags.TypeExpression */ | 33554432 /* EvalFlags.NoConvertSpecialForm */;
10836
11134
  if ((flags & typeExprFlags) === 0) {
10837
- return { type: classType };
11135
+ type = types_1.ClassType.cloneAsInstance(classType);
11136
+ if (typeArgs && typeArgs.length >= 1 && ((_a = typeArgs[0].type.props) === null || _a === void 0 ? void 0 : _a.typeForm)) {
11137
+ type = types_1.TypeBase.cloneWithTypeForm(type, typeArgs[0].type.props.typeForm);
11138
+ }
11139
+ return { type };
10838
11140
  }
10839
- let type;
10840
11141
  if (typeArgs && typeArgs.length > 0) {
10841
11142
  type = typeArgs[0].type;
10842
11143
  if (typeArgs.length < 2) {
@@ -10853,14 +11154,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10853
11154
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgListNotAllowed(), typeArgs[0].node);
10854
11155
  }
10855
11156
  return {
10856
- type: types_1.TypeBase.cloneAsSpecialForm(type, classType),
11157
+ type: types_1.TypeBase.cloneAsSpecialForm(type, types_1.ClassType.cloneAsInstance(classType)),
10857
11158
  isReadOnly: typeArgs[0].isReadOnly,
10858
11159
  isRequired: typeArgs[0].isRequired,
10859
11160
  isNotRequired: typeArgs[0].isNotRequired,
10860
11161
  };
10861
11162
  }
10862
- // Enforces metadata consistency as specified in PEP 746 and associates
10863
- // refinement type predicates with the base type.
11163
+ // Enforces metadata consistency as specified in PEP 746.
10864
11164
  function validateAnnotatedMetadata(errorNode, baseType, metaArgs) {
10865
11165
  for (const metaArg of metaArgs) {
10866
11166
  validateTypeMetadata(errorNode, baseType, metaArg);
@@ -11025,9 +11325,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
11025
11325
  }
11026
11326
  // Unpacks the index expression for a "Union[X, Y, Z]" type annotation.
11027
11327
  function createUnionType(classType, errorNode, typeArgs, flags) {
11328
+ var _a;
11028
11329
  const fileInfo = AnalyzerNodeInfo.getFileInfo(errorNode);
11029
11330
  const types = [];
11030
11331
  let allowSingleTypeArg = false;
11332
+ let isValidTypeForm = true;
11031
11333
  if (!typeArgs) {
11032
11334
  // If no type arguments are provided, the resulting type
11033
11335
  // depends on whether we're evaluating a type annotation or
@@ -11057,6 +11359,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
11057
11359
  else {
11058
11360
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.unionUnpackedTypeVarTuple(), errorNode);
11059
11361
  typeArgType = types_1.UnknownType.create();
11362
+ isValidTypeForm = false;
11060
11363
  }
11061
11364
  }
11062
11365
  types.push(typeArgType);
@@ -11066,11 +11369,21 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
11066
11369
  // since it is used to define NoReturn in typeshed stubs).
11067
11370
  if (types.length === 1 && !allowSingleTypeArg && !(0, typeUtils_1.isNoneInstance)(types[0])) {
11068
11371
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeArguments, localize_1.LocMessage.unionTypeArgCount(), errorNode);
11372
+ isValidTypeForm = false;
11069
11373
  }
11070
11374
  let unionType = (0, types_1.combineTypes)(types, { skipElideRedundantLiterals: true });
11071
11375
  if (unionTypeClass && (0, types_1.isInstantiableClass)(unionTypeClass)) {
11072
11376
  unionType = types_1.TypeBase.cloneAsSpecialForm(unionType, types_1.ClassType.cloneAsInstance(unionTypeClass));
11073
11377
  }
11378
+ if (!isValidTypeForm || types.some((t) => { var _a; return !((_a = t.props) === null || _a === void 0 ? void 0 : _a.typeForm); })) {
11379
+ if ((_a = unionType.props) === null || _a === void 0 ? void 0 : _a.typeForm) {
11380
+ unionType = types_1.TypeBase.cloneWithTypeForm(unionType, undefined);
11381
+ }
11382
+ }
11383
+ else if (isTypeFormSupported(errorNode)) {
11384
+ const typeFormType = (0, types_1.combineTypes)(types.map((t) => t.props.typeForm));
11385
+ unionType = types_1.TypeBase.cloneWithTypeForm(unionType, typeFormType);
11386
+ }
11074
11387
  return unionType;
11075
11388
  }
11076
11389
  // Creates a type that represents "Generic[T1, T2, ...]", used in the
@@ -11107,6 +11420,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
11107
11420
  return createSpecialType(classType, typeArgs, /* paramLimit */ undefined, /* allowParamSpec */ true);
11108
11421
  }
11109
11422
  function transformTypeForTypeAlias(type, name, errorNode, isPep695Syntax, isPep695TypeVarType, typeParams, typeParamNodes) {
11423
+ var _a;
11110
11424
  if (!types_1.TypeBase.isInstantiable(type)) {
11111
11425
  return type;
11112
11426
  }
@@ -11188,6 +11502,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
11188
11502
  typeAlias = types_1.TypeBase.cloneAsSpecialForm(typeAlias, types_1.ClassType.cloneAsInstance(typeAliasTypeClass));
11189
11503
  }
11190
11504
  }
11505
+ // Delete the TypeForm info. The type alias serves as its own TypeForm info.
11506
+ if ((_a = typeAlias.props) === null || _a === void 0 ? void 0 : _a.typeForm) {
11507
+ typeAlias = types_1.TypeBase.cloneWithTypeForm(typeAlias, undefined);
11508
+ }
11191
11509
  return typeAlias;
11192
11510
  }
11193
11511
  function createSpecialBuiltInClass(node, assignedName, aliasMapEntry) {
@@ -11309,20 +11627,30 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
11309
11627
  typeParamVariance: 2 /* Variance.Invariant */,
11310
11628
  },
11311
11629
  ],
11630
+ ['TypeForm', { alias: '', module: 'builtins', isSpecialForm: true, typeParamVariance: 3 /* Variance.Covariant */ }],
11312
11631
  ]);
11313
11632
  const aliasMapEntry = specialTypes.get(assignedName);
11314
11633
  if (aliasMapEntry) {
11315
11634
  const cachedType = readTypeCache(node, 0 /* EvalFlags.None */);
11316
11635
  if (cachedType) {
11317
- (0, debug_1.assert)((0, types_1.isInstantiableClass)(cachedType));
11318
11636
  return cachedType;
11319
11637
  }
11320
- const specialType = createSpecialBuiltInClass(node, assignedName, aliasMapEntry);
11638
+ let specialType = createSpecialBuiltInClass(node, assignedName, aliasMapEntry);
11321
11639
  // Handle 'LiteralString' specially because we want it to act as
11322
11640
  // though it derives from 'str'.
11323
11641
  if (assignedName === 'LiteralString') {
11324
11642
  specialType.shared.baseClasses.push(strClass !== null && strClass !== void 0 ? strClass : types_1.AnyType.create());
11325
11643
  (0, typeUtils_1.computeMroLinearization)(specialType);
11644
+ if (isTypeFormSupported(node)) {
11645
+ specialType = types_1.TypeBase.cloneWithTypeForm(specialType, (0, typeUtils_1.convertToInstance)(specialType));
11646
+ }
11647
+ }
11648
+ // Handle 'Never' and 'NoReturn' specially.
11649
+ if (assignedName === 'Never' || assignedName === 'NoReturn') {
11650
+ specialType = types_1.TypeBase.cloneAsSpecialForm(assignedName === 'Never' ? types_1.NeverType.createNever() : types_1.NeverType.createNoReturn(), specialType);
11651
+ if (isTypeFormSupported(node)) {
11652
+ specialType = types_1.TypeBase.cloneWithTypeForm(specialType, (0, typeUtils_1.convertToInstance)(specialType));
11653
+ }
11326
11654
  }
11327
11655
  writeTypeCache(node, { type: specialType }, 0 /* EvalFlags.None */);
11328
11656
  return specialType;
@@ -11414,7 +11742,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
11414
11742
  64 /* EvalFlags.NoTypeVarTuple */ |
11415
11743
  131072 /* EvalFlags.NoClassVar */;
11416
11744
  typeAliasNameNode = node.d.leftExpr.d.valueExpr;
11417
- if (!isLegalTypeAliasExpressionForm(node.d.rightExpr)) {
11745
+ if (!isLegalTypeAliasExpressionForm(node.d.rightExpr, /* allowStrLiteral */ true)) {
11418
11746
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeAliasIllegalExpressionForm(), node.d.rightExpr);
11419
11747
  }
11420
11748
  }
@@ -11722,6 +12050,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
11722
12050
  if ((0, types_1.isAny)(argType) && ((_b = argType.props) === null || _b === void 0 ? void 0 : _b.specialForm)) {
11723
12051
  argType = types_1.AnyType.create();
11724
12052
  }
12053
+ argType = (0, typeUtils_1.stripTypeFormRecursive)(argType);
11725
12054
  if (!(0, types_1.isAnyOrUnknown)(argType) && !(0, types_1.isUnbound)(argType)) {
11726
12055
  if ((0, typeUtils_1.isMetaclassInstance)(argType)) {
11727
12056
  (0, debug_1.assert)((0, types_1.isClassInstance)(argType));
@@ -11758,7 +12087,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
11758
12087
  if (pythonVersion_1.PythonVersion.isGreaterOrEqualTo(fileInfo.executionEnvironment.pythonVersion, pythonVersion_1.pythonVersion3_6)) {
11759
12088
  if (types_1.ClassType.isBuiltIn(argType, 'NamedTuple')) {
11760
12089
  isNamedTupleSubclass = true;
11761
- classType.shared.flags |= 524288 /* ClassTypeFlags.ReadOnlyInstanceVariables */;
11762
12090
  }
11763
12091
  }
11764
12092
  // If the class directly derives from TypedDict or from a class that is
@@ -12036,7 +12364,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
12036
12364
  if ((0, enums_1.isEnumMetaclass)(metaclassType)) {
12037
12365
  classType.shared.flags |= 65536 /* ClassTypeFlags.EnumClass */;
12038
12366
  }
12039
- if (types_1.ClassType.isBuiltIn(metaclassType, 'ABCMeta')) {
12367
+ if ((0, typeUtils_1.derivesFromStdlibClass)(metaclassType, 'ABCMeta')) {
12040
12368
  classType.shared.flags |= 64 /* ClassTypeFlags.SupportsAbstractMethods */;
12041
12369
  }
12042
12370
  }
@@ -13042,7 +13370,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
13042
13370
  skipInference = true;
13043
13371
  }
13044
13372
  if (!skipInference) {
13045
- inferredParamType = stripLiteralValue(defaultValueType);
13373
+ inferredParamType = convertSpecialFormToRuntimeValue(defaultValueType, 0 /* EvalFlags.None */);
13374
+ inferredParamType = (0, typeUtils_1.stripTypeForm)(inferredParamType);
13375
+ inferredParamType = stripLiteralValue(inferredParamType);
13046
13376
  }
13047
13377
  }
13048
13378
  if (inferredParamType) {
@@ -13215,6 +13545,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
13215
13545
  }
13216
13546
  return subtype;
13217
13547
  });
13548
+ // Do not retain TypeForm types in inferred return types.
13549
+ returnType = (0, typeUtils_1.stripTypeForm)(returnType);
13218
13550
  inferredReturnTypes.push(returnType);
13219
13551
  }
13220
13552
  else {
@@ -13323,10 +13655,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
13323
13655
  return false;
13324
13656
  }
13325
13657
  for (const raiseStatement of functionDecl.raiseStatements) {
13326
- if (!raiseStatement.d.typeExpression || raiseStatement.d.valueExpression) {
13658
+ if (!raiseStatement.d.expr || raiseStatement.d.fromExpr) {
13327
13659
  return false;
13328
13660
  }
13329
- const raiseType = getTypeOfExpression(raiseStatement.d.typeExpression).type;
13661
+ const raiseType = getTypeOfExpression(raiseStatement.d.expr).type;
13330
13662
  const classType = (0, types_1.isInstantiableClass)(raiseType)
13331
13663
  ? raiseType
13332
13664
  : (0, types_1.isClassInstance)(raiseType)
@@ -14253,6 +14585,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14253
14585
  // Returns the specialized type and a boolean indicating whether
14254
14586
  // the type indicates a class type (true) or an object type (false).
14255
14587
  function createSpecializedClassType(classType, typeArgs, flags, errorNode) {
14588
+ let isValidTypeForm = true;
14256
14589
  // Handle the special-case classes that are not defined
14257
14590
  // in the type stubs.
14258
14591
  if (types_1.ClassType.isSpecialBuiltIn(classType)) {
@@ -14261,17 +14594,17 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14261
14594
  case 'Callable': {
14262
14595
  return { type: createCallableType(classType, typeArgs, errorNode) };
14263
14596
  }
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
- }
14597
+ case 'Never':
14270
14598
  case 'NoReturn': {
14271
14599
  if (typeArgs && typeArgs.length > 0) {
14272
- addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgsExpectingNone().format({ name: 'NoReturn' }), typeArgs[0].node);
14600
+ addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgsExpectingNone().format({ name: aliasedName }), typeArgs[0].node);
14273
14601
  }
14274
- return { type: types_1.NeverType.createNoReturn() };
14602
+ let resultType = aliasedName === 'Never' ? types_1.NeverType.createNever() : types_1.NeverType.createNoReturn();
14603
+ resultType = types_1.TypeBase.cloneAsSpecialForm(resultType, classType);
14604
+ if (isTypeFormSupported(errorNode)) {
14605
+ resultType = types_1.TypeBase.cloneWithTypeForm(resultType, (0, typeUtils_1.convertToInstance)(resultType));
14606
+ }
14607
+ return { type: resultType };
14275
14608
  }
14276
14609
  case 'Optional': {
14277
14610
  return { type: createOptionalType(classType, errorNode, typeArgs, flags) };
@@ -14283,6 +14616,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14283
14616
  if ((0, types_1.isInstantiableClass)(typeType)) {
14284
14617
  typeType = (0, typeUtils_1.explodeGenericClass)(typeType);
14285
14618
  }
14619
+ if (isTypeFormSupported(errorNode)) {
14620
+ typeType = types_1.TypeBase.cloneWithTypeForm(typeType, (0, typeUtils_1.convertToInstance)(typeType));
14621
+ }
14286
14622
  return { type: typeType };
14287
14623
  }
14288
14624
  case 'ClassVar': {
@@ -14307,12 +14643,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14307
14643
  if ((flags & (67108864 /* EvalFlags.NoNonTypeSpecialForms */ | 256 /* EvalFlags.TypeExpression */)) !== 0) {
14308
14644
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typedDictNotAllowed(), errorNode);
14309
14645
  }
14646
+ isValidTypeForm = false;
14310
14647
  break;
14311
14648
  }
14312
14649
  case 'Literal': {
14313
14650
  if ((flags & (67108864 /* EvalFlags.NoNonTypeSpecialForms */ | 256 /* EvalFlags.TypeExpression */)) !== 0) {
14314
14651
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.literalNotAllowed(), errorNode);
14315
14652
  }
14653
+ isValidTypeForm = false;
14316
14654
  break;
14317
14655
  }
14318
14656
  case 'Tuple': {
@@ -14358,6 +14696,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14358
14696
  case 'LiteralString': {
14359
14697
  return { type: createSpecialType(classType, typeArgs, 0) };
14360
14698
  }
14699
+ case 'TypeForm': {
14700
+ return { type: createTypeFormType(classType, errorNode, typeArgs) };
14701
+ }
14361
14702
  }
14362
14703
  }
14363
14704
  const fileInfo = AnalyzerNodeInfo.getFileInfo(errorNode);
@@ -14382,18 +14723,23 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14382
14723
  if ((0, types_1.isInstantiableClass)(typeType)) {
14383
14724
  typeType = (0, typeUtils_1.explodeGenericClass)(typeType);
14384
14725
  }
14726
+ if (isTypeFormSupported(errorNode)) {
14727
+ typeType = types_1.TypeBase.cloneWithTypeForm(typeType, (0, typeUtils_1.convertToInstance)(typeType));
14728
+ }
14385
14729
  return { type: typeType };
14386
14730
  }
14387
14731
  }
14388
14732
  // Handle "tuple" specially, since it needs to act like "Tuple"
14389
14733
  // in Python 3.9 and newer.
14390
14734
  if ((0, typeUtils_1.isTupleClass)(classType)) {
14391
- return {
14392
- type: createSpecialType(classType, typeArgs,
14393
- /* paramLimit */ undefined,
14394
- /* allowParamSpec */ undefined,
14395
- /* isSpecialForm */ false),
14396
- };
14735
+ let specializedClass = createSpecialType(classType, typeArgs,
14736
+ /* paramLimit */ undefined,
14737
+ /* allowParamSpec */ undefined,
14738
+ /* isSpecialForm */ false);
14739
+ if (isTypeFormSupported(errorNode)) {
14740
+ specializedClass = types_1.TypeBase.cloneWithTypeForm(specializedClass, (0, typeUtils_1.convertToInstance)(specializedClass));
14741
+ }
14742
+ return { type: specializedClass };
14397
14743
  }
14398
14744
  }
14399
14745
  let typeArgCount = typeArgs ? typeArgs.length : 0;
@@ -14414,11 +14760,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14414
14760
  if (typeArgCount > typeParams.length) {
14415
14761
  if (!types_1.ClassType.isPartiallyEvaluated(classType) && !types_1.ClassType.isTupleClass(classType)) {
14416
14762
  if (typeParams.length === 0) {
14763
+ isValidTypeForm = false;
14417
14764
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeArguments, localize_1.LocMessage.typeArgsExpectingNone().format({
14418
14765
  name: classType.priv.aliasName || classType.shared.name,
14419
14766
  }), typeArgs[typeParams.length].node);
14420
14767
  }
14421
14768
  else if (typeParams.length !== 1 || !(0, types_1.isParamSpec)(typeParams[0])) {
14769
+ isValidTypeForm = false;
14422
14770
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeArguments, localize_1.LocMessage.typeArgsTooMany().format({
14423
14771
  name: classType.priv.aliasName || classType.shared.name,
14424
14772
  expected: typeParams.length,
@@ -14429,6 +14777,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14429
14777
  }
14430
14778
  }
14431
14779
  else if (typeArgCount < minTypeArgCount) {
14780
+ isValidTypeForm = false;
14432
14781
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeArguments, localize_1.LocMessage.typeArgsTooFew().format({
14433
14782
  name: classType.priv.aliasName || classType.shared.name,
14434
14783
  expected: minTypeArgCount,
@@ -14436,6 +14785,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14436
14785
  }), typeArgs.length > 0 ? typeArgs[0].node.parent : errorNode);
14437
14786
  }
14438
14787
  typeArgs.forEach((typeArg, index) => {
14788
+ var _a;
14789
+ if (!((_a = typeArg.type.props) === null || _a === void 0 ? void 0 : _a.typeForm)) {
14790
+ isValidTypeForm = false;
14791
+ }
14439
14792
  if (index === variadicTypeParamIndex) {
14440
14793
  // The types that make up the tuple that maps to the
14441
14794
  // TypeVarTuple have already been validated when the tuple
@@ -14444,16 +14797,20 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14444
14797
  return;
14445
14798
  }
14446
14799
  if ((0, types_1.isTypeVarTuple)(typeArg.type)) {
14447
- validateTypeVarTupleIsUnpacked(typeArg.type, typeArg.node);
14800
+ if (!validateTypeVarTupleIsUnpacked(typeArg.type, typeArg.node)) {
14801
+ isValidTypeForm = false;
14802
+ }
14448
14803
  return;
14449
14804
  }
14450
14805
  }
14451
14806
  const typeParam = index < typeParams.length ? typeParams[index] : undefined;
14452
14807
  const isParamSpecTarget = typeParam && (0, types_1.isParamSpec)(typeParam);
14453
- validateTypeArg(typeArg, {
14808
+ if (!validateTypeArg(typeArg, {
14454
14809
  allowParamSpec: true,
14455
14810
  allowTypeArgList: isParamSpecTarget,
14456
- });
14811
+ })) {
14812
+ isValidTypeForm = false;
14813
+ }
14457
14814
  });
14458
14815
  }
14459
14816
  // Handle ParamSpec arguments and fill in any missing type arguments with Unknown.
@@ -14483,10 +14840,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14483
14840
  else if (typeArgs.length > 1) {
14484
14841
  const paramSpecTypeArg = typeArgs.find((typeArg) => (0, types_1.isParamSpec)(typeArg.type));
14485
14842
  if (paramSpecTypeArg) {
14843
+ isValidTypeForm = false;
14486
14844
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.paramSpecContext(), paramSpecTypeArg.node);
14487
14845
  }
14488
14846
  const listTypeArg = typeArgs.find((typeArg) => !!typeArg.typeList);
14489
14847
  if (listTypeArg) {
14848
+ isValidTypeForm = false;
14490
14849
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgListNotAllowed(), listTypeArg.node);
14491
14850
  }
14492
14851
  }
@@ -14574,6 +14933,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14574
14933
  // Avoid emitting this error for a partially-constructed class.
14575
14934
  if (!(0, types_1.isClassInstance)(typeArgType) || !types_1.ClassType.isPartiallyEvaluated(typeArgType)) {
14576
14935
  (0, debug_1.assert)(typeArgs !== undefined);
14936
+ isValidTypeForm = false;
14577
14937
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeArguments, localize_1.LocMessage.typeVarAssignmentMismatch().format({
14578
14938
  type: printType(typeArgType),
14579
14939
  name: types_1.TypeVarType.getReadableName(typeParams[index]),
@@ -14589,7 +14949,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14589
14949
  if (typeArgs && classType.shared.typeParams.length === 0 && types_1.ClassType.isPartiallyEvaluated(classType)) {
14590
14950
  typeArgTypes = typeArgs.map((t) => (0, typeUtils_1.convertToInstance)(t.type));
14591
14951
  }
14592
- const specializedClass = types_1.ClassType.specialize(classType, typeArgTypes, typeArgs !== undefined);
14952
+ let specializedClass = types_1.ClassType.specialize(classType, typeArgTypes, typeArgs !== undefined);
14953
+ if (isTypeFormSupported(errorNode)) {
14954
+ specializedClass = types_1.TypeBase.cloneWithTypeForm(specializedClass, isValidTypeForm ? (0, typeUtils_1.convertToInstance)(specializedClass) : undefined);
14955
+ }
14593
14956
  return { type: specializedClass };
14594
14957
  }
14595
14958
  function getTypeOfArg(arg, inferenceContext) {
@@ -14628,7 +14991,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14628
14991
  flags |= 8192 /* EvalFlags.TypeVarGetsCurScope */;
14629
14992
  }
14630
14993
  if (options === null || options === void 0 ? void 0 : options.enforceClassTypeVarScope) {
14631
- flags |= 1073741824 /* EvalFlags.EnforceClassTypeVarScope */;
14994
+ flags |= -2147483648 /* EvalFlags.EnforceClassTypeVarScope */;
14632
14995
  }
14633
14996
  const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
14634
14997
  if ((0, analyzerFileInfo_1.isAnnotationEvaluationPostponed)(fileInfo) || (options === null || options === void 0 ? void 0 : options.forwardRefs)) {
@@ -14673,6 +15036,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14673
15036
  if (options === null || options === void 0 ? void 0 : options.notParsed) {
14674
15037
  flags |= 524288 /* EvalFlags.NotParsed */;
14675
15038
  }
15039
+ if (options === null || options === void 0 ? void 0 : options.typeFormArg) {
15040
+ flags |= 1073741824 /* EvalFlags.TypeFormArg */;
15041
+ }
14676
15042
  return getTypeOfExpression(node, flags);
14677
15043
  }
14678
15044
  function getBuiltInType(node, name) {
@@ -14929,7 +15295,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14929
15295
  // In general, string nodes don't have any declarations associated with them, but
14930
15296
  // we need to handle the special case of string literals used as keys within a
14931
15297
  // dictionary expression where those keys are associated with a known TypedDict.
14932
- function getDeclarationsForStringNode(node) {
15298
+ function getDeclInfoForStringNode(node) {
14933
15299
  var _a;
14934
15300
  const declarations = [];
14935
15301
  const expectedType = (_a = getExpectedType(node)) === null || _a === void 0 ? void 0 : _a.type;
@@ -14951,7 +15317,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14951
15317
  }
14952
15318
  });
14953
15319
  }
14954
- return declarations.length === 0 ? undefined : declarations;
15320
+ return declarations.length === 0 ? undefined : { decls: declarations, synthesizedTypes: [] };
14955
15321
  }
14956
15322
  function getAliasFromImport(node) {
14957
15323
  if (node.parent &&
@@ -14962,12 +15328,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14962
15328
  }
14963
15329
  return undefined;
14964
15330
  }
14965
- function getDeclarationsForNameNode(node, skipUnreachableCode = true) {
15331
+ function getDeclInfoForNameNode(node, skipUnreachableCode = true) {
14966
15332
  var _a, _b, _c;
14967
15333
  if (skipUnreachableCode && AnalyzerNodeInfo.isCodeUnreachable(node)) {
14968
15334
  return undefined;
14969
15335
  }
14970
15336
  const declarations = [];
15337
+ const synthesizedTypes = [];
14971
15338
  // If the node is part of a "from X import Y as Z" statement and the node
14972
15339
  // is the "Y" (non-aliased) name, we need to look up the alias symbol
14973
15340
  // since the non-aliased name is not in the symbol table.
@@ -15037,7 +15404,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
15037
15404
  (0, collectionUtils_1.appendArray)(declarations, typedDecls);
15038
15405
  }
15039
15406
  else {
15040
- (0, collectionUtils_1.appendArray)(declarations, symbol.getDeclarations());
15407
+ const synthesizedType = symbol.getSynthesizedType();
15408
+ if (synthesizedType) {
15409
+ synthesizedTypes.push(synthesizedType);
15410
+ }
15411
+ else {
15412
+ (0, collectionUtils_1.appendArray)(declarations, symbol.getDeclarations());
15413
+ }
15041
15414
  }
15042
15415
  }
15043
15416
  });
@@ -15119,7 +15492,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
15119
15492
  (0, collectionUtils_1.appendArray)(declarations, symbolWithScope.symbol.getDeclarations());
15120
15493
  }
15121
15494
  }
15122
- return declarations;
15495
+ return { decls: declarations, synthesizedTypes };
15123
15496
  }
15124
15497
  function getTypeForDeclaration(declaration) {
15125
15498
  var _a, _b;
@@ -15665,6 +16038,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
15665
16038
  return true;
15666
16039
  }
15667
16040
  }
16041
+ if (decl.type === 2 /* DeclarationType.Param */) {
16042
+ return true;
16043
+ }
15668
16044
  return false;
15669
16045
  });
15670
16046
  }
@@ -15821,9 +16197,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
15821
16197
  isConstant = true;
15822
16198
  }
15823
16199
  // If the symbol is constant, we can retain the literal
15824
- // value. Otherwise, strip literal values to widen the type.
16200
+ // value and TypeForm types. Otherwise, strip literal values
16201
+ // and TypeForm types to widen.
15825
16202
  if (types_1.TypeBase.isInstance(type) && !isConstant && !isExplicitTypeAliasDeclaration(decl)) {
15826
- type = stripLiteralValue(type);
16203
+ type = (0, typeUtils_1.stripTypeForm)(stripLiteralValue(type));
15827
16204
  }
15828
16205
  }
15829
16206
  typesToCombine.push(type);
@@ -16181,7 +16558,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
16181
16558
  paramType = types_1.UnknownType.create();
16182
16559
  }
16183
16560
  if (stripLiteralArgTypes) {
16184
- paramType = stripLiteralValue(paramType);
16561
+ paramType = (0, typeUtils_1.stripTypeForm)(convertSpecialFormToRuntimeValue(stripLiteralValue(paramType), 0 /* EvalFlags.None */));
16185
16562
  }
16186
16563
  paramTypes.push(paramType);
16187
16564
  writeTypeCache(param.d.name, { type: paramType }, 0 /* EvalFlags.None */);
@@ -16461,8 +16838,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
16461
16838
  let flags = 0 /* AssignTypeFlags.Default */;
16462
16839
  if ((primaryDecl === null || primaryDecl === void 0 ? void 0 : primaryDecl.type) === 1 /* DeclarationType.Variable */ &&
16463
16840
  !isFinalVariableDeclaration(primaryDecl) &&
16464
- !types_1.ClassType.isReadOnlyInstanceVariables(destType) &&
16465
- !types_1.ClassType.isDataClassFrozen(destType)) {
16841
+ !(0, typeUtils_1.isMemberReadOnly)(destType, name)) {
16466
16842
  // Class and instance variables that are mutable need to
16467
16843
  // enforce invariance. We will exempt variables that are
16468
16844
  // private or protected, since these are presumably
@@ -16534,18 +16910,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
16534
16910
  inferVarianceForClass(destType);
16535
16911
  // If we're enforcing invariance, literal types must match.
16536
16912
  if ((flags & 1 /* AssignTypeFlags.Invariant */) !== 0) {
16537
- const srcIsLiteral = srcType.priv.literalValue !== undefined;
16538
- const destIsLiteral = destType.priv.literalValue !== undefined;
16913
+ const srcIsLiteral = (0, typeUtils_1.isLiteralLikeType)(srcType);
16914
+ const destIsLiteral = (0, typeUtils_1.isLiteralLikeType)(destType);
16539
16915
  if (srcIsLiteral !== destIsLiteral) {
16540
16916
  return false;
16541
16917
  }
16542
16918
  }
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
16919
  for (let ancestorIndex = inheritanceChain.length - 1; ancestorIndex >= 0; ancestorIndex--) {
16550
16920
  const ancestorType = inheritanceChain[ancestorIndex];
16551
16921
  // If we've hit an "unknown", all bets are off, and we need to assume
@@ -16719,7 +17089,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
16719
17089
  // in the dest type is not in the type map already, it is assigned a type
16720
17090
  // and added to the map.
16721
17091
  function assignType(destType, srcType, diag, constraints, flags = 0 /* AssignTypeFlags.Default */, recursionCount = 0) {
16722
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
17092
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
16723
17093
  // Handle the case where the dest and src types are the same object.
16724
17094
  // We can normally shortcut this and say that they are compatible,
16725
17095
  // but if the type includes TypeVars, we need to go through
@@ -16740,7 +17110,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
16740
17110
  }
16741
17111
  }
16742
17112
  if (!isSpecialFormExempt) {
16743
- srcType = specialForm;
17113
+ if (((_b = srcType.props) === null || _b === void 0 ? void 0 : _b.typeForm) && !((_c = specialForm.props) === null || _c === void 0 ? void 0 : _c.typeForm)) {
17114
+ srcType = types_1.TypeBase.cloneWithTypeForm(specialForm, srcType.props.typeForm);
17115
+ }
17116
+ else {
17117
+ srcType = specialForm;
17118
+ }
16744
17119
  }
16745
17120
  }
16746
17121
  if (recursionCount > types_1.maxTypeRecursionCount) {
@@ -16753,8 +17128,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
16753
17128
  destType.shared.recursiveAlias &&
16754
17129
  (0, types_1.isTypeVar)(srcType) &&
16755
17130
  srcType.shared.recursiveAlias) {
16756
- const destAliasInfo = (_b = destType.props) === null || _b === void 0 ? void 0 : _b.typeAliasInfo;
16757
- const srcAliasInfo = (_c = srcType.props) === null || _c === void 0 ? void 0 : _c.typeAliasInfo;
17131
+ const destAliasInfo = (_d = destType.props) === null || _d === void 0 ? void 0 : _d.typeAliasInfo;
17132
+ const srcAliasInfo = (_e = srcType.props) === null || _e === void 0 ? void 0 : _e.typeAliasInfo;
16758
17133
  // Do the source and dest refer to the same recursive type alias?
16759
17134
  if ((destAliasInfo === null || destAliasInfo === void 0 ? void 0 : destAliasInfo.typeArgs) &&
16760
17135
  (srcAliasInfo === null || srcAliasInfo === void 0 ? void 0 : srcAliasInfo.typeArgs) &&
@@ -16804,8 +17179,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
16804
17179
  if ((0, types_1.isUnbound)(destType) || (0, types_1.isUnbound)(srcType)) {
16805
17180
  return true;
16806
17181
  }
16807
- // Before performing any other checks, see if the dest type is a
16808
- // TypeVar that we are attempting to match.
17182
+ // Handle TypeForm assignments.
17183
+ if (assignToTypeFormType(destType, srcType, constraints, flags, recursionCount)) {
17184
+ return true;
17185
+ }
16809
17186
  if ((0, types_1.isTypeVar)(destType)) {
16810
17187
  if ((0, typeUtils_1.isTypeVarSame)(destType, srcType)) {
16811
17188
  return true;
@@ -16820,7 +17197,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
16820
17197
  // and the bound TypeVar matches the condition, the types are compatible.
16821
17198
  const destTypeVar = destType;
16822
17199
  if (types_1.TypeBase.isInstantiable(destType) === types_1.TypeBase.isInstantiable(srcType) &&
16823
- ((_d = srcType.props) === null || _d === void 0 ? void 0 : _d.condition) &&
17200
+ ((_f = srcType.props) === null || _f === void 0 ? void 0 : _f.condition) &&
16824
17201
  srcType.props.condition.some((cond) => {
16825
17202
  return (!types_1.TypeVarType.hasConstraints(cond.typeVar) &&
16826
17203
  cond.typeVar.priv.nameWithScope === destTypeVar.priv.nameWithScope);
@@ -16921,7 +17298,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
16921
17298
  if ((0, types_1.isAnyOrUnknown)(destType)) {
16922
17299
  return true;
16923
17300
  }
16924
- if ((0, types_1.isAnyOrUnknown)(srcType) && !((_e = srcType.props) === null || _e === void 0 ? void 0 : _e.specialForm)) {
17301
+ if ((0, types_1.isAnyOrUnknown)(srcType) && !((_g = srcType.props) === null || _g === void 0 ? void 0 : _g.specialForm)) {
16925
17302
  if (constraints) {
16926
17303
  // If it's an ellipsis type, convert it to a regular "Any"
16927
17304
  // type. These are functionally equivalent, but "Any" looks
@@ -17005,12 +17382,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
17005
17382
  return false;
17006
17383
  }
17007
17384
  }
17008
- if (types_1.ClassType.isBuiltIn(destType, 'type') && ((_g = (_f = srcType.props) === null || _f === void 0 ? void 0 : _f.instantiableDepth) !== null && _g !== void 0 ? _g : 0) > 0) {
17385
+ if (types_1.ClassType.isBuiltIn(destType, 'type') && ((_j = (_h = srcType.props) === null || _h === void 0 ? void 0 : _h.instantiableDepth) !== null && _j !== void 0 ? _j : 0) > 0) {
17009
17386
  return true;
17010
17387
  }
17011
17388
  if (isSpecialFormClass(expandedSrcType, flags)) {
17012
- if ((_h = destType.props) === null || _h === void 0 ? void 0 : _h.specialForm) {
17013
- return assignType(destType.props.specialForm, expandedSrcType, diag, constraints, flags, recursionCount);
17389
+ // Special form classes are compatible only with other special form
17390
+ // classes, not with 'object' or 'type'.
17391
+ const destSpecialForm = (_l = (_k = destType.props) === null || _k === void 0 ? void 0 : _k.specialForm) !== null && _l !== void 0 ? _l : destType;
17392
+ if (isSpecialFormClass(destSpecialForm, flags)) {
17393
+ return assignType(destSpecialForm, expandedSrcType, diag, constraints, flags, recursionCount);
17014
17394
  }
17015
17395
  }
17016
17396
  else if (assignClass(destType, expandedSrcType, diag, constraints, flags, recursionCount,
@@ -17067,7 +17447,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
17067
17447
  assignType(destType, concreteSrcType.priv.literalValue.itemType)) {
17068
17448
  return true;
17069
17449
  }
17070
- if (destType.priv.literalValue !== undefined) {
17450
+ if (destType.priv.literalValue !== undefined &&
17451
+ types_1.ClassType.isSameGenericClass(destType, concreteSrcType)) {
17071
17452
  const srcLiteral = concreteSrcType.priv.literalValue;
17072
17453
  if (srcLiteral === undefined || !types_1.ClassType.isLiteralValueSame(concreteSrcType, destType)) {
17073
17454
  diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.LocAddendum.literalAssignmentMismatch().format({
@@ -17143,7 +17524,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
17143
17524
  }
17144
17525
  }
17145
17526
  }
17146
- else if ((0, types_1.isAnyOrUnknown)(concreteSrcType) && !((_j = concreteSrcType.props) === null || _j === void 0 ? void 0 : _j.specialForm)) {
17527
+ else if ((0, types_1.isAnyOrUnknown)(concreteSrcType) && !((_m = concreteSrcType.props) === null || _m === void 0 ? void 0 : _m.specialForm)) {
17147
17528
  return (flags & 16 /* AssignTypeFlags.OverloadOverlap */) === 0;
17148
17529
  }
17149
17530
  else if ((0, types_1.isUnion)(concreteSrcType)) {
@@ -17271,6 +17652,45 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
17271
17652
  diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.LocAddendum.typeAssignmentMismatch().format(printSrcDestTypes(srcType, destType)));
17272
17653
  return false;
17273
17654
  }
17655
+ // If the destination type is an explicit TypeForm type, see if the source
17656
+ // type has an implicit TypeForm type that can be assigned to it. Also see
17657
+ // of the source is a type[T] type that is assignable.
17658
+ function assignToTypeFormType(destType, srcType, constraints, flags, recursionCount) {
17659
+ var _a, _b;
17660
+ if (!(0, types_1.isClassInstance)(destType) || !types_1.ClassType.isBuiltIn(destType, 'TypeForm')) {
17661
+ return false;
17662
+ }
17663
+ const destTypeFormType = destType.priv.typeArgs && destType.priv.typeArgs.length > 0
17664
+ ? destType.priv.typeArgs[0]
17665
+ : types_1.UnknownType.create();
17666
+ let srcTypeFormType;
17667
+ // Is the source is a TypeForm type?
17668
+ if ((_a = srcType.props) === null || _a === void 0 ? void 0 : _a.typeForm) {
17669
+ srcTypeFormType = srcType.props.typeForm;
17670
+ }
17671
+ else if ((0, types_1.isClass)(srcType)) {
17672
+ if (types_1.TypeBase.isInstantiable(srcType)) {
17673
+ if (!types_1.ClassType.isSpecialBuiltIn(srcType)) {
17674
+ srcTypeFormType = types_1.ClassType.cloneAsInstance(srcType);
17675
+ }
17676
+ }
17677
+ else if (types_1.ClassType.isBuiltIn(srcType, 'type')) {
17678
+ srcTypeFormType =
17679
+ ((_b = srcType.priv.typeArgs) === null || _b === void 0 ? void 0 : _b.length) && srcType.priv.typeArgs.length > 0
17680
+ ? srcType.priv.typeArgs[0]
17681
+ : types_1.UnknownType.create();
17682
+ }
17683
+ }
17684
+ else if ((0, types_1.isTypeVar)(srcType) && types_1.TypeBase.isInstantiable(srcType)) {
17685
+ if (!(0, types_1.isTypeVarTuple)(srcType) || !srcType.priv.isInUnion) {
17686
+ srcTypeFormType = (0, typeUtils_1.convertToInstance)(srcType);
17687
+ }
17688
+ }
17689
+ if (!srcTypeFormType) {
17690
+ return false;
17691
+ }
17692
+ return assignType(destTypeFormType, srcTypeFormType, /* diag */ undefined, constraints, flags, recursionCount);
17693
+ }
17274
17694
  function assignFromUnionType(destType, srcType, diag, constraints, flags, recursionCount) {
17275
17695
  // Start by checking for an exact match. This is needed to handle unions
17276
17696
  // that contain recursive type aliases.
@@ -17410,8 +17830,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
17410
17830
  }
17411
17831
  else {
17412
17832
  // Try to assign a union of the remaining source types to
17413
- // the first destination TypeVar.
17414
- if (!assignType(isContra ? (0, types_1.combineTypes)(remainingDestSubtypes) : remainingDestSubtypes[0], isContra ? remainingSrcSubtypes[0] : (0, types_1.combineTypes)(remainingSrcSubtypes), diag === null || diag === void 0 ? void 0 : diag.createAddendum(), constraints, flags, recursionCount)) {
17833
+ // the first destination TypeVar. If this is a contravariant
17834
+ // context, use the full dest type rather than the remaining
17835
+ // dest subtypes to keep the lower bound as wide as possible.
17836
+ if (!assignType(isContra ? destType : remainingDestSubtypes[0], isContra ? remainingSrcSubtypes[0] : (0, types_1.combineTypes)(remainingSrcSubtypes), diag === null || diag === void 0 ? void 0 : diag.createAddendum(), constraints, flags, recursionCount)) {
17415
17837
  canUseFastPath = false;
17416
17838
  }
17417
17839
  }
@@ -17590,7 +18012,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
17590
18012
  // in case the destType is a union with hundreds of literals.
17591
18013
  if ((0, types_1.isClassInstance)(srcType) &&
17592
18014
  (0, typeUtils_1.isLiteralType)(srcType) &&
17593
- types_1.UnionType.containsType(destType, srcType, /* exclusionSet */ undefined, recursionCount)) {
18015
+ types_1.UnionType.containsType(destType, srcType,
18016
+ /* options */ undefined,
18017
+ /* exclusionSet */ undefined, recursionCount)) {
17594
18018
  return true;
17595
18019
  }
17596
18020
  (0, typeUtils_1.doForEachSubtype)(destType, (subtype) => {
@@ -17918,11 +18342,21 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
17918
18342
  }
17919
18343
  }
17920
18344
  }
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;
18345
+ if (destParam.defaultType) {
18346
+ if (!srcParam.defaultType && paramIndex !== srcParamDetails.argsIndex) {
18347
+ diag === null || diag === void 0 ? void 0 : diag.createAddendum().addMessage(localize_1.LocAddendum.functionParamDefaultMissing().format({
18348
+ name: srcParamName,
18349
+ }));
18350
+ canAssign = false;
18351
+ }
18352
+ // If we're performing a partial overload match and both the source
18353
+ // and dest parameters provide defaults, assume that there could
18354
+ // be a match.
18355
+ if ((flags & 32 /* AssignTypeFlags.PartialOverloadOverlap */) !== 0) {
18356
+ if (srcParam.defaultType) {
18357
+ continue;
18358
+ }
18359
+ }
17926
18360
  }
17927
18361
  // Handle the special case of an overloaded __init__ method whose self
17928
18362
  // parameter is annotated.
@@ -17993,7 +18427,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
17993
18427
  // fixed.
17994
18428
  if (defaultArgType &&
17995
18429
  !assignType(paramInfo.type, defaultArgType, diag === null || diag === void 0 ? void 0 : diag.createAddendum(), constraints, flags, recursionCount)) {
17996
- canAssign = false;
18430
+ if ((flags & 32 /* AssignTypeFlags.PartialOverloadOverlap */) === 0) {
18431
+ canAssign = false;
18432
+ }
17997
18433
  }
17998
18434
  continue;
17999
18435
  }
@@ -18051,6 +18487,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
18051
18487
  if (destParamDetails.argsIndex !== undefined && destParamDetails.argsIndex < destPositionalCount) {
18052
18488
  adjDestPositionalCount--;
18053
18489
  }
18490
+ // If we're doing a partial overload overlap check, ignore dest positional
18491
+ // params with default values.
18492
+ if ((flags & 32 /* AssignTypeFlags.PartialOverloadOverlap */) !== 0) {
18493
+ while (adjDestPositionalCount > 0 &&
18494
+ destParamDetails.params[adjDestPositionalCount - 1].defaultType) {
18495
+ adjDestPositionalCount--;
18496
+ }
18497
+ }
18054
18498
  if (srcPositionalCount < adjDestPositionalCount) {
18055
18499
  diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.LocAddendum.functionTooManyParams().format({
18056
18500
  expected: srcPositionalCount,
@@ -18141,7 +18585,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
18141
18585
  const defaultArgType = (_a = srcParamInfo.defaultType) !== null && _a !== void 0 ? _a : srcParamInfo.defaultType;
18142
18586
  if (defaultArgType &&
18143
18587
  !assignType(srcParamInfo.type, defaultArgType, diag === null || diag === void 0 ? void 0 : diag.createAddendum(), constraints, flags, recursionCount)) {
18144
- canAssign = false;
18588
+ if ((flags & 32 /* AssignTypeFlags.PartialOverloadOverlap */) === 0) {
18589
+ canAssign = false;
18590
+ }
18145
18591
  }
18146
18592
  }
18147
18593
  }
@@ -18245,9 +18691,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
18245
18691
  // is the first remaining param.
18246
18692
  }
18247
18693
  else {
18248
- remainingParams.push(types_1.FunctionParam.create(p.category, types_1.FunctionType.getParamType(effectiveSrcType, index), p.flags, p.name, types_1.FunctionType.getParamDefaultType(effectiveSrcType, index)
18249
- ? types_1.AnyType.create(/* isEllipsis */ true)
18250
- : undefined, p.defaultExpr));
18694
+ remainingParams.push(types_1.FunctionParam.create(p.category, types_1.FunctionType.getParamType(effectiveSrcType, index), p.flags, p.name, types_1.FunctionType.getParamDefaultType(effectiveSrcType, index), p.defaultExpr));
18251
18695
  }
18252
18696
  });
18253
18697
  // If there are remaining parameters and the source and dest do not contain
@@ -18998,7 +19442,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
18998
19442
  }
18999
19443
  const baseObj = (0, types_1.isClassInstance)(baseType)
19000
19444
  ? baseType
19001
- : types_1.ClassType.cloneAsInstance((0, typeUtils_1.specializeClassType)(baseType));
19445
+ : types_1.ClassType.cloneAsInstance((0, typeUtils_1.specializeWithDefaultTypeArgs)(baseType));
19002
19446
  let stripFirstParam = false;
19003
19447
  if ((0, types_1.isClassInstance)(baseType)) {
19004
19448
  stripFirstParam = true;
@@ -19107,9 +19551,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
19107
19551
  }
19108
19552
  // Perform a sanity check on the RHS expression. Some expression
19109
19553
  // forms should never be considered legitimate for type aliases.
19110
- return isLegalTypeAliasExpressionForm(decl.node.parent.d.rightExpr);
19554
+ return isLegalTypeAliasExpressionForm(decl.node.parent.d.rightExpr, /* allowStrLiteral */ false);
19111
19555
  }
19112
- function isLegalTypeAliasExpressionForm(node) {
19556
+ function isLegalTypeAliasExpressionForm(node, allowStrLiteral) {
19113
19557
  switch (node.nodeType) {
19114
19558
  case 0 /* ParseNodeType.Error */:
19115
19559
  case 55 /* ParseNodeType.UnaryOperation */:
@@ -19130,14 +19574,19 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
19130
19574
  case 34 /* ParseNodeType.List */:
19131
19575
  case 45 /* ParseNodeType.Set */:
19132
19576
  return false;
19577
+ case 48 /* ParseNodeType.StringList */:
19578
+ case 49 /* ParseNodeType.String */:
19579
+ return allowStrLiteral;
19580
+ case 14 /* ParseNodeType.Constant */:
19581
+ return node.d.constType === 26 /* KeywordType.None */;
19133
19582
  case 7 /* ParseNodeType.BinaryOperation */:
19134
19583
  return (node.d.operator === 6 /* OperatorType.BitwiseOr */ &&
19135
- isLegalTypeAliasExpressionForm(node.d.leftExpr) &&
19136
- isLegalTypeAliasExpressionForm(node.d.rightExpr));
19584
+ isLegalTypeAliasExpressionForm(node.d.leftExpr, /* allowStrLiteral */ true) &&
19585
+ isLegalTypeAliasExpressionForm(node.d.rightExpr, /* allowStrLiteral */ true));
19137
19586
  case 27 /* ParseNodeType.Index */:
19138
- return isLegalTypeAliasExpressionForm(node.d.leftExpr);
19587
+ return isLegalTypeAliasExpressionForm(node.d.leftExpr, allowStrLiteral);
19139
19588
  case 35 /* ParseNodeType.MemberAccess */:
19140
- return isLegalTypeAliasExpressionForm(node.d.leftExpr);
19589
+ return isLegalTypeAliasExpressionForm(node.d.leftExpr, allowStrLiteral);
19141
19590
  }
19142
19591
  return true;
19143
19592
  }
@@ -19216,6 +19665,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
19216
19665
  }
19217
19666
  return { sourceType: simpleSrcType, destType: simpleDestType };
19218
19667
  }
19668
+ function isTypeFormSupported(node) {
19669
+ const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
19670
+ // For now, enable only if enableExperimentalFeatures is true.
19671
+ return fileInfo.diagnosticRuleSet.enableExperimentalFeatures;
19672
+ }
19219
19673
  function printType(type, options) {
19220
19674
  let flags = evaluatorOptions.printTypeFlags;
19221
19675
  if (options === null || options === void 0 ? void 0 : options.expandTypeAlias) {
@@ -19248,7 +19702,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
19248
19702
  // call produces an expression tree that is not attached to the main parse
19249
19703
  // expression tree because we don't want to mutate the latter; the
19250
19704
  // expression tree created by this function is therefore used only temporarily.
19251
- function parseStringAsTypeAnnotation(node) {
19705
+ function parseStringAsTypeAnnotation(node, reportErrors) {
19252
19706
  const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
19253
19707
  const parser = new parser_1.Parser();
19254
19708
  const textValue = node.d.strings[0].d.value;
@@ -19269,14 +19723,21 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
19269
19723
  const parseResults = parser.parseTextExpression(dummyFileContents, valueOffset, textValue.length, parseOptions, 0 /* ParseTextMode.Expression */,
19270
19724
  /* initialParenDepth */ undefined, fileInfo.typingSymbolAliases);
19271
19725
  if (parseResults.parseTree) {
19726
+ // If there are errors but we are not reporting them, return
19727
+ // undefined to indicate that the parse failed.
19728
+ if (!reportErrors && parseResults.diagnostics.length > 0) {
19729
+ return undefined;
19730
+ }
19272
19731
  const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
19273
19732
  parseResults.diagnostics.forEach((diag) => {
19274
19733
  fileInfo.diagnosticSink.addDiagnosticWithTextRange('error', diag.message, node);
19275
19734
  });
19276
19735
  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;
19736
+ // Optionally add the new subtree to the parse tree so it can
19737
+ // participate in language server operations like find and replace.
19738
+ if (reportErrors) {
19739
+ node.d.annotation = parseResults.parseTree;
19740
+ }
19280
19741
  return parseResults.parseTree;
19281
19742
  }
19282
19743
  return undefined;
@@ -19340,8 +19801,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
19340
19801
  isAsymmetricAccessorAssignment,
19341
19802
  suppressDiagnostics,
19342
19803
  isSpecialFormClass,
19343
- getDeclarationsForStringNode,
19344
- getDeclarationsForNameNode,
19804
+ getDeclInfoForStringNode,
19805
+ getDeclInfoForNameNode,
19345
19806
  getTypeForDeclaration,
19346
19807
  resolveAliasDeclaration,
19347
19808
  resolveAliasDeclarationWithInfo,