Make parameter optional¶
Convert a required function parameter into an optional parameter without manually editing the signature. BetterPy adds | None to its type annotation and supplies a None default when no default exists. The function body stays unchanged.
How to invoke¶
Place the caret on a required parameter and use ⌥⏎ / Alt+Enter -> "BetterPy: Make optional".
Example¶
becomes:
What it supports¶
- Annotated function and method parameters, preserving an existing default value.
- Annotated attributes.
- Parameters in regular Python source files in your project.
- Existing annotations and surrounding formatting.
What it deliberately avoids¶
- Untyped parameters and annotations that already accept
None. - Parameters whose position cannot accept a default without changing Python's required-parameter ordering rules.
*args,**kwargs, and synthetic parameters.- Files outside your own project sources.
Notes¶
- Python 3.10 and later use
T | None; older Python versions usetyping.Optional[T], adding an import when needed. - Review how the function handles
None: the intention does not update its body. - The feature is gated by the Make parameter optional setting under Settings | BetterPy | Refactoring & code transformations | Arguments.