From 7d38c9ac5a49d934d2ec748b8c627433a7f7e7b1 Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Sun, 14 Jan 2024 15:44:35 -0800 Subject: [PATCH] Fixed a bug that resulted in a false positive when a contravariant TypeVar was used in a return type in a non-method. --- packages/pyright-internal/src/analyzer/checker.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/pyright-internal/src/analyzer/checker.ts b/packages/pyright-internal/src/analyzer/checker.ts index 16cf89ffce26..18dfb2ea7170 100644 --- a/packages/pyright-internal/src/analyzer/checker.ts +++ b/packages/pyright-internal/src/analyzer/checker.ts @@ -163,6 +163,7 @@ import { Type, TypeBase, TypeCategory, + TypeVarScopeType, TypeVarType, TypedDictEntry, UnknownType, @@ -4736,7 +4737,11 @@ export class Checker extends ParseTreeWalker { let isContraTypeVar = false; doForEachSubtype(returnType, (subtype) => { - if (isTypeVar(subtype) && subtype.details.declaredVariance === Variance.Contravariant) { + if ( + isTypeVar(subtype) && + subtype.details.declaredVariance === Variance.Contravariant && + subtype.scopeType === TypeVarScopeType.Class + ) { isContraTypeVar = true; } });