from __future__ import annotations

import gc
import inspect
import os
import time
import types
from io import StringIO
from pathlib import Path
from typing import List, Optional, Type
from unittest.mock import MagicMock, patch

import dill as pickle
import pytest
from inference.core.workflows.errors import (
    DynamicBlockError, WorkflowEnvironmentConfigurationError)
from inference.core.workflows.execution_engine.v1.dynamic_blocks.block_scaffolding import (
    _get_python_code_imports, assembly_custom_python_block,
    create_dynamic_module, extract_code_snippet)
from inference.core.workflows.execution_engine.v1.dynamic_blocks.entities import \
    PythonCode
from inference.core.workflows.prototypes.block import (WorkflowBlock,
                                                       WorkflowBlockManifest)
from pydantic import BaseModel, Field


def codeflash_wrap(codeflash_wrapped, codeflash_test_module_name, codeflash_test_class_name, codeflash_test_name, codeflash_function_name, codeflash_line_id, codeflash_loop_index, *args, **kwargs):
    test_id = f'{codeflash_test_module_name}:{codeflash_test_class_name}:{codeflash_test_name}:{codeflash_line_id}:{codeflash_loop_index}'
    if not hasattr(codeflash_wrap, 'index'):
        codeflash_wrap.index = {}
    if test_id in codeflash_wrap.index:
        codeflash_wrap.index[test_id] += 1
    else:
        codeflash_wrap.index[test_id] = 0
    codeflash_test_index = codeflash_wrap.index[test_id]
    invocation_id = f'{codeflash_line_id}_{codeflash_test_index}'
    test_stdout_tag = f"{codeflash_test_module_name}:{(codeflash_test_class_name + '.' if codeflash_test_class_name else '')}{codeflash_test_name}:{codeflash_function_name}:{codeflash_loop_index}:{invocation_id}"
    print(f'!$######{test_stdout_tag}######$!')
    exception = None
    gc.disable()
    try:
        counter = time.perf_counter_ns()
        return_value = codeflash_wrapped(*args, **kwargs)
        codeflash_duration = time.perf_counter_ns() - counter
    except Exception as e:
        codeflash_duration = time.perf_counter_ns() - counter
        exception = e
    gc.enable()
    print(f'!######{test_stdout_tag}######!')
    iteration = os.environ['CODEFLASH_TEST_ITERATION']
    with Path('{codeflash_run_tmp_dir_client_side}', f'test_return_values_{iteration}.bin').open('ab') as f:
        pickled_values = pickle.dumps((args, kwargs, exception)) if exception else pickle.dumps((args, kwargs, return_value))
        _test_name = f"{codeflash_test_module_name}:{(codeflash_test_class_name + '.' if codeflash_test_class_name else '')}{codeflash_test_name}:{codeflash_function_name}:{codeflash_line_id}".encode('ascii')
        f.write(len(_test_name).to_bytes(4, byteorder='big'))
        f.write(_test_name)
        f.write(codeflash_duration.to_bytes(8, byteorder='big'))
        f.write(len(pickled_values).to_bytes(4, byteorder='big'))
        f.write(pickled_values)
        f.write(codeflash_loop_index.to_bytes(8, byteorder='big'))
        f.write(len(invocation_id).to_bytes(4, byteorder='big'))
        f.write(invocation_id.encode('ascii'))
    if exception:
        raise exception
    return return_value

class TestBlockManifest(WorkflowBlockManifest):
    """Minimal test manifest for testing purposes."""
    type: str = 'test_block'
    name: str = 'test'

    @classmethod
    def describe_outputs(cls) -> List:
        return []

def test_assembly_custom_python_block_creates_type():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that assembly_custom_python_block returns a valid class type.'
    python_code = PythonCode(type='PythonCode', run_function_code="def run(self):\n    return {'result': 'success'}", run_function_name='run', init_function_code=None, init_function_name='init', imports=[])
    with patch('inference.core.workflows.execution_engine.v1.dynamic_blocks.block_scaffolding.ALLOW_CUSTOM_PYTHON_EXECUTION_IN_WORKFLOWS', True):
        _call__bound__arguments = inspect.signature(assembly_custom_python_block).bind(block_type_name='TestBlock', unique_identifier='test_123', manifest=TestBlockManifest, python_code=python_code, api_key=None, skip_class_eval=False)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(assembly_custom_python_block, 'tests.inference.unit_tests.test_assembly_custom_python_block__unit_test_1', None, 'test_assembly_custom_python_block_creates_type', 'assembly_custom_python_block', '2_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

def test_assembly_custom_python_block_with_skip_class_eval_true():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that assembly_custom_python_block works with skip_class_eval=True.'
    python_code = PythonCode(type='PythonCode', run_function_code="def run(self):\n    return {'result': 'success'}", run_function_name='run', init_function_code=None, init_function_name='init', imports=[])
    _call__bound__arguments = inspect.signature(assembly_custom_python_block).bind(block_type_name='TestBlock', unique_identifier='test_skip', manifest=TestBlockManifest, python_code=python_code, api_key=None, skip_class_eval=True)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(assembly_custom_python_block, 'tests.inference.unit_tests.test_assembly_custom_python_block__unit_test_1', None, 'test_assembly_custom_python_block_with_skip_class_eval_true', 'assembly_custom_python_block', '2', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_assembly_custom_python_block_get_manifest():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that the generated class has get_manifest method.'
    python_code = PythonCode(type='PythonCode', run_function_code='def run(self):\n    return {}', run_function_name='run', init_function_code=None, init_function_name='init', imports=[])
    with patch('inference.core.workflows.execution_engine.v1.dynamic_blocks.block_scaffolding.ALLOW_CUSTOM_PYTHON_EXECUTION_IN_WORKFLOWS', True):
        _call__bound__arguments = inspect.signature(assembly_custom_python_block).bind(block_type_name='TestBlock', unique_identifier='test_manifest', manifest=TestBlockManifest, python_code=python_code, skip_class_eval=True)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(assembly_custom_python_block, 'tests.inference.unit_tests.test_assembly_custom_python_block__unit_test_1', None, 'test_assembly_custom_python_block_get_manifest', 'assembly_custom_python_block', '2_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        block_class = codeflash_output

def test_assembly_custom_python_block_get_init_parameters():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that the generated class has get_init_parameters method.'
    python_code = PythonCode(type='PythonCode', run_function_code='def run(self):\n    return {}', run_function_name='run', init_function_code=None, init_function_name='init', imports=[])
    with patch('inference.core.workflows.execution_engine.v1.dynamic_blocks.block_scaffolding.ALLOW_CUSTOM_PYTHON_EXECUTION_IN_WORKFLOWS', True):
        _call__bound__arguments = inspect.signature(assembly_custom_python_block).bind(block_type_name='TestBlock', unique_identifier='test_params', manifest=TestBlockManifest, python_code=python_code, skip_class_eval=True)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(assembly_custom_python_block, 'tests.inference.unit_tests.test_assembly_custom_python_block__unit_test_1', None, 'test_assembly_custom_python_block_get_init_parameters', 'assembly_custom_python_block', '2_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        block_class = codeflash_output
    init_params = block_class.get_init_parameters()

def test_create_dynamic_module_with_skip_class_eval():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test create_dynamic_module with skip_class_eval=True.'
    python_code = PythonCode(type='PythonCode', run_function_code="def run(self):\n    return {'test': 'result'}", run_function_name='run', init_function_code=None, init_function_name='init', imports=[])
    module = create_dynamic_module(block_type_name='TestBlock', python_code=python_code, module_name='test_module', api_key=None, skip_class_eval=True)

def test_extract_code_snippet_basic():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test extract_code_snippet with basic code.'
    code = 'def test():\n    x = 1\n    y = 2\n    z = 3'
    error_line = 2
    snippet = extract_code_snippet(code, error_line, context=1)

def test_extract_code_snippet_with_none_code():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test extract_code_snippet when code is None.'
    code = None
    error_line = 1
    snippet = extract_code_snippet(code, error_line)

def test_get_python_code_imports():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test _get_python_code_imports generates correct import string.'
    python_code = PythonCode(type='PythonCode', run_function_code='def run(self):\n    pass', run_function_name='run', init_function_code=None, init_function_name='init', imports=['import custom_lib'])
    imports_str = _get_python_code_imports(python_code)

def test_assembly_custom_python_block_missing_run_function():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that assembly_custom_python_block raises error when run function not found.'
    python_code = PythonCode(type='PythonCode', run_function_code='def other_func():\n    pass', run_function_name='missing_run', init_function_code=None, init_function_name='init', imports=[])
    with pytest.raises(DynamicBlockError) as exc_info:
        _call__bound__arguments = inspect.signature(assembly_custom_python_block).bind(block_type_name='TestBlock', unique_identifier='test_missing', manifest=TestBlockManifest, python_code=python_code, skip_class_eval=False)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(assembly_custom_python_block, 'tests.inference.unit_tests.test_assembly_custom_python_block__unit_test_1', None, 'test_assembly_custom_python_block_missing_run_function', 'assembly_custom_python_block', '2_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)

def test_assembly_custom_python_block_missing_init_function():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that assembly_custom_python_block raises error when init function specified but not found.'
    python_code = PythonCode(type='PythonCode', run_function_code='def run(self):\n    pass', run_function_name='run', init_function_code='def init():\n    pass', init_function_name='missing_init', imports=[])
    with pytest.raises(DynamicBlockError) as exc_info:
        _call__bound__arguments = inspect.signature(assembly_custom_python_block).bind(block_type_name='TestBlock', unique_identifier='test_missing_init', manifest=TestBlockManifest, python_code=python_code, skip_class_eval=False)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(assembly_custom_python_block, 'tests.inference.unit_tests.test_assembly_custom_python_block__unit_test_1', None, 'test_assembly_custom_python_block_missing_init_function', 'assembly_custom_python_block', '2_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)

def test_assembly_custom_python_block_custom_execution_disabled():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that WorkflowEnvironmentConfigurationError raised when custom execution disabled.'
    python_code = PythonCode(type='PythonCode', run_function_code='def run(self):\n    return {}', run_function_name='run', init_function_code=None, init_function_name='init', imports=[])
    with patch('inference.core.workflows.execution_engine.v1.dynamic_blocks.block_scaffolding.ALLOW_CUSTOM_PYTHON_EXECUTION_IN_WORKFLOWS', True):
        _call__bound__arguments = inspect.signature(assembly_custom_python_block).bind(block_type_name='TestBlock', unique_identifier='test_exec', manifest=TestBlockManifest, python_code=python_code, skip_class_eval=False)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(assembly_custom_python_block, 'tests.inference.unit_tests.test_assembly_custom_python_block__unit_test_1', None, 'test_assembly_custom_python_block_custom_execution_disabled', 'assembly_custom_python_block', '2_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        block_class = codeflash_output
    instance = block_class(api_key='test_key')
    with patch('inference.core.workflows.execution_engine.v1.dynamic_blocks.block_scaffolding.ALLOW_CUSTOM_PYTHON_EXECUTION_IN_WORKFLOWS', False):
        with patch('inference.core.workflows.execution_engine.v1.dynamic_blocks.block_scaffolding.WORKFLOWS_CUSTOM_PYTHON_EXECUTION_MODE', 'local'):
            with pytest.raises(WorkflowEnvironmentConfigurationError):
                instance.run()

def test_extract_code_snippet_boundary_lines():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test extract_code_snippet at file boundaries.'
    code = 'line1\nline2\nline3'
    snippet = extract_code_snippet(code, 1, context=100)
    snippet = extract_code_snippet(code, 3, context=100)

def test_create_dynamic_module_with_init_function():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test create_dynamic_module includes init function when provided.'
    python_code = PythonCode(type='PythonCode', run_function_code='def run(self):\n    return {}', run_function_name='run', init_function_code="def init():\n    return {'initialized': True}", init_function_name='init', imports=[])
    module = create_dynamic_module(block_type_name='TestBlock', python_code=python_code, module_name='test_module_init', skip_class_eval=True)

def test_extract_code_snippet_empty_context():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test extract_code_snippet with context=0.'
    code = 'line1\nline2\nerror_line\nline4\nline5'
    error_line = 3
    snippet = extract_code_snippet(code, error_line, context=0)

def test_assembly_custom_python_block_with_api_key():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test assembly_custom_python_block accepts and stores api_key.'
    python_code = PythonCode(type='PythonCode', run_function_code='def run(self):\n    return {}', run_function_name='run', init_function_code=None, init_function_name='init', imports=[])
    with patch('inference.core.workflows.execution_engine.v1.dynamic_blocks.block_scaffolding.ALLOW_CUSTOM_PYTHON_EXECUTION_IN_WORKFLOWS', True):
        _call__bound__arguments = inspect.signature(assembly_custom_python_block).bind(block_type_name='TestBlock', unique_identifier='test_api_key', manifest=TestBlockManifest, python_code=python_code, api_key='test_api_key_123', skip_class_eval=True)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(assembly_custom_python_block, 'tests.inference.unit_tests.test_assembly_custom_python_block__unit_test_1', None, 'test_assembly_custom_python_block_with_api_key', 'assembly_custom_python_block', '2_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        block_class = codeflash_output
    instance = block_class(api_key='test_api_key_456')

def test_create_dynamic_module_syntax_error():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test create_dynamic_module raises PythonBlockError on syntax error.'
    python_code = PythonCode(type='PythonCode', run_function_code='def run(self\n    return {}', run_function_name='run', init_function_code=None, init_function_name='init', imports=[])
    from inference.core.workflows.errors import PythonBlockError
    with pytest.raises(PythonBlockError):
        create_dynamic_module(block_type_name='TestBlock', python_code=python_code, module_name='test_module_syntax', skip_class_eval=False)

def test_get_python_code_imports_with_empty_imports():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test _get_python_code_imports with empty custom imports list.'
    python_code = PythonCode(type='PythonCode', run_function_code='def run(self):\n    pass', run_function_name='run', init_function_code=None, init_function_name='init', imports=[])
    imports_str = _get_python_code_imports(python_code)

def test_extract_code_snippet_multiline_statement():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test extract_code_snippet with multiline code statements.'
    code = 'def func():\n    result = (\n        value1 +\n        value2\n    )\n    return result'
    error_line = 3
    snippet = extract_code_snippet(code, error_line, context=2)

def test_assembly_custom_python_block_large_imports_list():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test assembly_custom_python_block with many custom imports.'
    imports_list = [f'import module_{i}' for i in range(100)]
    python_code = PythonCode(type='PythonCode', run_function_code="def run(self):\n    return {'result': 'ok'}", run_function_name='run', init_function_code=None, init_function_name='init', imports=imports_list)
    with patch('inference.core.workflows.execution_engine.v1.dynamic_blocks.block_scaffolding.ALLOW_CUSTOM_PYTHON_EXECUTION_IN_WORKFLOWS', True):
        _call__bound__arguments = inspect.signature(assembly_custom_python_block).bind(block_type_name='TestBlock', unique_identifier='test_large_imports', manifest=TestBlockManifest, python_code=python_code, skip_class_eval=True)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(assembly_custom_python_block, 'tests.inference.unit_tests.test_assembly_custom_python_block__unit_test_1', None, 'test_assembly_custom_python_block_large_imports_list', 'assembly_custom_python_block', '3_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        block_class = codeflash_output

def test_create_dynamic_module_large_code_function():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test create_dynamic_module with large code function (1000 lines).'
    lines = ['    x = 1'] * 1000
    large_function_code = 'def run(self):\n' + '\n'.join(lines) + "\n    return {'status': 'done'}"
    python_code = PythonCode(type='PythonCode', run_function_code=large_function_code, run_function_name='run', init_function_code=None, init_function_name='init', imports=[])
    module = create_dynamic_module(block_type_name='TestBlock', python_code=python_code, module_name='test_large_code', skip_class_eval=True)

def test_extract_code_snippet_large_file():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test extract_code_snippet with a large code file (1000 lines).'
    lines = [f'line_{i} = {i}' for i in range(1000)]
    code = '\n'.join(lines)
    error_line = 500
    snippet = extract_code_snippet(code, error_line, context=5)

def test_assembly_custom_python_block_repeated_creation():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test repeated creation of dynamic blocks with different identifiers.'
    python_code = PythonCode(type='PythonCode', run_function_code="def run(self):\n    return {'count': 1}", run_function_name='run', init_function_code=None, init_function_name='init', imports=[])
    with patch('inference.core.workflows.execution_engine.v1.dynamic_blocks.block_scaffolding.ALLOW_CUSTOM_PYTHON_EXECUTION_IN_WORKFLOWS', True):
        block_classes = []
        for i in range(100):
            _call__bound__arguments = inspect.signature(assembly_custom_python_block).bind(block_type_name=f'TestBlock_{i}', unique_identifier=f'test_{i}', manifest=TestBlockManifest, python_code=python_code, skip_class_eval=True)
            _call__bound__arguments.apply_defaults()
            codeflash_return_value = codeflash_wrap(assembly_custom_python_block, 'tests.inference.unit_tests.test_assembly_custom_python_block__unit_test_1', None, 'test_assembly_custom_python_block_repeated_creation', 'assembly_custom_python_block', '2_1_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
            codeflash_output = codeflash_return_value
            block_class = codeflash_output
            block_classes.append(block_class)

def test_get_python_code_imports_large_custom_imports():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test _get_python_code_imports with 500 custom imports.'
    imports_list = [f'from package_{i} import module_{i}' for i in range(500)]
    python_code = PythonCode(type='PythonCode', run_function_code='def run(self):\n    pass', run_function_name='run', init_function_code=None, init_function_name='init', imports=imports_list)
    imports_str = _get_python_code_imports(python_code)

def test_assembly_custom_python_block_constructor_called_100_times():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that constructor can be called 100 times without issues.'
    python_code = PythonCode(type='PythonCode', run_function_code='def run(self):\n    return {}', run_function_name='run', init_function_code="def init():\n    return {'value': 42}", init_function_name='init', imports=[])
    with patch('inference.core.workflows.execution_engine.v1.dynamic_blocks.block_scaffolding.ALLOW_CUSTOM_PYTHON_EXECUTION_IN_WORKFLOWS', True):
        _call__bound__arguments = inspect.signature(assembly_custom_python_block).bind(block_type_name='TestBlock', unique_identifier='test_constructor', manifest=TestBlockManifest, python_code=python_code, skip_class_eval=True)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(assembly_custom_python_block, 'tests.inference.unit_tests.test_assembly_custom_python_block__unit_test_1', None, 'test_assembly_custom_python_block_constructor_called_100_times', 'assembly_custom_python_block', '2_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        block_class = codeflash_output
    instances = []
    for i in range(100):
        instance = block_class(api_key=f'key_{i}')
        instances.append(instance)

def test_extract_code_snippet_performance_large_context():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test extract_code_snippet performance with large context value.'
    lines = [f'statement_{i}' for i in range(1000)]
    code = '\n'.join(lines)
    error_line = 2500
    snippet = extract_code_snippet(code, error_line, context=500)

def test_create_dynamic_module_complex_code_with_loops():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test create_dynamic_module with complex code including loops (1000 iterations).'
    loop_code = "def run(self):\n    result = []\n    for i in range(1000):\n        result.append(i * 2)\n    return {'items': len(result)}\n"
    python_code = PythonCode(type='PythonCode', run_function_code=loop_code, run_function_name='run', init_function_code=None, init_function_name='init', imports=[])
    module = create_dynamic_module(block_type_name='TestBlock', python_code=python_code, module_name='test_complex_code', skip_class_eval=True)

def test_assembly_custom_python_block_name_contains_large_unique_id():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test assembly_custom_python_block with very long unique_identifier.'
    long_id = 'a' * 1000
    python_code = PythonCode(type='PythonCode', run_function_code='def run(self):\n    return {}', run_function_name='run', init_function_code=None, init_function_name='init', imports=[])
    with patch('inference.core.workflows.execution_engine.v1.dynamic_blocks.block_scaffolding.ALLOW_CUSTOM_PYTHON_EXECUTION_IN_WORKFLOWS', True):
        _call__bound__arguments = inspect.signature(assembly_custom_python_block).bind(block_type_name='TestBlock', unique_identifier=long_id, manifest=TestBlockManifest, python_code=python_code, skip_class_eval=True)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(assembly_custom_python_block, 'tests.inference.unit_tests.test_assembly_custom_python_block__unit_test_1', None, 'test_assembly_custom_python_block_name_contains_large_unique_id', 'assembly_custom_python_block', '3_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        block_class = codeflash_output