Skip to content

Support for enum values in Function.params() #123

@prashanthsandela

Description

@prashanthsandela

Error generating pdoc when param default is enum value. Below method which has enum as default. Is there any alternate way to achieve this?

Example:

import enum

class Foo(enum.Enum):
    f = "foo"
    b = "bar"

def print_foo(f = Foo.f):
    print(f.value)

When I run pdoc --html --f module_name, it throws error

Expected Behavior

pdoc command should run file without errors

Actual Behavior

Traceback (most recent call last):
  File "venv/lib/python3.7/site-packages/pdoc/cli.py", line 318, in write_files
    w.write(m.html(**kwargs))
  File "venv/lib/python3.7/site-packages/pdoc/__init__.py", line 686, in html
    html = _render_template('/html.mako', module=self, **kwargs)
  File "venv/lib/python3.7/site-packages/pdoc/__init__.py", line 122, in _render_template
    return t.render(**config).strip()
  File "venv/lib/python3.7/site-packages/mako/template.py", line 476, in render
    return runtime._render(self, self.callable_, args, data)
  File "venv/lib/python3.7/site-packages/mako/runtime.py", line 883, in _render
    **_kwargs_for_callable(callable_, data)
  File "venv/lib/python3.7/site-packages/mako/runtime.py", line 920, in _render_context
    _exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
  File "venv/lib/python3.7/site-packages/mako/runtime.py", line 947, in _exec_template
    callable_(context, *args, **kwargs)
  File "_html_mako", line 127, in render_body
  File "_html_mako", line 30, in show_module
  File "_html_mako", line 396, in render_show_module
  File "_html_mako", line 303, in show_func
  File "venv/lib/python3.7/site-packages/pdoc/__init__.py", line 1085, in params
    return self._params(self.obj, annotate=annotate, link=link, module=self.module)
  File "venv/lib/python3.7/site-packages/pdoc/__init__.py", line 1137, in _params
    p = safe_default_value(p)
  File "venv/lib/python3.7/site-packages/pdoc/__init__.py", line 1110, in safe_default_value
    replacement = html.escape(replacement or p.default)
  File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/html/__init__.py", line 19, in escape
    s = s.replace("&", "&") # Must be done first!
AttributeError: 'Foo' object has no attribute 'replace'

Additional info

elif isinstance(p.default, enum.Enum):
    replacement = str(p.default)
  • pdoc version: 0.3.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions