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

typeguard does not check elements (except for the first element) in a list #452

Closed
2 tasks done
yifengshao opened this issue Apr 12, 2024 · 5 comments
Closed
2 tasks done
Labels

Comments

@yifengshao
Copy link

yifengshao commented Apr 12, 2024

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.2.1

Python version

3.11.0rc1

What happened?

typeguard does not check elements of a list except for the first element. Please see my example here:

  • The first element is checked properly.

    check_type(['123', 123], list[int, int])
    

    Output:

    typeguard.TypeCheckError: item 0 of list is not an instance of int
    
  • The second element was not checked.

    check_type([123, '123'], list[int, int])
    

    Output:

    [123, '123']
    
  • Also not working for list with flexible length:

    check_type([123, '123'], list[int, ...])
    

    Output:

    [123, '123']
    

How can we reproduce the bug?

Run the following codes in the console:

from typeguard import check_type

check_type(['123', 123], list[int, int])

check_type([123, '123'], list[int, int])

check_type([123, '123'], list[int, ...])
@yifengshao yifengshao added the bug label Apr 12, 2024
@yifengshao yifengshao changed the title typeguard does not check elements (except for the first element) in a list or tuple typeguard does not check elements (except for the first element) in a list Apr 12, 2024
@yifengshao
Copy link
Author

It also does not check the length of a list. See my example below:

Screenshot 2024-04-12 at 09 42 22

@agronholm
Copy link
Owner

You have not searched the documentation OR the issues.
Documentation: https://typeguard.readthedocs.io/en/stable/api.html#typeguard.TypeCheckConfiguration.collection_check_strategy
Duplicate issues: #446, #441, #439, #430, #422, #418, #417, #360

@yifengshao
Copy link
Author

Thank you for your quick response. I just upgraded from typeguard 2.13.1 to 4.*, and apparently, I missed this important information. Now it works perfectly. Thanks!

@agronholm
Copy link
Owner

agronholm commented Apr 12, 2024

If you can suggest a place in the documentation where you yourself would've found the relevant information before creating the issue, let me know and I can add it there.

@agronholm
Copy link
Owner

Regarding that "list length check", list only takes one type parameter. tuple is the one that takes multiple parameters.

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