-
Notifications
You must be signed in to change notification settings - Fork 276
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 support for type casting in the typed decorator #635
Conversation
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
lgtm @ncoop57 ! And yeah for the typemap values, just use the various |
…en casting with union types
@jph00 Okay, I think the PR is ready for a full review! |
You got some conflicts @ncoop57 ! |
@jph00 whoops! Should be good now 🤓 |
@jph00 hold up on merging! |
Looking at an error when previewing the docs locally |
@jph00 okay, fixed the issue I had previewing the docs locally and made some slight tweaks by hiding some additional tests that I don't think need to be in the docs as it didn't look that great when previewing. Lmk what you think! |
lgtm! |
This PR adds support for the
typed
decorator to perform automatic casting when thecast
flag is set toTrue
. It maintains the current API for backwards compatibility so that users can now specify the following:The conversions are handled by a
type_map
dictionary that maps the expected type (i.e. the one specified as a type hint)to the function to perform the conversion.
This allows for better validation handling for libraries like FastHTML instead of immediately throwing an error for common cases that would be expected to be properly handled like the str "on" to be convert to the int 1 or the str "none" to be converted to the int 0.
TODOs before merging:
type_map
that handles common cases such as converting the string "True" to the bool True if the expected type is a bool and the given one is a str.a: int:None
Handle container types likeNot a priority. Can revisit if it is needed.list[str]
orset[int]