NiklasRosenstein / pydoc-markdown

Create Python API documentation in Markdown format.
http://niklasrosenstein.github.io/pydoc-markdown/
Other
460 stars 105 forks source link

Does not work with parameter defaults without a compatible __str__ method #100

Closed jdthorpe closed 4 years ago

jdthorpe commented 4 years ago

Trying to generate docs for a module like this:

# some_module.py
DEFAULT = lambda x: x
class foo:
    def __init__(self, fun=DEFAULT):
        pass

by calling:

pydocmd simple some_module.foo

generates a lib2to3.pgen2.parse.ParseError with

Traceback (most recent call last):
   ...
  File "<unknown>", line 1
    foo(fun=<function <lambda> at 0x107033440>)
            ^

because the the string returned by DEFAULT.__str__() begins with a <.

NiklasRosenstein commented 4 years ago

Hey @jdthorpe ,

Thanks for reporting this issue. This is because Pydoc-Markdown 2 actual executes the code and cannot know that the default value for the fun argument was originally DEFAULT in the source code.

A while back someone added a call to use yapf to format function arguments, but as you have noted the repr() of the lambda is not valid syntax. We could resolve this by catching it and just not using yapf for the argument formatting (or never use it).

Anyway, I strongly suggest that you take a look at Pydoc-Markdown 3, which you can find in the develop branch at the moment. It will also be released on PyPI soon. There isn't currently a similar CLI to pydocmd simple in v3, the closest you can get with the current CLI is

$ pydoc-markdown '{loaders: [{type: python, modules: [some_module], search_path: [.]}]}'

Output:

image

To disable the table of contents, add , renderer: {type: markdown, render_toc: false}}

NiklasRosenstein commented 4 years ago

Hey @jdthorpe , I had some time to fix this for Pydoc-Markdown v2. This should be fixed in 2.1.1 (available on PyPI).