Skip to content

Commit fdc1954

Browse files
authored
Add tests for TestCase.assertDictEqual() (#11154)
1 parent 1600850 commit fdc1954

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

stdlib/unittest/case.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ class TestCase:
249249
def assertListEqual(self, list1: list[Any], list2: list[Any], msg: Any = None) -> None: ...
250250
def assertTupleEqual(self, tuple1: tuple[Any, ...], tuple2: tuple[Any, ...], msg: Any = None) -> None: ...
251251
def assertSetEqual(self, set1: AbstractSet[object], set2: AbstractSet[object], msg: Any = None) -> None: ...
252+
# assertDictEqual accepts only true dict instances. We can't use that here, since that would make
253+
# assertDictEqual incompatible with TypedDict.
252254
def assertDictEqual(self, d1: Mapping[Any, object], d2: Mapping[Any, object], msg: Any = None) -> None: ...
253255
def fail(self, msg: Any = None) -> NoReturn: ...
254256
def countTestCases(self) -> int: ...

test_cases/stdlib/check_unittest.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from __future__ import annotations
22

33
import unittest
4+
from collections.abc import Iterator, Mapping
45
from datetime import datetime, timedelta
56
from decimal import Decimal
67
from fractions import Fraction
7-
from typing_extensions import assert_type
8+
from typing_extensions import TypedDict, assert_type
89
from unittest.mock import MagicMock, Mock, patch
910

1011
case = unittest.TestCase()
@@ -89,6 +90,54 @@ def __gt__(self, other: Bacon) -> bool:
8990
case.assertGreater(Ham(), Bacon()) # type: ignore
9091
case.assertGreater(Bacon(), Ham()) # type: ignore
9192

93+
94+
###
95+
# Tests for assertDictEqual
96+
###
97+
98+
99+
class TD1(TypedDict):
100+
x: int
101+
y: str
102+
103+
104+
class TD2(TypedDict):
105+
a: bool
106+
b: bool
107+
108+
109+
class MyMapping(Mapping[str, int]):
110+
def __getitem__(self, __key: str) -> int:
111+
return 42
112+
113+
def __iter__(self) -> Iterator[str]:
114+
return iter([])
115+
116+
def __len__(self) -> int:
117+
return 0
118+
119+
120+
td1: TD1 = {"x": 1, "y": "foo"}
121+
td2: TD2 = {"a": True, "b": False}
122+
m = MyMapping()
123+
124+
case.assertDictEqual({}, {})
125+
case.assertDictEqual({"x": 1, "y": 2}, {"x": 1, "y": 2})
126+
case.assertDictEqual({"x": 1, "y": "foo"}, {"y": "foo", "x": 1})
127+
case.assertDictEqual({"x": 1}, {})
128+
case.assertDictEqual({}, {"x": 1})
129+
case.assertDictEqual({1: "x"}, {"y": 222})
130+
case.assertDictEqual({1: "x"}, td1)
131+
case.assertDictEqual(td1, {1: "x"})
132+
case.assertDictEqual(td1, td2)
133+
134+
case.assertDictEqual(1, {}) # type: ignore
135+
case.assertDictEqual({}, 1) # type: ignore
136+
137+
# These should fail, but don't due to TypedDict limitations:
138+
# case.assertDictEqual(m, {"": 0}) # xtype: ignore
139+
# case.assertDictEqual({"": 0}, m) # xtype: ignore
140+
92141
###
93142
# Tests for mock.patch
94143
###

0 commit comments

Comments
 (0)