diff --git a/packages/pyright-internal/src/analyzer/typeEvaluator.ts b/packages/pyright-internal/src/analyzer/typeEvaluator.ts index dd7efe510886..e712c51753d5 100644 --- a/packages/pyright-internal/src/analyzer/typeEvaluator.ts +++ b/packages/pyright-internal/src/analyzer/typeEvaluator.ts @@ -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 }; diff --git a/packages/pyright-internal/src/tests/samples/classVar7.py b/packages/pyright-internal/src/tests/samples/classVar7.py new file mode 100644 index 000000000000..09bac19ea591 --- /dev/null +++ b/packages/pyright-internal/src/tests/samples/classVar7.py @@ -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 = "" diff --git a/packages/pyright-internal/src/tests/typeEvaluator7.test.ts b/packages/pyright-internal/src/tests/typeEvaluator7.test.ts index 4c47830a6ca0..2e6e651aff16 100644 --- a/packages/pyright-internal/src/tests/typeEvaluator7.test.ts +++ b/packages/pyright-internal/src/tests/typeEvaluator7.test.ts @@ -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']);