Fixed a bug that led to a false positive error when using a subclass of type as a base class in a class declaration. This addresses https://github.com/microsoft/pyright/issues/4737.

This commit is contained in:
Eric Traut 2023-03-05 23:10:19 -07:00
parent dbe9e3ab88
commit 315031a47b
2 changed files with 17 additions and 4 deletions

View File

@ -15983,7 +15983,13 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
}
if (!isAnyOrUnknown(argType) && !isUnbound(argType)) {
if (isClass(argType) && TypeBase.isInstance(argType) && ClassType.isBuiltIn(argType, 'type')) {
if (
isClass(argType) &&
TypeBase.isInstance(argType) &&
argType.details.mro.some(
(mroClass) => isClass(mroClass) && ClassType.isBuiltIn(mroClass, 'type')
)
) {
argType =
argType.typeArguments && argType.typeArguments.length > 0
? argType.typeArguments[0]

View File

@ -1987,9 +1987,16 @@ export function isEffectivelyInstantiable(type: Type): boolean {
return true;
}
// Handle the special case of 'type', which is instantiable.
if (isClassInstance(type) && ClassType.isBuiltIn(type, 'type')) {
return true;
// Handle the special case of 'type' (or subclasses thereof),
// which are instantiable.
if (isClassInstance(type)) {
if (
type.details.mro.some((mroClass) => {
return isClass(mroClass) && ClassType.isBuiltIn(mroClass, 'type');
})
) {
return true;
}
}
if (isUnion(type)) {