← All recipes
Moderne licensed

Replace typing.Optional[X] with X | None

Recipe IDorg.openrewrite.python.migrate.ReplaceTypingOptionalWithUnionArtifactopenrewrite-migrate-python

PEP 604 introduced the | operator for union types in Python 3.10. Replace Optional[X] with the more concise X | None syntax. Applies inside if TYPE_CHECKING: blocks as well; X | None is valid at runtime from Python 3.10 on, so no from __future__ import annotations is needed.

Single recipepythonmigrationtyping3.10pep604Proprietary

Usage

This recipe has no required configuration options. You’ll need the Moderne CLI configured before running the command below.

mod run . --recipe org.openrewrite.python.migrate.ReplaceTypingOptionalWithUnion

If the recipe isn’t available locally, install it with:

mod config recipes pip install openrewrite-migrate-python