Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

canismarko
Copy link
Contributor

The docstring to the utility function make_decorator provides the example of customized_count = some_decorator(count). However, the correct usage should be customized_count = some_decorator()(count) otherwise we actually get dec(gen_func) as the decorated function, rather than dec_inner(...) as intended.

Description

The equivalent syntactic-sugar version of customized_count = some_decorator(count) would be

@some_decorator
def customized_count(...):
   ...

This is a common python pattern for decorators that do not require arguments.

However, the wrappers that are turned into decorators may accept additional arguments and so what comes back from make_decorator needs to be a decorator factory. The syntactic-sugar version would then be something like:

@some_decorator(arg0, kwargs0=...)
def customized_count(...):
   ...

This is the version that make_decorator() produces, and is also how the usage is described in the curated documentation for plans: @relative_set_decorator([motor]).

The equivalent without the sugar is customized_count = some_decorator()(count).

Motivation and Context

This PR will update the docstring for make_decorator to match the curated documentation, and provide the correct usage for the resulting decorators.

If the instructions are followed as previously written, there is no mechanism to provide arguments to the original wrapper, and any attempt to call the decorated plan results in the following error:

>>> plan = customized_count([], num=6)
TypeError: make_decorator.<locals>.dec_outer.<locals>.dec() got an unexpected keyword argument 'num'

(the original plan accepts a keyword argument num).

How Has This Been Tested?

I added wrappers to some common functions. If I use count = baseline_decorator(count) I get the error described above. If I change this to count = baseline_decorator(devices=[...])(count, ...). I do not get the error.

I have not tested whether the messages for the baseline are actually applied.

The docstring provided the example of ``customized_count =
some_decorator(count)``. However, the correct usage should be
``customized_count = some_decorator()(count)`` otherwise we actually
get ``dec(gen_func)`` as the decorated function, rather than
``dec_inner(...)`` as intended.
@tacaswell
Copy link
Contributor

The windows failure is another version of the zict issue so going to disregard that and coverage error looks like something went wrong with coverage (as this is only changing docs!).

@tacaswell tacaswell merged commit e047be6 into bluesky:main Dec 31, 2024
15 of 17 checks passed
@canismarko canismarko deleted the decorator_docstring branch December 31, 2024 02:51
@tacaswell
Copy link
Contributor

Thank you @canismarko, That looks like an error from ~2016 that was there in the first draft of this function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants