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

Type checking on generic only checks first value entry in data structure #418

Closed
2 tasks done
teddius opened this issue Nov 18, 2023 · 3 comments
Closed
2 tasks done
Labels

Comments

@teddius
Copy link

teddius commented Nov 18, 2023

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

What happened?

The function check_type(value=value, expected_type=expected_type) only checks the first entry of a Dict / List etc.

How can we reproduce the bug?

Reproduce with (pseudo-code)
check_type(value={'a': 1, 1: 1}, expected_type=Dict[str, int]) confirms correct type but it is not true for the second entry in the dict.

Same issue for
Dict[str, Dict[str, int]]
List[Dict[int, int]]

@teddius teddius added the bug label Nov 18, 2023
@teddius teddius changed the title Type checking on generic only checks first type Type checking on generic only checks first value entry in data structure Nov 18, 2023
@agronholm
Copy link
Owner

This isn't a bug. Typeguard and Beartype both only check the first element of each collection by default, as checking every element would significantly slow down the application. This can be configured by setting typeguard.config.collection_check_strategy to CollectionCheckStrategy.ALL_ITEMS.

Where should I document this so people wouldn't send bug reports about this all the time (see #417 for the last one)?

@DanielYang59
Copy link

DanielYang59 commented Feb 15, 2024

Sorry @agronholm, I seems to have sent another issue regarding this behaviour in #439. But for some reason the following doesn't trigger an error:

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])

Update: should use the following:

from typeguard import check_type, CollectionCheckStrategy

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

@agronholm
Copy link
Owner

Closing due to not a bug. I'll think about reverting the default setting in the next major release.

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