Title: bug: `merge_init_into_class` renders inherited `__init__` source (leaks site-packages paths) · Issue #333 · mkdocstrings/python · GitHub
Open Graph Title: bug: `merge_init_into_class` renders inherited `__init__` source (leaks site-packages paths) · Issue #333 · mkdocstrings/python
X Title: bug: `merge_init_into_class` renders inherited `__init__` source (leaks site-packages paths) · Issue #333 · mkdocstrings/python
Description: Description of the bug With merge_init_into_class: true, the source block in templates/material/_base/class.html.jinja unconditionally prefers all_members["__init__"] over the class itself: {% if config.merge_init_into_class %} {% if "__...
Open Graph Description: Description of the bug With merge_init_into_class: true, the source block in templates/material/_base/class.html.jinja unconditionally prefers all_members["__init__"] over the class itself: {% if c...
X Description: Description of the bug With merge_init_into_class: true, the source block in templates/material/_base/class.html.jinja unconditionally prefers all_members["__init__"] over the class itsel...
Opengraph URL: https://github.com/mkdocstrings/python/issues/333
X: @github
Domain: redirect.github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"bug: `merge_init_into_class` renders inherited `__init__` source (leaks site-packages paths)","articleBody":"### Description of the bug\n\nWith `merge_init_into_class: true`, the `source` block in\n`templates/material/_base/class.html.jinja` unconditionally prefers\n`all_members[\"__init__\"]` over the class itself:\n\n```jinja\n{% if config.merge_init_into_class %}\n {% if \"__init__\" in all_members and all_members[\"__init__\"].source %}\n```\n\n`all_members` includes inherited members, so when a class does not\ndefine `__init__` itself (e.g. any pydantic `BaseModel` subclass, with\n`preload_modules: [pydantic]` so griffe can resolve the base), the\nrendered source block is `pydantic.BaseModel.__init__` with a summary\nlike:\n\n> Source code in `.venv/lib/python3.10/site-packages/pydantic/main.py`\n\ninstead of the documented class's own file. Two problems:\n\n1. The environment-specific `.venv/...` path leaks into published docs.\n2. The source shown is the base class's `__init__`, which is not the\n documented object's code at all.\n\nAdditionally, when the merged `__init__` has no source, the block\nrenders nothing rather than falling back to the class source.\n\n### To Reproduce\n\nVerified with mkdocs 1.6.1, mkdocstrings 1.0.4, mkdocstrings-python\n2.0.5, griffe 2.1.0, pydantic 2.13.4 on Python 3.10.\n\n```sh\nmkdir -p repro/src/pkg repro/docs && cd repro\npython -m venv .venv && . .venv/bin/activate\npip install mkdocs mkdocs-material \"mkdocstrings[python]\" pydantic\n```\n\n`src/pkg/__init__.py`:\n\n```python\nfrom pydantic import BaseModel\n\n\nclass Model(BaseModel):\n \"\"\"A pydantic model without its own `__init__`.\"\"\"\n\n x: int = 0\n```\n\n`docs/index.md`:\n\n```markdown\n::: pkg.Model\n```\n\n`mkdocs.yml`:\n\n```yaml\nsite_name: repro\ntheme:\n name: material\nplugins:\n - mkdocstrings:\n handlers:\n python:\n paths: [src]\n options:\n merge_init_into_class: true\n preload_modules: [pydantic]\n show_source: true\n```\n\nBuild and inspect the rendered source block:\n\n```console\n$ mkdocs build\n$ grep -o \"Source code in \u003ccode>[^\u003c]*\u003c/code>\" site/index.html\nSource code in \u003ccode>.venv/lib/python3.10/site-packages/pydantic/main.py\u003c/code>\n```\n\nExpected: `src/pkg/__init__.py`.\n\nThe mechanism, shown at the griffe level (run from the same directory):\n\n```python\nimport sysconfig\n\nimport griffe\n\nloader = griffe.GriffeLoader(\n search_paths=[\"src\", sysconfig.get_paths()[\"purelib\"]]\n)\nloader.load(\"pydantic\")\npkg = loader.load(\"pkg\")\nloader.resolve_aliases()\n\ncls = pkg[\"Model\"]\ninit = cls.all_members[\"__init__\"]\nprint(cls.relative_filepath) # src/pkg/__init__.py\nprint(init.inherited) # True\nprint(init.relative_filepath) # .venv/lib/python3.10/site-packages/pydantic/main.py\n```\n\n### Expected behavior\n\nOnly use `__init__`'s source when the class defines it itself\n(griffe exposes `inherited` on the member); otherwise fall back to the\nclass's own source.\n\n### Environment information\n\n```bash\npython -m mkdocstrings_handlers.python._internal.debug # | xclip -selection clipboard\n```\n\n- __System__: macOS-26.5.1-arm64-arm-64bit\n- __Python__: cpython 3.10.20 (\u003credacted>/.venv/bin/python3)\n- __Environment variables__:\n- __Installed packages__:\n - `mkdocstrings-python` v2.0.5\n\n### Additional context\n\nI'm using a template override in\n`docs/templates/python/materialx/class.html.jinja`, wired\nup via the `custom_templates` option in `mkdocs.yaml`:\n\n```jinja\n{% extends \"_base/class.html.jinja\" %}\n\n{% block source %}\n {% import \"language.html.jinja\" as lang with context %}\n {% if config.show_source %}\n {% if config.merge_init_into_class\n and \"__init__\" in all_members\n and not all_members[\"__init__\"].inherited\n and all_members[\"__init__\"].source %}\n {% with init = all_members[\"__init__\"] %}\n \u003cdetails class=\"mkdocstrings-source\">\n \u003csummary>{{ lang.t(\"Source code in\") }} \u003ccode>\n {%- if init.relative_filepath.is_absolute() -%}\n {{ init.relative_package_filepath }}\n {%- else -%}\n {{ init.relative_filepath }}\n {%- endif -%}\n \u003c/code>\u003c/summary>\n {{ init.source|highlight(language=\"python\", linestart=init.lineno or 0, linenums=True) }}\n \u003c/details>\n {% endwith %}\n {% elif class.source %}\n \u003cdetails class=\"mkdocstrings-source\">\n \u003csummary>{{ lang.t(\"Source code in\") }} \u003ccode>\n {%- if class.relative_filepath.is_absolute() -%}\n {{ class.relative_package_filepath }}\n {%- else -%}\n {{ class.relative_filepath }}\n {%- endif -%}\n \u003c/code>\u003c/summary>\n {{ class.source|highlight(language=\"python\", linestart=class.lineno or 0, linenums=True) }}\n \u003c/details>\n {% endif %}\n {% endif %}\n{% endblock source %}\n```","author":{"url":"https://github.com/cdwilson","@type":"Person","name":"cdwilson"},"datePublished":"2026-07-09T15:37:45Z","url":"https://github.com/mkdocstrings/python/issues/333"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:d067e1fe-09b3-af25-ff95-2b326154a56d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9326:2E5258:537E59:74287B:6AF855F5 |
| html-safe-nonce | e17b695d3cf218cb088fee6410544aaaffb79c10358de483d27647a582d429f7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MzI2OjJFNTI1ODo1MzdFNTk6NzQyODdCOjZBRjg1NUY1IiwidmlzaXRvcl9pZCI6IjEzMTA2NDU1NjQ2NDI3MTEwMjkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | c0f71935b39a4c6110ae66fe221c85badd413e045598be062c9de3c5b1067067 |
| hovercard-subject-tag | issue:4847679963 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/mkdocstrings/python/333/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ccb0b72f7328c23f97d0d475ebaf35cb4d70b7dbc81df252ae8355b690f55873/mkdocstrings/python/issues/333 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ccb0b72f7328c23f97d0d475ebaf35cb4d70b7dbc81df252ae8355b690f55873/mkdocstrings/python/issues/333 |
| og:image:alt | Description of the bug With merge_init_into_class: true, the source block in templates/material/_base/class.html.jinja unconditionally prefers all_members["__init__"] over the class itself: {% if c... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | cdwilson |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5713ecd83001b2f55c3509fb7a292bf7d66d0f167c7ae87a9f7eaec5a52bf8db |
| turbo-cache-control | no-preview |
| go-import | github.com/mkdocstrings/python git https://github.com/mkdocstrings/python.git |
| octolytics-dimension-user_id | 75664361 |
| octolytics-dimension-user_login | mkdocstrings |
| octolytics-dimension-repository_id | 422878404 |
| octolytics-dimension-repository_nwo | mkdocstrings/python |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 422878404 |
| octolytics-dimension-repository_network_root_nwo | mkdocstrings/python |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 653be32ff5c755082a4db662491bfbbcfc9112c1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width