Completed
Pull Request — master (#28)
by Wilfred
06:41 queued 01:15
created

check_args()   D

Complexity

Conditions 8

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
c 1
b 0
f 0
dl 0
loc 33
rs 4
1
from trifle_types import List
2
from errors import ArityError
3
4
5
# Normally we'd use None for this, but RPython doesn't like mixing
6
# None and integers.
7
ANY = -1
8
9
10
# TODO: ideally we'd name the arguments we're expected
11
def check_args(name, args, min=ANY, max=ANY):
12
    """Check whether args is the right length for this function or special
13
    expression.
14
15
    args is a Python list.
16
17
    """
18
    if min == max:
19
        if len(args) != min:
20
            if min == 1:
21
                raise ArityError(
22
                    u"%s takes 1 argument, but got %d: %s" % (name, len(args), List(args).repr())
23
                )
24
            else:
25
                raise ArityError(
26
                    u"%s takes %d arguments, but got %d: %s" % (name, min, len(args), List(args).repr())
27
                )
28
29
    elif max == ANY:
30
        if len(args) < min:
31
            if min == 1:
32
                raise ArityError(
33
                    u"%s takes at least 1 argument, but got %d: %s" % (name, len(args), List(args).repr())
34
                )
35
            else:
36
                raise ArityError(
37
                    u"%s takes at least %d arguments, but got %d: %s" % (name, min, len(args), List(args).repr())
38
                )
39
40
    else:
41
        if not (min <= len(args) <= max):
42
            raise ArityError(
43
                u"%s takes between %d and %d arguments, but got %d: %s" % (name, min, max, len(args), List(args).repr())
44
            )
45