Skip to content

Add type hints. #157

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

Open
fabiocaccamo opened this issue Dec 3, 2022 · 5 comments
Open

Add type hints. #157

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

Comments

@fabiocaccamo
Copy link
Owner

fabiocaccamo commented Dec 3, 2022

No description provided.

@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

@Tatsh
Copy link

Tatsh commented Apr 27, 2025

I can do this at least in the generic form. TypedBenedict is also an interesting idea.

@Tatsh
Copy link

Tatsh commented Apr 28, 2025

I've been working on adding type hints. Issue with generic is it won't be very generic. A lot of the core functionality is reliant on the key type always being str. The values can be generic. Most functions will require that the key type always be str even though plausibly it could be bytes as well.

@pirate
Copy link

pirate commented Apr 28, 2025

I think it should still allow other types for the keys but then it's on the dev to know that they can't use some of the special features Benedict's provide.

I often use Benedict's for the parsing and serialization functions, those should still work with numeric keys i think.

@Tatsh Tatsh mentioned this issue Apr 29, 2025
3 tasks
@Tatsh
Copy link

Tatsh commented Apr 29, 2025

@pirate Please have a look at the PR. I have tried really hard to use abstract base classes and generics but I am not sure how far we can go with it. In the type system there is no way to type-ify the paths so unfortunately anything like some_dict['key1.key2'] is always going to return Any. There would have to be a Mypy plugin to get around this (and then that's limited to Mypy of course and won't work with Pyright). Types would have to be specified somewhere like a TypedBenedict class (which could include TypedDict entries). It gets crazy but it would be very useful.

If you have familiarity with making a Mypy plugin that would be great and we could make a separate package for that (e.g. mypy-benedict-plugin). The best example of a plugin is mypy_django_plugin.

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

3 participants