Prompt
When annotating optional or nullable types in Python, ensure compatibility across all supported Python versions. For codebases supporting Python 3.9:
- Add
from __future__ import annotationsat the top of files using the union type syntax:from __future__ import annotations class ExInfo: name: str retry: bool description: str | None - Alternatively, use
typing.Optional[X]instead ofX | Nonesyntax:import typing class CoderPrompts: repo_content_prefix: typing.Optional[str] = """Here are summaries..."""
This ensures proper null handling with type annotations while maintaining backward compatibility with Python 3.9, preventing runtime errors like TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'.