-
-
Notifications
You must be signed in to change notification settings - Fork 60
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
Accessors and grouping documentation entries #845
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #845 +/- ##
==========================================
- Coverage 87.06% 87.04% -0.03%
==========================================
Files 402 402
Lines 51887 51899 +12
==========================================
Hits 45175 45175
- Misses 6712 6724 +12 ☔ View full report in Codecov by Sentry. |
Wow, first of all let me say that what you have managed to do inside the jinja template is very impressive 😅 Also, I love the automatic finding of the dispatchers to ensure everything is automatically documented. This is something that I would do, but I didn't think you would like :) I have two suggestions:
|
For suggestion (2) we could also contribute to |
Well actually we could also contribute the thing about creating a per-class context for the template. |
I decided to take a chance sphinx-doc/sphinx#13003, let's see :) |
Great, I didn't find this doc, only
Yes, that is really required, but so far still manageable if |
I can't figure out how to do namespace retrieval of variables... {% set check = namespace() %}
{% for item in ["foo", "bar"] %}
{% set check.{{item}} = false %} <- does not work
{% set check[{{item}}] = false %} <- does not work
{% endfor %} so, some variables just needs to be hard-coded... :( |
It should be |
And probably without the
But I don't understand why you need to set things inside the template. |
I tried both, none work... The idea was to have the special fields defined in the |
Why do you use |
But I don't understand why you need to set variables inside the template, can't you set them all in conf.py? |
No, because I want to change them in the template. I have to discover the methods in the template. I.e. if I found a way, it ain't pretty.. Dynamic lists are the way to go... |
What I had in mind was that in autosummary_context = {
"info": {
"Hamiltonian": {...info for the Hamiltonian},
"BrillouinZone": {...info for the BrillouinZone}
}
} And then in the template do something like:
So you would compute all the needed variables for each class in |
Lets, take that as a 2nd step, lets start by getting this to work, which is already hard enough ;) Also, parsing dictionaries in jinja is not exactly fun.... ;) |
Ok, I thought it would be much easier. I don't understand what you want to use the |
@pfebrer I think this is ready for a quick review, just whether you like the overall details? Some issues remaining: The method that is documented is controlled here dispatcher_name = (dispatcher_name, None) then you'll find:
In both cases are the signatures wrong. And generally the signatures change depending on what you request provide. You'll also note that the latest commits fixes #728 ! :)
|
53d1eb5
to
c40a6ba
Compare
I think signatures should display exactly the way in which the functions can be called. This is what I will do with the plotting in #839 once this gets in. I guess logically what should be documented is the sisl/src/sisl/viz/_plotables.py Lines 80 to 88 in 0af33db
That is, I create a subclass for each dispatch to be able to document them individually. |
Are you sure that you want to display apply dispatchs as functions? I think across sisl documentation the displayed usage is as an attribute: bz.apply.average.eigenstate() instead of: bz.apply.average("eigenstate") Isn't it? |
This is just the first of some more fixes that should improve the documentation of the dispatchers used in sisl. Basically things are hard to change in sphinx. The simplest way is really to do it in the jinja templates. The templates has some hard-coded details in them. Which is kind of bad... On the other hand, it works nicely because all the dispatchers has unique names. Only when new dispatchers are used, should we amend specific sections, or join them elsewhere (if needed). So, the class template is very verbose with lots of macros, and actual code. It is not ideal, on the other hand it is self-contained and relatively simple to understand (except the call(...) macro_call()). The template changes was also the simplest way to manually control the order of segments. Probably this requires some finetuning. Signed-off-by: Nick Papior <[email protected]>
Signed-off-by: Nick Papior <[email protected]>
I still can't do it fully programmatically, that would require a very dense jinja template. I think this is *ok* for now. Perhaps some discovery of which flags are really used is necessary. Signed-off-by: Nick Papior <[email protected]>
It encompasses nicely with the way this accessor thing patches the documentation. Signed-off-by: Nick Papior <[email protected]>
This should warn when adding new dispatchers, and also indicate exactly which one is missing. Also made nice use of the whalrus op. Signed-off-by: Nick Papior <[email protected]>
It still doesn't work fully. But the basis should be there. Signed-off-by: Nick Papior <[email protected]>
This obviously restricts the usage from super() in the function calls. Signed-off-by: Nick Papior <[email protected]>
c40a6ba
to
17e5631
Compare
Signed-off-by: Nick Papior <[email protected]>
While we can not modify upstream EDIT: I think the approach in the next comment is better. This will use "|" as a marker for where the display name starts, as you were proposing: from sphinx.ext.autosummary import Autosummary
class CustomAutosummary(Autosummary):
"""Wrapper around the autosummary directive to allow for custom display names.
In particular, | is interpreted as the mark to indicate where the display name
should begin.
"""
def get_items(self, names):
# Find all names that have a | marker, and store their index along with the name that we want to display
display_names = [(i, name.split("|")[-1]) for i, name in enumerate(names) if "|" in name ]
# Clean the names so that Autosummary can process them
names = [name.replace("|", "") for name in names]
# Process names with Autosummary
items = super().get_items(names)
# Substitute the display names that we determined. This is only possible if the number of
# items is the same as the number of names, i.e. no import errors have ocurred for any item.
# Otherwise the indices will not match.
if len(names) == len(items):
for i, name in display_names:
items[i] = (name, *items[i][1:])
return items This custom class must be registered as the autosummary directive: def setup(app):
app.add_directive("autosummary", CustomAutosummary, override=True) Then you add the "|" marks to the template and it looks beautiful :) |
Another approach to display names that I think is much more robust: It adds an option from sphinx.ext.autosummary import Autosummary
from docutils.parsers.rst import directives
class CustomAutosummary(Autosummary):
"""Wrapper around the autosummary directive to allow for custom display names.
Adds a new option `:removeprefix:` which removes a prefix from the display names.
"""
option_spec = {
**Autosummary.option_spec,
"removeprefix": directives.unchanged
}
def get_items(self, *args, **kwargs):
items = super().get_items(*args, **kwargs)
remove_prefix = self.options.get("removeprefix")
if remove_prefix is not None:
items = [(item[0].removeprefix(remove_prefix), *item[1:]) for item in items]
return items
def setup(app):
app.add_directive("autosummary", CustomAutosummary, override=True) Then you add the option to the autosummary directives for the dispatchers, e.g.: .. autosummary::
:removeprefix: {{ name }}.
{{ name }}.plot
{{ extract_startswith('plot.', false, attributes, methods) }} |
Signed-off-by: Nick Papior <[email protected]>
This is simple, and works. Signed-off-by: Nick Papior <[email protected]>
@pfebrer thanks, I have implemented your last suggestion!!! |
Signed-off-by: Nick Papior <[email protected]>
@pfebrer perhaps we should merge this, and then continue on the documentation stuff in later PR's? |
Yes, sounds good! It is already much better than it was 👍 Now it is just a matter of making sure all the dispatchs are properly documented in general, and sphinx is ready to show the documentation. |
This superseeds #839. |
This is an extended try of @pfebrer try in #839.
I took the details there, and played with it, and extended it a bit more.
So now basically all is done in the template files.
It ain't pretty, but is self-contained.
I kind-of-don't like it. But it seems much simpler than hacking in the python code, and extending what autodoc does.
The advantages are:
to
/plot
could both be in their designated sections, but also in theMethods
section.sisl._dispatch.AbstractDispatcher
, and patches the attributes based on that.Disadvantages:
it gives the effect. It just means that tool-tips might not work... :(
@pfebrer let me know what you think?
Of course, the actual docs are still missing... ;)
Here are a few pics:
Type 1:
Type 2:
Type 3:
isort .
andblack .
[24.2.0] at top-leveldocs/
CHANGELOG.md