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
Don't recommend using addCallback as a function decorator #2523
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,6 +96,7 @@ code for a particular function or method within the same indented block, using | |
nested functions:: | ||
|
||
def getRevInfo(revname): | ||
# for example only! See above a better implementation with inlineCallbacks | ||
results = {} | ||
d = defer.succeed(None) | ||
def rev_parse(_): # note use of '_' to quietly indicate an ignored parameter | ||
|
@@ -122,6 +123,15 @@ Deferred. As a shortcut, ``d.addCallback`` works as a decorator:: | |
def rev_parse(_): # note use of '_' to quietly indicate an ignored parameter | ||
return utils.getProcessOutput(git, [ 'rev-parse', revname ]) | ||
|
||
.. note:: | ||
|
||
``d.addCallback`` is not really a decorator as it does not return a modified function. | ||
As a result in previous code, ``rev_parse`` value is actually the Deferred. | ||
In general the :class:`inlineCallbacks` method is preferred inside new code as it keeps the code easier to read. | ||
As a general rule of thumb, when you need more than 2 callbacks in the same method, its time to switch it to :class:`inlineCallbacks`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo "it's" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❓ Same here |
||
This would be for example the case for previous :py:func:`getRevInfo`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❓ And as i understand it, this would attempt to link to the Python docs....should probably just be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For me, this is just a styling hint that this bloc is a function, if there is no corresponding
|
||
See this `discussion <https://github.com/buildbot/buildbot/pull/2523>`_ with twisted experts for more information. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Twisted is a proper noun (not an adjective:wink:). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ahah! right. You made my day. You guys are really twisted! |
||
|
||
Be careful with local variables. For example, if ``parse_rev_parse``, above, | ||
merely assigned ``rev = res.strip()``, then that variable would be local to | ||
``parse_rev_parse`` and not available in ``set_results``. Mutable variables | ||
|
@@ -176,6 +186,19 @@ losing any readability. | |
|
||
Note that code using ``deferredGenerator`` is no longer acceptable in Buildbot. | ||
|
||
The previous :py:func:`getRevInfo` example implementation should rather be written as: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❓ Same here. |
||
|
||
.. code-block:: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Actually I notice there's a sphinx lint warning reported for this code-block. |
||
|
||
@defer.inlineCallbacks | ||
def getRevInfo(revname): | ||
results = {} | ||
res = yield utils.getProcessOutput(git, [ 'rev-parse', revname ]) | ||
results['rev'] = res.strip() | ||
res = yield utils.getProcessOutput(git, [ 'log', '-1', '--format=%s%n%b', results['rev'] ]) | ||
results['comments'] = res.strip() | ||
defer.returnValue(results) | ||
|
||
Locking | ||
....... | ||
|
||
|
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.
❓ shouldn't this be something like
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.
Otherwise how will it know where to link to when rendering?
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.
It does not this is just used for styling. We don't have back reference helpers to twisted docs