• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pomponchik / transfunctions / 16415392355

21 Jul 2025 11:11AM UTC coverage: 94.064% (+0.4%) from 93.706%
16415392355

push

github

web-flow
Merge pull request #2 from pomponchik/develop

0.0.2

83 of 89 new or added lines in 5 files covered. (93.26%)

1 existing line in 1 file now uncovered.

206 of 219 relevant lines covered (94.06%)

5.62 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.08
/transfunctions/decorators/superfunction.py
1
import sys
6✔
2
import weakref
6✔
3
from ast import NodeTransformer, Expr, AST
6✔
4
from inspect import currentframe
6✔
5
from functools import wraps
6✔
6
from typing import Dict, Any, Optional, Union, List
6✔
7
from collections.abc import Coroutine
6✔
8

9
if sys.version_info <= (3, 10):  # pragma: no cover
10
    from typing_extensions import TypeAlias
11
else:  # pragma: no cover
12
    from typing import TypeAlias
13

14
from displayhooks import not_display
6✔
15

16
from transfunctions.transformer import FunctionTransformer
6✔
17
from transfunctions.errors import WrongTransfunctionSyntaxError
6✔
18

19

20
if sys.version_info <= (3, 9):  # pragma: no cover
21
    CoroutineClass = Coroutine
22
else:  # pragma: no cover
23
    CoroutineClass: TypeAlias = Coroutine[Any, Any, None]
24

25
class UsageTracer(CoroutineClass):
6✔
26
    def __init__(self, args, kwargs, transformer) -> None:
6✔
27
        self.flags: Dict[str, bool] = {}
6✔
28
        self.args = args
6✔
29
        self.kwargs = kwargs
6✔
30
        self.transformer = transformer
6✔
31
        self.coroutine = self.async_sleep_option(self.flags, args, kwargs, transformer)
6✔
32
        self.finalizer = weakref.finalize(self, self.sync_sleep_option, self.flags, args, kwargs, transformer, self.coroutine)
6✔
33

34
    def __iter__(self):
6✔
35
        self.flags['used'] = True
6✔
36
        self.coroutine.close()
6✔
37
        generator_function = self.transformer.get_generator_function()
6✔
38
        generator = generator_function(*(self.args), **(self.kwargs))
6✔
39
        yield from generator
6✔
40

41
    def __await__(self) -> Any:  # pragma: no cover
42
        return self.coroutine.__await__()
43

44
    def __invert__(self):
6✔
45
        print(self.finalizer)
6✔
46
        print(self.finalizer.alive)
6✔
47
        result = self.finalizer()
6✔
48
        print('result:', result)
6✔
49
        return result
6✔
50

51
    def send(self, value: Any) -> Any:
6✔
52
        return self.coroutine.send(value)
6✔
53

54
    def throw(self, exception_type: Any, value: Any = None, traceback: Any = None) -> None:  # pragma: no cover
55
        pass
56

57
    def close(self) -> None:  # pragma: no cover
58
        pass
59

60
    @staticmethod
6✔
61
    def sync_sleep_option(flags: Dict[str, bool], args, kwargs, transformer, wrapped_coroutine: CoroutineClass) -> None:
6✔
62
        if not flags.get('used', False):
6✔
63
            wrapped_coroutine.close()
6✔
64
            return transformer.get_usual_function()(*args, **kwargs)
6✔
65

66
    @staticmethod
6✔
67
    async def async_sleep_option(flags: Dict[str, bool], args, kwargs, transformer) -> None:
6✔
68
        flags['used'] = True
6✔
69
        await transformer.get_async_function()(*args, **kwargs)
6✔
70

71

72
not_display(UsageTracer)
6✔
73

74
def superfunction(function):
6✔
75
    class NoReturns(NodeTransformer):
6✔
76
        def visit_Return(self, node: Expr) -> Optional[Union[AST, List[AST]]]:
6✔
NEW
77
            raise WrongTransfunctionSyntaxError('A superfunction cannot contain a return statement.')
×
78

79
    transformer = FunctionTransformer(
6✔
80
        function,
81
        currentframe().f_back.f_lineno,
82
        'superfunction',
83
        extra_transformers=[
84
            #NoReturns(),
85
        ],
86
    )
87

88
    @wraps(function)
6✔
89
    def wrapper(*args, **kwargs):
6✔
90
        return UsageTracer(args, kwargs, transformer)
6✔
91

92
    wrapper.__is_superfunction__ = True
6✔
93

94
    return wrapper
6✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc