Skip to content

Fix TypeError when version is shadowed by import in conf.py - #14641

Draft
aryansk wants to merge 1 commit into
sphinx-doc:masterfrom
aryansk:fix-intersphinx-version-shadowing-13879
Draft

Fix TypeError when version is shadowed by import in conf.py#14641
aryansk wants to merge 1 commit into
sphinx-doc:masterfrom
aryansk:fix-intersphinx-version-shadowing-13879

Conversation

@aryansk

@aryansk aryansk commented Aug 25, 2026

Copy link
Copy Markdown

Problem

When conf.py does from importlib.metadata import version, the configuration value version is shadowed by the imported function. The inventory dump (sphinx/util/inventory.py:InventoryFile.dump) then fails with:

TypeError: expected string or bytes-like object

at re.sub(r'\s+', ' ', env.config.version) because env.config.version is a function, not a string. The build emits build-finished with this exception and objects.inv is not written correctly.

Example conf.py:

from importlib.metadata import version
project = 'test'
release = '1.0.0'
# version is now the function, not a string

Fixes #13879

Change

Make InventoryFile.dump robust to non-string version/project:

Single-file, narrow fix in sphinx/util/inventory.py.

Why this approach

The config values project, version, release are expected to be strings. Shadowing by an import is a user error, but it should not crash the build with a TypeError; it should warn (Sphinx already warns The config value 'version' has type 'function'; expected 'str'.) and write a valid inventory. Falling back to release for version matches the maintainer's suggestion and preserves the most useful version information.

Testing

Reproduction with shadowed version:

command: uv run --project /tmp/sphinx-l9-13879/repo python /tmp/test_sphinx_version_shadow.py
result: Build succeeded, objects.inv contains "# Version: 1.0.0" (fallback to release), warning about version type, no TypeError

Existing inventory tests:

command: uv run --project /tmp/sphinx-l9-13879/repo pytest /tmp/sphinx-l9-13879/repo/tests -k inventory -v
result: 11 passed, 1 skipped

Documentation and release impact

  • No documentation impact (bug fix)
  • Changelog needed — will add if maintainer requests

Review notes

  • Follow-up: none
  • Security: no

AI disclosure

Muse Spark assisted in analysis and fix drafting; all changes manually reviewed and tested. The contributor understands and can explain the submitted code.

When conf.py does 'from importlib.metadata import version', the
configuration value 'version' is replaced by the imported function.
The inventory dump then fails with 'TypeError: expected string or
bytes-like object' when InventoryFile.dump tries to escape
env.config.version via re.sub.

The fix makes the dump robust:

- Make escape() handle non-string input via str() fallback
- If version is not a string, fall back to release when it is a
  string, otherwise to empty string (per maintainer suggestion)
- Similarly handle project when not a string

This preserves the build (with a config type warning) and writes a
valid objects.inv with the fallback version.

Fixes sphinx-doc#13879

AI disclosure: Muse Spark assisted in analysis and fix drafting;
changes reviewed and tested manually (reproduction with shadowed
version now builds successfully, objects.inv contains fallback
version, 11 inventory tests pass).
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.

Shadowing version in conf.py with an import causes an error in intersphinx

1 participant