Skip to content

Commit 0bbcceb

Browse files
committed
Meet: Add lots of tests.
1 parent d6bf8e9 commit 0bbcceb

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

mypy/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ def zipall(self, right: 'TypedDictType') \
888888
right_item_type = right.items.get(item_name)
889889
yield (item_name, left_item_type, right_item_type)
890890
for (item_name, right_item_type) in right.items.items():
891-
if item_name not in left.items:
891+
if item_name in left.items:
892892
continue
893893
yield (item_name, None, right_item_type)
894894

test-data/unit/check-typeddict.test

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,77 @@ reveal_type(joined2) # E: Revealed type is 'builtins.list[builtins.object*]'
257257

258258
-- Meet
259259

260-
-- TODO: Figure out some way to trigger the TypeMeetVisitor.visit_typeddict_type() path.
260+
[case testMeetOfTypedDictsWithCompatibleCommonKeysHasAllKeysAndNewFallback]
261+
from mypy_extensions import TypedDict
262+
from typing import TypeVar, Callable
263+
XY = TypedDict('XY', {'x': int, 'y': int})
264+
YZ = TypedDict('YZ', {'y': int, 'z': int})
265+
T = TypeVar('T')
266+
def f(x: Callable[[T, T], None]) -> T: pass
267+
def g(x: XY, y: YZ) -> None: pass
268+
reveal_type(f(g)) # E: Revealed type is 'TypedDict(x=builtins.int, y=builtins.int, z=builtins.int, _fallback=typing.Mapping[builtins.str, builtins.int])'
269+
[builtins fixtures/dict.pyi]
270+
271+
[case testMeetOfTypedDictsWithIncompatibleCommonKeysIsUninhabited]
272+
# flags: --strict-optional
273+
from mypy_extensions import TypedDict
274+
from typing import TypeVar, Callable
275+
XYa = TypedDict('XYa', {'x': int, 'y': int})
276+
YbZ = TypedDict('YbZ', {'y': object, 'z': int})
277+
T = TypeVar('T')
278+
def f(x: Callable[[T, T], None]) -> T: pass
279+
def g(x: XYa, y: YbZ) -> None: pass
280+
reveal_type(f(g)) # E: Revealed type is '<uninhabited>'
281+
[builtins fixtures/dict.pyi]
282+
283+
[case testMeetOfTypedDictsWithNoCommonKeysHasAllKeysAndNewFallback]
284+
from mypy_extensions import TypedDict
285+
from typing import TypeVar, Callable
286+
X = TypedDict('X', {'x': int})
287+
Z = TypedDict('Z', {'z': int})
288+
T = TypeVar('T')
289+
def f(x: Callable[[T, T], None]) -> T: pass
290+
def g(x: X, y: Z) -> None: pass
291+
reveal_type(f(g)) # E: Revealed type is 'TypedDict(x=builtins.int, z=builtins.int, _fallback=typing.Mapping[builtins.str, builtins.int])'
292+
[builtins fixtures/dict.pyi]
293+
294+
# TODO: It would be more accurate for the meet to be TypedDict instead.
295+
[case testMeetOfTypedDictWithCompatibleMappingIsUninhabitedForNow]
296+
# flags: --strict-optional
297+
from mypy_extensions import TypedDict
298+
from typing import TypeVar, Callable, Mapping
299+
X = TypedDict('X', {'x': int})
300+
M = Mapping[str, int]
301+
T = TypeVar('T')
302+
def f(x: Callable[[T, T], None]) -> T: pass
303+
def g(x: X, y: M) -> None: pass
304+
reveal_type(f(g)) # E: Revealed type is '<uninhabited>'
305+
[builtins fixtures/dict.pyi]
306+
307+
[case testMeetOfTypedDictWithIncompatibleMappingIsUninhabited]
308+
# flags: --strict-optional
309+
from mypy_extensions import TypedDict
310+
from typing import TypeVar, Callable, Mapping
311+
X = TypedDict('X', {'x': int})
312+
M = Mapping[str, str]
313+
T = TypeVar('T')
314+
def f(x: Callable[[T, T], None]) -> T: pass
315+
def g(x: X, y: M) -> None: pass
316+
reveal_type(f(g)) # E: Revealed type is '<uninhabited>'
317+
[builtins fixtures/dict.pyi]
318+
319+
# TODO: It would be more accurate for the meet to be TypedDict instead.
320+
[case testMeetOfTypedDictWithCompatibleMappingSuperclassIsUninhabitedForNow]
321+
# flags: --strict-optional
322+
from mypy_extensions import TypedDict
323+
from typing import TypeVar, Callable, Iterable
324+
X = TypedDict('X', {'x': int})
325+
I = Iterable[str]
326+
T = TypeVar('T')
327+
def f(x: Callable[[T, T], None]) -> T: pass
328+
def g(x: X, y: I) -> None: pass
329+
reveal_type(f(g)) # E: Revealed type is '<uninhabited>'
330+
[builtins fixtures/dict.pyi]
261331

262332

263333
-- Constraint Solver

0 commit comments

Comments
 (0)