-
Notifications
You must be signed in to change notification settings - Fork 3
Refactor descriptors, decorators and actions #213
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
Conversation
This module will soon be deleted, but this note felt worth preserving.
I've combined the decorator (`lt.fastapi_endpoint`) and the descriptor into one module. I've also made the descriptor a subclass of `BaseDescriptor` to deduplicate handling of `name`, `title`, `description`. This does change behaviour slightly: we use the attribute name rather than the function name (the two are equal, when it's used as a decorator). Also, an attribute docstring may override the function docstring - though most likely there won't be an attribute docstring.
Thing endpoints are now combined into one top-level module, rather than split between `descriptors` and `decorators`.
This moves actions to being a single-file module, and moves the descriptor and decorator into it. This commit also makes a new module `invocations` that currently just holds the invocation model. It might make sense to move Invocation (the Thread subclass) in here to shrink Actions slightly. I've refactored endpoints into a single module, which means the empty decorators and descriptors modules are now gone.
This brings `action` in line with `property` by dropping the `thing` prefix: by default, we use `lt.action` so `thing` is redundant.
Barecheck - Code coverage reportTotal: 95.04%Your code coverage diff: -0.05% ▾ Uncovered files and lines |
|
To the bigger picture questions/points: Not one of the questions, but I think
If this tidying doesn't affect the public API then it seems a good idea to do it at some point. I think having two separate PRs would be better as there is no good reason to minimise merge commits and making this any bigger will make review very hard. I would rather merge this and do it as a separate PR.
Makes sense to me,this should be an internal only change? As the downstream libraries don't talk to this directly?
I wouldn't we haven't done this for |
|
Thanks - that all sounds very sensible to me. My rationale for moving Invocation and ActionManager now rather than in the future is that I don't think it changes the size of the PR very much: the code is getting moved either way, it's just a question of where. However, you correctly point out that it's an internal-only rearrangement. Similarly, downstream code should not be aware of Point taken about warnings/compatibility - I will happily leave it as-is. I actually considered renaming endpoint but thought not to do it as I'd already scope-crept here. I would be happy to rename it, as you say So, happily I think we've concluded "no" to the three things I suggested could be added here. I may as well rename |
|
I think this is probably good for review now. I think I should probably just be OK with merging a 0.05% drop in coverage, I can add tests later, and it's a good idea to get in to the habit of not being too obsessive about coverage, as you've pointed out before. |
Yeah, I think a future PR makes sense for that. I think I would probably keep it as one decorator, just because it is something rarely used and if someone isn't used to HTTP a |
julianstirling
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great. I much prefer the new flatter structure.
It is also great to see that the tests only needed lt.thing_action changed to lt.action and nothing else. I think lt.action is the correct syntax and it should be a simple find and replace to update code.

This PR introduces one API change:
thing_actionis nowaction. This is largely for consistency withproperty. We dropped thethingprefix when we started encouragingimport labthings_fastapi as ltso it becomes@lt.actionwhich doesn't need a prefix.I've also rearranged code between some modules:
actionsis a single-file module that includesactions/__init__.pyand the action-related descriptor and decorator.InvocationModelis moved into its own top-level moduleinvocations.fastapi_endpointis moved into a top levelendpointsmodule which clears the way to get rid ofdescriptorsanddecoratorsbecause decorators and descriptors are now combined with the code they belong with.invocationsmight in the future gain theInvocationThreadsubclass and/orActionManager(which might want to be renamedInvocationManageras that's what it actually manages), but that's left for the future.There is a minor change in functionality, because
EndpointDescriptorandActionDescriptorare now subclasses ofBaseDescriptor. This means that:title,name, anddescriptionproperties are all now consolidated inBaseDescriptor.__doc__is set in__init__to the docstring of the decorated function (and will then be picked up bydescriptionfrom there, so the behaviour is unchanged).nameof the class attribute, rather than the name of the function being decorated. This was always the intended behaviour, and in normal usage as a decorator the two are identical.If used as a decorator, nothing changes. If, however, someone does something strange, we might see differences:
The code above would be valid before and after this change (give or take
lt.thing_actionbeing renamed), but before we'd have an action calleddo_somethingwithA docstringas its description, while afterwards we'd have an action calledstrangewithAn attribute docstring. This is not something I expect anybody will encounter, and I don't believe it occurs anywhere in the microscope codebase.To consider before merge:
Invocation, possibly andActionManagerintoinvocations. May as well only have one merge commit that moves that code?ActionManagercan we rename itInvocationManager?lt.thing_actionthat callslt.actionand raises aDeprecationWarning?