1

In vscode, I install autoDocstring plugin, it can generate doc with types based on Typing. like below:

def split_dataset(
        data: torch_geometric.data.Data,
        train: float = 0.1,
        test: float = 0.8,
        val: float = 0.1,
) -> tuple:
    """_summary_

    Parameters
    ----------
    data : torch_geometric.data.Data
        _description_
    train : float, optional
        _description_, by default 0.1
    test : float, optional
        _description_, by default 0.8
    val : float, optional
        _description_, by default 0.1

    Returns
    -------
    tuple
        _description_
    """
    pass

But in pycharm, I try to use docstring, it will not generate types automatically, like below:

def split_dataset(
        data: torch_geometric.data.Data,
        train: float = 0.1,
        test: float = 0.8,
        val: float = 0.1,
) -> tuple:
    """
    
    Parameters
    ----------
    data : 
    train : 
    test : 
    val : 

    Returns
    -------

    """
    pass

Is there any way let pycharm do the same thing as vscode?

  • Why do you want to maintain the types both as annotations and in the docstring? Typically only one or the other used. – ruohola Sep 27 '22 at 08:57
  • @ruohola I use docstring generate document, I think document should have more details, and I also use Typing and pylint to check code. So i think I need both of them – Miku ShaneG Sep 27 '22 at 09:17
  • @bad_coder I think it's not, it just change the style of the docstring, but not custom the variable type – Miku ShaneG Sep 27 '22 at 09:36
  • @MikuShaneG it is, and [there are at least a dozen duplicates](https://stackoverflow.com/search?q=%5Bdocstring%5D+%5Bpycharm%5D+stub+type&searchOn=3) – bad_coder Sep 27 '22 at 09:50

1 Answers1

1

There's only a semi-automatic way to do this in PyCharm. Most of the docstring stub can be generated automatically, but you need to provide the types.

There's an official example on how to do this here

TL;DR

    1. Press ⌘ + , and go to Editor | General | Smart Keys.

    2. Select the Insert type placeholders checkbox in the Smart Keys page of the editor settings.

    3. Place the caret at the function name, and press ⌥ ⏎.

    4. In the list of intention actions that opens, choose Insert documentation string stub. PyCharm creates a documentation stub, according to the selected docstring format, with the type specification, collected during the debugger session.

And then, manually specify the desired types.

baduker
  • 19,152
  • 9
  • 33
  • 56