FelixTheC / strongtyping

Decorator which checks whether the function is called with the correct type of parameters.
https://pypi.org/project/strongtyping/
108 stars 3 forks source link

Cannot validate typing of nested TypedDict having a NotRequired clause in Python 3.11 #109

Closed frallain closed 1 year ago

frallain commented 1 year ago

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.11 natively. (see https://peps.python.org/pep-0655/#usage-in-python-3-11 )

But trying to validate this dict with this configuration gives me the following import error:

Traceback (most recent call last):
  File "/app/script.py", line 7, in <module>
    from strongtyping.strong_typing import match_class_typing
  File "/usr/local/lib/python3.11/site-packages/strongtyping/strong_typing.py", line 14, in <module>
    from typing_extensions import NotRequired, Required
ModuleNotFoundError: No module named 'typing_extensions'

Python Version used Python 3.11.1 (main, Dec 8 2022, 00:05:00) [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:

  1. Run a container of this docker image python:3.11.1
    docker run -it --rm python:3.11.1
  2. In this container, install the following :
    pip install strongtyping==3.10.3
  3. In this container, run the following script:
    
    from typing import TypedDict, NotRequired
    from strongtyping.strong_typing import match_class_typing

@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 = {"not_required": 3, "child": {"key_a": 1, "key_b": "2"}} ParentType(parent)


**Expected behavior**
Should exit without error.

**Desktop (please complete the following information):**
 - OS: 
 ```bash
$ uname -a
Linux ab90f1fc83ca 5.13.0-25-generic #26~20.04.1-Ubuntu SMP Fri Jan 7 16:27:40 UTC 2022 x86_64 GNU/Linux

This time, this is not an edge case, Python 3.11 should be taken into account in the way strongtyping import NotRequired and Required. Thanks again for your work!

FelixTheC commented 1 year ago

I wanted to release a specific version for >3.11 where I drop the import of typing-extension. If you have time could you maybe try it out https://github.com/FelixTheC/strongtyping/tree/py_3_11 if everything works like expected I will create a release for it.

CyanChanges commented 1 year ago

When to fix?

FelixTheC commented 1 year ago

Do you tried the branch I metioned in my last commet??

I wanted to release a specific version for >3.11 where I drop the import of typing-extension. If you have time could you maybe try it out https://github.com/FelixTheC/strongtyping/tree/py_3_11 if everything works like expected I will create a release for it.

CyanChanges commented 1 year ago

I need use it in Pypi

获取Outlook for Androidhttps://aka.ms/AAb9ysg


From: FelixTheC @.> Sent: Saturday, May 13, 2023 12:11:28 PM To: FelixTheC/strongtyping @.> Cc: Cyan @.>; Manual @.> Subject: Re: [FelixTheC/strongtyping] Cannot validate typing of nested TypedDict having a NotRequired clause in Python 3.11 (Issue #109)

Do you tried the branch I metioned in my last commet??

I wanted to release a specific version for >3.11 where I drop the import of typing-extension. If you have time could you maybe try it out https://github.com/FelixTheC/strongtyping/tree/py_3_11 if everything works like expected I will create a release for it.

― Reply to this email directly, view it on GitHubhttps://github.com/FelixTheC/strongtyping/issues/109#issuecomment-1546520000, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AQLAIBEWESTEMIE36I5MCL3XF4CXBANCNFSM6AAAAAASZEITQE. You are receiving this because you are subscribed to this thread.Message ID: @.***>

FelixTheC commented 1 year ago

fixed with https://pypi.org/project/strongtyping/3.11.1/