Fix TypeError when version is shadowed by import in conf.py - #14641
Draft
aryansk wants to merge 1 commit into
Draft
Fix TypeError when version is shadowed by import in conf.py#14641aryansk wants to merge 1 commit into
aryansk wants to merge 1 commit into
Conversation
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).
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.
Problem
When
conf.pydoesfrom importlib.metadata import version, the configuration valueversionis shadowed by the imported function. The inventory dump (sphinx/util/inventory.py:InventoryFile.dump) then fails with:at
re.sub(r'\s+', ' ', env.config.version)becauseenv.config.versionis a function, not a string. The build emitsbuild-finishedwith this exception andobjects.invis not written correctly.Example
conf.py:Fixes #13879
Change
Make
InventoryFile.dumprobust to non-stringversion/project:escape()now coerces non-string input viastr()(or empty string forNone)versionis not a string, fall back toreleasewhenreleaseis a string, otherwise to""(per maintainer suggestion in Shadowingversioninconf.pywith animportcauses an error in intersphinx #13879 (comment))projectwhen not a stringSingle-file, narrow fix in
sphinx/util/inventory.py.Why this approach
The config values
project,version,releaseare expected to be strings. Shadowing by an import is a user error, but it should not crash the build with aTypeError; it should warn (Sphinx already warnsThe config value 'version' has type 'function'; expected 'str'.) and write a valid inventory. Falling back toreleaseforversionmatches the maintainer's suggestion and preserves the most useful version information.Testing
Reproduction with shadowed version:
Existing inventory tests:
Documentation and release impact
Review notes
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.