Exemple #1
0
def test_parse_with_tag(schema_mock):
    map1 = FixedMapping(
        RequiredArgument('value', SimpleValue.str, ''),
    ).add_tag('tag', 'tag_value')

    res = map1.try_parse({'value': 'str'})
    assert res.value == 'str'
    assert res.tag == 'tag_value'

    # Tag is never copied from data
    res = map1.try_parse({'value': 'str', 'tag': 'another value'})
    assert res.value == 'str'
    assert res.tag == 'tag_value'

    # Tag does not show up in schema
    assert map1.to_open_api(schema_mock) == {
        'type': 'object',
        'properties': {
            'value': {
                'description': ('Comment', ''),
                'type': ('Convert', str),
            },
        },
        'required': ['value'],
    }
Exemple #2
0
def test_combine(schema_mock):
    map1 = FixedMapping(
        RequiredArgument('value1', SimpleValue.str, 'desc1'),
        OptionalArgument('value2', SimpleValue.int, 'desc2')
    )
    map2 = FixedMapping(RequiredArgument('value3', SimpleValue.bool, 'desc3'))
    map3 = map1.combine(map2)

    res = map3.try_parse({'value1': 'value1', 'value2': 100, 'value3': True})
    assert res.value1 == 'value1'
    assert res.value2.unsafe_extract() == 100
    assert res.value3 is True

    assert map3.to_open_api(schema_mock) == {
        'type': 'object',
        'properties': {
            'value1': {
                'description': ('Comment', 'desc1'),
                'type': ('Convert', str),
            },
            'value2': {
                'description': ('Comment', 'desc2'),
                'type': ('Convert', int),
            },
            'value3': {
                'description': ('Comment', 'desc3'),
                'type': ('Convert', bool),
            },
        },
        'required': ['value1', 'value3'],
    }
Exemple #3
0
def test_simple_parse_success():
    map1 = FixedMapping(
        RequiredArgument('a', SimpleValue.int, ''),
        RequiredArgument('b', SimpleValue.str, ''),
        OptionalArgument('c', SimpleValue.bool, ''),
    )
    res = map1.try_parse({'a': 5, 'b': 'hello'})
    assert res.a == 5
    assert res.b == 'hello'
    # Is present when not given but simply a Nothing
    assert res.c.is_nothing

    res = map1.try_parse({'a': 10, 'b': 'bye', 'c': True})
    assert res.a == 10
    assert res.b == 'bye'
    # Is present as a Maybe
    assert res.c.unsafe_extract() is True
Exemple #4
0
def test_simple_parse_failure():
    map1 = FixedMapping(
        RequiredArgument('a', SimpleValue.int, ''),
        RequiredArgument('b', SimpleValue.str, ''),
        OptionalArgument('c', SimpleValue.bool, ''),
    )
    with pytest.raises(SimpleParseError):
        map1.try_parse('not a dict')

    with pytest.raises(MultipleParseErrors) as err:
        map1.try_parse({'a': 5, 'c': True})
    assert 'at index "b" a str is required, but got Nothing' in str(err.value)

    with pytest.raises(MultipleParseErrors) as err:
        map1.try_parse({'a': 5, 'b': 'a string', 'c': 'not a bool'})
    msg = str(err.value)
    assert "at index \"c\" a bool is required, but got 'not a bool'" in msg
import typing as t

from typing_extensions import Literal

from cg_request_args import (SimpleValue, FixedMapping, OptionalArgument,
                             RequiredArgument)

FixedMapping(  # E:r Key 'a' was already present, but given again as argument 2\..*
    RequiredArgument('a', SimpleValue.int, ''),
    RequiredArgument('a', SimpleValue.int, ''),
)

arg: RequiredArgument[int, Literal[5]] = ...  # type: ignore
FixedMapping(  # E:r Key should be of type string, but was of type int for argument 1.*
    arg)

arg2: RequiredArgument[int, int] = ...  # type: ignore
FixedMapping(  # E:r Second parameter of the argument should be a literal, this was not the case for argument 1.*
    arg2)
FixedMapping(  # E:r Argument 1 was an "Any" which is not allowed as an argument to FixedMapping.*
    t.cast(t.Any, ...))

non_literal = 'hell' + 'o'
RequiredArgument(  # E:r The key to the _Argument constructor should be a literal.*
    non_literal, SimpleValue.str, '')

map1 = FixedMapping(RequiredArgument('b', SimpleValue.str, ''))
map1.add_tag(  # E:r The key to FixedMapping.add_tag should be a literal.*
    non_literal, 'literal')
map1.add_tag('literal', non_literal)
map1.add_tag(  # E:r The key to FixedMapping.add_tag should be a literal.*
Exemple #6
0
def test_dict_getter_unknown_attr():
    map1 = FixedMapping(RequiredArgument('a', SimpleValue.int, ''), )
    res = map1.try_parse({'a': 0})
    assert res.a == 0
    with pytest.raises(AttributeError):
        _ = res.b
import typing as t

from cg_request_args import (
    SimpleValue, FixedMapping, OptionalArgument, RequiredArgument
)

arg1 = RequiredArgument('help', SimpleValue.int, '')
reveal_type(
    arg1  # N: Revealed type is 'cg_request_args.RequiredArgument[builtins.int*, Literal['help']]'
)
arg2 = OptionalArgument('help2', SimpleValue.str, '')
reveal_type(
    arg2  # N: Revealed type is 'cg_request_args.OptionalArgument[builtins.str*, Literal['help2']]'
)
parser = FixedMapping(arg1, arg2)
reveal_type(
    parser  # N: Revealed type is 'cg_request_args.FixedMapping[TypedDict({'help': builtins.int*, 'help2': Union[cg_maybe.Just[builtins.str*], cg_maybe._Nothing[builtins.str*]]})]'
)
parsed = parser.try_parse({})

parsed.help3  # E:r The _DictGetter\[.*\] does not have the attribute 'help3', available attributes:.*
reveal_type(parsed.help)  # N: Revealed type is 'builtins.int*'
help2 = parsed.help2
reveal_type(
    help2  # N: Revealed type is 'Union[cg_maybe.Just[builtins.str*], cg_maybe._Nothing[builtins.str*]]'
)
import typing as t

from cg_request_args import (SimpleValue, FixedMapping, OptionalArgument,
                             RequiredArgument)

map1 = FixedMapping(RequiredArgument('a', SimpleValue.int, ''))
map2 = FixedMapping(OptionalArgument('a', SimpleValue.int, ''))
map3 = FixedMapping(RequiredArgument('b', SimpleValue.str, ''))

map1.combine(map2)  # E:r Cannot combine typeddict, got overlapping key 'a'.*
map4 = map1.combine(map3)
reveal_type(
    map4  # N: Revealed type is 'cg_request_args.FixedMapping[TypedDict({'a': builtins.int*, 'b': builtins.str*})]'
)