From 678e24a72114950c62666bbce78a5bb17289b397 Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Thu, 5 Dec 2024 17:00:02 -0800 Subject: [PATCH] Fixed bug that leads to incorrect evaluations when a `ClassVar` and `Final` qualifier are both used on the same dataclass attribute. This addresses #9550. --- packages/pyright-internal/src/analyzer/dataClasses.ts | 4 +--- packages/pyright-internal/src/tests/samples/dataclass17.py | 6 ++++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/pyright-internal/src/analyzer/dataClasses.ts b/packages/pyright-internal/src/analyzer/dataClasses.ts index b7c705fffd8f..43e772ed273b 100644 --- a/packages/pyright-internal/src/analyzer/dataClasses.ts +++ b/packages/pyright-internal/src/analyzer/dataClasses.ts @@ -396,11 +396,9 @@ export function synthesizeDataClassMethods( const variableSymbol = ClassType.getSymbolTable(classType).get(variableName); namedTupleEntries.add(variableName); - if (variableSymbol?.isClassVar() && !variableSymbol?.isFinalVarInClassBody()) { + if (variableSymbol?.isClassVar()) { // If an ancestor class declared an instance variable but this dataclass // declares a ClassVar, delete the older one from the full data class entries. - // We exclude final variables here because a Final type annotation is implicitly - // considered a ClassVar by the binder, but dataclass rules are different. const index = fullDataClassEntries.findIndex((p) => p.name === variableName); if (index >= 0) { fullDataClassEntries.splice(index, 1); diff --git a/packages/pyright-internal/src/tests/samples/dataclass17.py b/packages/pyright-internal/src/tests/samples/dataclass17.py index 81d64b73e374..1d770129b4b4 100644 --- a/packages/pyright-internal/src/tests/samples/dataclass17.py +++ b/packages/pyright-internal/src/tests/samples/dataclass17.py @@ -33,3 +33,9 @@ class A: # This should generate an error. A.e = 0 + + +@dataclass +class B: + a: ClassVar[Final[int]] = 0 + b: int = 1