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([1, "hi"], list[int]) doesn't raise error #439

Closed
2 tasks done
DanielYang59 opened this issue Feb 15, 2024 · 5 comments
Closed
2 tasks done

check_type([1, "hi"], list[int]) doesn't raise error #439

DanielYang59 opened this issue Feb 15, 2024 · 5 comments
Labels

Comments

@DanielYang59
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.7

What happened?

First time typeguard user and hope I'm not asking a silly question here. But the following code doesn't seem to trigger an error for some reason:

from typeguard import check_type

check_type(value=[1, "hi"], expected_type=list[int])

Maybe I missed something? Thanks.

How can we reproduce the bug?

from typeguard import check_type

check_type(value=[1, "hi"], expected_type=list[int])
@DanielYang59 DanielYang59 changed the title check_type([1, "2"], list[int]) doesn't raise error check_type([1, "hi"], list[int]) doesn't raise error Feb 15, 2024
@davidbrochart
Copy link

See #418 (comment).

@DanielYang59
Copy link
Author

Thanks for the quick response @davidbrochart . You are legend and that is exactly what I was experiencing. However I tried the following which still didn't got an error for some reason?

import typeguard
from typeguard import check_type, CollectionCheckStrategy

typeguard.config.collection_check_strategy = CollectionCheckStrategy.ALL_ITEMS
check_type(value=[1, "hi"], expected_type=list[int])

Do you have any advice? Thanks!

@davidbrochart
Copy link

Seems like a bug.

@agronholm
Copy link
Owner

Seems like a bug.

No, it's not. check_type() does not use the global configuration. You need to pass the collection_check_strategy=CollectionCheckStrategy.ALL_ITEMS as keyword argument to the function.

This default was adopted because Beartype adopted it, but given how many complaints I've received because of it, typically from users who post bug reports without reading the documentation first, I'm seriously considering reverting this behavior in the next major release.

@DanielYang59
Copy link
Author

typically from users who post bug reports without reading the documentation first

Apologies, I have been one of them 😭 . Yes could confirm this works as expected.

from typeguard import check_type, CollectionCheckStrategy

check_type(
    value=[1, "hi"], expected_type=list[int, int],
    collection_check_strategy=CollectionCheckStrategy.ALL_ITEMS
)

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

3 participants