Skip to content

Commit

Permalink
Fixed a bug that resulted in a false positive error when accessing `_…
Browse files Browse the repository at this point in the history
…_required_keys__` and `__optional_keys__` class variables from a TypedDict class. This addresses #4396.
  • Loading branch information
msfterictraut committed Jan 4, 2023
1 parent 440d54d commit fb563db
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5368,7 +5368,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
}

// Don't include variables within typed dict classes.
if (ClassType.isTypedDictClass(classType)) {
if (isClass(memberInfo.classType) && ClassType.isTypedDictClass(memberInfo.classType)) {
const typedDecls = memberInfo.symbol.getTypedDeclarations();
if (typedDecls.length > 0 && typedDecls[0].type === DeclarationType.Variable) {
diag?.addMessage(Localizer.DiagnosticAddendum.memberUnknown().format({ name: memberName }));
Expand Down
12 changes: 12 additions & 0 deletions packages/pyright-internal/src/tests/samples/typedDict22.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This sample tests that class variables for TypedDict are accessible.

from typing import TypedDict


class TD1(TypedDict):
...


reveal_type(TD1.__required_keys__, expected_text="frozenset[str]")
reveal_type(TD1.__optional_keys__, expected_text="frozenset[str]")
reveal_type(TD1.__total__, expected_text="bool")
6 changes: 6 additions & 0 deletions packages/pyright-internal/src/tests/typeEvaluator2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1347,3 +1347,9 @@ test('TypedDict21', () => {

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

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

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

0 comments on commit fb563db

Please sign in to comment.