mkdocstrings / python

A Python handler for mkdocstrings.
https://mkdocstrings.github.io/python
ISC License
175 stars 32 forks source link

Feature request: include classname for class members #79

Open couling opened 1 year ago

couling commented 1 year ago

Could there be an option (or options) to achieve a more python style headers for module members and class members.

In the python's own documentation, the full path of each class and object is not included. But for class members, the class name is included. Taking a random example, the module zipfile contains a class zipfile.ZipFile. All of the methods on that class are written as ZipFile.close(), ZipFile.getinfo(), ... See here: https://docs.python.org/3/library/zipfile.html#zipfile.ZipFile.close

The "include_full_path` options produce very verbose results.

pawamoy commented 1 year ago

Have you tried the following?

plugins:
- mkdocstrings:
    handlers:
      python:
        options:
          show_root_full_path: false
          show_root_members_full_path: true

Docs: https://mkdocstrings.github.io/python/usage/configuration/headings/#show_root_members_full_path

EDIT: ah, I understand now. No, there's no such option. I'm open to adding one :slightly_smiling_face:

pawamoy commented 1 year ago

Now that we have Jinja blocks in our templates, it's a bit easier to implement that with custom templates:

# mkdocs.yml
plugins:
- mkdocstrings:
    custom_templates: docs/templates
    ...

In docs/templates/python/material/function.html:

{% extends "_base/function.html" %}

{%- macro method_heading(function) -%}
  {%- if show_full_path -%}
    {{ function.path }}
  {%- elif function.parent.is_class -%}
    {{ function.parent.name }}.{{ function.name }}
  {%- else -%}
    {{ function.name }}
  {%- endif -%}
{%- endmacro -%}

{% block heading %}
  {% if config.separate_signature %}
    <span class="doc doc-object-name doc-function-name">{{ method_heading(function) }}</span>
  {% else %}
    {% filter highlight(language="python", inline=True) %}
      {{ method_heading(function) }}
      {% include "signature.html" with context %}
    {% endfilter %}
  {% endif %}
{% endblock heading %}