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
{{ message }}
Repository navigation
Inconsistent documentation around decorators #75748
I can see inconsistency in library documentation around functions that are suitable for decorators. I'd like to clarify if it is based on my misunderstanding, or a real documentation problem.
Both staticmethod() and functools.lru_cache() are used with decorator expressions, while they have slightly different explanations.
The first one looks like just a usual function while the detailed explanations say it is used with decorator expression. The second one is what I don't understand; it says "@functools.lru_cache()", where the function name is "decorated" with @ in the doc. What does @ mean here? If there's some meaning, the next question is, why doc for staticmethod()
(and classmethod() in the same page) does not have it?
I don't know which is better, but I believe consistency is good. Some other examples :
What does @ mean here? If there's some meaning, the next question is, why doc for staticmethod()
(and classmethod() in the same page) does not have it?
@ means that the function is meant to be used as a decorator (the markup looks like the actual code).
staticmethod and classmethod are older than the decorator syntax, which is older than the special sphinx markup for decorators (they used to just use the function markup).
For unittest.mock.patch, its result can be used as a decorator or as a context manager, so the current markup (no @) makes sense.
If you want to update staticmethod and classmethod to use the decorator markup, please send a pull request! (more info in the devguide)
Use of classmethod and staticmethod decorators as functions is still a valid use case. I think the old signatures should be kept. It should be possible to document both uses in same place:
I think existing uses of the decorator markup rely on the reader’s understanding that it’s syntactic sugar for a call and an assignment, and they don’t have decorator+function markup. The PRs for this ticket follow that.
That said, staticmethod as a non-decorator has an important use case for function injection in tests (using self.func in TestCase classes that work with a C module and an equivalent Python version). Maybe this deserves an extra example?
That said, staticmethod as a non-decorator has an important use case for
function injection in tests (using self.func in TestCase classes that work with
a C module and an equivalent Python version). Maybe this deserves an extra
example?
New changeset 205dd4e by Éric Araujo (Miss Islington (bot)) in branch '3.6':
[3.6] bpo-31567: add or fix decorator markup in docs (GH-3959) (GH-3966) 205dd4e
Activity
dmiyakawa commentedon Sep 24, 2017
I can see inconsistency in library documentation around functions that are suitable for decorators. I'd like to clarify if it is based on my misunderstanding, or a real documentation problem.
Examples:
Both staticmethod() and functools.lru_cache() are used with decorator expressions, while they have slightly different explanations.
The first one looks like just a usual function while the detailed explanations say it is used with decorator expression. The second one is what I don't understand; it says "@functools.lru_cache()", where the function name is "decorated" with @ in the doc. What does @ mean here? If there's some meaning, the next question is, why doc for staticmethod()
(and classmethod() in the same page) does not have it?
I don't know which is better, but I believe consistency is good. Some other examples :
merwok commentedon Oct 2, 2017
@ means that the function is meant to be used as a decorator (the markup looks like the actual code).
staticmethod and classmethod are older than the decorator syntax, which is older than the special sphinx markup for decorators (they used to just use the function markup).
For unittest.mock.patch, its result can be used as a decorator or as a context manager, so the current markup (no @) makes sense.
If you want to update staticmethod and classmethod to use the decorator markup, please send a pull request! (more info in the devguide)
merwok commentedon Oct 12, 2017
New changeset 0e61e67 by Éric Araujo (Daisuke Miyakawa) in branch 'master':
bpo-31567: add or fix decorator markup in docs (bpo-3959)
0e61e67
berkerpeksag commentedon Oct 12, 2017
Use of classmethod and staticmethod decorators as functions is still a valid use case. I think the old signatures should be kept. It should be possible to document both uses in same place:
merwok commentedon Oct 12, 2017
I think existing uses of the decorator markup rely on the reader’s understanding that it’s syntactic sugar for a call and an assignment, and they don’t have decorator+function markup. The PRs for this ticket follow that.
That said, staticmethod as a non-decorator has an important use case for function injection in tests (using self.func in TestCase classes that work with a C module and an equivalent Python version). Maybe this deserves an extra example?
berkerpeksag commentedon Oct 12, 2017
Yes, that would be nice.
merwok commentedon Oct 12, 2017
New changeset 03b9537 by Éric Araujo in branch 'master':
bpo-31567: more decorator markup fixes in docs (GH-3959) (bpo-3966)
03b9537
merwok commentedon Oct 12, 2017
New changeset 205dd4e by Éric Araujo (Miss Islington (bot)) in branch '3.6':
[3.6] bpo-31567: add or fix decorator markup in docs (GH-3959) (GH-3966)
205dd4e
merwok commentedon Oct 12, 2017
Cheers!