summaryrefslogtreecommitdiff
path: root/tests/functional/s/singledispatch_functions.py
blob: 931bfd30d68156a2f723662db458e592dcee0067 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# pylint: disable=missing-docstring,import-error,unused-import,assignment-from-no-return
# pylint: disable=invalid-name, too-few-public-methods
from UNINFERABLE import uninferable_func

try:
    from functools import singledispatch
except ImportError:
    from singledispatch import singledispatch

my_single_dispatch = singledispatch


class FakeSingleDispatch:

    @staticmethod
    def register(function):
        return function

    def __call__(self, function):
        return function

fake_singledispatch_decorator = FakeSingleDispatch()

@singledispatch
def func(arg):
    return arg


@func.register(str)
def _(arg):
    return 42


@func.register(float)
@func.register(int)
def _(arg):
    return 42


@my_single_dispatch
def func2(arg):
    return arg


@func2.register(int)
def _(arg):
    return 42


@singledispatch
def with_extra_arg(arg, verbose=False):
    if verbose:
        print(arg)
    return arg


@with_extra_arg.register(str)
def _(arg, verbose=False):
    unused = 42 # [unused-variable]
    return arg[::-1]


@fake_singledispatch_decorator
def not_single_dispatch(arg): # [unused-argument]
    return 'not yet implemented'


@fake_singledispatch_decorator.register(str)
def bad_single_dispatch(arg): # [unused-argument]
    return 42


@fake_singledispatch_decorator.register(str)
def bad_single_dispatch(arg): # [unused-argument, function-redefined]
    return 24