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
autodoc preserve defaults leads to exception on multiline lambda #11459
Comments
TL;DR Even the following simple example X = object()
Z = lambda x=X: x
"""The Z function.""" miserably fails. The reason is because the object being documented is a lambda function. Because of that, the extracted AST node is not a function definition node: sphinx/sphinx/ext/autodoc/preserve_defaults.py Lines 35 to 41 in d3c91f9
Your example also highlights another issue. The corresponding source is lambda self: None, doc="Foo.") You'll wrap it in a fake block and would ask to parse: if True:
lambda self: None, doc="Foo.") and obviously it fails. The reason why it works when no lambda is used is because the source code of your function is correctly found. For the simple example above, the corresponding source code is Z = lambda x=X: x which is an assignment and not a function definition. Note that I can work on the issue today but won't be able to guarantee that it can be easily solved. I think I'll just consider the case of a lambda function differently and handle the case when the source code of the lambda function is wrapped or decorated and whether there are trailing braces to close. |
Thanks for the analysis. I understand this report is fairly obscure, so I'll be surprised and impressed if you're able to solve it elegantly or quickly. From the sounds of things, my best bet would for now be to accept that this form isn't supported and work to port the usage in the library to use naturally-decorated property methods, which I'll do. |
Well, it should not be that hard actually. It's just that I don't necessarily have all the corner cases in mind (nor the time to look carefully). I'll only be able to look at it in mid July |
The PR that was merged should fix the issue (but Sphinx 7.2 is currently breaking other things so I'll advise waiting for 7.2.1). |
Describe the bug
In cssutils, I've stumbled into an issue where the docs builds are failing (jaraco/cssutils#36).
After some investigation, I learned that the issue seems to be related to the use of
autodoc
withautodoc_preserve_defaults = True
and the use ofproperty(lambda)
where the lambda is on a different line from theproperty
.How to Reproduce
Environment Information
Sphinx extensions
Additional context
Weirdly, removing the carriage return after
property(
suppresses the error. Also, converting to a traditional@property
decorator or replacing the lambda with a simple function also suppresses the error:The text was updated successfully, but these errors were encountered: