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

Add type hints. #157

Open
fabiocaccamo opened this issue Dec 3, 2022 · 1 comment
Open

Add type hints. #157

fabiocaccamo opened this issue Dec 3, 2022 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@fabiocaccamo
Copy link
Owner

fabiocaccamo commented Dec 3, 2022

Fund with Polar
@fabiocaccamo fabiocaccamo added the enhancement New feature or request label Dec 3, 2022
@fabiocaccamo fabiocaccamo self-assigned this Dec 3, 2022
@fabiocaccamo fabiocaccamo moved this to Todo in Open Source Dec 3, 2022
@pirate
Copy link

pirate commented Oct 27, 2024

It would be great to be able to define the key type and value type generically like Dict[str, str], e.g.:

from benedict import benedict
from pydantic import TypeAdapter  # just used for deep-checking equivalent to isinstance()

def example_function() -> benedict[str, int]:
    return benedict({'abc': 123})
    
TypeAdapter(benedict[str, int]).validate_python(example_function())
# this should succeed

But also separately I think it would be cool to be able to use them like TypedDicts as well, with hardcoded (total=True/False keys) that are always expected to be present, e.g.:

from benedict import TypedBenedict

class MyCustomBenedict(TypedBenedict, total=True):
    abc: int
    xyz: str
    
test1 = MyCustomBenedict({'abc': 123, 'xyz': 'test string'})
# this should work

test2 = MyCustomBenedict({'abc': 123, 'notallowed': 'something else'})
# this should raise an error because notallowed is not present in the spec

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Todo
Development

No branches or pull requests

2 participants