qiskit-machine-learning icon indicating copy to clipboard operation
qiskit-machine-learning copied to clipboard

Refactor argument handling to enforce positional-only and keyword-only arguments using `/` and `*`

Open edoaltamura opened this issue 1 year ago • 10 comments

What should we add?

Description

To improve readability and usability in the Qiskit codebase, we propose refactoring function signatures to clearly define positional-only, positional-or-keyword, and keyword-only arguments using the / and * symbols. Parameters before the slash will be strictly positional, while those after the asterisk will be keyword-only. This approach clarifies the API and aligns with Python standards, as in Scikit-learn.

We can start with function signatures and see which arguments should be made keyword-only or remain positional. Temporary lint disables may be necessary to prevent errors during the transition, as seen in #833.

Example from Pegasos QSVC

def __init__(
        self,
        quantum_kernel: BaseKernel | None = None,
        C: float = 1.0,
        num_steps: int = 1000,
        precomputed: bool = False,
        seed: int | None = None,
    ) -> None:

into

def __init__(
        self,
        quantum_kernel: BaseKernel | None = None,
        *,
        C: float = 1.0,
        num_steps: int = 1000,
        precomputed: bool = False,
        seed: int | None = None,
    ) -> None:

edoaltamura avatar Sep 27 '24 11:09 edoaltamura