-
-
Notifications
You must be signed in to change notification settings - Fork 46
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
Comments
It would be great to be able to define the key type and value type generically like 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 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 |
I can do this at least in the generic form. |
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 |
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. |
@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 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. |
No description provided.
The text was updated successfully, but these errors were encountered: