Ejemplo n.º 1
0
def get_stub(args: argparse.Namespace, stdout: IO,
             stderr: IO) -> Optional[Stub]:
    module, qualname = args.module_path
    thunks = args.config.trace_store().filter(module, qualname, args.limit)
    traces = []
    failed_to_decode_count = 0
    for thunk in thunks:
        try:
            traces.append(thunk.to_trace())
        except MonkeyTypeError as mte:
            if args.verbose:
                print(f'WARNING: Failed decoding trace: {mte}', file=stderr)
            failed_to_decode_count += 1
    if failed_to_decode_count and not args.verbose:
        print(
            f'{failed_to_decode_count} traces failed to decode; use -v for details',
            file=stderr)
    if not traces:
        return None
    rewriter = args.config.type_rewriter()
    if args.disable_type_rewriting:
        rewriter = NoOpRewriter()
    stubs = build_module_stubs_from_traces(
        traces,
        args.config.max_typed_dict_size(),
        existing_annotation_strategy=args.existing_annotation_strategy,
        rewriter=rewriter,
    )
    if args.sample_count:
        display_sample_count(traces, stderr)
    return stubs.get(module, None)
Ejemplo n.º 2
0
def get_updated_definition(
    func: Callable,
    traces: Iterable[CallTrace],
    rewriter: Optional[TypeRewriter] = None,
    ignore_existing_annotations: bool = False,
) -> FunctionDefinition:
    """Update the definition for func using the types collected in traces."""
    if rewriter is None:
        rewriter = NoOpRewriter()
    defn = FunctionDefinition.from_callable(func)
    arg_types, return_type, yield_type = shrink_traced_types(traces)
    arg_types = {
        name: rewriter.rewrite(typ)
        for name, typ in arg_types.items()
    }
    if return_type is not None:
        return_type = rewriter.rewrite(return_type)
    if yield_type is not None:
        yield_type = rewriter.rewrite(yield_type)
    sig = defn.signature
    sig = update_signature_args(sig, arg_types, defn.has_self,
                                ignore_existing_annotations)
    sig = update_signature_return(sig, return_type, yield_type,
                                  ignore_existing_annotations)
    return FunctionDefinition(defn.module, defn.qualname, defn.kind, sig,
                              defn.is_async)
Ejemplo n.º 3
0
def get_updated_definition(
    func: Callable,
    traces: Iterable[CallTrace],
    max_typed_dict_size: int,
    rewriter: Optional[TypeRewriter] = None,
    existing_annotation_strategy:
    ExistingAnnotationStrategy = ExistingAnnotationStrategy.REPLICATE,
) -> FunctionDefinition:
    """Update the definition for func using the types collected in traces."""
    if rewriter is None:
        rewriter = NoOpRewriter()
    arg_types, return_type, yield_type = shrink_traced_types(
        traces, max_typed_dict_size)
    arg_types = {
        name: rewriter.rewrite(typ)
        for name, typ in arg_types.items()
    }
    if return_type is not None:
        return_type = rewriter.rewrite(return_type)
    if yield_type is not None:
        yield_type = rewriter.rewrite(yield_type)
    # pyre-fixme[6]: Expected `Dict[str, typing.Type[typing.Any]]` for 2nd param but
    #  got `Dict[str, type]`.
    return FunctionDefinition.from_callable_and_traced_types(
        func, arg_types, return_type, yield_type, existing_annotation_strategy)
Ejemplo n.º 4
0
def get_updated_definition(
    func: Callable,
    traces: Iterable[CallTrace],
    rewriter: Optional[TypeRewriter] = None,
    existing_annotation_strategy: ExistingAnnotationStrategy = ExistingAnnotationStrategy.REPLICATE,
) -> FunctionDefinition:
    """Update the definition for func using the types collected in traces."""
    if rewriter is None:
        rewriter = NoOpRewriter()
    arg_types, return_type, yield_type = shrink_traced_types(traces)
    arg_types = {name: rewriter.rewrite(typ) for name, typ in arg_types.items()}
    if return_type is not None:
        return_type = rewriter.rewrite(return_type)
    if yield_type is not None:
        yield_type = rewriter.rewrite(yield_type)
    return FunctionDefinition.from_callable_and_traced_types(func, arg_types, return_type,
                                                             yield_type, existing_annotation_strategy)
Ejemplo n.º 5
0
def get_stub(args: argparse.Namespace, stdout: IO,
             stderr: IO) -> Optional[Stub]:
    module, qualname = args.module_path
    thunks = args.config.trace_store().filter(module, qualname, args.limit)
    traces = []
    for thunk in thunks:
        try:
            traces.append(thunk.to_trace())
        except MonkeyTypeError as mte:
            print(f'ERROR: Failed decoding trace: {mte}', file=stderr)
    if not traces:
        return None
    rewriter = args.config.type_rewriter()
    if args.disable_type_rewriting:
        rewriter = NoOpRewriter()
    stubs = build_module_stubs_from_traces(traces,
                                           args.include_unparsable_defaults,
                                           rewriter)
    return stubs.get(module, None)
Ejemplo n.º 6
0
 def type_rewriter(self) -> TypeRewriter:
     """Return the type rewriter for use when generating stubs."""
     return NoOpRewriter()