Skip to content

Fix handling of Literal annotation values in Sphinx#14410

Open
badcount wants to merge 1 commit intosphinx-doc:masterfrom
badcount:fix/literal-annotation-values
Open

Fix handling of Literal annotation values in Sphinx#14410
badcount wants to merge 1 commit intosphinx-doc:masterfrom
badcount:fix/literal-annotation-values

Conversation

@badcount
Copy link
Copy Markdown

Summary

This fix addresses the issue where Literal annotation values (like Literal[True], Literal[False]) trigger nitpick warnings because Sphinx incorrectly treats them as Python classes that need to be resolved as cross-references.

Changes

  • Added is_literal_subscript() function to check if a node is Literal[...] subscript
  • Added parse_annotation_with_literal_context() function to track in_literal context
  • Modified main loop to explicitly handle True, False, and None as plain text nodes
  • Added support for ast.UnaryOp to handle unary operators like -1, +1, ~1
  • Added support for literal strings, bytes, and numbers

Testing

The fix ensures that:

  1. Literal values in Literal[True], Literal[False], etc. are NOT cross-referenced
  2. Regular type annotations like int, List[int], None, List[None] still get cross-referenced correctly
  3. Nitpick warnings are not triggered for literal values when using -n -W mode

- 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.
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.

1 participant