Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/pyright-internal/src/analyzer/typeGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2257,7 +2257,8 @@ function narrowTypeForTypedDictKey(

if (isClassInstance(subtype) && ClassType.isTypedDictClass(subtype)) {
const entries = getTypedDictMembersForClass(evaluator, subtype, /* allowNarrowed */ true);
const tdEntry = entries.knownItems.get(literalKey.priv.literalValue as string) ?? entries.extraItems;
const knownItemEntry = entries.knownItems.get(literalKey.priv.literalValue as string);
const tdEntry = knownItemEntry ?? entries.extraItems;

if (isPositiveTest) {
// The code that is commented out below implements the behavior that is technically
Expand Down Expand Up @@ -2309,7 +2310,12 @@ function narrowTypeForTypedDictKey(
)
);
} else {
return tdEntry !== undefined && (tdEntry.isRequired || tdEntry.isProvided) ? undefined : subtype;
// Only a known item can be guaranteed to be present. If the key
// matched the "extra items" entry, the key may or may not be
// present, so the subtype cannot be eliminated in this case.
return knownItemEntry !== undefined && (knownItemEntry.isRequired || knownItemEntry.isProvided)
? undefined
: subtype;
}
}

Expand Down
24 changes: 24 additions & 0 deletions packages/pyright-internal/src/tests/samples/typedDictClosed1.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,30 @@ def func1(movie: Movie) -> None:
movie["other2"] = 1


def func2(movie: Movie) -> None:
# An extra item is not guaranteed to be present, so the negative
# branch of the "in" check must remain reachable.
if "novel_adaptation" in movie:
return

reveal_type(movie, expected_text="Movie")

# This should generate a type incompatibility error. If the statements
# above are incorrectly marked unreachable, no error is reported here.
movie["other3"] = 1

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning · Non-blocking recommendation

Please also cover a negative in check on a known required key (such as name) and assert that its following code remains unreachable. This would protect the existing narrowing behavior against an accidental over-relaxation of this change. [verified]



def func3(movie: Movie) -> None:
# "name" is a required known item, so it is always present and the
# negative branch of the "in" check must remain unreachable.
if "name" in movie:
return

# This would generate a type incompatibility error if the statement
# above were incorrectly considered reachable.
movie["other4"] = 1


class MovieBase(TypedDict, extra_items=ReadOnly[str | None]):
name: str

Expand Down
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/tests/typeEvaluator5.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ test('TypedDictReadOnly2', () => {

test('TypedDictClosed1', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['typedDictClosed1.py']);
TestUtils.validateResults(analysisResults, 7);
TestUtils.validateResults(analysisResults, 8);
});

test('TypedDictClosed2', () => {
Expand Down