예제 #1
0
def parse_parameter(key: str, value: Any) -> Parameter:
    if isinstance(value, dict):
        value["name"] = key
        if "default" in value and "type" not in value:
            value["type"] = get_parameter_type_name(key, value["default"])
        return Parameter.parse(value)

    parameter = Parameter(
        name=key,
        type=get_parameter_type_name(key, value),
        default=value,
    )

    if parameter.type == "flag":
        parameter.pass_true_as = f"--{parameter.name}=true"
        parameter.pass_false_as = f"--{parameter.name}=false"

    return parameter
def test_minmax(case):
    param = Parameter(name='test', type='integer', min=0, max=10)
    with pytest.raises(ValidationErrors):
        param.validate(case)
    param.validate(5)
def test_choice(case):
    param = Parameter(name='test', choices={'blep', 'mlem'})
    with pytest.raises(ValidationErrors):
        param.validate(case)
    param.validate('blep')
def test_float(case):
    param = Parameter(name='test', type='float')
    with pytest.raises(ValidationErrors):
        param.validate(case)
    param.validate(1.5)
def test_integer(case):
    param = Parameter(name='test', type='integer')
    with pytest.raises(ValidationErrors):
        param.validate(case)
    param.validate(5)