Example #1
0
def can_trace_files_without_annotating_them():
    # We trace increment so that we know its type as its passed into twice
    twice = """
def twice(func, value):
    return func(func(value))
"""
    typed_twice = """
def twice(func: Callable[[int], int], value: int) -> int:
    return func(func(value))
"""
    increment = """
def increment(value):
    return value + 1
"""
    run = """
import twice
import increment
print(twice.twice(increment.increment, 40))
"""
    files = {
        "run.py": run,
        "twice.py": twice,
        "increment.py": increment,
    }
    with create_temp_dir(files) as directory:
        farthing.run_and_annotate(
            trace_paths=[os.path.join(directory.path, "increment.py")],
            annotate_paths=[os.path.join(directory.path, "twice.py")],
            argv=[os.path.join(directory.path, "run.py")])
        assert_equal(typed_twice, _read_file(os.path.join(directory.path, "twice.py")))
        assert_equal(increment, _read_file(os.path.join(directory.path, "increment.py")))
Example #2
0
def _annotate_source(source):
    with program_with_module(source) as program:
        farthing.run_and_annotate(
            annotate_paths=[program.directory_path],
            trace_paths=[],
            argv=[program.run_path])
        return _read_file(program.module_path)
Example #3
0
def can_pass_trace_paths_for_files_instead_of_directories():
    source = """
def repeat(x, y):
    return x * y

print(repeat("hello ", 3))
"""
    typed_program = """
def repeat(x: str, y: int) -> str:
    return x * y

print(repeat("hello ", 3))
"""
    with program_with_module(source) as program:
        farthing.run_and_annotate(
            annotate_paths=[program.module_path],
            trace_paths=[],
            argv=[program.run_path])
        assert_equal(typed_program, _read_file(program.module_path))