Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Pyright supports [configuration files](configuration.md) that provide granular c
* [PEP 728](https://www.python.org/dev/peps/pep-0728/) TypedDict with typed extra items
* [PEP 742](https://www.python.org/dev/peps/pep-0742/) narrowing types with TypeIs
* [PEP 746](https://www.python.org/dev/peps/pep-0746/) (experimental) type checking annotated metadata
* [PEP 747](https://www.python.org/dev/peps/pep-0747/) (experimental) annotating type forms
* [PEP 747](https://www.python.org/dev/peps/pep-0747/) annotating type forms
* [PEP 764](https://www.python.org/dev/peps/pep-0764/) (experimental) inline typed dictionaries
* Type inference for function return values, instance variables, class variables, and globals
* Type guards that understand conditional code flow constructs like if/else statements
Expand Down
6 changes: 1 addition & 5 deletions packages/pyright-internal/src/analyzer/sentinel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,7 @@ export function createSentinelType(
classType = ClassType.cloneWithLiteral(classType, new SentinelLiteral(fullClassName, className));

let instanceType = ClassType.cloneAsInstance(classType);

// Is TypeForm supported?
if (fileInfo.diagnosticRuleSet.enableExperimentalFeatures) {
instanceType = TypeBase.cloneWithTypeForm(instanceType, instanceType);
}
instanceType = TypeBase.cloneWithTypeForm(instanceType, instanceType);

return instanceType;
}
88 changes: 20 additions & 68 deletions packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1089,10 +1089,7 @@ export function createTypeEvaluator(

if (isAny(anySpecialForm)) {
TypeBase.setSpecialForm(anySpecialForm, anyClass);

if (isTypeFormSupported(node)) {
TypeBase.setTypeForm(anySpecialForm, convertToInstance(anySpecialForm));
}
TypeBase.setTypeForm(anySpecialForm, convertToInstance(anySpecialForm));
}
}
}
Expand Down Expand Up @@ -1715,11 +1712,7 @@ export function createTypeEvaluator(
};
}

if (
node.d.strings.length !== 1 ||
node.d.strings[0].nodeType !== ParseNodeType.String ||
!isTypeFormSupported(node)
) {
if (node.d.strings.length !== 1 || node.d.strings[0].nodeType !== ParseNodeType.String) {
return typeResult;
}

Expand Down Expand Up @@ -4986,10 +4979,6 @@ export function createTypeEvaluator(
}

function addTypeFormForSymbol(node: ExpressionNode, type: Type, flags: EvalFlags, includesVarDecl: boolean): Type {
if (!isTypeFormSupported(node)) {
return type;
}

const isValid = isSymbolValidTypeExpression(type, includesVarDecl);

// If the type already has type information associated with it, don't replace.
Expand Down Expand Up @@ -7630,10 +7619,7 @@ export function createTypeEvaluator(
...aliasInfo,
typeArgs: aliasTypeArgs,
});

if (isTypeFormSupported(node)) {
type = TypeBase.cloneWithTypeForm(type, reportedError ? undefined : convertToInstance(type));
}
type = TypeBase.cloneWithTypeForm(type, reportedError ? undefined : convertToInstance(type));

if (baseType.props?.typeAliasInfo) {
return { type, node };
Expand Down Expand Up @@ -8804,7 +8790,7 @@ export function createTypeEvaluator(
}

const typeFormResult = getTypeOfArgExpectingType(convertNodeToArg(node.d.args[0]), {
typeFormArg: isTypeFormSupported(node),
typeFormArg: true,
noNonTypeSpecialForms: true,
typeExpression: true,
});
Expand Down Expand Up @@ -13824,9 +13810,7 @@ export function createTypeEvaluator(
? prefetched.noneTypeClass
: convertToInstance(prefetched.noneTypeClass);

if (isTypeFormSupported(node)) {
type = TypeBase.cloneWithTypeForm(type, convertToInstance(type));
}
type = TypeBase.cloneWithTypeForm(type, convertToInstance(type));
}
} else if (
node.d.constType === KeywordType.True ||
Expand Down Expand Up @@ -15699,7 +15683,7 @@ export function createTypeEvaluator(
FunctionType.addParamSpecVariadics(functionType, convertToInstance(paramSpec));
}

if (isTypeFormSupported(errorNode) && isValidTypeForm) {
if (isValidTypeForm) {
functionType = TypeBase.cloneWithTypeForm(functionType, convertToInstance(functionType));
}

Expand Down Expand Up @@ -15915,7 +15899,7 @@ export function createTypeEvaluator(
result = TypeBase.cloneAsSpecialForm(result, ClassType.cloneAsInstance(prefetched.unionTypeClass));
}

if (isTypeFormSupported(node) && isValidTypeForm) {
if (isValidTypeForm) {
result = TypeBase.cloneWithTypeForm(result, convertToInstance(result));
}

Expand Down Expand Up @@ -15985,10 +15969,7 @@ export function createTypeEvaluator(
return convertToInstance(validateTypeArg(typeArg) ? typeArg.type : UnknownType.create());
});
let resultType = ClassType.specialize(classType, convertedTypeArgs);

if (isTypeFormSupported(errorNode)) {
resultType = TypeBase.cloneWithTypeForm(resultType, convertToInstance(resultType));
}
resultType = TypeBase.cloneWithTypeForm(resultType, convertToInstance(resultType));

return resultType;
}
Expand Down Expand Up @@ -16019,10 +16000,7 @@ export function createTypeEvaluator(
});

let resultType = ClassType.specialize(classType, convertedTypeArgs);

if (isTypeFormSupported(errorNode)) {
resultType = TypeBase.cloneWithTypeForm(resultType, convertToInstance(resultType));
}
resultType = TypeBase.cloneWithTypeForm(resultType, convertToInstance(resultType));

return resultType;
}
Expand Down Expand Up @@ -16625,7 +16603,7 @@ export function createTypeEvaluator(
if (unionType.props?.typeForm) {
unionType = TypeBase.cloneWithTypeForm(unionType, undefined);
}
} else if (isTypeFormSupported(errorNode)) {
} else {
const typeFormType = combineTypes(types.map((t) => t.props!.typeForm!));
unionType = TypeBase.cloneWithTypeForm(unionType, typeFormType);
}
Expand Down Expand Up @@ -16985,10 +16963,7 @@ export function createTypeEvaluator(
if (assignedName === 'LiteralString') {
specialType.shared.baseClasses.push(prefetched?.strClass ?? AnyType.create());
computeMroLinearization(specialType);

if (isTypeFormSupported(node)) {
specialType = TypeBase.cloneWithTypeForm(specialType, convertToInstance(specialType));
}
specialType = TypeBase.cloneWithTypeForm(specialType, convertToInstance(specialType));
}

// Handle 'Never' and 'NoReturn' specially.
Expand All @@ -16998,9 +16973,7 @@ export function createTypeEvaluator(
specialType
);

if (isTypeFormSupported(node)) {
specialType = TypeBase.cloneWithTypeForm(specialType, convertToInstance(specialType));
}
specialType = TypeBase.cloneWithTypeForm(specialType, convertToInstance(specialType));
}

writeTypeCache(node, { type: specialType }, EvalFlags.None);
Expand Down Expand Up @@ -21197,9 +21170,7 @@ export function createTypeEvaluator(

let resultType = aliasedName === 'Never' ? NeverType.createNever() : NeverType.createNoReturn();
resultType = TypeBase.cloneAsSpecialForm(resultType, classType);
if (isTypeFormSupported(errorNode)) {
resultType = TypeBase.cloneWithTypeForm(resultType, convertToInstance(resultType));
}
resultType = TypeBase.cloneWithTypeForm(resultType, convertToInstance(resultType));

return { type: resultType };
}
Expand All @@ -21221,9 +21192,7 @@ export function createTypeEvaluator(
typeType = explodeGenericClass(typeType);
}

if (isTypeFormSupported(errorNode)) {
typeType = TypeBase.cloneWithTypeForm(typeType, convertToInstance(typeType));
}
typeType = TypeBase.cloneWithTypeForm(typeType, convertToInstance(typeType));

return { type: typeType };
}
Expand Down Expand Up @@ -21383,9 +21352,7 @@ export function createTypeEvaluator(
typeType = explodeGenericClass(typeType);
}

if (isTypeFormSupported(errorNode)) {
typeType = TypeBase.cloneWithTypeForm(typeType, convertToInstance(typeType));
}
typeType = TypeBase.cloneWithTypeForm(typeType, convertToInstance(typeType));

return { type: typeType };
}
Expand All @@ -21402,12 +21369,7 @@ export function createTypeEvaluator(
/* isSpecialForm */ false
);

if (isTypeFormSupported(errorNode)) {
specializedClass = TypeBase.cloneWithTypeForm(
specializedClass,
convertToInstance(specializedClass)
);
}
specializedClass = TypeBase.cloneWithTypeForm(specializedClass, convertToInstance(specializedClass));

return { type: specializedClass };
}
Expand Down Expand Up @@ -21667,13 +21629,10 @@ export function createTypeEvaluator(
}

let specializedClass = ClassType.specialize(classType, typeArgTypes, typeArgs !== undefined);

if (isTypeFormSupported(errorNode)) {
specializedClass = TypeBase.cloneWithTypeForm(
specializedClass,
isValidTypeForm ? convertToInstance(specializedClass) : undefined
);
}
specializedClass = TypeBase.cloneWithTypeForm(
specializedClass,
isValidTypeForm ? convertToInstance(specializedClass) : undefined
);

return { type: specializedClass };
}
Expand Down Expand Up @@ -28626,13 +28585,6 @@ export function createTypeEvaluator(
return { sourceType: simpleSrcType, destType: simpleDestType };
}

function isTypeFormSupported(node: ParseNode) {
const fileInfo = AnalyzerNodeInfo.getFileInfo(node);

// For now, enable only if enableExperimentalFeatures is true.
return fileInfo.diagnosticRuleSet.enableExperimentalFeatures;
}

function printType(type: Type, options?: PrintTypeOptions): string {
let flags = evaluatorOptions.printTypeFlags;

Expand Down
Loading