Beispiel #1
0
def apply_stub_annotations(stub_path: str, file_path: str) -> str:
    with open(stub_path) as stub_file, open(file_path) as source_file:
        stub = _parse(stub_file)
        source = _parse(source_file)
        context = CodemodContext()
        ApplyTypeAnnotationsVisitor.store_stub_in_context(context, stub)
        modified_tree = ApplyTypeAnnotationsVisitor(context).transform_module(source)
        return modified_tree.code
Beispiel #2
0
def apply_stub_annotations(stub_path: str, file_path: str) -> str:
    with open(stub_path) as stub_file, open(file_path) as source_file:
        stub = _parse(stub_file)
        source = _parse(source_file)
        context = CodemodContext()
        if LIBCST_VERSION >= "0.3.5":
            # pyre-ignore[16]: This is from the new version of libcst.
            ApplyTypeAnnotationsVisitor.store_stub_in_context(context, stub)
        else:
            ApplyTypeAnnotationsVisitor.add_stub_to_context(context, stub)
        modified_tree = ApplyTypeAnnotationsVisitor(context).transform_module(
            source)
        return modified_tree.code
    def test_count_annotations(
        self,
        stub: str,
        before: str,
        after: str,
        annotation_counts: AnnotationCounts,
        any_changes_applied: False,
    ):
        stub = self.make_fixture_data(stub)
        before = self.make_fixture_data(before)
        after = self.make_fixture_data(after)

        context = CodemodContext()
        ApplyTypeAnnotationsVisitor.store_stub_in_context(
            context=context, stub=parse_module(stub))
        visitor = ApplyTypeAnnotationsVisitor(context=context)

        output_code = visitor.transform_module(parse_module(before)).code

        self.assertEqual(after, output_code)
        self.assertEqual(str(annotation_counts),
                         str(visitor.annotation_counts))
        self.assertEqual(any_changes_applied,
                         visitor.annotation_counts.any_changes_applied())