Fix handling of Literal annotation values in Sphinx#14410
Open
badcount wants to merge 1 commit intosphinx-doc:masterfrom
Open
Fix handling of Literal annotation values in Sphinx#14410badcount wants to merge 1 commit intosphinx-doc:masterfrom
badcount wants to merge 1 commit intosphinx-doc:masterfrom
Conversation
- Add is_literal_subscript() to detect Literal[...] subscripts - Add parse_annotation_with_literal_context() to track in_literal context - Handle True, False, None as plain text (not cross-references) in Literal context - Support ast.UnaryOp for unary operators like -1, +1, ~1 - Support literal strings, bytes, and numbers as plain text This prevents nitpick warnings when using -n -W mode with Literal[True], Literal[False], etc.
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.
Summary
This fix addresses the issue where
Literalannotation values (likeLiteral[True],Literal[False]) trigger nitpick warnings because Sphinx incorrectly treats them as Python classes that need to be resolved as cross-references.Changes
is_literal_subscript()function to check if a node isLiteral[...]subscriptparse_annotation_with_literal_context()function to trackin_literalcontextTrue,False, andNoneas plain text nodesast.UnaryOpto handle unary operators like-1,+1,~1Testing
The fix ensures that:
Literal[True],Literal[False], etc. are NOT cross-referencedint,List[int],None,List[None]still get cross-referenced correctly-n -Wmode