|
|
import logging |
|
|
import sys |
|
|
import unittest |
|
|
|
|
|
from makefun.main import get_signature_from_string, with_signature |
|
|
from makefun import create_wrapper, wraps |
|
|
|
|
|
try: |
|
|
from inspect import signature, Signature, Parameter |
|
|
except ImportError: |
|
|
from funcsigs import signature, Signature, Parameter |
|
|
|
|
|
class TestMakeFun(unittest.TestCase): |
|
|
|
|
|
def test_non_representable_defaults(self): |
|
|
""" Tests that non-representable default values are handled correctly """ |
|
|
|
|
|
def foo(logger=logging.getLogger('default')): |
|
|
pass |
|
|
|
|
|
@wraps(foo) |
|
|
def bar(*args, **kwargs): |
|
|
pass |
|
|
|
|
|
bar() |
|
|
|
|
|
def test_preserve_attributes(self): |
|
|
""" Tests that attributes are preserved """ |
|
|
|
|
|
def foo(): |
|
|
pass |
|
|
|
|
|
setattr(foo, 'a', True) |
|
|
|
|
|
@wraps(foo) |
|
|
def bar(*args, **kwargs): |
|
|
pass |
|
|
|
|
|
self.assertTrue(bar.a) |
|
|
|
|
|
def test_empty_name_in_string(self): |
|
|
""" Tests that string signatures can now be provided without function name""" |
|
|
if sys.version_info < (3, 0): |
|
|
str_sig = '(a)' |
|
|
else: |
|
|
str_sig = '(a:int)' |
|
|
func_name, func_sig, func_sig_str = get_signature_from_string(str_sig, locals()) |
|
|
self.assertIsNone(func_name) |
|
|
|
|
|
self.assertEqual(str(func_sig).replace(' ', ''), str_sig) |
|
|
self.assertEqual(func_sig_str, str_sig + ':') |
|
|
|
|
|
def test_same_than_wraps_basic(self): |
|
|
"""Tests that the metadata set by @wraps is correct""" |
|
|
|
|
|
from tests.test_doc import test_from_sig_wrapper |
|
|
from functools import wraps as functools_wraps |
|
|
|
|
|
def foo_wrapper(*args, **kwargs): |
|
|
""" hoho """ |
|
|
pass |
|
|
|
|
|
functool_wrapped = functools_wraps(test_from_sig_wrapper)(foo_wrapper) |
|
|
|
|
|
|
|
|
def foo_wrapper(*args, **kwargs): |
|
|
""" hoho """ |
|
|
pass |
|
|
|
|
|
makefun_wrapped = wraps(test_from_sig_wrapper)(foo_wrapper) |
|
|
|
|
|
makefun_with_signature_inverted = with_signature(signature(test_from_sig_wrapper))(test_from_sig_wrapper) |
|
|
makefun_with_signature_normal = with_signature(signature(test_from_sig_wrapper))(foo_wrapper) |
|
|
|
|
|
for field in ('__module__', '__name__', '__qualname__', '__doc__', '__annotations__'): |
|
|
if sys.version_info < (3, 0) and field in {'__qualname__', '__annotations__'}: |
|
|
continue |
|
|
else: |
|
|
self.assertEqual(getattr(functool_wrapped, field), getattr(makefun_wrapped, field), f"field {field} is different") |
|
|
self.assertEqual(getattr(functool_wrapped, field), getattr(makefun_with_signature_inverted, field), f"field {field} is different") |
|
|
if field != '__annotations__': |
|
|
self.assertNotEqual(getattr(functool_wrapped, field), getattr(makefun_with_signature_normal, field), f"field {field} is identical") |
|
|
|
|
|
def tests_wraps_sigchange(self): |
|
|
""" Tests that wraps can be used to change the signature """ |
|
|
|
|
|
def foo(a): |
|
|
""" hoho """ |
|
|
return a |
|
|
|
|
|
@wraps(foo, new_sig="(a, b=0)") |
|
|
def goo(*args, **kwargs): |
|
|
kwargs.pop('b') |
|
|
return foo(*args, **kwargs) |
|
|
|
|
|
for field in ('__module__', '__name__', '__qualname__', '__doc__', '__annotations__'): |
|
|
if sys.version_info < (3, 0) and field in {'__qualname__', '__annotations__'}: |
|
|
continue |
|
|
else: |
|
|
self.assertEqual(getattr(goo, field), getattr(foo, field), f"field {field} is different") |
|
|
|
|
|
self.assertEqual(str(signature(goo)), "(a, b=0)") |
|
|
self.assertEqual(goo('hello'), 'hello') |
|
|
|
|
|
def tests_wraps_lambda(self): |
|
|
""" Tests that `@wraps` can duplicate the signature of a lambda """ |
|
|
foo = lambda a: a |
|
|
|
|
|
@wraps(foo) |
|
|
def goo(*args, **kwargs): |
|
|
return foo(*args, **kwargs) |
|
|
|
|
|
self.assertEqual(goo.__name__, (lambda: None).__name__) |
|
|
self.assertEqual(str(signature(goo)), '(a)') |
|
|
self.assertEqual(goo('hello'), 'hello') |
|
|
|
|
|
def tests_wraps_renamed_lambda(self): |
|
|
""" Tests that `@wraps` can duplicate the signature of a lambda that has been renamed """ |
|
|
foo = lambda a: a |
|
|
foo.__name__ = 'bar' |
|
|
|
|
|
@wraps(foo) |
|
|
def goo(*args, **kwargs): |
|
|
return foo(*args, **kwargs) |
|
|
|
|
|
self.assertEqual(goo.__name__, 'bar') |
|
|
self.assertEqual(str(signature(goo)), '(a)') |
|
|
self.assertEqual(goo('hello'), 'hello') |
|
|
|
|
|
def test_lambda_signature_str(self): |
|
|
""" Tests that `@with_signature` can create a lambda from a signature string """ |
|
|
new_sig = '(a, b=5)' |
|
|
|
|
|
@with_signature(new_sig, func_name='<lambda>') |
|
|
def foo(a, b): |
|
|
return a + b |
|
|
|
|
|
self.assertEqual(foo.__name__, '<lambda>') |
|
|
self.assertEqual(foo.__code__.co_name, '<lambda>') |
|
|
self.assertEqual(str(signature(foo)), new_sig) |
|
|
self.assertEqual(foo(a=4), 9) |
|
|
|
|
|
def test_co_name(self): |
|
|
""" Tests that `@with_signature` can be used to change the __code__.co_name """ |
|
|
@with_signature('()', co_name='bar') |
|
|
def foo(): |
|
|
return 'hello' |
|
|
|
|
|
self.assertEqual(foo.__name__, 'foo') |
|
|
self.assertEqual(foo.__code__.co_name, 'bar') |
|
|
self.assertEqual(foo(), 'hello') |
|
|
|
|
|
def test_with_signature_lambda(self): |
|
|
""" Tests that `@with_signature` can be used to change the __code__.co_name to `'<lambda>'` """ |
|
|
@with_signature('()', co_name='<lambda>') |
|
|
def foo(): |
|
|
return 'hello' |
|
|
|
|
|
self.assertEqual(foo.__code__.co_name, '<lambda>') |
|
|
self.assertEqual(foo(), 'hello') |
|
|
|
|
|
def test_create_wrapper_lambda(self): |
|
|
""" Tests that `create_wrapper` returns a lambda function when given a lambda function to wrap""" |
|
|
def foo(): |
|
|
return 'hello' |
|
|
bar = create_wrapper(lambda: None, foo) |
|
|
|
|
|
self.assertEqual(bar.__name__, '<lambda>') |
|
|
self.assertEqual(bar(), 'hello') |
|
|
|
|
|
def test_invalid_co_name(self): |
|
|
""" Tests that `@with_signature` raises a `ValueError` when given an `co_name` that cannot be duplicated. """ |
|
|
with self.assertRaises(ValueError): |
|
|
@with_signature('()', co_name='<invalid>') |
|
|
def foo(): |
|
|
return 'hello' |
|
|
|
|
|
def test_invalid_func_name(self): |
|
|
""" Tests that `@with_signature` can duplicate a func_name that is invalid in a function definition. """ |
|
|
@with_signature('()', func_name='<invalid>') |
|
|
def foo(): |
|
|
return 'hello' |
|
|
|
|
|
self.assertEqual(foo.__name__, '<invalid>') |
|
|
self.assertEqual(foo(), 'hello') |
|
|
|
|
|
@unittest.skipIf(sys.version_info < (3, 0), reason="requires python3 or higher") |
|
|
def test_qualname_when_nested(self): |
|
|
""" Tests that qualname is correctly set when `@with_signature` is applied on nested functions """ |
|
|
|
|
|
class C: |
|
|
def f(self): |
|
|
pass |
|
|
class D: |
|
|
@with_signature("(self, a)") |
|
|
def g(self): |
|
|
pass |
|
|
|
|
|
self.assertEqual(C.__qualname__, 'test_qualname_when_nested.<locals>.C') |
|
|
self.assertEqual(C.f.__qualname__, 'test_qualname_when_nested.<locals>.C.f') |
|
|
self.assertEqual(C.D.__qualname__, 'test_qualname_when_nested.<locals>.C.D') |
|
|
self.assertEqual(C.D.g.__qualname__, 'test_qualname_when_nested.<locals>.C.D.g') |
|
|
self.assertEqual(str(signature(C.D.g)), "(self, a)") |
|
|
|
|
|
@unittest.skipIf(sys.version_info < (3, 5), reason="requires python 3.5 or higher (non-comment type hints)") |
|
|
def test_type_hint_error(self): |
|
|
""" Test for https://github.com/smarie/python-makefun/issues/32 """ |
|
|
|
|
|
from tests._test_py35 import make_ref_function |
|
|
ref_f = make_ref_function() |
|
|
|
|
|
@wraps(ref_f) |
|
|
def foo(a): |
|
|
return a |
|
|
|
|
|
self.assertEqual(foo(10), 10) |
|
|
|
|
|
@unittest.skipIf(sys.version_info < (3, 5), reason="requires python 3.5 or higher (non-comment type hints)") |
|
|
def test_type_hint_error2(self): |
|
|
""" Test for https://github.com/smarie/python-makefun/issues/32 """ |
|
|
|
|
|
from tests._test_py35 import make_ref_function2 |
|
|
ref_f = make_ref_function2() |
|
|
|
|
|
@wraps(ref_f) |
|
|
def foo(a): |
|
|
return a |
|
|
|
|
|
self.assertEqual(foo(10), 10) |
|
|
|
|
|
@unittest.skipIf(sys.version_info < (3, 5), reason="requires python 3.5 or higher (non-comment type hints)") |
|
|
def test_type_hint_error_sigchange(self): |
|
|
""" Test for https://github.com/smarie/python-makefun/issues/32 """ |
|
|
|
|
|
from tests._test_py35 import make_ref_function |
|
|
from typing import Any |
|
|
ref_f = make_ref_function() |
|
|
|
|
|
@wraps(ref_f, new_sig="(a: Any)") |
|
|
def foo(a): |
|
|
return a |
|
|
|
|
|
self.assertEqual(foo(10), 10) |
|
|
|
|
|
if __name__ == '__main__': |
|
|
unittest.main() |