Skip to content

Commit

Permalink
Fixed mapping checks against Django's MultiValueDict
Browse files Browse the repository at this point in the history
Fixes #419.
  • Loading branch information
agronholm committed Mar 23, 2024
1 parent 9b10866 commit 3c8d46f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ This library adheres to
(`#432 <https://github.com/agronholm/typeguard/issues/432>`_, PR by Yongxin Wang)
- Fixed detection of optional fields (``NotRequired[...]``) in ``TypedDict`` when using
forward references (`#424 <https://github.com/agronholm/typeguard/issues/424>`_)
- Fixed mapping checks against Django's ``MultiValueDict``
(`#419 <https://github.com/agronholm/typeguard/issues/419>`_)

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

Expand Down
8 changes: 4 additions & 4 deletions src/typeguard/_config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from collections.abc import Collection
from collections.abc import Iterable
from dataclasses import dataclass
from enum import Enum, auto
from typing import TYPE_CHECKING, TypeVar
Expand Down Expand Up @@ -49,11 +49,11 @@ class CollectionCheckStrategy(Enum):
FIRST_ITEM = auto()
ALL_ITEMS = auto()

def iterate_samples(self, collection: Collection[T]) -> Collection[T]:
def iterate_samples(self, collection: Iterable[T]) -> Iterable[T]:
if self is CollectionCheckStrategy.FIRST_ITEM:
if len(collection):
try:
return [next(iter(collection))]
else:
except StopIteration:
return ()
else:
return collection
Expand Down
8 changes: 8 additions & 0 deletions tests/test_checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,14 @@ def test_bad_value_type_full_check(self):
collection_check_strategy=CollectionCheckStrategy.ALL_ITEMS,
).match("value of key 'y' of dict is not an instance of int")

def test_custom_dict_generator_items(self):
class CustomDict(dict):
def items(self):
for key in self:
yield key, self[key]

check_type(CustomDict(a=1), Dict[str, int])


class TestTypedDict:
@pytest.mark.parametrize(
Expand Down

0 comments on commit 3c8d46f

Please sign in to comment.