Check element types for Deque, MutableSequence and MutableSet - #577
Open
sneha4175 wants to merge 1 commit into
Open
Check element types for Deque, MutableSequence and MutableSet#577sneha4175 wants to merge 1 commit into
sneha4175 wants to merge 1 commit into
Conversation
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes
Typeguard checks the element types of
List,Sequence,SetandFrozenSetannotations, butDeque[T],MutableSequence[T]andMutableSet[T]did not have their element types checked. Their origins (collections.deque,collections.abc.MutableSequence,collections.abc.MutableSet) were not registered inorigin_type_checkers, so they fell through to the generic instance check, which validates the container type but ignoresT.As a result, a value with the wrong element type silently passed, even with the
ALL_ITEMScollection check strategy:check_setandcheck_mappingalready branch on the origin type to handle their concrete/mutable variants (e.g.frozensetvsset,MutableMappingvsMapping). This applies the same approach:check_sequencenow handlescollections.dequeandMutableSequence.check_setnow handlesMutableSet.origin_type_checkers.The stricter container-type check is preserved: a
listis still rejected as aDeque, atupleas aMutableSequence, and afrozensetas aMutableSet. The defaultFIRST_ITEMstrategy behaviour is unchanged.Checklist
tests/) which would fail without your patchdocs/versionhistory.rst)docs/versionhistory.rst)Tests:
TestDeque,TestMutableSequenceandTestMutableSetwere added intests/test_checkers.py, mirroring the existingTestList/TestSequence/TestSetclasses. The full suite passes (546 passed), andruff,ruff-formatandmypyare clean.