Skip to content

Commit

Permalink
Fixed bug that leads to incorrect evaluations when a ClassVar and `…
Browse files Browse the repository at this point in the history
…Final` qualifier are both used on the same dataclass attribute. This addresses #9550.
  • Loading branch information
erictraut committed Dec 6, 2024
1 parent 943a954 commit 678e24a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 1 addition & 3 deletions packages/pyright-internal/src/analyzer/dataClasses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions packages/pyright-internal/src/tests/samples/dataclass17.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 678e24a

Please sign in to comment.