コード例 #1
0
def emit_components(m: Module, *, target_module: ModuleType) -> Module:
    m.toplevel.from_("fastapi", "Depends")

    scanned = scan_module(target_module)
    spec_map = {
        component.__name__: fnspec(component) for component in scanned.components
    }
    code_map: t.Dict[str, Object] = {}

    seen: t.Set(str) = set()
    for name, spec in spec_map.items():
        if inspect.iscoroutinefunction(spec.body):
            raise NotImplementedError("async support")

        if len(spec.arguments) > 0:
            code = create_component_code(spec, spec_map)
            code_map[name] = code

    def _visit(name: str) -> None:
        if name in seen:
            return
        seen.add(name)

        if name not in code_map:
            return

        spec = spec_map[name]
        for subname, _, _ in spec.arguments:
            if subname not in code_map:
                continue
            _visit(subname)

        code = code_map[name]
        m.stmt(code)

    # emit with dependencies ordered
    for name in spec_map:
        _visit(name)

    return m
コード例 #2
0
    name: str,
    val: int,
    default: int = 0,
    *,
    nickname: t.Optional[str] = None,
    debug: bool = False,
    **metadata: t.Optional[t.Any],
) -> None:
    pass


m = Module()
m.toplevel = m.submodule(import_unique=True)
m.sep()

spec = fnspec(f)
with m.class_("F"):
    for name, typ, kind in spec.parameters:
        if typ.__module__ != "builtins":
            m.toplevel.import_(typ.__module__)

        info = typeinfo(typ)
        rhs = spec.type_str_of(info.normalized)
        if info.is_optional:
            rhs = LazyFormat("typing.Optional[{}]", rhs)

        if kind == "var_kw":
            rhs = LazyFormat("typing.Dict[str, {}]", rhs)
        elif kind == "var_args":
            rhs = LazyFormat("typing.List[{}]", rhs)
        elif kind == "kw_defaults" or kind == "args_defaults":
コード例 #3
0
    default: int = 0,
    *,
    nickname: t.Optional[str] = None,
    debug: bool = False,
    **metadata: t.Optional[t.Any],
) -> None:
    pass


m = Module()
m.toplevel = m.submodule(import_unique=True)
m.sep()

# todo: alias
for fn in [f, g]:
    spec = fnspec(fn)
    with m.class_(pascalcase(spec.name)):
        for name, typ, kind in spec.parameters:
            if typ.__module__ != "builtins":
                m.toplevel.import_(typ.__module__)

            info = typeinfo(typ)
            rhs = spec.type_str_of(info.normalized)
            if info.is_optional:
                rhs = LazyFormat("typing.Optional[{}]", rhs)

            if kind == "var_kw":
                rhs = LazyFormat("typing.Dict[str, {}]", rhs)
            elif kind == "var_args":
                rhs = LazyFormat("typing.List[{}]", rhs)
            elif kind == "kw_defaults" or kind == "args_defaults":