# Buggy code
def _capture_function_code_using_source_copy(func) -> str:
    import textwrap

    func_code = inspect.getsource(func)

    # Function might be defined in some indented scope (e.g. in another function).
    # We need to handle this and properly dedent the function source code
    func_code = textwrap.dedent(func_code)
    func_code_lines = func_code.split("\n")

    # Removing possible decorators (can be multiline) until the function definition is found
    while func_code_lines and not func_code_lines[0].startswith("def "):
        del func_code_lines[0]

    if not func_code_lines:
        raise ValueError(
            'Failed to dedent and clean up the source of function "{}". It is probably not properly indented.'.format(
                func.__name__
            )
        )

    func_code = "\n".join(func_code_lines)

    # Stripping type annotations to prevent import errors.
    # The most common cases are InputPath/OutputPath and typing.NamedTuple annotations
    func_code = strip_type_hints(func_code)

    return func_code


# Traceback (OSError)
Traceback (most recent call last):
File "/opt/conda/lib/python3.8/site-packages/torch/_utils_internal.py", line 49, in get_source_lines_and_file
sourcelines, file_lineno = inspect.getsourcelines(obj)
File "/opt/conda/lib/python3.8/inspect.py", line 967, in getsourcelines
lines, lnum = findsource(object)
File "/opt/conda/lib/python3.8/inspect.py", line 798, in findsource
raise OSError('could not get source code')
OSError: could not get source code
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<string>", line 55, in <module>
File "<string>", line 44, in create_fully_connected_pytorch_network
File "/opt/conda/lib/python3.8/site-packages/torch/jit/_script.py", line 897, in script
return torch.jit._recursive.create_script_module(
File "/opt/conda/lib/python3.8/site-packages/torch/jit/_recursive.py", line 352, in create_script_module
return create_script_module_impl(nn_module, concrete_type, stubs_fn)
File "/opt/conda/lib/python3.8/site-packages/torch/jit/_recursive.py", line 364, in create_script_module_impl
method_stubs = stubs_fn(nn_module)
File "/opt/conda/lib/python3.8/site-packages/torch/jit/_recursive.py", line 591, in infer_methods_to_compile
stubs.append(make_stub_from_method(nn_module, method))
File "/opt/conda/lib/python3.8/site-packages/torch/jit/_recursive.py", line 52, in make_stub_from_method
return make_stub(func, method_name)
File "/opt/conda/lib/python3.8/site-packages/torch/jit/_recursive.py", line 37, in make_stub
ast = get_jit_def(func, name, self_name="RecursiveScriptModule")
File "/opt/conda/lib/python3.8/site-packages/torch/jit/frontend.py", line 197, in get_jit_def
sourcelines, file_lineno, filename = get_source_lines_and_file(fn, torch._C.ErrorReport.call_stack())
File "/opt/conda/lib/python3.8/site-packages/torch/_utils_internal.py", line 56, in get_source_lines_and_file
raise OSError(msg) from e
OSError: Can't get source for <bound method create_fully_connected_pytorch_network.<locals>.FullyConnectedNetwork.forward of FullyConnectedNetwork(
(layers): ModuleList(
(0): Linear(in_features=10, out_features=100, bias=True)
(1): Linear(in_features=100, out_features=10, bias=True)
(2): Linear(in_features=10, out_features=1, bias=True)
)
)>. TorchScript requires source access in order to carry out compilation, make sure original .py files are available.

# Fixed code
def _capture_function_code_using_source_copy(func) -> str:
    func_code = inspect.getsource(func)

    # Function might be defined in some indented scope (e.g. in another function).
    # We need to handle this and properly dedent the function source code
    func_code = textwrap.dedent(func_code)
    func_code_lines = func_code.split("\n")

    # Removing possible decorators (can be multiline) until the function definition is found
    while func_code_lines and not func_code_lines[0].startswith("def "):
        del func_code_lines[0]

    if not func_code_lines:
        raise ValueError(
            'Failed to dedent and clean up the source of function "{}". It is probably not properly indented.'.format(
                func.__name__
            )
        )

    func_code = "\n".join(func_code_lines)

    # Stripping type annotations to prevent import errors.
    # The most common cases are InputPath/OutputPath and typing.NamedTuple annotations
    func_code = strip_type_hints(func_code)

    return func_code