Skip to content

Check element types for Deque, MutableSequence and MutableSet - #577

Open
sneha4175 wants to merge 1 commit into
agronholm:masterfrom
sneha4175:fix/check-deque-mutable-sequence-set-elements
Open

Check element types for Deque, MutableSequence and MutableSet#577
sneha4175 wants to merge 1 commit into
agronholm:masterfrom
sneha4175:fix/check-deque-mutable-sequence-set-elements

Conversation

@sneha4175

Copy link
Copy Markdown

Changes

Typeguard checks the element types of List, Sequence, Set and FrozenSet annotations, but Deque[T], MutableSequence[T] and MutableSet[T] did not have their element types checked. Their origins (collections.deque, collections.abc.MutableSequence, collections.abc.MutableSet) were not registered in origin_type_checkers, so they fell through to the generic instance check, which validates the container type but ignores T.

As a result, a value with the wrong element type silently passed, even with the ALL_ITEMS collection check strategy:

from collections import deque
from typeguard import check_type, CollectionCheckStrategy

# All of these wrongly passed before this change:
check_type(deque([1, "a"]), Deque[int],
           collection_check_strategy=CollectionCheckStrategy.ALL_ITEMS)
check_type([1, "a"], MutableSequence[int],
           collection_check_strategy=CollectionCheckStrategy.ALL_ITEMS)
check_type({1, "a"}, MutableSet[int],
           collection_check_strategy=CollectionCheckStrategy.ALL_ITEMS)

check_set and check_mapping already branch on the origin type to handle their concrete/mutable variants (e.g. frozenset vs set, MutableMapping vs Mapping). This applies the same approach:

  • check_sequence now handles collections.deque and MutableSequence.
  • check_set now handles MutableSet.
  • The three origins are registered in origin_type_checkers.

The stricter container-type check is preserved: a list is still rejected as a Deque, a tuple as a MutableSequence, and a frozenset as a MutableSet. The default FIRST_ITEM strategy behaviour is unchanged.

Checklist

  • You've added tests (in tests/) which would fail without your patch
  • You've updated the documentation (changelog entry in docs/versionhistory.rst)
  • You've added a new changelog entry (in docs/versionhistory.rst)

Tests: TestDeque, TestMutableSequence and TestMutableSet were added in tests/test_checkers.py, mirroring the existing TestList/TestSequence/TestSet classes. The full suite passes (546 passed), and ruff, ruff-format and mypy are clean.

typeguard checks the element types of List, Sequence, Set and FrozenSet
annotations, but Deque[T], MutableSequence[T] and MutableSet[T] fell
through to the generic instance check, which validated the container
type but silently ignored T. As a result a value like
deque([1, "a"]) passed a check_type(..., Deque[int]) even under the
ALL_ITEMS collection check strategy.

check_set and check_mapping already branch on the origin type to handle
their concrete/mutable variants; do the same in check_sequence for
collections.deque and MutableSequence, add a MutableSet branch to
check_set, and register the new origins so element types are checked
while the stricter container-type check is preserved (a list is still
rejected as a Deque, a tuple as a MutableSequence and a frozenset as a
MutableSet).
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 94.856% (+0.03%) from 94.83% — sneha4175:fix/check-deque-mutable-sequence-set-elements into agronholm:master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants