You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ZeroIntensity opened this issue
Jun 19, 2024
· 0 comments
Assignees
Labels
apiThis has to do with the Python API (view)c apiThis has to do with the C API (_view)complexThis should only be looked at by someone who knows what they're doingfeatureNew feature
view.py should support casting any arbitrary type through a __view_cast__ protocol. This will make file upload types in #174 possible. Adding asynchronous support will be a royal pain in the ass, but I can't really get around that.
Example API
fromviewimportnew_appfromtypingimportSelfapp=new_app()
ViewNativeType=str|int|bytes| ... # Rest of the native types, this will go in the view.typing moduleclassMyCustomType:
def__init__(self, whatever: str) ->None:
self.whatever=whatever@classmethodasyncdef__view_cast__(cls, obj: ViewNativeType) ->Self:
ifnotisinstance(obj, str):
raiseTypeError("") # This could possibly be something like a TypeCastError?returncls(obj)
@app.get("/")@app.body("something", MyCustomType)asyncdefindex(something: MyCustomType):
returnsomething.whateverapp.run()
It might be a good idea to remove __view_body__ and __view_construct__, or at least add some sort of base class to implement the behavior using __view_cast__:
classSupportsViewBody:
__view_body__: ClassVar[dict[str, type[Any]]]
asyncdef__view_cast__(cls, obj: ViewNativeType) ->Self:
ifnotisinstance(obj, dict):
raiseTypeError(...)
# We can use our own TypeCode API!tp=compile_type(cls.__view_body__)
returntp.cast(obj)
The text was updated successfully, but these errors were encountered:
ZeroIntensity
added
feature
New feature
complex
This should only be looked at by someone who knows what they're doing
c api
This has to do with the C API (_view)
api
This has to do with the Python API (view)
labels
Jun 19, 2024
apiThis has to do with the Python API (view)c apiThis has to do with the C API (_view)complexThis should only be looked at by someone who knows what they're doingfeatureNew feature
Proposal:
view.py should support casting any arbitrary type through a
__view_cast__
protocol. This will make file upload types in #174 possible. Adding asynchronous support will be a royal pain in the ass, but I can't really get around that.Example API
It might be a good idea to remove
__view_body__
and__view_construct__
, or at least add some sort of base class to implement the behavior using__view_cast__
:The text was updated successfully, but these errors were encountered: