Skip to content

Commit 3c8d46f

Browse files
committed
Fixed mapping checks against Django's MultiValueDict
Fixes #419.
1 parent 9b10866 commit 3c8d46f

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

docs/versionhistory.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ This library adheres to
1616
(`#432 <https://github.com/agronholm/typeguard/issues/432>`_, PR by Yongxin Wang)
1717
- Fixed detection of optional fields (``NotRequired[...]``) in ``TypedDict`` when using
1818
forward references (`#424 <https://github.com/agronholm/typeguard/issues/424>`_)
19+
- Fixed mapping checks against Django's ``MultiValueDict``
20+
(`#419 <https://github.com/agronholm/typeguard/issues/419>`_)
1921

2022
**4.1.5** (2023-09-11)
2123

src/typeguard/_config.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from collections.abc import Collection
3+
from collections.abc import Iterable
44
from dataclasses import dataclass
55
from enum import Enum, auto
66
from typing import TYPE_CHECKING, TypeVar
@@ -49,11 +49,11 @@ class CollectionCheckStrategy(Enum):
4949
FIRST_ITEM = auto()
5050
ALL_ITEMS = auto()
5151

52-
def iterate_samples(self, collection: Collection[T]) -> Collection[T]:
52+
def iterate_samples(self, collection: Iterable[T]) -> Iterable[T]:
5353
if self is CollectionCheckStrategy.FIRST_ITEM:
54-
if len(collection):
54+
try:
5555
return [next(iter(collection))]
56-
else:
56+
except StopIteration:
5757
return ()
5858
else:
5959
return collection

tests/test_checkers.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,14 @@ def test_bad_value_type_full_check(self):
433433
collection_check_strategy=CollectionCheckStrategy.ALL_ITEMS,
434434
).match("value of key 'y' of dict is not an instance of int")
435435

436+
def test_custom_dict_generator_items(self):
437+
class CustomDict(dict):
438+
def items(self):
439+
for key in self:
440+
yield key, self[key]
441+
442+
check_type(CustomDict(a=1), Dict[str, int])
443+
436444

437445
class TestTypedDict:
438446
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)