From 315031a47bb28bada6d583fb7adf820f024dcf27 Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Sun, 5 Mar 2023 23:10:19 -0700 Subject: [PATCH] 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. --- .../pyright-internal/src/analyzer/typeEvaluator.ts | 8 +++++++- packages/pyright-internal/src/analyzer/typeUtils.ts | 13 ++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/pyright-internal/src/analyzer/typeEvaluator.ts b/packages/pyright-internal/src/analyzer/typeEvaluator.ts index 621dc2f22..dcc57b392 100644 --- a/packages/pyright-internal/src/analyzer/typeEvaluator.ts +++ b/packages/pyright-internal/src/analyzer/typeEvaluator.ts @@ -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] diff --git a/packages/pyright-internal/src/analyzer/typeUtils.ts b/packages/pyright-internal/src/analyzer/typeUtils.ts index 2bc234b8b..44dc96773 100644 --- a/packages/pyright-internal/src/analyzer/typeUtils.ts +++ b/packages/pyright-internal/src/analyzer/typeUtils.ts @@ -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)) {