Add custom URL convertor register#1437
Merged
Conversation
Kludex
commented
Jan 27, 2022
tests/test_convertors.py
Outdated
| @@ -0,0 +1,99 @@ | |||
| import datetime as dt | |||
Owner
Author
There was a problem hiding this comment.
I've created multiple convertors here with the purpose of reproducing #1398. I can also simplify the test suite if wanted. Just let me know.
Contributor
There was a problem hiding this comment.
I agree with simplifying the tests, as this can be extended for other things like UUID and there's not really much difference. This is really good.
Contributor
|
Well this is nice and simple isn't it? Seems worthwhile. |
Contributor
Owner
Author
aminalaee
reviewed
Jan 27, 2022
aminalaee
approved these changes
Jan 27, 2022
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Custom URL Convertor
Currently, we don't have a way to create URL convertors, meaning that our users can only match the default types. Those are:
https://github.com/encode/starlette/blob/2b54f427613ebd1a6ccb2031aa97364e2d5070a5/starlette/convertors.py#L75-L81
To be fair, there's a way: adding a
key, valuepair on theCONVERTOR_TYPESdictionary. That's not available on Starlette's public API, or documented.The idea of this PR is to document and introduce a public API that can be used to create custom URL convertors.
How does Django and Flask handle this?
Both of them have a way to register custom URL convertors. You can find the way Django makes it possible here, and for Flask here.
Other
Recently, there was a PR to introduce time related convertors on #1398, and I think we should follow the path this PR proposes here, and not creating more convertors on Starlette itself.
Missing