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
While the docdict simplifies our lives and makes documentation more consistent by reducing redundancy, there's also an unfortunate side effect; namely, that IDEs may be unaware of our custom approach to docstrings. I'm often confronted with information like this:
which, really, is just as unhelpful as it can be if I'm not sure about the parameters the function expects.
I thought maybe we could provide some stubs of the documentation with those placeholders expanded; but apparently stubs are merely intended for type information.
Now I was thinking that maybe when preparing a release, we could expand those strings? So that at least the MNE version that most users will be using – a released "stable" version – would have "proper" docstrings?
Or… are there any ways to kind of inject the rendered HTML docs from our website or something? Anyone know about magic of that kind? @drammock maybe?
maybe when preparing a release, we could expand those strings?
That seems like it would fix the issue you're having. I think it would have to be an automated thing that expanded all the docdicts and made a commit, made a release, and then did a revert commit right after the tagged release to put the docdict back in place. Note that this would not fix your issue if you're using latest master in your IDE.
are there any ways to kind of inject the rendered HTML docs from our website or something?
Seems like that's really an issue for the IDE devs, not for MNE-Python devs. We can't control where they're looking for docs, and it appears they're pulling straight from the source code files.
I suppose if there were a more widespread standard for dealing with docstring redundancy, and that standard were already supported by various IDEs, then I'd consider switching from docdict to whatever that mystery other standard is. But off the top of my head I don't know of one.
Just wanted to let you know that this issue still has not been addressed upstream. If you have a minute, it would be great if you could add a 👍 to microsoft/pylance-release#346 and leave a comment there, to show this issue is of relevance.
I took a step forward here since I don't think we'll ever see a fix in the VS Code Python extension / Pylance. The problem is that Pylance and other language servers only perform static analysis without actually running or importing the code. This makes it impossible for these tools to get access to "expanded" (dynamically generated) docstrings like the ones we're using.
It's gotten so bad that for most MNE functionality, the docstrings are now completely useless to me within the IDE. I have to keep a browser window or an IPython window open to view the docs.
I therefore took the initiative to create a script that generates type stubs for all public functions and classes in MNE and injects (expanded) docstrings into the stubs. These docstrings will then be picked up and displayed correctly by Pylance in VS Code (I didn't test any other tools so far, but @cbrnr told me that it seems to be working nicely in PyCharm as well).
This is what MNE-Python main looks like for me out of the box:
And here with the stubs:
The stubs are created fully automatically. I packaged them such that they can be installed via pip. Hence, no modifications to MNE are required. There is no interference with any MNE-Python code at runtime.
Currently, default parameter values are still missing. That's next on my to-do list.
Rather than a separate project if it's fully automated we could add a script to tools/dev/* that we run manually before each release to generate the stubs. (We could do things in a more automated way, too, with a bit more work.) Then there's no need for a separate package. We already have a step like this with generate_codemeta.py.
We could add a little .yml to at least ensure the script runs on every PR (or run it on a schedule). It would be really neat to create an action that runs to recreate the stubs and pushes any changes to an open PR. That would be more work but could be a neat Sunday project for someone who is motivated.
This is amazing @hoechenberger - and I agree with the other posters that it should be integrated into MNE for (at least) releases. I assume many users use VSCode or PyCharm so this is an important enhancement.
thanks for the positive feedback on the type stubs!
For those who didn't follow the discussion:
I've created "stub files" (.pyi) that contain the "expanded" docstrings for all MNE-Python functions, classes, and methods.
The stubs do not contain any type hints that are not already present in MNE-Python, except for a few that are plainly obvious to stubgen (the tool I used for creating the stubs) and were added automatically.
The goal was to improve the user experience when working in an IDE.
With these type stubs, the VS Code experience changes from this unusable mess:
to this (markup still subject to change):
I am happy that you like it!
I have worked and talked with @cbrnr for many hours in the past couple of days to evaluate what could be a viable move forward from here. I will try to summarize our thoughts below and make a concrete proposal on how to proceed.
Background information
The stubs are currently optimized for the best experience in VS Code.
VS Code (or, rather, Pylance / Pyright, which are used by VS Code) cannot handle Numpydoc-style docstrings and basically has no support for reST-based markup.
Hence, I inject Markdown into the stubs in many places, and remove reST markup in others, in order to make things look better.
However, PyCharm doesn't support Markdown.
These stubs would therefore actually degrade the UX for PyCharm users!
At the same time, PyCharm does support Numpydoc-style docstrings pretty nicely out of the box.
So there's a conflict: what works well in VS Code doesn't work well in PyCharm, and vice versa.
Issues we're trying to address
But the biggest issue we set out to solve is, of course, that of the "un-expanded" doccer-based docstrings (e.g., %(verbose) etc.)
Second-biggest issue: Sphinx tags / blocks often simply disappear in both VS Code and PyCharm. For example, blocks like .. warning::, .. versionchanged:: etc. just don't show up at all!
The third issue is aesthetics when it comes to Sphinx-based references: reading ~mne.io.Raw is irritating with that leading tilde.
Proposal
We create type stubs with expanded docstrings from which we remove Sphinx tags / directives that yield hidden blocks and ugly markup (e.g., :class:, ~, .. warning:: etc). These stubs will automatically look good in PyCharm, and provide more useful documentation in VS Code (albeit it won't be rendered nicely in the latter IDE).
These type stubs live in a package types-mne that is separate from MNE-Python. This allows us to release this package independently of MNE-Python releases. This package should live under the mne-tools org. Having a package separate from mne also avoids unintended interference with the existing .pyi files when automatically creating the stubs.
While normally such stub packages depend on the package they're providing the stubs for, we could instead make mne depend on this package, so it gets installed automatically when you install MNE-Python.
VS Code-specific alterations to the docstrings should live in a separate package types-mne-vscode. This is where all the Markdown and emoji fun can happen.
Additional tasks
We'd also need to evaluate behavior of Spyder and Jupyter Notebook.
@cbrnr and I will reach out to pyright and inquire about the possibility of supporting Numpydoc (or if a contribution to add support would be appreciated).
These type stubs live in a package types-mne that is separate from MNE-Python. This allows us to release this package independently of MNE-Python releases. This package should live under the mne-tools org. Having a package separate from mne also avoids unintended interference with the existing .pyi files when automatically creating the stubs.
I'm not entirely sold on the separate package, the only.pyi files are the init for lazy loading right?
And I don't see the need to release separately from MNE, on the contrary it might create some conflicts if you include dev changes?
Besides that, amazing! (and apologies for the formatting, github on phone is not my preferred interface)
Thanks for tackling this! Summarizing the discussion at today's dev meeting:
getting this to "just work" for users should be a high priority for 1.7 release. @hoechenberger and @cbrnr please feel free to proceed and pester us for help (design decisions, implementation, testing, whatever) at your discretion.
There was a general sense that it might be better if the solution were not a separate package but instead lived inside MNE-Python, but for now it seems OK to develop as a separate package(s) if that's easier; once things are progressed we can see how hard it would be to migrate it into MNE-proper.
Personal notes from me (may not reflect opinions of others at the dev mtg):
please don't inject extra emoji into the docstrings. Other major scientific packages have headings that say e.g. "Parameters" not "🛠️ Parameters", and doing our own special thing here is not an improvement IMO
I'll mention rst2html5 as something that might be useful to look at, as well as MyST markdown (which aims to be as expressive as rST)... both may be irrelevant if vscode doesn't support MyST or if rst2html5 doesn't expose any low-level parsing functionality (I've only ever used it for high-level stuff)
I don't really see how Lars's efforts are related to what I was trying to do here
I was assuming that although the end goal is different, some of the machinery could probably be repurposed fairly easily. But maybe not... just wanted to make sure you knew about it in case it saved some effort.
VS Code (or, rather, Pylance / Pyright, which are used by VS Code) cannot handle Numpydoc-style docstrings and basically has no support for reST-based markup.
This is still an experimental feature and I believe @hoechenberger has already played around with it, but without much success (right?). Also, I think this only covers RST and not Numpydoc style (which is more than just RST formatting, e.g. parameter types etc.). But yes, I really hope that one day our docstrings will be properly rendered (we only need to replace the docdict, which is a separate but related issue).
Activity
drammock commentedon Sep 7, 2020
That seems like it would fix the issue you're having. I think it would have to be an automated thing that expanded all the docdicts and made a commit, made a release, and then did a revert commit right after the tagged release to put the docdict back in place. Note that this would not fix your issue if you're using latest master in your IDE.
Seems like that's really an issue for the IDE devs, not for MNE-Python devs. We can't control where they're looking for docs, and it appears they're pulling straight from the source code files.
I suppose if there were a more widespread standard for dealing with docstring redundancy, and that standard were already supported by various IDEs, then I'd consider switching from docdict to whatever that mystery other standard is. But off the top of my head I don't know of one.
agramfort commentedon Sep 8, 2020
hoechenberger commentedon Sep 8, 2020
Wow, you're right! Great, this gives me something to work with.
hoechenberger commentedon Sep 9, 2020
I've opened a feature request upstream at Pylance:
microsoft/pylance-release#346
drammock commentedon Sep 9, 2020
Closing because there's nothing to be done on our end, it seems. @hoechenberger feel free to reopen if you disagree.
hoechenberger commentedon Jan 30, 2021
Just wanted to let you know that this issue still has not been addressed upstream. If you have a minute, it would be great if you could add a 👍 to microsoft/pylance-release#346 and leave a comment there, to show this issue is of relevance.
hoechenberger commentedon Nov 24, 2023
I took a step forward here since I don't think we'll ever see a fix in the VS Code Python extension / Pylance. The problem is that Pylance and other language servers only perform static analysis without actually running or importing the code. This makes it impossible for these tools to get access to "expanded" (dynamically generated) docstrings like the ones we're using.
It's gotten so bad that for most MNE functionality, the docstrings are now completely useless to me within the IDE. I have to keep a browser window or an IPython window open to view the docs.
I therefore took the initiative to create a script that generates type stubs for all public functions and classes in MNE and injects (expanded) docstrings into the stubs. These docstrings will then be picked up and displayed correctly by Pylance in VS Code (I didn't test any other tools so far, but @cbrnr told me that it seems to be working nicely in PyCharm as well).
This is what MNE-Python

mainlooks like for me out of the box:And here with the stubs:

The stubs are created fully automatically. I packaged them such that they can be installed via
pip. Hence, no modifications to MNE are required. There is no interference with any MNE-Python code at runtime.Currently, default parameter values are still missing. That's next on my to-do list.
You can find the stubs and installation instructions at https://github.com/hoechenberger/mne-python-stubs
cc @larsoner @mscheltienne @britta-wstnr @wmvanvliet
larsoner commentedon Nov 24, 2023
Rather than a separate project if it's fully automated we could add a script to
tools/dev/*that we run manually before each release to generate the stubs. (We could do things in a more automated way, too, with a bit more work.) Then there's no need for a separate package. We already have a step like this withgenerate_codemeta.py.We could add a little
.ymlto at least ensure the script runs on every PR (or run it on a schedule). It would be really neat to create an action that runs to recreate the stubs and pushes any changes to an open PR. That would be more work but could be a neat Sunday project for someone who is motivated.cbrnr commentedon Nov 25, 2023
This is great! I agree that it should probably be integrated into MNE, since it runs automatically anyway.
I would not run it on every PR though, because it takes a non-trivial amount of time (and resources).
britta-wstnr commentedon Nov 27, 2023
This is amazing @hoechenberger - and I agree with the other posters that it should be integrated into MNE for (at least) releases. I assume many users use VSCode or PyCharm so this is an important enhancement.
hoechenberger commentedon Nov 28, 2023
Hello all,
thanks for the positive feedback on the type stubs!
For those who didn't follow the discussion:
I've created "stub files" (
.pyi) that contain the "expanded" docstrings for all MNE-Python functions, classes, and methods.The stubs do not contain any type hints that are not already present in MNE-Python, except for a few that are plainly obvious to
stubgen(the tool I used for creating the stubs) and were added automatically.The goal was to improve the user experience when working in an IDE.
With these type stubs, the VS Code experience changes from this unusable mess:

to this (markup still subject to change):

I am happy that you like it!
I have worked and talked with @cbrnr for many hours in the past couple of days to evaluate what could be a viable move forward from here. I will try to summarize our thoughts below and make a concrete proposal on how to proceed.
Background information
Issues we're trying to address
%(verbose)etc.).. warning::,.. versionchanged::etc. just don't show up at all!~mne.io.Rawis irritating with that leading tilde.Proposal
:class:,~,.. warning::etc). These stubs will automatically look good in PyCharm, and provide more useful documentation in VS Code (albeit it won't be rendered nicely in the latter IDE).types-mnethat is separate from MNE-Python. This allows us to release this package independently of MNE-Python releases. This package should live under themne-toolsorg. Having a package separate frommnealso avoids unintended interference with the existing.pyifiles when automatically creating the stubs.mnedepend on this package, so it gets installed automatically when you install MNE-Python.types-mne-vscode. This is where all the Markdown and emoji fun can happen.Additional tasks
pyrightand inquire about the possibility of supporting Numpydoc (or if a contribution to add support would be appreciated).Please share your thoughts! 👍
mscheltienne commentedon Nov 29, 2023
These type stubs live in a package types-mne that is separate from MNE-Python. This allows us to release this package independently of MNE-Python releases. This package should live under the mne-tools org. Having a package separate from mne also avoids unintended interference with the existing .pyi files when automatically creating the stubs.
I'm not entirely sold on the separate package, the only.pyi files are the init for lazy loading right?
And I don't see the need to release separately from MNE, on the contrary it might create some conflicts if you include dev changes?
Besides that, amazing! (and apologies for the formatting, github on phone is not my preferred interface)
drammock commentedon Dec 1, 2023
Thanks for tackling this! Summarizing the discussion at today's dev meeting:
Personal notes from me (may not reflect opinions of others at the dev mtg):
30 remaining items
drammock commentedon Jun 4, 2024
I was assuming that although the end goal is different, some of the machinery could probably be repurposed fairly easily. But maybe not... just wanted to make sure you knew about it in case it saved some effort.
hoechenberger commentedon Jun 4, 2024
Yep, absolutely, and I intend to take a look! Thanks for letting me know!
drammock commentedon Jun 11, 2024
@cbrnr et al, FYI here's a crossref to the wider community discussion scientific-python/summit-2024#27
drammock commentedon Jul 15, 2024
FYI, this is now fixed in VSCode: https://code.visualstudio.com/updates/v1_91#_support-for-restructuredtext-docstrings
cbrnr commentedon Jul 15, 2024
This is still an experimental feature and I believe @hoechenberger has already played around with it, but without much success (right?). Also, I think this only covers RST and not Numpydoc style (which is more than just RST formatting, e.g. parameter types etc.). But yes, I really hope that one day our docstrings will be properly rendered (we only need to replace the docdict, which is a separate but related issue).
drammock commentedon Jul 15, 2024
yep. but good that they're working on it
I think that's right. But again, progress is being made.
yep, this means that once our docdict is gone, things will render nicely on hover.
hoechenberger commentedon Jul 19, 2024
FYI Pylance produces lots of error messages like "ERROR: Pylance failed to parse docstring at index 86" when it encounters "docdict'ed" parameters.