Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check_type() on nested list of TypedDict doesn't check all elements of list #417

Closed
2 tasks done
devinnasar opened this issue Nov 16, 2023 · 1 comment
Closed
2 tasks done
Labels

Comments

@devinnasar
Copy link

Things to check first

  • I have searched the existing issues and didn't find my bug already reported there

  • I have checked that my bug is still present in the latest release

Typeguard version

4.1.5

Python version

3.11

What happened?

I'm attempting to force validation on my function arguments, one of which is itself a list of dicts with known keys. Typeguard only seems to be performing type validation on the first element of the list of dicts. I'm stumped as to why this is happening.

from typing import TypedDict, Unpack, Required, NotRequired, List
from typeguard import check_type

class entitySearch_tag_param_type(TypedDict):
    key: str
    value: str

class entitySearch_query_param_types(TypedDict, total=False):
    alertSeverity: str
    domain: str 
    name: str 
    tags: List[entitySearch_tag_param_type]
    type: str 

class myClass():
    # inits etc ...
    def construct_entitySearch_querystring(self, **kwargs):
        print(kwargs)
        check_type(kwargs, entitySearch_query_param_types)

def main():

    # tagsDict = [{'key':"12",'value':'y'}] # passes

    # tagsDict = [{'snarfblat':'x', 'dinglehopper':'y'}] # fails with:
    # File "/Users/devin.nasar/project/.venv/lib/python3.11/site-packages/typeguard/_checkers.py", line 256, in check_typed_dict
    #     raise TypeCheckError(f"has unexpected extra key(s): {keys_formatted}")
    # typeguard.TypeCheckError: item 0 of value of key 'tags' of dict has unexpected extra key(s): "dinglehopper", "snarfblat"

    tagsDict=[ # passes again, what gives?
        {'key':"12",'value':'y'}, # valid
        {'snarfblat':'x', 'dinglehopper':'y'}, # invalid keys, should raise
        {'bangarang':1, 'blargh':'x'}, # invalid values, should raise
        "teststring" # not a dict, should raise
    ]

    myClass.construct_entitySearch_querystring(
        alertSeverity="12",
        tags=tagsDict
    )

if __name__ == "__main__":
    main()

How can we reproduce the bug?

Run the code in the bug report.

@devinnasar devinnasar added the bug label Nov 16, 2023
@agronholm
Copy link
Owner

This is not a bug, it's a configuration setting.
Set typeguard.config.collection_check_strategy = CollectionCheckStrategy.ALL_ITEMS and you're golden.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants