Skip to content

Commit

Permalink
Fixed a bug that results in incorrect handling of a "bare" ClassVar a…
Browse files Browse the repository at this point in the history
…nnotation either with or without an assignment. This addresses #9720. (#9724)
  • Loading branch information
erictraut authored Jan 18, 2025
1 parent 8e70199 commit 951983a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22961,7 +22961,13 @@ export function createTypeEvaluator(

type = combineTypes(typesToCombine);
} else {
type = UnboundType.create();
// We can encounter this situation in the case of a bare ClassVar annotation.
if (symbol.isClassVar()) {
type = UnknownType.create();
isIncomplete = false;
} else {
type = UnboundType.create();
}
}

return { type, isIncomplete, includesSpeculativeResult, evaluationAttempts };
Expand Down
38 changes: 38 additions & 0 deletions packages/pyright-internal/src/tests/samples/classVar7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# This sample tests the handling of a "bare" ClassVar with no
# subscript.

from typing import ClassVar


class A:
a: ClassVar
b: ClassVar = 2
c: ClassVar
d: ClassVar

d = 3

@classmethod
def m1(cls) -> None:
cls.c = ""


reveal_type(A.a, expected_text="Unknown")
A.a = 3
A.a = ""

reveal_type(A.b, expected_text="int")
A.b = 2

# This should generate an error
A.b = ""

reveal_type(A.c, expected_text="Unknown")
A.c = 2
A.c = ""

reveal_type(A.d, expected_text="int")
A.d = 2

# This should generate an error
A.d = ""
6 changes: 6 additions & 0 deletions packages/pyright-internal/src/tests/typeEvaluator7.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,12 @@ test('ClassVar6', () => {
TestUtils.validateResults(analysisResults, 4);
});

test('ClassVar7', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['classVar7.py']);

TestUtils.validateResults(analysisResults, 2);
});

test('TypeVar1', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['typeVar1.py']);

Expand Down

0 comments on commit 951983a

Please sign in to comment.