Closed frallain closed 1 year ago
Thanks for finding this, I also think that NotRequired
brings a lot for the typing. I will see that I can fix this as fast as possible. The same for https://github.com/FelixTheC/strongtyping/issues/109
I think this should raise an error, have a look at key_b
parent = {"not_required": 3, "child": {"key_a": 1, "key_b": "2"}}
ParentType(parent)
This new version should fix this issue pip install strongtyping==3.10.4
I want to validate a dictionary with a key whose value is another dictionary. Therefore, I have a parent TypedDict with a key targeting another TypedDict.
The thing is, I am using the
NotRequired
clause, which is available in Python 3.10 through thetyping_extensions
package (4.4.0 being the latest version for now). As seen at https://peps.python.org/pep-0655/#usage-in-python-3-11 , in Python 3.10, when using theNotRequired
clause, it is recommended to use the TypedDict class fromtyping_extensions
But trying to validate this dict with this configuration gives me the following error:
Python Version used Python 3.10.4 (main, May 28 2022, 13:25:38) [GCC 10.2.1 20210110] on linux
Package Version used strongtyping==3.10.3
Addon in use strongtyping_modules [yes/no] : no
To Reproduce Steps to reproduce the behavior:
python:3.10.4-slim-bullseye
@match_class_typing class ChildType(TypedDict): key_a: int key_b: int
@match_class_typing class ParentType(TypedDict): child: ChildType not_required: NotRequired[int]
parent = {"child": {"key_a": 1, "key_b": 2}} ParentType(parent)
Additional context
It is to note that the following works as expected though:
But then, in this case, the
NotRequired
clause is not respected, the following will error with a TypeMisMatch error, and it should not:It is an edge case, but I thought it was worth reporting it because
NotRequired
brings a lot for typing. Thanks for your work!