Better DX with custom decorators #47
Replies: 4 comments 3 replies
-
I'm not sure to see what could be really simplified for from typing import Callable, Type, TypeVar
from antidote import implements, interface, world
@interface
class View:
pass
V = TypeVar('V', bound=Type[View])
def view(context: object) -> Callable[[V], V]:
def decorate(cls: V) -> V:
implements(View).when(qualified_by=context)(cls)
return cls
return decorate
Customer = object()
@view(context=Customer)
class ViewImpl(View):
pass IMHO, that's as far as it can get. |
Beta Was this translation helpful? Give feedback.
-
This was really cool! Then I changed it to a Protocol and of course, it failed because |
Beta Was this translation helpful? Give feedback.
-
Using from typing import Callable, Protocol, Type, TypeVar
from antidote import implements, inject, interface, world
@interface
class View(Protocol):
pass
V = TypeVar('V', bound=Type[View])
def view(context: object) -> Callable[[V], V]:
def decorate(cls: V) -> V:
implements(View).when(qualified_by=context)(cls)
return cls
return decorate
Customer = object()
@view(context=Customer)
class ViewImpl:
pass
@inject
def f(customer_view: View = inject.me()) -> View:
return customer_view
assert f() is world.get(ViewImpl) However, it's a good point, I didn't expect the following Mypy error when calling
It's unclear to me how to properly handle this though... |
Beta Was this translation helpful? Give feedback.
-
@Finistere Would it be worth me putting out a call on Twitter for suggestions? I don't think the Antidote parts are part of the issue. |
Beta Was this translation helpful? Give feedback.
-
In Pyramid etc. you write
@view(context=Customer)
. In Antidote you would write this as:@implements(View).when(qualified_by=Customer)
.This is nice as it lets framework writers create domain-specific concepts pretty easily.
In Hopscotch I made it easy to write custom decorators such as @view using a base class.
I wonder if we could promote this pattern in Antidote using as a partial function.
Beta Was this translation helpful? Give feedback.
All reactions