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

Using NotRequired disables Literal typechecks #454

Open
2 tasks done
ionelmc opened this issue Apr 17, 2024 · 0 comments
Open
2 tasks done

Using NotRequired disables Literal typechecks #454

ionelmc opened this issue Apr 17, 2024 · 0 comments
Labels

Comments

@ionelmc
Copy link

ionelmc commented Apr 17, 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.8

What happened?

Python 3.11.8 (main, Feb 28 2024, 00:00:00) [GCC 13.2.1 20231011 (Red Hat 13.2.1-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import typing
>>> class StuffOptional(typing.TypedDict):
...   foo: typing.NotRequired[typing.Literal['bar']]
...
>>> check_type({'foo': 'caca'}, StuffOptional)
{'foo': 'caca'}
>>>

The expected result would be exactly like this:

>>> class Stuff(typing.TypedDict):
...   foo: typing.Literal['bar']
...
>>> from typeguard import check_type
>>> check_type({'foo': 'caca'}, Stuff)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".tox/py311/lib/python3.11/site-packages/typeguard/_functions.py", line 106, in check_type
    check_type_internal(value, expected_type, memo)
  File ".tox/py311/lib/python3.11/site-packages/typeguard/_checkers.py", line 779, in check_type_internal
    checker(value, origin_type, args, memo)
  File ".tox/py311/lib/python3.11/site-packages/typeguard/_checkers.py", line 284, in check_typed_dict
    check_type_internal(argvalue, argtype, memo)
  File ".tox/py311/lib/python3.11/site-packages/typeguard/_checkers.py", line 779, in check_type_internal
    checker(value, origin_type, args, memo)
  File ".tox/py311/lib/python3.11/site-packages/typeguard/_checkers.py", line 587, in check_literal
    raise TypeCheckError(f"is not any of ({formatted_args})") from None
typeguard.TypeCheckError: value of key 'foo' of dict is not any of ('bar')

How can we reproduce the bug?

^

@ionelmc ionelmc added the bug label Apr 17, 2024
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

1 participant