It is not possible to pass a typed parameter to type r

62 views Asked by At

I have this code

import typer

app = typer.Typer()


class Parametrs:
    one: int
    two: str


@app.command()
def calib(parametrs: Parametrs):
    print(f"One: {parametrs.one}, Two: {parametrs.two}")


if __name__ == "__main__":
    app()

when I try to send a command to the console, I get an error

Then i get this error

RuntimeError: Type not yet supported: <class 'main.Parametrs'>

2

There are 2 answers

0
PCDSandwichMan On

Yup you're exactly right it's a typer thing. But! We may be able to make this work. Could you do something like this?

import typer

app = typer.Typer()

class Parametrs:
    def __init__(self, one: int, two: str):
        self.one = one
        self.two = two

@app.command()
def calib(one: int, two: str):
    parametrs = Parametrs(one, two)
    print(f"One: {parametrs.one}, Two: {parametrs.two}")

if __name__ == "__main__":
    app()
0
Silvio Mayolo On

You can supply a custom parser to teach typer how to parse your type. Assuming Parameters is required,

import typer
from typing import Annotated

class Parameters:
    ...

def parse_parameters(input_data: str) -> Parameters:
    # Construct a parameters instance from the input parameter here
    ...

@app.command()
def calib(parameters: Annotated[Parameters, typer.Argument(parser=parse_parameters)]):
    ...