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

Dictionary check fails #508

Closed
2 tasks done
MarekNieslanik opened this issue Jan 22, 2025 · 1 comment
Closed
2 tasks done

Dictionary check fails #508

MarekNieslanik opened this issue Jan 22, 2025 · 1 comment
Labels

Comments

@MarekNieslanik
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.4.1

Python version

3.10.12

What happened?

When I try to check the type of a dictionary defined as Dict[str, str], check_type is correct only if the wrong value is first. See the xample.
The same issue occurs, when the type to check is defined as dict[str.str].

>>> from typeguard import check_type
>>> A = {"int": 1}
>>> B = {"str": "hello world", "int": 1}
>>> from typing import  Dict
>>> type_to_check = Dict[str, str]
>>> check_type(A, type_to_check)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/marek/it4i/Lexis/api-v2/env/lib/python3.10/site-packages/typeguard/_functions.py", line 106, in check_type
    check_type_internal(value, expected_type, memo)
  File "/home/marek/it4i/Lexis/api-v2/env/lib/python3.10/site-packages/typeguard/_checkers.py", line 861, in check_type_internal
    checker(value, origin_type, args, memo)
  File "/home/marek/it4i/Lexis/api-v2/env/lib/python3.10/site-packages/typeguard/_checkers.py", line 242, in check_mapping
    check_type_internal(v, value_type, memo)
  File "/home/marek/it4i/Lexis/api-v2/env/lib/python3.10/site-packages/typeguard/_checkers.py", line 866, in check_type_internal
    raise TypeCheckError(f"is not an instance of {qualified_name(origin_type)}")
typeguard.TypeCheckError: value of key 'int' of dict is not an instance of str
>>> check_type(B, type_to_check)
{'str': 'hello world', 'int': 1}
>>> 

How can we reproduce the bug?

Try same snippet as I sent in the issue description.

@agronholm
Copy link
Owner

You didn't check the existing issues. This was among the two pinned issues: #418
As this is causing so much trouble, I'm reversing this logic 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

2 participants