@@ -3236,3 +3236,146 @@ def foo(x: int) -> Foo: ...
3236
3236
f: Foo = {**foo("no")} # E: Argument 1 to "foo" has incompatible type "str"; expected "int"
3237
3237
[builtins fixtures/dict.pyi]
3238
3238
[typing fixtures/typing-typeddict.pyi]
3239
+
3240
+
3241
+ [case testTypedDictWith__or__method]
3242
+ from typing import Dict
3243
+ from mypy_extensions import TypedDict
3244
+
3245
+ class Foo(TypedDict):
3246
+ key: int
3247
+
3248
+ foo1: Foo = {'key': 1}
3249
+ foo2: Foo = {'key': 2}
3250
+
3251
+ reveal_type(foo1 | foo2) # N: Revealed type is "TypedDict('__main__.Foo', {'key': builtins.int})"
3252
+ reveal_type(foo1 | {'key': 1}) # N: Revealed type is "TypedDict('__main__.Foo', {'key': builtins.int})"
3253
+ reveal_type(foo1 | {'key': 'a'}) # N: Revealed type is "builtins.dict[builtins.str, builtins.object]"
3254
+ reveal_type(foo1 | {}) # N: Revealed type is "TypedDict('__main__.Foo', {'key': builtins.int})"
3255
+
3256
+ d1: Dict[str, int]
3257
+ d2: Dict[int, str]
3258
+
3259
+ reveal_type(foo1 | d1) # N: Revealed type is "builtins.dict[builtins.str, builtins.object]"
3260
+ foo1 | d2 # E: Unsupported operand types for | ("Foo" and "Dict[int, str]")
3261
+
3262
+
3263
+ class Bar(TypedDict):
3264
+ key: int
3265
+ value: str
3266
+
3267
+ bar: Bar
3268
+ reveal_type(bar | {}) # N: Revealed type is "TypedDict('__main__.Bar', {'key': builtins.int, 'value': builtins.str})"
3269
+ reveal_type(bar | {'key': 1, 'value': 'v'}) # N: Revealed type is "TypedDict('__main__.Bar', {'key': builtins.int, 'value': builtins.str})"
3270
+ reveal_type(bar | {'key': 1}) # N: Revealed type is "TypedDict('__main__.Bar', {'key': builtins.int, 'value': builtins.str})"
3271
+ reveal_type(bar | {'value': 'v'}) # N: Revealed type is "TypedDict('__main__.Bar', {'key': builtins.int, 'value': builtins.str})"
3272
+ reveal_type(bar | {'key': 'a'}) # N: Revealed type is "builtins.dict[builtins.str, builtins.object]"
3273
+ reveal_type(bar | {'value': 1}) # N: Revealed type is "builtins.dict[builtins.str, builtins.object]"
3274
+ reveal_type(bar | {'key': 'a', 'value': 1}) # N: Revealed type is "builtins.dict[builtins.str, builtins.object]"
3275
+
3276
+ reveal_type(bar | foo1) # N: Revealed type is "TypedDict('__main__.Bar', {'key': builtins.int, 'value': builtins.str})"
3277
+ reveal_type(bar | d1) # N: Revealed type is "builtins.dict[builtins.str, builtins.object]"
3278
+ bar | d2 # E: Unsupported operand types for | ("Bar" and "Dict[int, str]")
3279
+ [builtins fixtures/dict.pyi]
3280
+ [typing fixtures/typing-typeddict-iror.pyi]
3281
+
3282
+ [case testTypedDictWith__or__method_error]
3283
+ from mypy_extensions import TypedDict
3284
+
3285
+ class Foo(TypedDict):
3286
+ key: int
3287
+
3288
+ foo: Foo = {'key': 1}
3289
+ foo | 1
3290
+
3291
+ class SubDict(dict): ...
3292
+ foo | SubDict()
3293
+ [out]
3294
+ main:7: error: No overload variant of "__or__" of "TypedDict" matches argument type "int"
3295
+ main:7: note: Possible overload variants:
3296
+ main:7: note: def __or__(self, TypedDict({'key'?: int}), /) -> Foo
3297
+ main:7: note: def __or__(self, Dict[str, Any], /) -> Dict[str, object]
3298
+ main:10: error: No overload variant of "__ror__" of "dict" matches argument type "Foo"
3299
+ main:10: note: Possible overload variants:
3300
+ main:10: note: def __ror__(self, Dict[Any, Any], /) -> Dict[Any, Any]
3301
+ main:10: note: def [T, T2] __ror__(self, Dict[T, T2], /) -> Dict[Union[Any, T], Union[Any, T2]]
3302
+ [builtins fixtures/dict.pyi]
3303
+ [typing fixtures/typing-typeddict-iror.pyi]
3304
+
3305
+ [case testTypedDictWith__ror__method]
3306
+ from typing import Dict
3307
+ from mypy_extensions import TypedDict
3308
+
3309
+ class Foo(TypedDict):
3310
+ key: int
3311
+
3312
+ foo: Foo = {'key': 1}
3313
+
3314
+ reveal_type({'key': 1} | foo) # N: Revealed type is "TypedDict('__main__.Foo', {'key': builtins.int})"
3315
+ reveal_type({'key': 'a'} | foo) # N: Revealed type is "builtins.dict[builtins.str, builtins.object]"
3316
+ reveal_type({} | foo) # N: Revealed type is "TypedDict('__main__.Foo', {'key': builtins.int})"
3317
+ {1: 'a'} | foo # E: Dict entry 0 has incompatible type "int": "str"; expected "str": "Any"
3318
+
3319
+ d1: Dict[str, int]
3320
+ d2: Dict[int, str]
3321
+
3322
+ reveal_type(d1 | foo) # N: Revealed type is "builtins.dict[builtins.str, builtins.object]"
3323
+ d2 | foo # E: Unsupported operand types for | ("Dict[int, str]" and "Foo")
3324
+ 1 | foo # E: Unsupported left operand type for | ("int")
3325
+
3326
+
3327
+ class Bar(TypedDict):
3328
+ key: int
3329
+ value: str
3330
+
3331
+ bar: Bar
3332
+ reveal_type({} | bar) # N: Revealed type is "TypedDict('__main__.Bar', {'key': builtins.int, 'value': builtins.str})"
3333
+ reveal_type({'key': 1, 'value': 'v'} | bar) # N: Revealed type is "TypedDict('__main__.Bar', {'key': builtins.int, 'value': builtins.str})"
3334
+ reveal_type({'key': 1} | bar) # N: Revealed type is "TypedDict('__main__.Bar', {'key': builtins.int, 'value': builtins.str})"
3335
+ reveal_type({'value': 'v'} | bar) # N: Revealed type is "TypedDict('__main__.Bar', {'key': builtins.int, 'value': builtins.str})"
3336
+ reveal_type({'key': 'a'} | bar) # N: Revealed type is "builtins.dict[builtins.str, builtins.object]"
3337
+ reveal_type({'value': 1} | bar) # N: Revealed type is "builtins.dict[builtins.str, builtins.object]"
3338
+ reveal_type({'key': 'a', 'value': 1} | bar) # N: Revealed type is "builtins.dict[builtins.str, builtins.object]"
3339
+
3340
+ reveal_type(d1 | bar) # N: Revealed type is "builtins.dict[builtins.str, builtins.object]"
3341
+ d2 | bar # E: Unsupported operand types for | ("Dict[int, str]" and "Bar")
3342
+ [builtins fixtures/dict.pyi]
3343
+ [typing fixtures/typing-typeddict-iror.pyi]
3344
+
3345
+ [case testTypedDictWith__ior__method]
3346
+ from typing import Dict
3347
+ from mypy_extensions import TypedDict
3348
+
3349
+ class Foo(TypedDict):
3350
+ key: int
3351
+
3352
+ foo: Foo = {'key': 1}
3353
+ foo |= {'key': 2}
3354
+
3355
+ foo |= {}
3356
+ foo |= {'key': 'a', 'b': 'a'} # E: Expected TypedDict key "key" but found keys ("key", "b") \
3357
+ # E: Incompatible types (expression has type "str", TypedDict item "key" has type "int")
3358
+ foo |= {'b': 2} # E: Unexpected TypedDict key "b"
3359
+
3360
+ d1: Dict[str, int]
3361
+ d2: Dict[int, str]
3362
+
3363
+ foo |= d1 # E: Argument 1 to "__ior__" of "TypedDict" has incompatible type "Dict[str, int]"; expected "TypedDict({'key'?: int})"
3364
+ foo |= d2 # E: Argument 1 to "__ior__" of "TypedDict" has incompatible type "Dict[int, str]"; expected "TypedDict({'key'?: int})"
3365
+
3366
+
3367
+ class Bar(TypedDict):
3368
+ key: int
3369
+ value: str
3370
+
3371
+ bar: Bar
3372
+ bar |= {}
3373
+ bar |= {'key': 1, 'value': 'a'}
3374
+ bar |= {'key': 'a', 'value': 'a', 'b': 'a'} # E: Expected TypedDict keys ("key", "value") but found keys ("key", "value", "b") \
3375
+ # E: Incompatible types (expression has type "str", TypedDict item "key" has type "int")
3376
+
3377
+ bar |= foo
3378
+ bar |= d1 # E: Argument 1 to "__ior__" of "TypedDict" has incompatible type "Dict[str, int]"; expected "TypedDict({'key'?: int, 'value'?: str})"
3379
+ bar |= d2 # E: Argument 1 to "__ior__" of "TypedDict" has incompatible type "Dict[int, str]"; expected "TypedDict({'key'?: int, 'value'?: str})"
3380
+ [builtins fixtures/dict.pyi]
3381
+ [typing fixtures/typing-typeddict-iror.pyi]
0 commit comments