@zzzen/pyright-internal 1.2.0-dev.20240901 → 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 (78) 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 +23 -41
  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/constraintSolver.js +2 -2
  8. package/dist/analyzer/constraintSolver.js.map +1 -1
  9. package/dist/analyzer/constructors.js +7 -1
  10. package/dist/analyzer/constructors.js.map +1 -1
  11. package/dist/analyzer/dataClasses.js +6 -1
  12. package/dist/analyzer/dataClasses.js.map +1 -1
  13. package/dist/analyzer/namedTuples.js +4 -1
  14. package/dist/analyzer/namedTuples.js.map +1 -1
  15. package/dist/analyzer/operations.js +5 -0
  16. package/dist/analyzer/operations.js.map +1 -1
  17. package/dist/analyzer/parseTreeWalker.js +1 -1
  18. package/dist/analyzer/parseTreeWalker.js.map +1 -1
  19. package/dist/analyzer/protocols.js +5 -8
  20. package/dist/analyzer/protocols.js.map +1 -1
  21. package/dist/analyzer/sourceMapper.js +2 -1
  22. package/dist/analyzer/sourceMapper.js.map +1 -1
  23. package/dist/analyzer/typeEvaluator.js +536 -155
  24. package/dist/analyzer/typeEvaluator.js.map +1 -1
  25. package/dist/analyzer/typeEvaluatorTypes.d.ts +11 -5
  26. package/dist/analyzer/typeEvaluatorTypes.js +4 -1
  27. package/dist/analyzer/typeEvaluatorTypes.js.map +1 -1
  28. package/dist/analyzer/typeGuards.js +11 -8
  29. package/dist/analyzer/typeGuards.js.map +1 -1
  30. package/dist/analyzer/typePrinter.d.ts +2 -1
  31. package/dist/analyzer/typePrinter.js +24 -4
  32. package/dist/analyzer/typePrinter.js.map +1 -1
  33. package/dist/analyzer/typeUtils.d.ts +4 -0
  34. package/dist/analyzer/typeUtils.js +54 -1
  35. package/dist/analyzer/typeUtils.js.map +1 -1
  36. package/dist/analyzer/types.d.ts +8 -4
  37. package/dist/analyzer/types.js +68 -26
  38. package/dist/analyzer/types.js.map +1 -1
  39. package/dist/commands/dumpFileDebugInfoCommand.js +0 -1
  40. package/dist/commands/dumpFileDebugInfoCommand.js.map +1 -1
  41. package/dist/languageService/callHierarchyProvider.js +2 -1
  42. package/dist/languageService/callHierarchyProvider.js.map +1 -1
  43. package/dist/languageService/completionProvider.js +12 -12
  44. package/dist/languageService/completionProvider.js.map +1 -1
  45. package/dist/languageService/definitionProvider.js +6 -6
  46. package/dist/languageService/definitionProvider.js.map +1 -1
  47. package/dist/languageService/documentSymbolCollector.js +9 -9
  48. package/dist/languageService/documentSymbolCollector.js.map +1 -1
  49. package/dist/languageService/hoverProvider.d.ts +1 -0
  50. package/dist/languageService/hoverProvider.js +30 -3
  51. package/dist/languageService/hoverProvider.js.map +1 -1
  52. package/dist/languageService/signatureHelpProvider.js +2 -2
  53. package/dist/languageService/signatureHelpProvider.js.map +1 -1
  54. package/dist/languageService/tooltipUtils.js +2 -2
  55. package/dist/languageService/tooltipUtils.js.map +1 -1
  56. package/dist/localization/localize.d.ts +2 -0
  57. package/dist/localization/localize.js +2 -0
  58. package/dist/localization/localize.js.map +1 -1
  59. package/dist/localization/package.nls.en-us.json +2 -0
  60. package/dist/parser/parseNodes.d.ts +2 -3
  61. package/dist/parser/parseNodes.js.map +1 -1
  62. package/dist/parser/parser.js +6 -20
  63. package/dist/parser/parser.js.map +1 -1
  64. package/dist/tests/checker.test.js +5 -1
  65. package/dist/tests/checker.test.js.map +1 -1
  66. package/dist/tests/importStatementUtils.test.js +1 -1
  67. package/dist/tests/importStatementUtils.test.js.map +1 -1
  68. package/dist/tests/typeEvaluator1.test.js +1 -1
  69. package/dist/tests/typeEvaluator2.test.js +13 -1
  70. package/dist/tests/typeEvaluator2.test.js.map +1 -1
  71. package/dist/tests/typeEvaluator3.test.js +1 -1
  72. package/dist/tests/typeEvaluator5.test.js +6 -0
  73. package/dist/tests/typeEvaluator5.test.js.map +1 -1
  74. package/dist/tests/typeEvaluator8.test.js +40 -0
  75. package/dist/tests/typeEvaluator8.test.js.map +1 -1
  76. package/dist/workspaceFactory.js +7 -7
  77. package/dist/workspaceFactory.js.map +1 -1
  78. package/package.json +1 -1
@@ -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,10 +901,20 @@ 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) {
906
+ if ((flags & 8 /* EvalFlags.StrLiteralAsType */) !== 0 && (flags & 1073741824 /* EvalFlags.TypeFormArg */) === 0) {
894
907
  return getTypeOfStringListAsType(node, flags);
895
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;
896
918
  let isLiteralString = true;
897
919
  let isIncomplete = false;
898
920
  node.d.strings.forEach((expr) => {
@@ -913,7 +935,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
913
935
  isLiteralString = false;
914
936
  }
915
937
  });
916
- const isBytes = (node.d.strings[0].d.token.flags & 32 /* StringTokenFlags.Bytes */) !== 0;
917
938
  // Don't create a literal type if it's an f-string.
918
939
  if (node.d.strings.some((str) => str.nodeType === 30 /* ParseNodeType.FormatString */)) {
919
940
  if (isLiteralString) {
@@ -935,6 +956,26 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
935
956
  isIncomplete,
936
957
  };
937
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
+ }
938
979
  return typeResult;
939
980
  }
940
981
  function getTypeOfStringListAsType(node, flags) {
@@ -946,6 +987,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
946
987
  if ((flags & 8388608 /* EvalFlags.ParsesStringLiteral */) === 0) {
947
988
  updatedFlags |= 524288 /* EvalFlags.NotParsed */;
948
989
  }
990
+ updatedFlags &= ~1073741824 /* EvalFlags.TypeFormArg */;
949
991
  if (node.d.annotation) {
950
992
  return getTypeOfExpression(node.d.annotation, updatedFlags);
951
993
  }
@@ -2260,13 +2302,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
2260
2302
  // If this is a member name (within a class scope) and the member name
2261
2303
  // appears to be a constant, use the strict source type. If it's a member
2262
2304
  // variable that can be overridden by a child class, use the more general
2263
- // version by stripping off the literal.
2305
+ // version by stripping off the literal and TypeForm.
2264
2306
  const scope = ScopeUtils.getScopeForNode(nameNode);
2265
2307
  if ((scope === null || scope === void 0 ? void 0 : scope.type) === 3 /* ScopeType.Class */) {
2266
2308
  if (types_1.TypeBase.isInstance(destType) &&
2267
2309
  !(0, symbolNameUtils_1.isConstantName)(nameValue) &&
2268
2310
  !isFinalVariable(symbolWithScope.symbol)) {
2269
- destType = stripLiteralValue(destType);
2311
+ destType = (0, typeUtils_1.stripTypeForm)(stripLiteralValue(destType));
2270
2312
  }
2271
2313
  }
2272
2314
  }
@@ -2567,6 +2609,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
2567
2609
  // If conditionFilter is specified and the TypeVar is a constrained
2568
2610
  // TypeVar, only the conditions that match the filter will be included.
2569
2611
  function makeTopLevelTypeVarsConcrete(type, makeParamSpecsConcrete = false, conditionFilter) {
2612
+ type = (0, typeUtils_1.transformPossibleRecursiveTypeAlias)(type);
2570
2613
  return (0, typeUtils_1.mapSubtypes)(type, (subtype) => {
2571
2614
  var _a;
2572
2615
  if ((0, types_1.isParamSpec)(subtype)) {
@@ -2869,55 +2912,53 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
2869
2912
  }
2870
2913
  function verifyRaiseExceptionType(node) {
2871
2914
  const baseExceptionType = getBuiltInType(node, 'BaseException');
2872
- if (node.d.typeExpression) {
2873
- const exceptionType = getTypeOfExpression(node.d.typeExpression).type;
2874
- // Validate that the argument of "raise" is an exception object or class.
2875
- // If it is a class, validate that the class's constructor accepts zero
2876
- // arguments.
2877
- if (exceptionType && baseExceptionType && (0, types_1.isInstantiableClass)(baseExceptionType)) {
2878
- const diagAddendum = new diagnostic_1.DiagnosticAddendum();
2879
- (0, typeUtils_1.doForEachSubtype)(exceptionType, (subtype) => {
2880
- const concreteSubtype = makeTopLevelTypeVarsConcrete(subtype);
2881
- if (!(0, types_1.isAnyOrUnknown)(concreteSubtype)) {
2882
- if ((0, types_1.isInstantiableClass)(concreteSubtype) && concreteSubtype.priv.literalValue === undefined) {
2883
- if (!(0, typeUtils_1.derivesFromClassRecursive)(concreteSubtype, baseExceptionType,
2884
- /* ignoreUnknown */ false)) {
2885
- diagAddendum.addMessage(localize_1.LocMessage.exceptionTypeIncorrect().format({
2886
- type: printType(subtype),
2887
- }));
2888
- }
2889
- else {
2890
- let callResult;
2891
- suppressDiagnostics(node.d.typeExpression, () => {
2892
- callResult = (0, constructors_1.validateConstructorArgs)(evaluatorInterface, node.d.typeExpression, [], concreteSubtype,
2893
- /* skipUnknownArgCheck */ false,
2894
- /* inferenceContext */ undefined);
2895
- });
2896
- if (callResult && callResult.argumentErrors) {
2897
- diagAddendum.addMessage(localize_1.LocMessage.exceptionTypeNotInstantiable().format({
2898
- type: printType(subtype),
2899
- }));
2900
- }
2901
- }
2902
- }
2903
- else if ((0, types_1.isClassInstance)(concreteSubtype)) {
2904
- if (!(0, typeUtils_1.derivesFromClassRecursive)(types_1.ClassType.cloneAsInstantiable(concreteSubtype), baseExceptionType,
2905
- /* ignoreUnknown */ false)) {
2906
- diagAddendum.addMessage(localize_1.LocMessage.exceptionTypeIncorrect().format({
2907
- type: printType(subtype),
2908
- }));
2909
- }
2910
- }
2911
- else {
2912
- 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({
2913
2941
  type: printType(subtype),
2914
2942
  }));
2915
2943
  }
2916
2944
  }
2917
- });
2918
- if (!diagAddendum.isEmpty()) {
2919
- addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.expectedExceptionClass() + diagAddendum.getString(), node.d.typeExpression);
2920
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);
2921
2962
  }
2922
2963
  }
2923
2964
  }
@@ -3073,6 +3114,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
3073
3114
  if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0) {
3074
3115
  type = validateSymbolIsTypeExpression(node, type, !!effectiveTypeInfo.includesVariableDecl);
3075
3116
  }
3117
+ // Add TypeForm details if appropriate.
3118
+ type = addTypeFormForSymbol(node, type, flags, !!effectiveTypeInfo.includesVariableDecl);
3076
3119
  }
3077
3120
  else {
3078
3121
  // Handle the special case of "reveal_type" and "reveal_locals".
@@ -3106,19 +3149,67 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
3106
3149
  }
3107
3150
  return { type, isIncomplete };
3108
3151
  }
3109
- // Reports diagnostics if type isn't valid within a type expression.
3110
- function validateSymbolIsTypeExpression(node, type, includesVariableDecl) {
3111
- var _a;
3112
- // Verify that the name does not refer to a (non type alias) variable.
3113
- 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)) {
3114
3155
  return type;
3115
3156
  }
3116
- 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
+ }
3117
3166
  return type;
3118
3167
  }
3119
- // Exempts class types that are created by calling
3120
- // NewType, NamedTuple, etc.
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) {
3171
+ return type;
3172
+ }
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.
3121
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)) {
3122
3213
  return type;
3123
3214
  }
3124
3215
  // Disable for assignments in the typings.pyi file, since it defines special forms.
@@ -3133,7 +3224,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
3133
3224
  // evaluated in a value expression context, convert it from its special
3134
3225
  // meaning to its runtime value.
3135
3226
  function convertSpecialFormToRuntimeValue(type, flags) {
3136
- var _a, _b;
3227
+ var _a, _b, _c, _d;
3137
3228
  const exemptFlags = 256 /* EvalFlags.TypeExpression */ | 128 /* EvalFlags.InstantiableType */ | 33554432 /* EvalFlags.NoConvertSpecialForm */;
3138
3229
  if ((flags & exemptFlags) !== 0) {
3139
3230
  return type;
@@ -3148,6 +3239,16 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
3148
3239
  if (!((_b = type.props) === null || _b === void 0 ? void 0 : _b.specialForm)) {
3149
3240
  return type;
3150
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
+ }
3151
3252
  return type.props.specialForm;
3152
3253
  }
3153
3254
  // Handles the case where a variable or parameter is defined in an outer
@@ -3257,7 +3358,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
3257
3358
  if ((0, types_1.isUnpackedTypeVarTuple)(type)) {
3258
3359
  type = types_1.TypeVarType.cloneForPacked(type);
3259
3360
  }
3260
- if ((flags & 1073741824 /* EvalFlags.EnforceClassTypeVarScope */) !== 0 && !enforceClassTypeVarScope(node, type)) {
3361
+ if ((flags & -2147483648 /* EvalFlags.EnforceClassTypeVarScope */) !== 0 && !enforceClassTypeVarScope(node, type)) {
3261
3362
  return types_1.UnknownType.create();
3262
3363
  }
3263
3364
  return type;
@@ -3720,6 +3821,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
3720
3821
  if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0) {
3721
3822
  type = validateSymbolIsTypeExpression(node, type, !!typeResult.includesVariableDecl);
3722
3823
  }
3824
+ // Add TypeForm details if appropriate.
3825
+ type = addTypeFormForSymbol(node, type, flags, !!typeResult.includesVariableDecl);
3723
3826
  if ((0, types_1.isTypeVar)(type)) {
3724
3827
  type = validateTypeVarUsage(node, type, flags);
3725
3828
  }
@@ -4083,9 +4186,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
4083
4186
  }
4084
4187
  // Check for an attempt to overwrite or delete an instance variable that is
4085
4188
  // read-only (e.g. in a named tuple).
4086
- if ((memberInfo === null || memberInfo === void 0 ? void 0 : memberInfo.isInstanceMember) &&
4087
- (0, types_1.isClass)(memberInfo.classType) &&
4088
- 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) {
4089
4190
  diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.LocAddendum.readOnlyAttribute().format({ name: memberName }));
4090
4191
  isDescriptorError = true;
4091
4192
  }
@@ -4744,6 +4845,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
4744
4845
  }
4745
4846
  const typeParams = aliasInfo.typeParams;
4746
4847
  let typeArgs = adjustTypeArgsForTypeVarTuple(getTypeArgs(node, flags), typeParams, node);
4848
+ let reportedError = false;
4747
4849
  // PEP 612 says that if the class has only one type parameter consisting
4748
4850
  // of a ParamSpec, the list of arguments does not need to be enclosed in
4749
4851
  // a list. We'll handle that case specially here. Presumably this applies to
@@ -4770,6 +4872,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
4770
4872
  expected: typeParams.length,
4771
4873
  received: typeArgs.length,
4772
4874
  }), typeArgs[typeParams.length].node);
4875
+ reportedError = true;
4773
4876
  }
4774
4877
  else if (typeArgs.length < minTypeArgCount) {
4775
4878
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgsTooFew().format({
@@ -4777,6 +4880,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
4777
4880
  expected: typeParams.length,
4778
4881
  received: typeArgs.length,
4779
4882
  }), node.d.items[node.d.items.length - 1]);
4883
+ reportedError = true;
4780
4884
  }
4781
4885
  // Handle the mypy_extensions.FlexibleAlias type specially.
4782
4886
  if ((0, types_1.isInstantiableClass)(baseType) &&
@@ -4832,11 +4936,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
4832
4936
  }
4833
4937
  else {
4834
4938
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgListExpected(), typeArgs[index].node);
4939
+ reportedError = true;
4835
4940
  }
4836
4941
  }
4837
4942
  else {
4838
4943
  if (index < typeArgs.length && typeArgs[index].typeList) {
4839
4944
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgListNotAllowed(), typeArgs[index].node);
4945
+ reportedError = true;
4840
4946
  }
4841
4947
  let typeArgType;
4842
4948
  if (index < typeArgs.length) {
@@ -4878,6 +4984,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
4878
4984
  });
4879
4985
  if (!diag.isEmpty()) {
4880
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;
4881
4988
  }
4882
4989
  const solutionSet = (0, constraintSolver_1.solveConstraints)(evaluatorInterface, constraints).getMainSolutionSet();
4883
4990
  const aliasTypeArgs = [];
@@ -4890,18 +4997,43 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
4890
4997
  }
4891
4998
  aliasTypeArgs.push(typeVarType);
4892
4999
  });
4893
- const type = types_1.TypeBase.cloneForTypeAlias(solveAndApplyConstraints(baseType, constraints), {
5000
+ let type = types_1.TypeBase.cloneForTypeAlias(solveAndApplyConstraints(baseType, constraints), {
4894
5001
  ...aliasInfo,
4895
5002
  typeArgs: aliasTypeArgs,
4896
5003
  });
5004
+ if (isTypeFormSupported(node)) {
5005
+ type = types_1.TypeBase.cloneWithTypeForm(type, reportedError ? undefined : (0, typeUtils_1.convertToInstance)(type));
5006
+ }
4897
5007
  return { type, node };
4898
5008
  }
4899
5009
  function getTypeOfIndexWithBaseType(node, baseTypeResult, usage, flags) {
5010
+ var _a, _b, _c;
4900
5011
  // Handle the case where we're specializing a generic type alias.
4901
5012
  const typeAliasResult = createSpecializedTypeAlias(node, baseTypeResult.type, flags);
4902
5013
  if (typeAliasResult) {
4903
5014
  return typeAliasResult;
4904
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
+ }
4905
5037
  if ((0, types_1.isTypeVar)(baseTypeResult.type) && (0, typeUtils_1.isTypeAliasPlaceholder)(baseTypeResult.type)) {
4906
5038
  const typeArgTypes = getTypeArgs(node, flags).map((t) => (0, typeUtils_1.convertToInstance)(t.type));
4907
5039
  const type = types_1.TypeBase.cloneForTypeAlias(baseTypeResult.type, {
@@ -5324,6 +5456,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5324
5456
  function getTypeArgs(node, flags, options) {
5325
5457
  const typeArgs = [];
5326
5458
  let adjFlags = flags | 33554432 /* EvalFlags.NoConvertSpecialForm */;
5459
+ adjFlags &= ~1073741824 /* EvalFlags.TypeFormArg */;
5327
5460
  const allowFinalClassVar = () => {
5328
5461
  // If the annotation is a variable within the body of a dataclass, a
5329
5462
  // Final is allowed with a ClassVar annotation. In all other cases,
@@ -5558,7 +5691,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5558
5691
  }
5559
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)));
5560
5693
  const isIncomplete = entryTypeResults.some((result) => result.isIncomplete);
5561
- const type = makeTupleObject(buildTupleTypesList(entryTypeResults, /* stripLiterals */ false));
5562
5694
  // Copy any expected type diag addenda for precision error reporting.
5563
5695
  let expectedTypeDiagAddendum;
5564
5696
  if (entryTypeResults.some((result) => result.expectedTypeDiagAddendum)) {
@@ -5569,11 +5701,27 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5569
5701
  }
5570
5702
  });
5571
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
+ }
5572
5714
  return { type, expectedTypeDiagAddendum, isIncomplete };
5573
5715
  }
5574
5716
  function getTypeOfTupleInferred(node, flags) {
5575
5717
  const entryTypeResults = node.d.items.map((expr) => getTypeOfExpression(expr, flags | 268435456 /* EvalFlags.StripTupleLiterals */));
5576
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
+ }
5577
5725
  const type = makeTupleObject(buildTupleTypesList(entryTypeResults, (flags & 268435456 /* EvalFlags.StripTupleLiterals */) !== 0));
5578
5726
  if (isIncomplete) {
5579
5727
  if ((0, typeUtils_1.getContainerDepth)(type) > maxInferredContainerDepth) {
@@ -5611,7 +5759,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5611
5759
  entryTypes.push({ type: types_1.UnknownType.create(/* isIncomplete */ true), isUnbounded: false });
5612
5760
  }
5613
5761
  else {
5614
- 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;
5615
5764
  entryTypes.push({ type: entryType, isUnbounded: !!typeResult.unpackedType });
5616
5765
  }
5617
5766
  }
@@ -5675,6 +5824,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5675
5824
  // Handle the "typing.assert_type" call.
5676
5825
  typeResult = getTypeOfAssertType(node, inferenceContext);
5677
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
+ }
5678
5831
  else if ((0, types_1.isAnyOrUnknown)(baseTypeResult.type) &&
5679
5832
  node.d.leftExpr.nodeType === 38 /* ParseNodeType.Name */ &&
5680
5833
  node.d.leftExpr.d.value === 'reveal_locals') {
@@ -5778,7 +5931,21 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5778
5931
  }
5779
5932
  return typeResult;
5780
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
+ }
5781
5947
  function getTypeOfAssertType(node, inferenceContext) {
5948
+ var _a;
5782
5949
  if (node.d.args.length !== 2 ||
5783
5950
  node.d.args[0].d.argCategory !== 0 /* ArgCategory.Simple */ ||
5784
5951
  node.d.args[0].d.name !== undefined ||
@@ -5798,7 +5965,24 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5798
5965
  // The spec is unclear on whether this is the correct behavior, but it seems to be
5799
5966
  // what mypy does -- and what various library authors expect.
5800
5967
  const arg0Type = stripTypeGuard(arg0TypeResult.type);
5801
- 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) {
5802
5986
  const srcDestTypes = printSrcDestTypes(arg0TypeResult.type, assertedType, { expandTypeAlias: true });
5803
5987
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportAssertTypeFailure, localize_1.LocMessage.assertTypeTypeMismatch().format({
5804
5988
  expected: srcDestTypes.destType,
@@ -5944,30 +6128,33 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
5944
6128
  }
5945
6129
  const concreteTargetClassType = makeTopLevelTypeVarsConcrete(targetClassType);
5946
6130
  // Determine whether to further narrow the type.
6131
+ let secondArgType;
5947
6132
  let bindToType;
5948
6133
  if (node.d.args.length > 1) {
5949
- 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);
5950
6136
  let reportError = false;
5951
- if ((0, types_1.isAnyOrUnknown)(secondArgType)) {
6137
+ if ((0, types_1.isAnyOrUnknown)(secondArgConcreteType)) {
5952
6138
  // Ignore unknown or any types.
5953
6139
  }
5954
- else if ((0, types_1.isClassInstance)(secondArgType)) {
6140
+ else if ((0, types_1.isClassInstance)(secondArgConcreteType)) {
5955
6141
  if ((0, types_1.isInstantiableClass)(concreteTargetClassType)) {
5956
- if (!(0, typeUtils_1.derivesFromClassRecursive)(types_1.ClassType.cloneAsInstantiable(secondArgType), concreteTargetClassType,
6142
+ if (!(0, typeUtils_1.derivesFromClassRecursive)(types_1.ClassType.cloneAsInstantiable(secondArgConcreteType), concreteTargetClassType,
5957
6143
  /* ignoreUnknown */ true)) {
5958
6144
  reportError = true;
5959
6145
  }
5960
6146
  }
5961
- bindToType = secondArgType;
6147
+ bindToType = secondArgConcreteType;
5962
6148
  }
5963
- else if ((0, types_1.isInstantiableClass)(secondArgType)) {
6149
+ else if ((0, types_1.isInstantiableClass)(secondArgConcreteType)) {
5964
6150
  if ((0, types_1.isInstantiableClass)(concreteTargetClassType)) {
5965
6151
  if (!types_1.ClassType.isBuiltIn(concreteTargetClassType, 'type') &&
5966
- !(0, typeUtils_1.derivesFromClassRecursive)(secondArgType, concreteTargetClassType, /* ignoreUnknown */ true)) {
6152
+ !(0, typeUtils_1.derivesFromClassRecursive)(secondArgConcreteType, concreteTargetClassType,
6153
+ /* ignoreUnknown */ true)) {
5967
6154
  reportError = true;
5968
6155
  }
5969
6156
  }
5970
- bindToType = secondArgType;
6157
+ bindToType = secondArgConcreteType;
5971
6158
  }
5972
6159
  else {
5973
6160
  reportError = true;
@@ -6069,10 +6256,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
6069
6256
  }
6070
6257
  let bindToSelfType;
6071
6258
  if (bindToType) {
6072
- if (node.d.args.length > 1) {
6073
- // If this is a two-argument form of super(), use the
6074
- // bindToType evaluated from the arguments.
6075
- 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
+ }
6076
6265
  }
6077
6266
  else {
6078
6267
  // If this is a zero-argument form of super(), synthesize
@@ -7145,7 +7334,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
7145
7334
  // validation is left to the caller.
7146
7335
  // This logic is based on PEP 3102: https://www.python.org/dev/peps/pep-3102/
7147
7336
  function matchArgsToParams(errorNode, argList, typeResult, overloadIndex) {
7148
- var _a, _b, _c, _d, _e, _f, _g, _h;
7337
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
7149
7338
  const overload = typeResult.type;
7150
7339
  const paramDetails = (0, parameterUtils_1.getParamListDetails)(overload);
7151
7340
  const paramSpec = types_1.FunctionType.getParamSpecFromArgsKwargs(overload);
@@ -7562,9 +7751,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
7562
7751
  else if ((0, types_1.isClassInstance)(argType) && types_1.ClassType.isTypedDictClass(argType)) {
7563
7752
  // Handle the special case where it is a TypedDict and we know which
7564
7753
  // keys are present.
7565
- const typedDictEntries = (0, typedDicts_1.getTypedDictMembersForClass)(evaluatorInterface, argType);
7754
+ const tdEntries = (0, typedDicts_1.getTypedDictMembersForClass)(evaluatorInterface, argType);
7566
7755
  const diag = new diagnostic_1.DiagnosticAddendum();
7567
- typedDictEntries.knownItems.forEach((entry, name) => {
7756
+ tdEntries.knownItems.forEach((entry, name) => {
7757
+ var _a, _b;
7568
7758
  const paramEntry = paramMap.get(name);
7569
7759
  if (paramEntry && !paramEntry.isPositionalOnly) {
7570
7760
  if (paramEntry.argsReceived > 0) {
@@ -7583,7 +7773,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
7583
7773
  argCategory: 0 /* ArgCategory.Simple */,
7584
7774
  typeResult: { type: entry.valueType },
7585
7775
  },
7586
- errorNode: argList[argIndex].valueExpression || errorNode,
7776
+ errorNode: (_a = argList[argIndex].valueExpression) !== null && _a !== void 0 ? _a : errorNode,
7587
7777
  paramName: name,
7588
7778
  });
7589
7779
  }
@@ -7598,7 +7788,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
7598
7788
  argCategory: 0 /* ArgCategory.Simple */,
7599
7789
  typeResult: { type: entry.valueType },
7600
7790
  },
7601
- errorNode: argList[argIndex].valueExpression || errorNode,
7791
+ errorNode: (_b = argList[argIndex].valueExpression) !== null && _b !== void 0 ? _b : errorNode,
7602
7792
  paramName: name,
7603
7793
  });
7604
7794
  // Remember that this parameter has already received a value.
@@ -7618,6 +7808,23 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
7618
7808
  }
7619
7809
  }
7620
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
+ }
7621
7828
  if (!diag.isEmpty()) {
7622
7829
  if (!canSkipDiagnosticForNode(errorNode) && !isTypeIncomplete) {
7623
7830
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportCallIssue, localize_1.LocMessage.unpackedTypedDictArgument() + diag.getString(), argList[argIndex].valueExpression || errorNode);
@@ -7724,7 +7931,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
7724
7931
  paramType,
7725
7932
  requiresTypeVarMatching: (0, typeUtils_1.requiresSpecialization)(paramType),
7726
7933
  argument: argList[argIndex],
7727
- errorNode: (_f = argList[argIndex].valueExpression) !== null && _f !== void 0 ? _f : errorNode,
7934
+ errorNode: (_j = argList[argIndex].valueExpression) !== null && _j !== void 0 ? _j : errorNode,
7728
7935
  paramName: paramNameValue,
7729
7936
  });
7730
7937
  trySetActive(argList[argIndex], paramDetails.params[paramInfoIndex].param);
@@ -7747,7 +7954,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
7747
7954
  paramType,
7748
7955
  requiresTypeVarMatching: (0, typeUtils_1.requiresSpecialization)(paramType),
7749
7956
  argument: argList[argIndex],
7750
- errorNode: (_g = argList[argIndex].valueExpression) !== null && _g !== void 0 ? _g : errorNode,
7957
+ errorNode: (_k = argList[argIndex].valueExpression) !== null && _k !== void 0 ? _k : errorNode,
7751
7958
  paramName: paramNameValue,
7752
7959
  });
7753
7960
  // Remember that this parameter has already received a value.
@@ -7798,7 +8005,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
7798
8005
  requiresTypeVarMatching: false,
7799
8006
  argument: argList[argIndex],
7800
8007
  argType: (0, types_1.isParamSpec)(argType) ? undefined : types_1.AnyType.create(),
7801
- errorNode: (_h = argList[argIndex].valueExpression) !== null && _h !== void 0 ? _h : errorNode,
8008
+ errorNode: (_l = argList[argIndex].valueExpression) !== null && _l !== void 0 ? _l : errorNode,
7802
8009
  });
7803
8010
  }
7804
8011
  }
@@ -9208,6 +9415,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
9208
9415
  if (node.d.constType === 26 /* KeywordType.None */) {
9209
9416
  if (noneTypeClass) {
9210
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
+ }
9211
9421
  }
9212
9422
  }
9213
9423
  else if (node.d.constType === 33 /* KeywordType.True */ ||
@@ -9472,9 +9682,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
9472
9682
  if (keyValueResult.typeErrors) {
9473
9683
  typeErrors = true;
9474
9684
  }
9475
- // Strip any literal values.
9476
- const keyTypes = keyTypeResults.map((t) => stripLiteralValue(t.type));
9477
- 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)));
9478
9688
  keyType = keyTypes.length > 0 ? (0, types_1.combineTypes)(keyTypes) : fallbackType;
9479
9689
  // If the value type differs and we're not using "strict inference mode",
9480
9690
  // we need to back off because we can't properly represent the mappings
@@ -9825,6 +10035,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
9825
10035
  else {
9826
10036
  entryTypeResult = getTypeOfExpression(entry, flags | 268435456 /* EvalFlags.StripTupleLiterals */);
9827
10037
  }
10038
+ entryTypeResult.type = (0, typeUtils_1.stripTypeForm)(convertSpecialFormToRuntimeValue(entryTypeResult.type, flags));
9828
10039
  if (entryTypeResult.isIncomplete) {
9829
10040
  isIncomplete = true;
9830
10041
  }
@@ -10359,8 +10570,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10359
10570
  // a ParamSpec, a Concatenate, or a list of positional parameter types.
10360
10571
  // The second argument, if present, should specify the return type.
10361
10572
  function createCallableType(classType, typeArgs, errorNode) {
10362
- const functionType = types_1.FunctionType.createInstantiable(0 /* FunctionTypeFlags.None */);
10573
+ let functionType = types_1.FunctionType.createInstantiable(0 /* FunctionTypeFlags.None */);
10363
10574
  let paramSpec;
10575
+ let isValidTypeForm = true;
10364
10576
  types_1.TypeBase.setSpecialForm(functionType, types_1.ClassType.cloneAsInstance(classType));
10365
10577
  functionType.shared.declaredReturnType = types_1.UnknownType.create();
10366
10578
  functionType.shared.typeVarScopeId = ParseTreeUtils.getScopeIdForNode(errorNode);
@@ -10376,6 +10588,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10376
10588
  if (!reportedUnpackedError) {
10377
10589
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.variadicTypeArgsTooMany(), entry.node);
10378
10590
  reportedUnpackedError = true;
10591
+ isValidTypeForm = false;
10379
10592
  }
10380
10593
  }
10381
10594
  sawUnpacked = true;
@@ -10437,6 +10650,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10437
10650
  }
10438
10651
  else {
10439
10652
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.callableFirstArg(), typeArgs[0].node);
10653
+ isValidTypeForm = false;
10440
10654
  }
10441
10655
  }
10442
10656
  if (typeArgs.length > 1) {
@@ -10449,22 +10663,31 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10449
10663
  else {
10450
10664
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportMissingTypeArgument, localize_1.LocMessage.callableSecondArg(), errorNode);
10451
10665
  functionType.shared.declaredReturnType = types_1.UnknownType.create();
10666
+ isValidTypeForm = false;
10452
10667
  }
10453
10668
  if (typeArgs.length > 2) {
10454
10669
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.callableExtraArgs(), typeArgs[2].node);
10670
+ isValidTypeForm = false;
10455
10671
  }
10456
10672
  }
10457
10673
  else {
10458
10674
  types_1.FunctionType.addDefaultParams(functionType, /* useUnknown */ true);
10459
10675
  functionType.shared.flags |= 32768 /* FunctionTypeFlags.GradualCallableForm */;
10676
+ if (typeArgs && typeArgs.length === 0) {
10677
+ isValidTypeForm = false;
10678
+ }
10460
10679
  }
10461
10680
  if (paramSpec) {
10462
10681
  types_1.FunctionType.addParamSpecVariadics(functionType, (0, typeUtils_1.convertToInstance)(paramSpec));
10463
10682
  }
10683
+ if (isTypeFormSupported(errorNode) && isValidTypeForm) {
10684
+ functionType = types_1.TypeBase.cloneWithTypeForm(functionType, (0, typeUtils_1.convertToInstance)(functionType));
10685
+ }
10464
10686
  return functionType;
10465
10687
  }
10466
10688
  // Creates an Optional[X] type.
10467
10689
  function createOptionalType(classType, errorNode, typeArgs, flags) {
10690
+ var _a;
10468
10691
  if (!typeArgs) {
10469
10692
  // If no type arguments are provided, the resulting type
10470
10693
  // depends on whether we're evaluating a type annotation or
@@ -10487,6 +10710,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10487
10710
  if (unionTypeClass && (0, types_1.isInstantiableClass)(unionTypeClass)) {
10488
10711
  optionalType = types_1.TypeBase.cloneAsSpecialForm(optionalType, types_1.ClassType.cloneAsInstance(unionTypeClass));
10489
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
+ }
10490
10720
  return optionalType;
10491
10721
  }
10492
10722
  function cloneBuiltinObjectWithLiteral(node, builtInName, value) {
@@ -10605,6 +10835,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10605
10835
  if ((0, types_1.isUnion)(result) && unionTypeClass && (0, types_1.isInstantiableClass)(unionTypeClass)) {
10606
10836
  result = types_1.TypeBase.cloneAsSpecialForm(result, types_1.ClassType.cloneAsInstance(unionTypeClass));
10607
10837
  }
10838
+ if (isTypeFormSupported(node)) {
10839
+ result = types_1.TypeBase.cloneWithTypeForm(result, (0, typeUtils_1.convertToInstance)(result));
10840
+ }
10608
10841
  return result;
10609
10842
  }
10610
10843
  // Creates a ClassVar type.
@@ -10634,11 +10867,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10634
10867
  return type;
10635
10868
  }
10636
10869
  function createTypeFormType(classType, errorNode, typeArgs) {
10637
- // If no type arguments are provided, the resulting type
10638
- // depends on whether we're evaluating a type annotation or
10639
- // we're in some other context.
10640
- if (!typeArgs) {
10641
- return classType;
10870
+ if (!typeArgs || typeArgs.length === 0) {
10871
+ return types_1.ClassType.specialize(classType, [types_1.UnknownType.create()]);
10642
10872
  }
10643
10873
  if (typeArgs.length > 1) {
10644
10874
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgsTooMany().format({
@@ -10651,7 +10881,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10651
10881
  const convertedTypeArgs = typeArgs.map((typeArg) => {
10652
10882
  return (0, typeUtils_1.convertToInstance)(validateTypeArg(typeArg) ? typeArg.type : types_1.UnknownType.create());
10653
10883
  });
10654
- const resultType = types_1.ClassType.specialize(classType, convertedTypeArgs);
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
+ }
10655
10888
  return resultType;
10656
10889
  }
10657
10890
  // Creates a "TypeGuard" and "TypeIs" type.
@@ -10672,7 +10905,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10672
10905
  const convertedTypeArgs = typeArgs.map((typeArg) => {
10673
10906
  return (0, typeUtils_1.convertToInstance)(validateTypeArg(typeArg) ? typeArg.type : types_1.UnknownType.create());
10674
10907
  });
10675
- const resultType = 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
+ }
10676
10912
  return resultType;
10677
10913
  }
10678
10914
  function createSelfType(classType, errorNode, typeArgs, flags) {
@@ -10892,10 +11128,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
10892
11128
  return createSpecialType(classType, typeArgs, /* paramLimit */ undefined, /* allowParamSpec */ true);
10893
11129
  }
10894
11130
  function createAnnotatedType(classType, errorNode, typeArgs, flags) {
11131
+ var _a;
10895
11132
  let type;
10896
11133
  const typeExprFlags = 256 /* EvalFlags.TypeExpression */ | 33554432 /* EvalFlags.NoConvertSpecialForm */;
10897
11134
  if ((flags & typeExprFlags) === 0) {
10898
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
+ }
10899
11139
  return { type };
10900
11140
  }
10901
11141
  if (typeArgs && typeArgs.length > 0) {
@@ -11085,9 +11325,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
11085
11325
  }
11086
11326
  // Unpacks the index expression for a "Union[X, Y, Z]" type annotation.
11087
11327
  function createUnionType(classType, errorNode, typeArgs, flags) {
11328
+ var _a;
11088
11329
  const fileInfo = AnalyzerNodeInfo.getFileInfo(errorNode);
11089
11330
  const types = [];
11090
11331
  let allowSingleTypeArg = false;
11332
+ let isValidTypeForm = true;
11091
11333
  if (!typeArgs) {
11092
11334
  // If no type arguments are provided, the resulting type
11093
11335
  // depends on whether we're evaluating a type annotation or
@@ -11117,6 +11359,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
11117
11359
  else {
11118
11360
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.unionUnpackedTypeVarTuple(), errorNode);
11119
11361
  typeArgType = types_1.UnknownType.create();
11362
+ isValidTypeForm = false;
11120
11363
  }
11121
11364
  }
11122
11365
  types.push(typeArgType);
@@ -11126,11 +11369,21 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
11126
11369
  // since it is used to define NoReturn in typeshed stubs).
11127
11370
  if (types.length === 1 && !allowSingleTypeArg && !(0, typeUtils_1.isNoneInstance)(types[0])) {
11128
11371
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeArguments, localize_1.LocMessage.unionTypeArgCount(), errorNode);
11372
+ isValidTypeForm = false;
11129
11373
  }
11130
11374
  let unionType = (0, types_1.combineTypes)(types, { skipElideRedundantLiterals: true });
11131
11375
  if (unionTypeClass && (0, types_1.isInstantiableClass)(unionTypeClass)) {
11132
11376
  unionType = types_1.TypeBase.cloneAsSpecialForm(unionType, types_1.ClassType.cloneAsInstance(unionTypeClass));
11133
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
+ }
11134
11387
  return unionType;
11135
11388
  }
11136
11389
  // Creates a type that represents "Generic[T1, T2, ...]", used in the
@@ -11167,6 +11420,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
11167
11420
  return createSpecialType(classType, typeArgs, /* paramLimit */ undefined, /* allowParamSpec */ true);
11168
11421
  }
11169
11422
  function transformTypeForTypeAlias(type, name, errorNode, isPep695Syntax, isPep695TypeVarType, typeParams, typeParamNodes) {
11423
+ var _a;
11170
11424
  if (!types_1.TypeBase.isInstantiable(type)) {
11171
11425
  return type;
11172
11426
  }
@@ -11248,6 +11502,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
11248
11502
  typeAlias = types_1.TypeBase.cloneAsSpecialForm(typeAlias, types_1.ClassType.cloneAsInstance(typeAliasTypeClass));
11249
11503
  }
11250
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
+ }
11251
11509
  return typeAlias;
11252
11510
  }
11253
11511
  function createSpecialBuiltInClass(node, assignedName, aliasMapEntry) {
@@ -11375,15 +11633,24 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
11375
11633
  if (aliasMapEntry) {
11376
11634
  const cachedType = readTypeCache(node, 0 /* EvalFlags.None */);
11377
11635
  if (cachedType) {
11378
- (0, debug_1.assert)((0, types_1.isInstantiableClass)(cachedType));
11379
11636
  return cachedType;
11380
11637
  }
11381
- const specialType = createSpecialBuiltInClass(node, assignedName, aliasMapEntry);
11638
+ let specialType = createSpecialBuiltInClass(node, assignedName, aliasMapEntry);
11382
11639
  // Handle 'LiteralString' specially because we want it to act as
11383
11640
  // though it derives from 'str'.
11384
11641
  if (assignedName === 'LiteralString') {
11385
11642
  specialType.shared.baseClasses.push(strClass !== null && strClass !== void 0 ? strClass : types_1.AnyType.create());
11386
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
+ }
11387
11654
  }
11388
11655
  writeTypeCache(node, { type: specialType }, 0 /* EvalFlags.None */);
11389
11656
  return specialType;
@@ -11475,7 +11742,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
11475
11742
  64 /* EvalFlags.NoTypeVarTuple */ |
11476
11743
  131072 /* EvalFlags.NoClassVar */;
11477
11744
  typeAliasNameNode = node.d.leftExpr.d.valueExpr;
11478
- if (!isLegalTypeAliasExpressionForm(node.d.rightExpr)) {
11745
+ if (!isLegalTypeAliasExpressionForm(node.d.rightExpr, /* allowStrLiteral */ true)) {
11479
11746
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeAliasIllegalExpressionForm(), node.d.rightExpr);
11480
11747
  }
11481
11748
  }
@@ -11783,6 +12050,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
11783
12050
  if ((0, types_1.isAny)(argType) && ((_b = argType.props) === null || _b === void 0 ? void 0 : _b.specialForm)) {
11784
12051
  argType = types_1.AnyType.create();
11785
12052
  }
12053
+ argType = (0, typeUtils_1.stripTypeFormRecursive)(argType);
11786
12054
  if (!(0, types_1.isAnyOrUnknown)(argType) && !(0, types_1.isUnbound)(argType)) {
11787
12055
  if ((0, typeUtils_1.isMetaclassInstance)(argType)) {
11788
12056
  (0, debug_1.assert)((0, types_1.isClassInstance)(argType));
@@ -11819,7 +12087,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
11819
12087
  if (pythonVersion_1.PythonVersion.isGreaterOrEqualTo(fileInfo.executionEnvironment.pythonVersion, pythonVersion_1.pythonVersion3_6)) {
11820
12088
  if (types_1.ClassType.isBuiltIn(argType, 'NamedTuple')) {
11821
12089
  isNamedTupleSubclass = true;
11822
- classType.shared.flags |= 524288 /* ClassTypeFlags.ReadOnlyInstanceVariables */;
11823
12090
  }
11824
12091
  }
11825
12092
  // If the class directly derives from TypedDict or from a class that is
@@ -12097,7 +12364,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
12097
12364
  if ((0, enums_1.isEnumMetaclass)(metaclassType)) {
12098
12365
  classType.shared.flags |= 65536 /* ClassTypeFlags.EnumClass */;
12099
12366
  }
12100
- if (types_1.ClassType.isBuiltIn(metaclassType, 'ABCMeta')) {
12367
+ if ((0, typeUtils_1.derivesFromStdlibClass)(metaclassType, 'ABCMeta')) {
12101
12368
  classType.shared.flags |= 64 /* ClassTypeFlags.SupportsAbstractMethods */;
12102
12369
  }
12103
12370
  }
@@ -13103,7 +13370,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
13103
13370
  skipInference = true;
13104
13371
  }
13105
13372
  if (!skipInference) {
13106
- inferredParamType = stripLiteralValue(defaultValueType);
13373
+ inferredParamType = convertSpecialFormToRuntimeValue(defaultValueType, 0 /* EvalFlags.None */);
13374
+ inferredParamType = (0, typeUtils_1.stripTypeForm)(inferredParamType);
13375
+ inferredParamType = stripLiteralValue(inferredParamType);
13107
13376
  }
13108
13377
  }
13109
13378
  if (inferredParamType) {
@@ -13276,6 +13545,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
13276
13545
  }
13277
13546
  return subtype;
13278
13547
  });
13548
+ // Do not retain TypeForm types in inferred return types.
13549
+ returnType = (0, typeUtils_1.stripTypeForm)(returnType);
13279
13550
  inferredReturnTypes.push(returnType);
13280
13551
  }
13281
13552
  else {
@@ -13384,10 +13655,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
13384
13655
  return false;
13385
13656
  }
13386
13657
  for (const raiseStatement of functionDecl.raiseStatements) {
13387
- if (!raiseStatement.d.typeExpression || raiseStatement.d.valueExpression) {
13658
+ if (!raiseStatement.d.expr || raiseStatement.d.fromExpr) {
13388
13659
  return false;
13389
13660
  }
13390
- const raiseType = getTypeOfExpression(raiseStatement.d.typeExpression).type;
13661
+ const raiseType = getTypeOfExpression(raiseStatement.d.expr).type;
13391
13662
  const classType = (0, types_1.isInstantiableClass)(raiseType)
13392
13663
  ? raiseType
13393
13664
  : (0, types_1.isClassInstance)(raiseType)
@@ -14314,6 +14585,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14314
14585
  // Returns the specialized type and a boolean indicating whether
14315
14586
  // the type indicates a class type (true) or an object type (false).
14316
14587
  function createSpecializedClassType(classType, typeArgs, flags, errorNode) {
14588
+ let isValidTypeForm = true;
14317
14589
  // Handle the special-case classes that are not defined
14318
14590
  // in the type stubs.
14319
14591
  if (types_1.ClassType.isSpecialBuiltIn(classType)) {
@@ -14327,7 +14599,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14327
14599
  if (typeArgs && typeArgs.length > 0) {
14328
14600
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgsExpectingNone().format({ name: aliasedName }), typeArgs[0].node);
14329
14601
  }
14330
- const resultType = aliasedName === 'Never' ? types_1.NeverType.createNever() : 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
+ }
14331
14607
  return { type: resultType };
14332
14608
  }
14333
14609
  case 'Optional': {
@@ -14340,6 +14616,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14340
14616
  if ((0, types_1.isInstantiableClass)(typeType)) {
14341
14617
  typeType = (0, typeUtils_1.explodeGenericClass)(typeType);
14342
14618
  }
14619
+ if (isTypeFormSupported(errorNode)) {
14620
+ typeType = types_1.TypeBase.cloneWithTypeForm(typeType, (0, typeUtils_1.convertToInstance)(typeType));
14621
+ }
14343
14622
  return { type: typeType };
14344
14623
  }
14345
14624
  case 'ClassVar': {
@@ -14364,12 +14643,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14364
14643
  if ((flags & (67108864 /* EvalFlags.NoNonTypeSpecialForms */ | 256 /* EvalFlags.TypeExpression */)) !== 0) {
14365
14644
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typedDictNotAllowed(), errorNode);
14366
14645
  }
14646
+ isValidTypeForm = false;
14367
14647
  break;
14368
14648
  }
14369
14649
  case 'Literal': {
14370
14650
  if ((flags & (67108864 /* EvalFlags.NoNonTypeSpecialForms */ | 256 /* EvalFlags.TypeExpression */)) !== 0) {
14371
14651
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.literalNotAllowed(), errorNode);
14372
14652
  }
14653
+ isValidTypeForm = false;
14373
14654
  break;
14374
14655
  }
14375
14656
  case 'Tuple': {
@@ -14442,16 +14723,22 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14442
14723
  if ((0, types_1.isInstantiableClass)(typeType)) {
14443
14724
  typeType = (0, typeUtils_1.explodeGenericClass)(typeType);
14444
14725
  }
14726
+ if (isTypeFormSupported(errorNode)) {
14727
+ typeType = types_1.TypeBase.cloneWithTypeForm(typeType, (0, typeUtils_1.convertToInstance)(typeType));
14728
+ }
14445
14729
  return { type: typeType };
14446
14730
  }
14447
14731
  }
14448
14732
  // Handle "tuple" specially, since it needs to act like "Tuple"
14449
14733
  // in Python 3.9 and newer.
14450
14734
  if ((0, typeUtils_1.isTupleClass)(classType)) {
14451
- const specializedClass = createSpecialType(classType, typeArgs,
14735
+ let specializedClass = createSpecialType(classType, typeArgs,
14452
14736
  /* paramLimit */ undefined,
14453
14737
  /* allowParamSpec */ undefined,
14454
14738
  /* isSpecialForm */ false);
14739
+ if (isTypeFormSupported(errorNode)) {
14740
+ specializedClass = types_1.TypeBase.cloneWithTypeForm(specializedClass, (0, typeUtils_1.convertToInstance)(specializedClass));
14741
+ }
14455
14742
  return { type: specializedClass };
14456
14743
  }
14457
14744
  }
@@ -14473,11 +14760,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14473
14760
  if (typeArgCount > typeParams.length) {
14474
14761
  if (!types_1.ClassType.isPartiallyEvaluated(classType) && !types_1.ClassType.isTupleClass(classType)) {
14475
14762
  if (typeParams.length === 0) {
14763
+ isValidTypeForm = false;
14476
14764
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeArguments, localize_1.LocMessage.typeArgsExpectingNone().format({
14477
14765
  name: classType.priv.aliasName || classType.shared.name,
14478
14766
  }), typeArgs[typeParams.length].node);
14479
14767
  }
14480
14768
  else if (typeParams.length !== 1 || !(0, types_1.isParamSpec)(typeParams[0])) {
14769
+ isValidTypeForm = false;
14481
14770
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeArguments, localize_1.LocMessage.typeArgsTooMany().format({
14482
14771
  name: classType.priv.aliasName || classType.shared.name,
14483
14772
  expected: typeParams.length,
@@ -14488,6 +14777,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14488
14777
  }
14489
14778
  }
14490
14779
  else if (typeArgCount < minTypeArgCount) {
14780
+ isValidTypeForm = false;
14491
14781
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeArguments, localize_1.LocMessage.typeArgsTooFew().format({
14492
14782
  name: classType.priv.aliasName || classType.shared.name,
14493
14783
  expected: minTypeArgCount,
@@ -14495,6 +14785,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14495
14785
  }), typeArgs.length > 0 ? typeArgs[0].node.parent : errorNode);
14496
14786
  }
14497
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
+ }
14498
14792
  if (index === variadicTypeParamIndex) {
14499
14793
  // The types that make up the tuple that maps to the
14500
14794
  // TypeVarTuple have already been validated when the tuple
@@ -14503,16 +14797,20 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14503
14797
  return;
14504
14798
  }
14505
14799
  if ((0, types_1.isTypeVarTuple)(typeArg.type)) {
14506
- validateTypeVarTupleIsUnpacked(typeArg.type, typeArg.node);
14800
+ if (!validateTypeVarTupleIsUnpacked(typeArg.type, typeArg.node)) {
14801
+ isValidTypeForm = false;
14802
+ }
14507
14803
  return;
14508
14804
  }
14509
14805
  }
14510
14806
  const typeParam = index < typeParams.length ? typeParams[index] : undefined;
14511
14807
  const isParamSpecTarget = typeParam && (0, types_1.isParamSpec)(typeParam);
14512
- validateTypeArg(typeArg, {
14808
+ if (!validateTypeArg(typeArg, {
14513
14809
  allowParamSpec: true,
14514
14810
  allowTypeArgList: isParamSpecTarget,
14515
- });
14811
+ })) {
14812
+ isValidTypeForm = false;
14813
+ }
14516
14814
  });
14517
14815
  }
14518
14816
  // Handle ParamSpec arguments and fill in any missing type arguments with Unknown.
@@ -14542,10 +14840,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14542
14840
  else if (typeArgs.length > 1) {
14543
14841
  const paramSpecTypeArg = typeArgs.find((typeArg) => (0, types_1.isParamSpec)(typeArg.type));
14544
14842
  if (paramSpecTypeArg) {
14843
+ isValidTypeForm = false;
14545
14844
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.paramSpecContext(), paramSpecTypeArg.node);
14546
14845
  }
14547
14846
  const listTypeArg = typeArgs.find((typeArg) => !!typeArg.typeList);
14548
14847
  if (listTypeArg) {
14848
+ isValidTypeForm = false;
14549
14849
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgListNotAllowed(), listTypeArg.node);
14550
14850
  }
14551
14851
  }
@@ -14633,6 +14933,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14633
14933
  // Avoid emitting this error for a partially-constructed class.
14634
14934
  if (!(0, types_1.isClassInstance)(typeArgType) || !types_1.ClassType.isPartiallyEvaluated(typeArgType)) {
14635
14935
  (0, debug_1.assert)(typeArgs !== undefined);
14936
+ isValidTypeForm = false;
14636
14937
  addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeArguments, localize_1.LocMessage.typeVarAssignmentMismatch().format({
14637
14938
  type: printType(typeArgType),
14638
14939
  name: types_1.TypeVarType.getReadableName(typeParams[index]),
@@ -14648,7 +14949,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14648
14949
  if (typeArgs && classType.shared.typeParams.length === 0 && types_1.ClassType.isPartiallyEvaluated(classType)) {
14649
14950
  typeArgTypes = typeArgs.map((t) => (0, typeUtils_1.convertToInstance)(t.type));
14650
14951
  }
14651
- 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
+ }
14652
14956
  return { type: specializedClass };
14653
14957
  }
14654
14958
  function getTypeOfArg(arg, inferenceContext) {
@@ -14687,7 +14991,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14687
14991
  flags |= 8192 /* EvalFlags.TypeVarGetsCurScope */;
14688
14992
  }
14689
14993
  if (options === null || options === void 0 ? void 0 : options.enforceClassTypeVarScope) {
14690
- flags |= 1073741824 /* EvalFlags.EnforceClassTypeVarScope */;
14994
+ flags |= -2147483648 /* EvalFlags.EnforceClassTypeVarScope */;
14691
14995
  }
14692
14996
  const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
14693
14997
  if ((0, analyzerFileInfo_1.isAnnotationEvaluationPostponed)(fileInfo) || (options === null || options === void 0 ? void 0 : options.forwardRefs)) {
@@ -14732,6 +15036,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14732
15036
  if (options === null || options === void 0 ? void 0 : options.notParsed) {
14733
15037
  flags |= 524288 /* EvalFlags.NotParsed */;
14734
15038
  }
15039
+ if (options === null || options === void 0 ? void 0 : options.typeFormArg) {
15040
+ flags |= 1073741824 /* EvalFlags.TypeFormArg */;
15041
+ }
14735
15042
  return getTypeOfExpression(node, flags);
14736
15043
  }
14737
15044
  function getBuiltInType(node, name) {
@@ -14988,7 +15295,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
14988
15295
  // In general, string nodes don't have any declarations associated with them, but
14989
15296
  // we need to handle the special case of string literals used as keys within a
14990
15297
  // dictionary expression where those keys are associated with a known TypedDict.
14991
- function getDeclarationsForStringNode(node) {
15298
+ function getDeclInfoForStringNode(node) {
14992
15299
  var _a;
14993
15300
  const declarations = [];
14994
15301
  const expectedType = (_a = getExpectedType(node)) === null || _a === void 0 ? void 0 : _a.type;
@@ -15010,7 +15317,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
15010
15317
  }
15011
15318
  });
15012
15319
  }
15013
- return declarations.length === 0 ? undefined : declarations;
15320
+ return declarations.length === 0 ? undefined : { decls: declarations, synthesizedTypes: [] };
15014
15321
  }
15015
15322
  function getAliasFromImport(node) {
15016
15323
  if (node.parent &&
@@ -15021,12 +15328,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
15021
15328
  }
15022
15329
  return undefined;
15023
15330
  }
15024
- function getDeclarationsForNameNode(node, skipUnreachableCode = true) {
15331
+ function getDeclInfoForNameNode(node, skipUnreachableCode = true) {
15025
15332
  var _a, _b, _c;
15026
15333
  if (skipUnreachableCode && AnalyzerNodeInfo.isCodeUnreachable(node)) {
15027
15334
  return undefined;
15028
15335
  }
15029
15336
  const declarations = [];
15337
+ const synthesizedTypes = [];
15030
15338
  // If the node is part of a "from X import Y as Z" statement and the node
15031
15339
  // is the "Y" (non-aliased) name, we need to look up the alias symbol
15032
15340
  // since the non-aliased name is not in the symbol table.
@@ -15096,7 +15404,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
15096
15404
  (0, collectionUtils_1.appendArray)(declarations, typedDecls);
15097
15405
  }
15098
15406
  else {
15099
- (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
+ }
15100
15414
  }
15101
15415
  }
15102
15416
  });
@@ -15178,7 +15492,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
15178
15492
  (0, collectionUtils_1.appendArray)(declarations, symbolWithScope.symbol.getDeclarations());
15179
15493
  }
15180
15494
  }
15181
- return declarations;
15495
+ return { decls: declarations, synthesizedTypes };
15182
15496
  }
15183
15497
  function getTypeForDeclaration(declaration) {
15184
15498
  var _a, _b;
@@ -15724,6 +16038,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
15724
16038
  return true;
15725
16039
  }
15726
16040
  }
16041
+ if (decl.type === 2 /* DeclarationType.Param */) {
16042
+ return true;
16043
+ }
15727
16044
  return false;
15728
16045
  });
15729
16046
  }
@@ -15880,9 +16197,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
15880
16197
  isConstant = true;
15881
16198
  }
15882
16199
  // If the symbol is constant, we can retain the literal
15883
- // value. Otherwise, strip literal values to widen the type.
16200
+ // value and TypeForm types. Otherwise, strip literal values
16201
+ // and TypeForm types to widen.
15884
16202
  if (types_1.TypeBase.isInstance(type) && !isConstant && !isExplicitTypeAliasDeclaration(decl)) {
15885
- type = stripLiteralValue(type);
16203
+ type = (0, typeUtils_1.stripTypeForm)(stripLiteralValue(type));
15886
16204
  }
15887
16205
  }
15888
16206
  typesToCombine.push(type);
@@ -16240,7 +16558,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
16240
16558
  paramType = types_1.UnknownType.create();
16241
16559
  }
16242
16560
  if (stripLiteralArgTypes) {
16243
- paramType = stripLiteralValue(paramType);
16561
+ paramType = (0, typeUtils_1.stripTypeForm)(convertSpecialFormToRuntimeValue(stripLiteralValue(paramType), 0 /* EvalFlags.None */));
16244
16562
  }
16245
16563
  paramTypes.push(paramType);
16246
16564
  writeTypeCache(param.d.name, { type: paramType }, 0 /* EvalFlags.None */);
@@ -16520,8 +16838,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
16520
16838
  let flags = 0 /* AssignTypeFlags.Default */;
16521
16839
  if ((primaryDecl === null || primaryDecl === void 0 ? void 0 : primaryDecl.type) === 1 /* DeclarationType.Variable */ &&
16522
16840
  !isFinalVariableDeclaration(primaryDecl) &&
16523
- !types_1.ClassType.isReadOnlyInstanceVariables(destType) &&
16524
- !types_1.ClassType.isDataClassFrozen(destType)) {
16841
+ !(0, typeUtils_1.isMemberReadOnly)(destType, name)) {
16525
16842
  // Class and instance variables that are mutable need to
16526
16843
  // enforce invariance. We will exempt variables that are
16527
16844
  // private or protected, since these are presumably
@@ -16772,7 +17089,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
16772
17089
  // in the dest type is not in the type map already, it is assigned a type
16773
17090
  // and added to the map.
16774
17091
  function assignType(destType, srcType, diag, constraints, flags = 0 /* AssignTypeFlags.Default */, recursionCount = 0) {
16775
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
17092
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
16776
17093
  // Handle the case where the dest and src types are the same object.
16777
17094
  // We can normally shortcut this and say that they are compatible,
16778
17095
  // but if the type includes TypeVars, we need to go through
@@ -16793,7 +17110,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
16793
17110
  }
16794
17111
  }
16795
17112
  if (!isSpecialFormExempt) {
16796
- 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
+ }
16797
17119
  }
16798
17120
  }
16799
17121
  if (recursionCount > types_1.maxTypeRecursionCount) {
@@ -16806,8 +17128,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
16806
17128
  destType.shared.recursiveAlias &&
16807
17129
  (0, types_1.isTypeVar)(srcType) &&
16808
17130
  srcType.shared.recursiveAlias) {
16809
- const destAliasInfo = (_b = destType.props) === null || _b === void 0 ? void 0 : _b.typeAliasInfo;
16810
- 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;
16811
17133
  // Do the source and dest refer to the same recursive type alias?
16812
17134
  if ((destAliasInfo === null || destAliasInfo === void 0 ? void 0 : destAliasInfo.typeArgs) &&
16813
17135
  (srcAliasInfo === null || srcAliasInfo === void 0 ? void 0 : srcAliasInfo.typeArgs) &&
@@ -16857,8 +17179,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
16857
17179
  if ((0, types_1.isUnbound)(destType) || (0, types_1.isUnbound)(srcType)) {
16858
17180
  return true;
16859
17181
  }
16860
- // Before performing any other checks, see if the dest type is a
16861
- // TypeVar that we are attempting to match.
17182
+ // Handle TypeForm assignments.
17183
+ if (assignToTypeFormType(destType, srcType, constraints, flags, recursionCount)) {
17184
+ return true;
17185
+ }
16862
17186
  if ((0, types_1.isTypeVar)(destType)) {
16863
17187
  if ((0, typeUtils_1.isTypeVarSame)(destType, srcType)) {
16864
17188
  return true;
@@ -16873,7 +17197,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
16873
17197
  // and the bound TypeVar matches the condition, the types are compatible.
16874
17198
  const destTypeVar = destType;
16875
17199
  if (types_1.TypeBase.isInstantiable(destType) === types_1.TypeBase.isInstantiable(srcType) &&
16876
- ((_d = srcType.props) === null || _d === void 0 ? void 0 : _d.condition) &&
17200
+ ((_f = srcType.props) === null || _f === void 0 ? void 0 : _f.condition) &&
16877
17201
  srcType.props.condition.some((cond) => {
16878
17202
  return (!types_1.TypeVarType.hasConstraints(cond.typeVar) &&
16879
17203
  cond.typeVar.priv.nameWithScope === destTypeVar.priv.nameWithScope);
@@ -16974,7 +17298,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
16974
17298
  if ((0, types_1.isAnyOrUnknown)(destType)) {
16975
17299
  return true;
16976
17300
  }
16977
- 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)) {
16978
17302
  if (constraints) {
16979
17303
  // If it's an ellipsis type, convert it to a regular "Any"
16980
17304
  // type. These are functionally equivalent, but "Any" looks
@@ -17058,12 +17382,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
17058
17382
  return false;
17059
17383
  }
17060
17384
  }
17061
- 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) {
17062
17386
  return true;
17063
17387
  }
17064
17388
  if (isSpecialFormClass(expandedSrcType, flags)) {
17065
- if ((_h = destType.props) === null || _h === void 0 ? void 0 : _h.specialForm) {
17066
- 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);
17067
17394
  }
17068
17395
  }
17069
17396
  else if (assignClass(destType, expandedSrcType, diag, constraints, flags, recursionCount,
@@ -17197,7 +17524,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
17197
17524
  }
17198
17525
  }
17199
17526
  }
17200
- 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)) {
17201
17528
  return (flags & 16 /* AssignTypeFlags.OverloadOverlap */) === 0;
17202
17529
  }
17203
17530
  else if ((0, types_1.isUnion)(concreteSrcType)) {
@@ -17325,6 +17652,45 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
17325
17652
  diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.LocAddendum.typeAssignmentMismatch().format(printSrcDestTypes(srcType, destType)));
17326
17653
  return false;
17327
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
+ }
17328
17694
  function assignFromUnionType(destType, srcType, diag, constraints, flags, recursionCount) {
17329
17695
  // Start by checking for an exact match. This is needed to handle unions
17330
17696
  // that contain recursive type aliases.
@@ -17464,8 +17830,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
17464
17830
  }
17465
17831
  else {
17466
17832
  // Try to assign a union of the remaining source types to
17467
- // the first destination TypeVar.
17468
- 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)) {
17469
17837
  canUseFastPath = false;
17470
17838
  }
17471
17839
  }
@@ -17644,7 +18012,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
17644
18012
  // in case the destType is a union with hundreds of literals.
17645
18013
  if ((0, types_1.isClassInstance)(srcType) &&
17646
18014
  (0, typeUtils_1.isLiteralType)(srcType) &&
17647
- types_1.UnionType.containsType(destType, srcType, /* exclusionSet */ undefined, recursionCount)) {
18015
+ types_1.UnionType.containsType(destType, srcType,
18016
+ /* options */ undefined,
18017
+ /* exclusionSet */ undefined, recursionCount)) {
17648
18018
  return true;
17649
18019
  }
17650
18020
  (0, typeUtils_1.doForEachSubtype)(destType, (subtype) => {
@@ -18321,9 +18691,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
18321
18691
  // is the first remaining param.
18322
18692
  }
18323
18693
  else {
18324
- remainingParams.push(types_1.FunctionParam.create(p.category, types_1.FunctionType.getParamType(effectiveSrcType, index), p.flags, p.name, types_1.FunctionType.getParamDefaultType(effectiveSrcType, index)
18325
- ? types_1.AnyType.create(/* isEllipsis */ true)
18326
- : 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));
18327
18695
  }
18328
18696
  });
18329
18697
  // If there are remaining parameters and the source and dest do not contain
@@ -19183,9 +19551,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
19183
19551
  }
19184
19552
  // Perform a sanity check on the RHS expression. Some expression
19185
19553
  // forms should never be considered legitimate for type aliases.
19186
- return isLegalTypeAliasExpressionForm(decl.node.parent.d.rightExpr);
19554
+ return isLegalTypeAliasExpressionForm(decl.node.parent.d.rightExpr, /* allowStrLiteral */ false);
19187
19555
  }
19188
- function isLegalTypeAliasExpressionForm(node) {
19556
+ function isLegalTypeAliasExpressionForm(node, allowStrLiteral) {
19189
19557
  switch (node.nodeType) {
19190
19558
  case 0 /* ParseNodeType.Error */:
19191
19559
  case 55 /* ParseNodeType.UnaryOperation */:
@@ -19206,14 +19574,19 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
19206
19574
  case 34 /* ParseNodeType.List */:
19207
19575
  case 45 /* ParseNodeType.Set */:
19208
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 */;
19209
19582
  case 7 /* ParseNodeType.BinaryOperation */:
19210
19583
  return (node.d.operator === 6 /* OperatorType.BitwiseOr */ &&
19211
- isLegalTypeAliasExpressionForm(node.d.leftExpr) &&
19212
- isLegalTypeAliasExpressionForm(node.d.rightExpr));
19584
+ isLegalTypeAliasExpressionForm(node.d.leftExpr, /* allowStrLiteral */ true) &&
19585
+ isLegalTypeAliasExpressionForm(node.d.rightExpr, /* allowStrLiteral */ true));
19213
19586
  case 27 /* ParseNodeType.Index */:
19214
- return isLegalTypeAliasExpressionForm(node.d.leftExpr);
19587
+ return isLegalTypeAliasExpressionForm(node.d.leftExpr, allowStrLiteral);
19215
19588
  case 35 /* ParseNodeType.MemberAccess */:
19216
- return isLegalTypeAliasExpressionForm(node.d.leftExpr);
19589
+ return isLegalTypeAliasExpressionForm(node.d.leftExpr, allowStrLiteral);
19217
19590
  }
19218
19591
  return true;
19219
19592
  }
@@ -19292,6 +19665,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
19292
19665
  }
19293
19666
  return { sourceType: simpleSrcType, destType: simpleDestType };
19294
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
+ }
19295
19673
  function printType(type, options) {
19296
19674
  let flags = evaluatorOptions.printTypeFlags;
19297
19675
  if (options === null || options === void 0 ? void 0 : options.expandTypeAlias) {
@@ -19345,12 +19723,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
19345
19723
  const parseResults = parser.parseTextExpression(dummyFileContents, valueOffset, textValue.length, parseOptions, 0 /* ParseTextMode.Expression */,
19346
19724
  /* initialParenDepth */ undefined, fileInfo.typingSymbolAliases);
19347
19725
  if (parseResults.parseTree) {
19348
- if (reportErrors) {
19349
- const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
19350
- parseResults.diagnostics.forEach((diag) => {
19351
- fileInfo.diagnosticSink.addDiagnosticWithTextRange('error', diag.message, node);
19352
- });
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;
19353
19730
  }
19731
+ const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
19732
+ parseResults.diagnostics.forEach((diag) => {
19733
+ fileInfo.diagnosticSink.addDiagnosticWithTextRange('error', diag.message, node);
19734
+ });
19354
19735
  parseResults.parseTree.parent = node;
19355
19736
  // Optionally add the new subtree to the parse tree so it can
19356
19737
  // participate in language server operations like find and replace.
@@ -19420,8 +19801,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
19420
19801
  isAsymmetricAccessorAssignment,
19421
19802
  suppressDiagnostics,
19422
19803
  isSpecialFormClass,
19423
- getDeclarationsForStringNode,
19424
- getDeclarationsForNameNode,
19804
+ getDeclInfoForStringNode,
19805
+ getDeclInfoForNameNode,
19425
19806
  getTypeForDeclaration,
19426
19807
  resolveAliasDeclaration,
19427
19808
  resolveAliasDeclarationWithInfo,