Skip to content

Commit a68b87a

Browse files
committed
remove more @finals + improve tests
1 parent a8dbd22 commit a68b87a

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

test-data/unit/check-typeddict.test

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2035,25 +2035,28 @@ else:
20352035
[typing fixtures/typing-typeddict.pyi]
20362036

20372037
[case testOperatorContainsNarrowsTypedDicts_total]
2038+
# flags: --warn-unreachable
20382039
from __future__ import annotations
20392040
from typing import assert_type, Literal, TypedDict, TypeVar, Union
2040-
from typing_extensions import final
20412041

20422042
class D1(TypedDict):
20432043
foo: int
20442044

2045-
20462045
class D2(TypedDict):
20472046
bar: int
20482047

2049-
20502048
d: D1 | D2
20512049

20522050
if 'foo' in d:
20532051
assert_type(d, D1)
20542052
else:
20552053
assert_type(d, D2)
20562054

2055+
if 'unknown_key' in d:
2056+
object() # E: Statement is unreachable
2057+
else:
2058+
assert_type(d, Union[D1, D2])
2059+
20572060
foo_or_bar: Literal['foo', 'bar']
20582061
if foo_or_bar in d:
20592062
assert_type(d, Union[D1, D2])
@@ -2079,25 +2082,20 @@ def f(arg: TD) -> None:
20792082
else:
20802083
assert_type(arg['bar'], int)
20812084

2082-
20832085
[builtins fixtures/dict.pyi]
20842086
[typing fixtures/typing-typeddict.pyi]
20852087

2086-
[case testOperatorContainsNarrowsTypedDicts_partialThroughTotalFalse]
2088+
[case testOperatorContainsNarrowsTypedDicts_totalFalse]
2089+
# flags: --warn-unreachable
20872090
from __future__ import annotations
20882091
from typing import assert_type, Literal, TypedDict, Union
2089-
from typing_extensions import final
20902092

2091-
@final
20922093
class DTotal(TypedDict):
20932094
required_key: int
20942095

2095-
2096-
@final
20972096
class DNotTotal(TypedDict, total=False):
20982097
optional_key: int
20992098

2100-
21012099
d: DTotal | DNotTotal
21022100

21032101
if 'required_key' in d:
@@ -2110,6 +2108,11 @@ if 'optional_key' in d:
21102108
else:
21112109
assert_type(d, Union[DTotal, DNotTotal])
21122110

2111+
if 'unknown_key' in d:
2112+
object() # E: Statement is unreachable
2113+
else:
2114+
assert_type(d, Union[DTotal, DNotTotal])
2115+
21132116
key: Literal['optional_key', 'required_key']
21142117
if key in d:
21152118
assert_type(d, Union[DTotal, DNotTotal])
@@ -2119,23 +2122,19 @@ else:
21192122
[builtins fixtures/dict.pyi]
21202123
[typing fixtures/typing-typeddict.pyi]
21212124

2122-
[case testOperatorContainsNarrowsTypedDicts_partialThroughNotRequired]
2125+
[case testOperatorContainsNarrowsTypedDicts_NotRequired]
2126+
# flags: --warn-unreachable
21232127
from __future__ import annotations
21242128
from typing import assert_type, Required, NotRequired, TypedDict, Union
2125-
from typing_extensions import final
21262129

2127-
@final
21282130
class D1(TypedDict):
21292131
required_key: Required[int]
21302132
optional_key: NotRequired[int]
21312133

2132-
2133-
@final
21342134
class D2(TypedDict):
21352135
abc: int
21362136
xyz: int
21372137

2138-
21392138
d: D1 | D2
21402139

21412140
if 'required_key' in d:
@@ -2148,6 +2147,11 @@ if 'optional_key' in d:
21482147
else:
21492148
assert_type(d, Union[D1, D2])
21502149

2150+
if 'unknown_key' in d:
2151+
object() # E: Statement is unreachable
2152+
else:
2153+
assert_type(d, Union[D1, D2])
2154+
21512155
[builtins fixtures/dict.pyi]
21522156
[typing fixtures/typing-typeddict.pyi]
21532157

0 commit comments

Comments
 (0)