Skip to content

Commit

Permalink
Fixed frozenset not being checked
Browse files Browse the repository at this point in the history
Fixes #367.
  • Loading branch information
agronholm committed Jul 27, 2023
1 parent 686f25a commit 00ac621
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ This library adheres to `Semantic Versioning 2.0 <https://semver.org/#semantic-v
- Fixed ``@typechecked`` failing to instrument functions when there are more than one
function within the same scope
(`#355 <https://github.com/agronholm/typeguard/issues/355>`_)
- Fixed ``frozenset`` not being checked
(`#367 <https://github.com/agronholm/typeguard/issues/367>`_)

**4.0.0** (2023-05-12)

Expand Down
6 changes: 5 additions & 1 deletion src/typeguard/_checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,10 @@ def check_set(
args: tuple[Any, ...],
memo: TypeCheckMemo,
) -> None:
if not isinstance(value, AbstractSet):
if origin_type is frozenset:
if not isinstance(value, frozenset):
raise TypeCheckError("is not a frozenset")
elif not isinstance(value, AbstractSet):
raise TypeCheckError("is not a set")

if args and args != (Any,):
Expand Down Expand Up @@ -794,6 +797,7 @@ def check_type_internal(
dict: check_mapping,
Dict: check_mapping,
float: check_number,
frozenset: check_set,
IO: check_io,
list: check_list,
List: check_list,
Expand Down
33 changes: 33 additions & 0 deletions tests/test_checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
ContextManager,
Dict,
ForwardRef,
FrozenSet,
Iterator,
List,
Mapping,
Expand Down Expand Up @@ -606,6 +607,38 @@ def test_full_check_fail(self):
).match("set is not an instance of int")


class TestFrozenSet:
def test_bad_type(self):
pytest.raises(TypeCheckError, check_type, 5, FrozenSet[int]).match(
"int is not a frozenset"
)

def test_valid(self):
check_type(frozenset({1, 2}), FrozenSet[int])

def test_first_check_empty(self):
check_type(frozenset(), FrozenSet[int])

def test_first_check_fail(self, sample_set: set):
pytest.raises(
TypeCheckError, check_type, frozenset(sample_set), FrozenSet[int]
).match("set is not an instance of int")

def test_full_check_fail(self):
pytest.raises(
TypeCheckError,
check_type,
frozenset({1, 2, "bb"}),
FrozenSet[int],
collection_check_strategy=CollectionCheckStrategy.ALL_ITEMS,
).match("set is not an instance of int")

def test_set_against_frozenset(self, sample_set: set):
pytest.raises(TypeCheckError, check_type, sample_set, FrozenSet[int]).match(
"set is not a frozenset"
)


@pytest.mark.parametrize(
"annotated_type",
[
Expand Down

0 comments on commit 00ac621

Please sign in to comment.