Skip to content

fix: Don't render environment paths in source block labels - #338

Merged
pawamoy merged 3 commits into
mkdocstrings:mainfrom
cdwilson:fix/class-source-block
Aug 17, 2026
Merged

fix: Don't render environment paths in source block labels#338
pawamoy merged 3 commits into
mkdocstrings:mainfrom
cdwilson:fix/class-source-block

Conversation

@cdwilson

Copy link
Copy Markdown
Contributor

For reviewers

  • I did not use AI
  • I used AI and thoroughly reviewed every code/docs change

Description of the change

This PR is a follow-up to the discussion in #333 (and tinydantic/tinydantic#131): when an object's file lives in an in-project virtual environment, source block labels leaked the environment path, e.g.

.venv/lib/python3.10/site-packages/pydantic/main.py

The path is relative to the CWD, so it slipped past the is_absolute() check that normally keeps environment paths out of rendered docs.

Just a heads up regarding scope vs. what we discussed in the issue: Claude found the same is_absolute()-only pattern at every source label site. Rather than copy the compound condition to five places, the label logic now lives in a single Jinja filter, source_location, used by every source block:

  • paths containing a site-packages segment are truncated to the part below it
  • otherwise, absolute paths fall back to relative_package_filepath as before
  • ordinary project-relative paths render unchanged

Truncating below site-packages (instead of switching to relative_package_filepath) also fixes labels for single-file modules installed directly in site-packages, whose package-relative path itself still contains the environment directory:

label site before after
merged __init__ inherited from an in-project venv .venv/lib/python3.10/site-packages/pydantic/main.py pydantic/main.py
class / method labels from an in-project venv .venv/lib/python3.10/site-packages/pydantic/main.py pydantic/main.py
single-file module (six.py-style distribution) site-packages/six.py six.py
__init__ inherited from the same package src/pkg/base.py src/pkg/base.py (unchanged)

Both themes are covered by tests parameterized over three scenarios:

  1. Merged __init__
  2. A class with its methods
  3. A single-file module

Relevant resources

Closes #333: #333
Related to tinydantic/tinydantic#131: tinydantic/tinydantic#131
Assisted-by: Claude:claude-fable-5

cdwilson and others added 3 commits August 16, 2026 23:28
When an object's file lives in a virtual environment inside the
current working directory (for example a pydantic `BaseModel`
subclass documented with `preload_modules: [pydantic]` and an
in-project `.venv`), source blocks were labeled with the environment
path, like `.venv/lib/python3.10/site-packages/pydantic/main.py`.
The path is relative to the current working directory, so it slipped
past the `is_absolute()` check that normally keeps environment paths
out of rendered docs.

Add a `source_location` filter that strips everything up to and
including a `site-packages` directory from displayed source paths,
and use it for every source block label: merged `__init__`, class,
and function, in both themes. Centralizing the logic also fixes the
label for single-file modules installed directly in `site-packages`
(like `six.py`), whose package-relative path still contained the
environment directory. Labels of objects belonging to the documented
package itself are unchanged.

Issue mkdocstrings#333: mkdocstrings#333
Assisted-by: Claude:claude-fable-5
Assisted-by: Claude:claude-fable-5
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

@pawamoy pawamoy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks! I just restyled one of the test, nothing important.

@pawamoy
pawamoy merged commit 84ba73e into mkdocstrings:main Aug 17, 2026
19 of 33 checks passed
@cdwilson
cdwilson deleted the fix/class-source-block branch August 17, 2026 16:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: merge_init_into_class renders inherited __init__ source (leaks site-packages paths)

2 participants