NiklasRosenstein / pydoc-markdown

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

Syntax error for class with only metaclass #272

Closed NiklasRosenstein closed 1 year ago

NiklasRosenstein commented 1 year ago

Environment

Describe the bug

When the definition of a class is rendered and that class only has a metaclass, the generated code is invalid and thus formatting the code with Yapf fails.

class TypeHint(metaclass=_TypeHintMeta):
  ...

Results in


Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.10.8/x64/lib/python3.10/site-packages/yapf/yapflib/yapf_api.py", line 183, in FormatCode
    tree = pytree_utils.ParseCodeToTree(unformatted_source)
  File "/opt/hostedtoolcache/Python/3.10.8/x64/lib/python3.10/site-packages/yapf/yapflib/pytree_utils.py", line 131, in ParseCodeToTree
    raise e
  File "/opt/hostedtoolcache/Python/3.10.8/x64/lib/python3.10/site-packages/yapf/yapflib/pytree_utils.py", line 129, in ParseCodeToTree
    ast.parse(code)
  File "/opt/hostedtoolcache/Python/3.10.8/x64/lib/python3.10/ast.py", line 50, in parse
    return compile(source, filename, mode, flags,
  File "<unknown>", line 1
    class TypeHint(, metaclass=_TypeHintMeta): pass
                   ^
SyntaxError: invalid syntax

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.10.8/x64/lib/python3.10/site-packages/novella/build.py", line 150, in _run_actions
    action.execute(self)
  File "/opt/hostedtoolcache/Python/3.10.8/x64/lib/python3.10/site-packages/novella/markdown/preprocessor.py", line 158, in execute
    preprocessor.process_files(files)
  File "/opt/hostedtoolcache/Python/3.10.8/x64/lib/python3.10/site-packages/pydoc_markdown/novella/preprocessor.py", line 133, in process_files
    file.content = replace_tags(file.content, tags, lambda t: self._replace_pydoc_tag(self._suite, file, t))
  File "/opt/hostedtoolcache/Python/3.10.8/x64/lib/python3.10/site-packages/novella/markdown/tagparser.py", line 219, in replace_tags
    replacement = repl(tag)
  File "/opt/hostedtoolcache/Python/3.10.8/x64/lib/python3.10/site-packages/pydoc_markdown/novella/preprocessor.py", line 133, in <lambda>
    file.content = replace_tags(file.content, tags, lambda t: self._replace_pydoc_tag(self._suite, file, t))
  File "/opt/hostedtoolcache/Python/3.10.8/x64/lib/python3.10/site-packages/pydoc_markdown/novella/preprocessor.py", line 148, in _replace_pydoc_tag
    self._renderer.render_object(fp, objects[0], tag.options)
  File "/opt/hostedtoolcache/Python/3.10.8/x64/lib/python3.10/site-packages/pydoc_markdown/contrib/renderers/markdown.py", line 448, in render_object
    self._render_recursive(fp, 0, obj)
  File "/opt/hostedtoolcache/Python/3.10.8/x64/lib/python3.10/site-packages/pydoc_markdown/contrib/renderers/markdown.py", line 368, in _render_recursive
    self._render_recursive(fp, level, member)
  File "/opt/hostedtoolcache/Python/3.10.8/x64/lib/python3.10/site-packages/pydoc_markdown/contrib/renderers/markdown.py", line 365, in _render_recursive
    self._render_object(fp, level, obj)
  File "/opt/hostedtoolcache/Python/3.10.8/x64/lib/python3.10/site-packages/pydoc_markdown/contrib/renderers/markdown.py", line 350, in _render_object
    self._render_signature_block(fp, obj)
  File "/opt/hostedtoolcache/Python/3.10.8/x64/lib/python3.10/site-packages/pydoc_markdown/contrib/renderers/markdown.py", line 327, in _render_signature_block
    code = self._format_classdef_signature(obj)
  File "/opt/hostedtoolcache/Python/3.10.8/x64/lib/python3.10/site-packages/pydoc_markdown/contrib/renderers/markdown.py", line 312, in _format_classdef_signature
    code = self._yapf_code(code + ': pass').rpartition(':')[0].strip()
  File "/opt/hostedtoolcache/Python/3.10.8/x64/lib/python3.10/site-packages/pydoc_markdown/contrib/renderers/markdown.py", line 281, in _yapf_code
    return FormatCode(code, style_config=style)[0]
  File "/opt/hostedtoolcache/Python/3.10.8/x64/lib/python3.10/site-packages/yapf/yapflib/yapf_api.py", line 186, in FormatCode
    raise errors.YapfError(errors.FormatErrorMsg(e))
yapf.yapflib.errors.YapfError: <unknown>:1:16: invalid syntax
Error: Process completed with exit code 1.

Expected behavior

The generated code is correct.

Workaround

Use object as a base class.

NiklasRosenstein commented 1 year ago

c364221