Failed Conditions
Pull Request — master (#1152)
by Lasse
03:36
created

non_optional_params()   A

Complexity

Conditions 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 8
rs 9.4286
1
class CircularDependencyError(Exception):
2
    @classmethod
3
    def for_bears(cls, bears):
4
        """
5
        Creates the CircularDependencyError with a helpful message about the
6
        dependency.
7
        """
8
        bear_names = []
9
10
        for bear in bears:
11
            bear_names.append(bear.__name__)
12
13
        return cls("Circular dependency detected: " + " -> ".join(bear_names))
14
15
16
def _resolve(bears, resolved_bears, seen):
17
    for bear in bears:
18
        if bear in resolved_bears:
19
            continue
20
21
        missing = bear.missing_dependencies(resolved_bears)
22
        if missing == []:
23
            resolved_bears.append(bear)
24
            continue
25
26
        if bear in seen:
27
            seen.append(bear)
28
            raise CircularDependencyError.for_bears(seen)
29
30
        seen.append(bear)
31
        resolved_bears = _resolve(missing, resolved_bears, seen)
32
        resolved_bears.append(bear)
33
        seen.remove(bear)  # Already resolved, no candidate for circular dep
34
35
    return resolved_bears
36
37
38
def resolve(bears):
39
    """
40
    Collects all dependencies of the given bears. This will also remove
41
    duplicates.
42
43
    :param bears: The given bears. Will not be modified.
44
    :return:      The new list of bears, sorted so that it can be executed
45
                  sequentially without dependency issues.
46
    """
47
    return _resolve(bears, [], [])
48