Exemple #1
0
    def __new__(cls, class_name, bases, namespace):

        if not bases:
            namespace["__dependencies__"] = {}
            return type.__new__(cls, class_name, bases, namespace)

        check_inheritance(bases, Injector)
        ns = {}
        for attr in ("__module__", "__doc__", "__weakref__", "__qualname__"):
            try:
                ns[attr] = namespace.pop(attr)
            except KeyError:
                pass
        for name in namespace:
            check_dunder_name(name)
            check_attrs_redefinition(name)
        dependencies = {}
        for base in reversed(bases):
            dependencies.update(base.__dependencies__)
        for name, dep in namespace.items():
            dependencies[name] = make_dependency_spec(name, dep)
        check_loops(class_name, dependencies)
        check_circles(dependencies)
        ns["__dependencies__"] = dependencies
        return type.__new__(cls, class_name, bases, ns)
Exemple #2
0
def deep_replace_dependency(injector, current_attr, replace):

    spec = make_dependency_spec(current_attr, replace.dependency)
    marker, attribute, args, have_defaults = spec
    attribute = Attributes(attribute, replace.attrs)
    spec = (marker, attribute, args, have_defaults)

    for base in injector.__mro__:
        if current_attr in base.__dependencies__:
            base.__dependencies__[current_attr] = spec
        else:
            break