fix: Don't render environment paths in source block labels - #338
Merged
Conversation
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
approved these changes
Aug 17, 2026
pawamoy
left a comment
Member
There was a problem hiding this comment.
LGTM, thanks! I just restyled one of the test, nothing important.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
For reviewers
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.
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:site-packagessegment are truncated to the part below itrelative_package_filepathas beforeTruncating below
site-packages(instead of switching torelative_package_filepath) also fixes labels for single-file modules installed directly insite-packages, whose package-relative path itself still contains the environment directory:__init__inherited from an in-project venv.venv/lib/python3.10/site-packages/pydantic/main.pypydantic/main.py.venv/lib/python3.10/site-packages/pydantic/main.pypydantic/main.pysix.py-style distribution)site-packages/six.pysix.py__init__inherited from the same packagesrc/pkg/base.pysrc/pkg/base.py(unchanged)Both themes are covered by tests parameterized over three scenarios:
__init__Relevant resources
Closes #333: #333
Related to tinydantic/tinydantic#131: tinydantic/tinydantic#131
Assisted-by: Claude:claude-fable-5