Exemplo n.º 1
0
def test_without_parameters():
    sig = Signature([Parameter('foo'), Parameter('bar'), Parameter('baz')])

    expected = Signature([Parameter('bar')])

    result = sig.without_parameters(0, 'baz')
    assert result == expected
Exemplo n.º 2
0
def test_replace(attrs):
    param = Parameter('foo')

    param = param.replace(**attrs)

    assert isinstance(param, Parameter)

    for key, value in attrs.items():
        assert getattr(param, key) == value
Exemplo n.º 3
0
def test_num_required_arguments():
    sig = Signature([
        Parameter('a', Parameter.POSITIONAL_ONLY),
        Parameter('b', Parameter.VAR_POSITIONAL),
        Parameter('c', Parameter.KEYWORD_ONLY),
        Parameter('d', Parameter.VAR_KEYWORD)
    ])

    assert sig.num_required_arguments == 2
Exemplo n.º 4
0
def test_replace():
    sig = Signature([Parameter('foo')], return_annotation=int)

    expected = Signature(return_annotation=str)

    result = sig.replace(parameters=[], return_annotation=str)
    assert isinstance(result, Signature)
    assert result == expected
Exemplo n.º 5
0
def test_is_vararg(kind, expected):
    param = Parameter('foo', kind)

    assert param.is_vararg == expected
Exemplo n.º 6
0
def test_from_parameter():
    in_param = Parameter('foo', Parameter.KEYWORD_ONLY, object(), int)
    param = Parameter.from_parameter(in_param)

    assert in_param == param
Exemplo n.º 7
0
def test_from_inspect_parameter():
    in_param = inspect.Parameter('foo', Parameter.KEYWORD_ONLY, default=object(), annotation=int)
    param = Parameter.from_parameter(in_param)

    assert in_param == param
Exemplo n.º 8
0
import pytest

import inspect
import typing

from introspection import Parameter


@pytest.mark.parametrize('param1, param2, expected', [
    (inspect.Parameter('a', Parameter.POSITIONAL_ONLY), Parameter('a', Parameter.POSITIONAL_ONLY), True),
    (Parameter('a', Parameter.POSITIONAL_ONLY), inspect.Parameter('a', Parameter.POSITIONAL_ONLY), True),
    (inspect.Parameter('not_a', Parameter.POSITIONAL_ONLY), Parameter('a', Parameter.POSITIONAL_ONLY), False),
    (inspect.Parameter('a', Parameter.VAR_KEYWORD), Parameter('a', Parameter.VAR_KEYWORD), True),
])
def test_equality(param1, param2, expected):
    hash_equal = hash(param1) == hash(param2)
    assert hash_equal == expected

    equal = param1 == param2
    assert equal == expected


def test_from_inspect_parameter():
    in_param = inspect.Parameter('foo', Parameter.KEYWORD_ONLY, default=object(), annotation=int)
    param = Parameter.from_parameter(in_param)

    assert in_param == param


def test_from_parameter():
Exemplo n.º 9
0
        'd': 'D'
    }, {
        'a': ('A', 'B'),
        'c': {
            'd': 'D'
        }
    }),
])
def test_bind_partial(func, args, kwargs, expected_result):
    sig = Signature.from_callable(func)
    assert sig.bind_partial(*args, **kwargs).arguments == expected_result


@pytest.mark.parametrize('signature, expected', [
    (Signature([
        Parameter('a', Parameter.POSITIONAL_ONLY),
        Parameter('b', Parameter.VAR_POSITIONAL)
    ]), '(a, /, *b)'),
    (Signature([
        Parameter('a', Parameter.POSITIONAL_ONLY),
        Parameter('b', Parameter.POSITIONAL_ONLY)
    ]), '(a, b, /)'),
    (Signature([Parameter('a', default=Parameter.missing)]), '([a])'),
    (Signature([
        Parameter('a', Parameter.POSITIONAL_ONLY, default=Parameter.missing)
    ]), '([a], /)'),
    (Signature([
        Parameter('a', Parameter.POSITIONAL_ONLY, default=Parameter.missing),
        Parameter('b', default=Parameter.missing)
    ]), '([a], /[, b])'),
    (Signature([