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

Are there errors for undefined keys? #129

Closed
yupix opened this issue Dec 3, 2023 · 5 comments · Fixed by #130
Closed

Are there errors for undefined keys? #129

yupix opened this issue Dec 3, 2023 · 5 comments · Fixed by #130
Assignees
Labels
enhancement_medium A feature or request which should be adopted with medium priority

Comments

@yupix
Copy link

yupix commented Dec 3, 2023

First of all, thank you for creating such a useful library.

Is it possible to return an exception for undefined keys?
As an example, if I pass a dict to User with a key age that I have not defined, it will return an exception.

The reason I want this feature is to help find things that are not defined in the TypedDict. It would be nice to be able to log warnings, etc. even if they are not exceptions.

from typing import TypedDict

from strongtyping.strong_typing import match_class_typing

@match_class_typing
class User(TypedDict):.
    id: str
    username: str
    description: str | None

User({"id": "0123", "username": "test", "description": None, "age": 10})

Due to my inexperience with English, I am using DeepL. I apologize if you find anything offensive.

@FelixTheC
Copy link
Owner

@yupix Thank you for using DeepL for easier communication.

The current behavior is that you won't get any exceptions if you have an undefined key inside your dictionary.
This behavior should be supported. I will fill in a new issue/feature request for it.
I'm thinking about something like.

@match_class_typing(throw_on_undefined=True)
class User(TypedDict):
    id: str
    username: str
    description: str | None

or

@match_class_typing
class User(TypedDict):
    id: str
    username: str
    description: str | None

    def throw_on_undefined(self):
        return True

what did you say to the solution ideas??

@FelixTheC FelixTheC self-assigned this Dec 3, 2023
@yupix
Copy link
Author

yupix commented Dec 3, 2023

Thanks for the quick reply. I think the first code is better in my opinion.

class User(TypedDict):
   id: str
   username: str
   description: str | None

@FelixTheC
Copy link
Owner

Could you please tell me which Python version you're using?
I plan to put this feature in the latest version which requires Python 3.12.

I will then extend the decorator to support
throw_on_undefined

this will be the TestCase:

def test_undefined_keys_raise_error():

    @match_class_typing(throw_on_undefined=True)
    class User(TypedDict):
        id: str
        username: str
        description: str | None
    
    with pytest.raises(UndefinedKey):
        User({"id": "0123", "username": "test", "description": None, "age": 10})
    
    assert User({"id": "0123", "username": "test", "description": None})

@yupix
Copy link
Author

yupix commented Dec 3, 2023

The project I am creating is currently using Python 3.11 and will change to 3.12 on January 2 next year, three months after the release of 3.12. Therefore, it would be helpful if you could implement this project in Python 3.11 if possible.

As for the testing, I think it is good.

@FelixTheC FelixTheC added the enhancement_medium A feature or request which should be adopted with medium priority label Dec 4, 2023
@FelixTheC FelixTheC linked a pull request Dec 4, 2023 that will close this issue
@FelixTheC
Copy link
Owner

feature is now available at https://pypi.org/project/strongtyping/3.12.1/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement_medium A feature or request which should be adopted with medium priority
Projects
None yet
2 participants