Skip to content

Make parameter optional

Convert a required function parameter into an optional parameter without manually editing the signature. BetterPy adds a default value at the declaration site and leaves the rest of the signature intact.

How to invoke

Place the caret on a required parameter and use ⌥⏎ / Alt+Enter -> "BetterPy: Make optional".

Example

def greet(name: str, title: str):
    return f"{title} {name}"

becomes:

def greet(name: str, title: str = ""):
    return f"{title} {name}"

What it supports

  • Function and method parameters that currently have no default value.
  • Typed and untyped parameters.
  • Parameters in regular Python source files in your project.
  • Existing annotations and surrounding formatting.

What it deliberately avoids

  • Parameters that already have a default value.
  • 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

  • The inserted default is intentionally conservative. Review it after applying the intention when the domain needs a different sentinel or value.
  • The feature is gated by the Make parameter optional setting under Settings | BetterPy | Refactoring & code transformations | Arguments.