예제 #1
0
def test_inspect_signature_property():
    if not PY3:
        return

    # By adding AddX to our signature registry, we can inspect the class
    # itself and objects of the class.  `inspect.signature` doesn't like
    # it when `obj.__signature__` is a property.
    class AddX(object):
        def __init__(self, func):
            self.func = func

        def __call__(self, addx, *args, **kwargs):
            return addx + self.func(*args, **kwargs)

        @property
        def __signature__(self):
            sig = inspect.signature(self.func)
            params = list(sig.parameters.values())
            kind = inspect.Parameter.POSITIONAL_OR_KEYWORD
            newparam = inspect.Parameter("addx", kind)
            params = [newparam] + params
            return sig.replace(parameters=params)

    addx = AddX(lambda x: x)
    sig = inspect.signature(addx)
    assert sig == inspect.Signature(parameters=[
        inspect.Parameter("addx", inspect.Parameter.POSITIONAL_OR_KEYWORD),
        inspect.Parameter("x", inspect.Parameter.POSITIONAL_OR_KEYWORD),
    ])

    assert num_required_args(AddX) is False
    _sigs.signatures[AddX] = (_sigs.expand_sig((0, lambda func: None)), )
    assert num_required_args(AddX) == 1
    del _sigs.signatures[AddX]
예제 #2
0
def test_inspect_signature_property():
    if not PY3:
        return

    # By adding AddX to our signature registry, we can inspect the class
    # itself and objects of the class.  `inspect.signature` doesn't like
    # it when `obj.__signature__` is a property.
    class AddX(object):
        def __init__(self, func):
            self.func = func

        def __call__(self, addx, *args, **kwargs):
            return addx + self.func(*args, **kwargs)

        @property
        def __signature__(self):
            sig = inspect.signature(self.func)
            params = list(sig.parameters.values())
            kind = inspect.Parameter.POSITIONAL_OR_KEYWORD
            newparam = inspect.Parameter('addx', kind)
            params = [newparam] + params
            return sig.replace(parameters=params)

    addx = AddX(lambda x: x)
    sig = inspect.signature(addx)
    assert sig == inspect.Signature(parameters=[
        inspect.Parameter('addx', inspect.Parameter.POSITIONAL_OR_KEYWORD),
        inspect.Parameter('x', inspect.Parameter.POSITIONAL_OR_KEYWORD)])

    assert num_required_args(AddX) is False
    _sigs.signatures[AddX] = (_sigs.expand_sig((0, lambda func: None)),)
    assert num_required_args(AddX) == 1
    del _sigs.signatures[AddX]
예제 #3
0
def test_inspect_wrapped_property():
    class Wrapped(object):
        def __init__(self, func):
            self.func = func

        def __call__(self, *args, **kwargs):
            return self.func(*args, **kwargs)

        @property
        def __wrapped__(self):
            return self.func

    func = lambda x: x
    wrapped = Wrapped(func)
    assert inspect.signature(func) == inspect.signature(wrapped)

    assert num_required_args(Wrapped) is None
    _sigs.signatures[Wrapped] = (_sigs.expand_sig((0, lambda func: None)), )
    assert num_required_args(Wrapped) == 1
예제 #4
0
def test_inspect_wrapped_property():
    class Wrapped(object):
        def __init__(self, func):
            self.func = func

        def __call__(self, *args, **kwargs):
            return self.func(*args, **kwargs)

        @property
        def __wrapped__(self):
            return self.func

    func = lambda x: x
    wrapped = Wrapped(func)
    if PY3:
        assert inspect.signature(func) == inspect.signature(wrapped)

    assert num_required_args(Wrapped) == (False if PY33 else None)
    _sigs.signatures[Wrapped] = (_sigs.expand_sig((0, lambda func: None)),)
    assert num_required_args(Wrapped) == 1