Skip to content

Commit 9e686ba

Browse files
authored
fix(matchers): loosed DictMatching types to accept any key type (#296)
1 parent 9a77b1a commit 9e686ba

2 files changed

Lines changed: 15 additions & 8 deletions

File tree

decoy/matchers.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ def test_logger_called(decoy: Decoy):
2828
"""
2929

3030
from re import compile as compile_re
31-
from typing import cast, Any, List, Mapping, Optional, Pattern, Type, TypeVar
32-
31+
from typing import Any, List, Mapping, Optional, Pattern, Type, TypeVar, cast
3332

3433
__all__ = [
3534
"Anything",
@@ -212,9 +211,9 @@ class HelloWorld:
212211

213212

214213
class _DictMatching:
215-
_values: Mapping[str, Any]
214+
_values: Mapping[Any, Any]
216215

217-
def __init__(self, values: Mapping[str, Any]) -> None:
216+
def __init__(self, values: Mapping[Any, Any]) -> None:
218217
self._values = values
219218

220219
def __eq__(self, target: object) -> bool:
@@ -235,7 +234,7 @@ def __repr__(self) -> str:
235234
return f"<DictMatching {self._values!r}>"
236235

237236

238-
def DictMatching(values: Mapping[str, Any]) -> Any:
237+
def DictMatching(values: Mapping[Any, Any]) -> Any:
239238
"""Match any dictionary with the passed in keys / values.
240239
241240
Arguments:

tests/test_matchers.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
"""Matcher tests."""
22

3-
import pytest
43
from collections import namedtuple
5-
from decoy import Decoy, matchers
64
from typing import Any, List, NamedTuple
5+
6+
import pytest
7+
8+
from decoy import Decoy, matchers
9+
710
from .fixtures import SomeClass
811

912

@@ -91,7 +94,7 @@ def test_has_attribute_matcher() -> None:
9194

9295

9396
def test_dict_matching_matcher() -> None:
94-
"""It should have an "anything with these attributes" matcher."""
97+
"""It should have a "dictionary matching" matcher."""
9598
assert {"hello": "world"} == matchers.DictMatching({"hello": "world"})
9699
assert {"hello": "world", "goodbye": "so long"} == matchers.DictMatching(
97100
{"hello": "world"}
@@ -106,6 +109,11 @@ def test_dict_matching_matcher() -> None:
106109
assert [] != matchers.DictMatching({"hello": "world"})
107110

108111

112+
def test_dict_matching_matcher_with_int_key() -> None:
113+
"""Dict matcher should suport non-string keys."""
114+
assert {1: "hello", 2: "world"} == matchers.DictMatching({2: "world"})
115+
116+
109117
def test_list_matching_matcher() -> None:
110118
"""It should have a "contains this sub-list" matcher."""
111119
assert [1, 2, 3] == matchers.ListMatching([1])

0 commit comments

Comments
 (0)