def test_preserve_annotations(self): func = f('self, a:2, b, c:3', 4) tr = modifiers._PokTranslator(func, kwoargs=('a', 'b')) self.assertSigsEqual(s('self, c:3, *, a:2, b', 4), signature(tr)) self.assertSigsEqual(s('c:3, *, a:2, b', 4), signature(safe_get(tr, object(), object)))
def _test(self, outer, inner, args, kwargs, expected, expected_get, exp_src, exp_src_get): outer_f = support.f(outer, name='outer') inner_f = support.f(inner, name='inner') forw = specifiers.forwards_to_function(inner_f, *args, **kwargs)(outer_f) sig = specifiers.signature(forw) self.assertSigsEqual(sig, support.s(expected)) self.assertSourcesEqual( sig.sources, { 'outer': exp_src[0], 'inner': exp_src[1], '+depths': ['outer', 'inner'] }) sig_get = specifiers.signature(_util.safe_get(forw, object(), object)) self.assertSigsEqual(sig_get, support.s(expected_get)) self.assertSourcesEqual( sig_get.sources, { 'outer': exp_src_get[0], 'inner': exp_src_get[1], '+depths': ['outer', 'inner'] })
def test_get_from_object(self): class A(object): def wrapped(self, x, y): pass def method(self, a, *p, **k): self.wrapped(a, *p, **k) method = _util.safe_get(A.__dict__['method'], object(), type(A)) self._test(method)
def __get__(self, instance, owner): # apply __new__ staticmethod automatic transform # and any other ones Python may come up with if not self._transformed: self.__wrapped__ = _transform(self.__wrapped__, type(owner)) self._transformed = True return type(self)( _util.safe_get(self.__wrapped__, instance, owner), self._signature_forger)
def __get__(self, instance, owner): # apply __new__ staticmethod automatic transform # and any other ones Python may come up with if not self._transformed: self.__wrapped__ = _transform(self.__wrapped__, type(owner)) self._transformed = True return type(self)( _util.safe_get(self.__wrapped__, instance, owner), self._signature_forger)
def test_call(self): outer = support.f('*args, **kwargs') inner = support.f('a, *, b') forw = specifiers.forwards_to_function(inner)(outer) instance = object() forw_get_prox = _util.safe_get(forw, instance, object) self.assertEqual( forw_get_prox(1, b=2), {'args': (instance, 1), 'kwargs': {'b': 2}})
def forwards_tests(self, outer, inner, args, kwargs, expected, expected_get): outer_f = support.f(outer) inner_f = support.f(inner) forw = specifiers.forwards_to_function(inner_f, *args, **kwargs)(outer_f) self.assertSigsEqual(specifiers.signature(forw), support.s(expected)) self.assertSigsEqual( specifiers.signature(_util.safe_get(forw, object(), object)), support.s(expected_get))
def test_get_from_object(self): class A(object): def wrapped(self, x, y): pass def method(self, a, *p, **k): self.wrapped(a, *p, **k) method = _util.safe_get(A.__dict__['method'], object(), type(A)) self._test(method)
def test_pok_interact(self): pok = f('self, a, *, b') annotated = modifiers.annotate(a=1, b=2)(pok) self.assertSigsEqual( s('self, a:1, *, b:2'), signature(annotated) ) self.assertSigsEqual( s('a:1, *, b:2'), signature(safe_get(annotated, object(), object)) )
def test_call(self): outer = support.f('*args, **kwargs') inner = support.f('a, *, b') forw = specifiers.forwards_to_function(inner)(outer) instance = object() forw_get_prox = _util.safe_get(forw, instance, object) self.assertEqual(forw_get_prox(1, b=2), { 'args': (instance, 1), 'kwargs': { 'b': 2 } })
def test_preserve_annotations(self): func = f('self, a:2, b, c:3', 4) tr = modifiers._PokTranslator(func, kwoargs=('a', 'b')) self.assertSigsEqual( s('self, c:3, *, a:2, b', 4), signature(tr) ) self.assertSigsEqual( s('c:3, *, a:2, b', 4), signature(safe_get(tr, object(), object)) )
def _test(self, outer, inner, args, kwargs, expected, expected_get, exp_src, exp_src_get): outer_f = support.f(outer, name='outer') inner_f = support.f(inner, name='inner') forw = specifiers.forwards_to_function(inner_f, *args, **kwargs)(outer_f) sig = specifiers.signature(forw) self.assertSigsEqual(sig, support.s(expected)) self.assertSourcesEqual(sig.sources, { 'outer': exp_src[0], 'inner': exp_src[1], '+depths': ['outer', 'inner']}) sig_get = specifiers.signature(_util.safe_get(forw, object(), object)) self.assertSigsEqual(sig_get, support.s(expected_get)) self.assertSourcesEqual(sig_get.sources, { 'outer': exp_src_get[0], 'inner': exp_src_get[1], '+depths': ['outer', 'inner']})
def process_signature(app, what, name, obj, options, sig, return_annotation): try: parent, obj = fetch_dotted_name(name) except AttributeError: return sig, return_annotation if isinstance(obj, instancemethod): # python 2 unbound methods obj = obj.__func__ if isinstance(parent, type) and callable(obj): obj = _util.safe_get(obj, object(), type(parent)) try: sig = specifiers.signature(obj) except (TypeError, ValueError): # inspect.signature raises ValueError if obj is callable but it can't # determine a signature, eg. built-in objects return sig, return_annotation ret_annot = sig.return_annotation if ret_annot != sig.empty: sret_annot = '-> {0!r}'.format(ret_annot) sig = sig.replace(return_annotation=sig.empty) else: sret_annot = '' return str(sig), sret_annot
def process_signature(app, what, name, obj, options, sig, return_annotation): try: parent, obj = fetch_dotted_name(name) except AttributeError: return sig, return_annotation if isinstance(obj, instancemethod): # python 2 unbound methods obj = obj.__func__ if isinstance(parent, type) and callable(obj): obj = _util.safe_get(obj, object(), type(parent)) try: sig = specifiers.signature(obj) except (TypeError, ValueError): # inspect.signature raises ValueError if obj is callable but it can't # determine a signature, eg. built-in objects return sig, return_annotation ret_annot = sig.return_annotation if ret_annot != sig.empty: sret_annot = '-> {0!r}'.format(ret_annot) sig = sig.replace(return_annotation=sig.empty) else: sret_annot = '' return str(sig), sret_annot
def test_pok_interact(self): pok = f('self, a, *, b') annotated = modifiers.annotate(a=1, b=2)(pok) self.assertSigsEqual(s('self, a:1, *, b:2'), signature(annotated)) self.assertSigsEqual(s('a:1, *, b:2'), signature(safe_get(annotated, object(), object)))
def __get__(self, instance, owner): return type(self)( self.decorator, self.wrapper, _util.safe_get(self.__wrapped__, instance, owner))