7

I am writing documentation for a project and I would like to make sure I did not miss any method. The code is written in Python and I am using PyCharm as an IDE.

Basically, I would need a REGEX to match something like:

def method_name(with, parameters):
    someVar = something()
    ...

but it should NOT match:

def method_name(with, parameters):
    """ The doc string """
    ...

I tried using PyCharm's search with REGEX feature with the pattern ):\s*[^"'] so it would match any line after : that doesn't start with " or ' after whitespace, but it doesn't work. Any idea why?

Vic
  • 21,473
  • 11
  • 76
  • 97
Ciprian Tomoiagă
  • 3,773
  • 4
  • 41
  • 65

4 Answers4

6

You mentioned you were using PyCharm: there is an inspection "Missing, empty, or incorrect docstring" that you can enable and will do that for you.

Note that you can then change the severity for it to show up more or less prominently.

enter image description here

Thomas Orozco
  • 53,284
  • 11
  • 113
  • 116
  • 1
    this is useful, thank you. but it is a bit too general. I was able to make it report missing documentation by using *Code > Run Inspection by Name* , but it also reported for classes, modules, missing parameters etc, which is too much for what I wanted – Ciprian Tomoiagă Jun 25 '13 at 07:40
3

There is a tool called pydocstyle which checks if all classes, functions, etc. have properly formatted docstrings.

Example from the README:

$ pydocstyle test.py
test.py:18 in private nested class `meta`:
        D101: Docstring missing
test.py:27 in public function `get_user`:
    D300: Use """triple double quotes""" (found '''-quotes)
test:75 in public function `init_database`:
    D201: No blank lines allowed before function docstring (found 1)

I don't know about PyCharm, but pydocstyle can, for example, be integrated in Vim using the Syntastic plugin.

luator
  • 4,769
  • 3
  • 30
  • 51
2

I don't know python, but I do know my regex.

And your regex has issues. First of all, as comments have mentioned, you may have to escape the closing parenthesis. Secondly, you don't match the new line following the function declaration. Finally, you look for single or double quotations at the START of a line, yet the start of a line contains whitespace.

I was able to match your sample file with \):\s*\n\s*["']. This is a multiline regex. Not all programs are able to match multiline regex. With grep, for example, you'd have to use this method.

A quick explanation of what this regex matches: it looks for a closing parenthesis followed by a semicolon. Any number of optional whitespace may follow that. Then there should be a new line followed by any number of whitespace (indentation, in this case). Finally, there must be a single or double quote. Note that this matches functions that do have comments. You'd want to invert this to find those without.

Community
  • 1
  • 1
Kat
  • 4,645
  • 4
  • 29
  • 81
0

In case PyCharm is not available, there is a little tool called ckdoc written in Python 3.5.

Given one or more files, it finds modules, classes and functions without a docstring. It doesn't search in imported built-in or external libraries – it only considers objects defined in files residing in the same folder as the given file, or subfolders of that folder.

Example usage (after removing some docstrings)

> ckdoc/ckdoc.py "ckdoc/ckdoc.py"
ckdoc/ckdoc.py
    module
        ckdoc
    function
        Check.documentable
        anykey_defaultdict.__getitem__
        group_by
        namegetter
    type
        Check

There are cases when it doesn't work. One such case is when using Anaconda with modules. A possible workaround in that case is to use ckdoc from Python shell. Import necessary modules and then call the check function.

> import ckdoc, main
> ckdoc.check(main)
/tmp/main.py
    module
        main
    function
        main
/tmp/custom_exception.py
    type
        CustomException
    function
        CustomException.__str__
False

The check function returns True if there are no missing docstrings.

Community
  • 1
  • 1
kyrill
  • 986
  • 10
  • 27