@@ -2146,6 +2146,132 @@ Foo = TypedDict('Foo', {'camelCaseKey': str})
21462146value: Foo = {} # E: Missing key "camelCaseKey" for TypedDict "Foo"
21472147[builtins fixtures/dict.pyi]
21482148
2149+ -- Required[]
2150+
2151+ [case testDoesRecognizeRequiredInTypedDictWithClass]
2152+ from typing import TypedDict
2153+ from typing import Required
2154+ class Movie(TypedDict, total=False):
2155+ title: Required[str]
2156+ year: int
2157+ m = Movie(title='The Matrix')
2158+ m = Movie() # E: Missing key "title" for TypedDict "Movie"
2159+ [typing fixtures/typing-typeddict.pyi]
2160+
2161+ [case testDoesRecognizeRequiredInTypedDictWithAssignment]
2162+ from typing import TypedDict
2163+ from typing import Required
2164+ Movie = TypedDict('Movie', {
2165+ 'title': Required[str],
2166+ 'year': int,
2167+ }, total=False)
2168+ m = Movie(title='The Matrix')
2169+ m = Movie() # E: Missing key "title" for TypedDict "Movie"
2170+ [typing fixtures/typing-typeddict.pyi]
2171+
2172+ [case testDoesDisallowRequiredOutsideOfTypedDict]
2173+ from typing import Required
2174+ x: Required[int] = 42 # E: Required[] can be only used in a TypedDict definition
2175+ [typing fixtures/typing-typeddict.pyi]
2176+
2177+ [case testDoesOnlyAllowRequiredInsideTypedDictAtTopLevel]
2178+ from typing import TypedDict
2179+ from typing import Union
2180+ from typing import Required
2181+ Movie = TypedDict('Movie', {
2182+ 'title': Union[
2183+ Required[str], # E: Required[] can be only used in a TypedDict definition
2184+ bytes
2185+ ],
2186+ 'year': int,
2187+ }, total=False)
2188+ [typing fixtures/typing-typeddict.pyi]
2189+
2190+ [case testDoesDisallowRequiredInsideRequired]
2191+ from typing import TypedDict
2192+ from typing import Union
2193+ from typing import Required
2194+ Movie = TypedDict('Movie', {
2195+ 'title': Required[Union[
2196+ Required[str], # E: Required[] can be only used in a TypedDict definition
2197+ bytes
2198+ ]],
2199+ 'year': int,
2200+ }, total=False)
2201+ [typing fixtures/typing-typeddict.pyi]
2202+
2203+ [case testRequiredOnlyAllowsOneItem]
2204+ from typing import TypedDict
2205+ from typing import Required
2206+ class Movie(TypedDict, total=False):
2207+ title: Required[str, bytes] # E: Required[] must have exactly one type argument
2208+ year: int
2209+ [typing fixtures/typing-typeddict.pyi]
2210+
2211+
2212+ -- NotRequired[]
2213+
2214+ [case testDoesRecognizeNotRequiredInTypedDictWithClass]
2215+ from typing import TypedDict
2216+ from typing import NotRequired
2217+ class Movie(TypedDict):
2218+ title: str
2219+ year: NotRequired[int]
2220+ m = Movie(title='The Matrix')
2221+ m = Movie() # E: Missing key "title" for TypedDict "Movie"
2222+ [typing fixtures/typing-typeddict.pyi]
2223+
2224+ [case testDoesRecognizeNotRequiredInTypedDictWithAssignment]
2225+ from typing import TypedDict
2226+ from typing import NotRequired
2227+ Movie = TypedDict('Movie', {
2228+ 'title': str,
2229+ 'year': NotRequired[int],
2230+ })
2231+ m = Movie(title='The Matrix')
2232+ m = Movie() # E: Missing key "title" for TypedDict "Movie"
2233+ [typing fixtures/typing-typeddict.pyi]
2234+
2235+ [case testDoesDisallowNotRequiredOutsideOfTypedDict]
2236+ from typing import NotRequired
2237+ x: NotRequired[int] = 42 # E: NotRequired[] can be only used in a TypedDict definition
2238+ [typing fixtures/typing-typeddict.pyi]
2239+
2240+ [case testDoesOnlyAllowNotRequiredInsideTypedDictAtTopLevel]
2241+ from typing import TypedDict
2242+ from typing import Union
2243+ from typing import NotRequired
2244+ Movie = TypedDict('Movie', {
2245+ 'title': Union[
2246+ NotRequired[str], # E: NotRequired[] can be only used in a TypedDict definition
2247+ bytes
2248+ ],
2249+ 'year': int,
2250+ })
2251+ [typing fixtures/typing-typeddict.pyi]
2252+
2253+ [case testDoesDisallowNotRequiredInsideNotRequired]
2254+ from typing import TypedDict
2255+ from typing import Union
2256+ from typing import NotRequired
2257+ Movie = TypedDict('Movie', {
2258+ 'title': NotRequired[Union[
2259+ NotRequired[str], # E: NotRequired[] can be only used in a TypedDict definition
2260+ bytes
2261+ ]],
2262+ 'year': int,
2263+ })
2264+ [typing fixtures/typing-typeddict.pyi]
2265+
2266+ [case testNotRequiredOnlyAllowsOneItem]
2267+ from typing import TypedDict
2268+ from typing import NotRequired
2269+ class Movie(TypedDict):
2270+ title: NotRequired[str, bytes] # E: NotRequired[] must have exactly one type argument
2271+ year: int
2272+ [typing fixtures/typing-typeddict.pyi]
2273+
2274+ -- Union dunders
21492275
21502276[case testTypedDictUnionGetItem]
21512277from typing import TypedDict, Union
0 commit comments