File: test_dispatcher_3only.py

package info (click to toggle)
python-multipledispatch 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 272 kB
  • sloc: python: 1,116; makefile: 141
file content (94 lines) | stat: -rw-r--r-- 1,659 bytes parent folder | download
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# import sys

# from nose import SkipTest

from multipledispatch import dispatch
from multipledispatch.dispatcher import Dispatcher


def test_function_annotation_register():
    f = Dispatcher("f")

    @f.register()
    def inc(x: int):
        return x + 1

    @f.register()
    def inc(x: float):
        return x - 1

    assert f(1) == 2
    assert f(1.0) == 0.0


def test_function_annotation_dispatch():
    @dispatch()
    def inc(x: int):
        return x + 1

    @dispatch()
    def inc(x: float):
        return x - 1

    assert inc(1) == 2
    assert inc(1.0) == 0.0


def test_function_annotation_dispatch_custom_namespace():
    namespace = {}

    @dispatch(namespace=namespace)
    def inc(x: int):
        return x + 2

    @dispatch(namespace=namespace)
    def inc(x: float):
        return x - 2

    assert inc(1) == 3
    assert inc(1.0) == -1.0

    assert namespace["inc"] == inc
    assert set(inc.funcs.keys()) == set([(int,), (float,)])


def test_method_annotations():
    class Foo:
        @dispatch()
        def f(self, x: int):
            return x + 1

        @dispatch()
        def f(self, x: float):
            return x - 1

    foo = Foo()

    assert foo.f(1) == 2
    assert foo.f(1.0) == 0.0


def test_overlaps():
    @dispatch(int)
    def inc(x: int):
        return x + 1

    @dispatch(float)
    def inc(x: float):
        return x - 1

    assert inc(1) == 2
    assert inc(1.0) == 0.0


def test_overlaps_conflict_annotation():
    @dispatch(int)
    def inc(x: str):
        return x + 1

    @dispatch(float)
    def inc(x: int):
        return x - 1

    assert inc(1) == 2
    assert inc(1.0) == 0.0