Skip to content

Commit

Permalink
Run pre-commit (All-Hands-AI#4163)
Browse files Browse the repository at this point in the history
  • Loading branch information
neubig authored Oct 2, 2024
1 parent 240a470 commit 178dbfa
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 22 deletions.
2 changes: 1 addition & 1 deletion openhands/linter/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .cmd import run_cmd, check_tool_installed
from .cmd import check_tool_installed, run_cmd

__all__ = ['run_cmd', 'check_tool_installed']
3 changes: 2 additions & 1 deletion openhands/linter/utils/cmd.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import subprocess
import os
import subprocess


def run_cmd(cmd: str, cwd: str | None = None) -> str | None:
"""Run a command and return the output.
Expand Down
2 changes: 1 addition & 1 deletion openhands/runtime/browser/browser_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

from openhands.core.exceptions import BrowserInitException
from openhands.core.logger import openhands_logger as logger
from openhands.utils.tenacity_stop import stop_if_should_exit
from openhands.runtime.utils.shutdown_listener import should_continue, should_exit
from openhands.utils.tenacity_stop import stop_if_should_exit

BROWSER_EVAL_GET_GOAL_ACTION = 'GET_EVAL_GOAL'
BROWSER_EVAL_GET_REWARDS_ACTION = 'GET_EVAL_REWARDS'
Expand Down
5 changes: 4 additions & 1 deletion openhands/runtime/builder/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
from openhands.core.logger import openhands_logger as logger
from openhands.runtime.builder import RuntimeBuilder
from openhands.runtime.utils.request import send_request
from openhands.runtime.utils.shutdown_listener import should_exit, sleep_if_should_continue
from openhands.runtime.utils.shutdown_listener import (
should_exit,
sleep_if_should_continue,
)


class RemoteRuntimeBuilder(RuntimeBuilder):
Expand Down
6 changes: 4 additions & 2 deletions openhands/runtime/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ def _continue_bash(
_exit_code_output = self.shell.before
try:
exit_code = int(_exit_code_output.strip().split()[0])
except:
except Exception:
logger.error('Error getting exit code from bash script')
# If we try to run an invalid shell script the output sometimes includes error text
# rather than the error code - we assume this is an error
Expand Down Expand Up @@ -628,7 +628,9 @@ async def execute_action(action_request: ActionRequest):
observation = await client.run_action(action)
return event_to_dict(observation)
except Exception as e:
logger.error(f'Error processing command: {str(e)}', exc_info=True, stack_info=True)
logger.error(
f'Error processing command: {str(e)}', exc_info=True, stack_info=True
)
raise HTTPException(status_code=500, detail=str(e))

@app.post('/upload_file')
Expand Down
2 changes: 1 addition & 1 deletion openhands/runtime/remote/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
from openhands.runtime.builder.remote import RemoteRuntimeBuilder
from openhands.runtime.plugins import PluginRequirement
from openhands.runtime.runtime import Runtime
from openhands.utils.tenacity_stop import stop_if_should_exit
from openhands.runtime.utils.request import (
DEFAULT_RETRY_EXCEPTIONS,
is_404_error,
send_request,
)
from openhands.runtime.utils.runtime_build import build_runtime_image
from openhands.utils.tenacity_stop import stop_if_should_exit


class RemoteRuntime(Runtime):
Expand Down
2 changes: 1 addition & 1 deletion openhands/runtime/utils/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def send_request(
if retry_fns is not None:
for fn in retry_fns:
retry_condition |= retry_if_exception(fn)
kwargs["timeout"] = timeout
kwargs['timeout'] = timeout

@retry(
stop=stop_after_delay(timeout) | stop_if_should_exit(),
Expand Down
4 changes: 3 additions & 1 deletion openhands/runtime/utils/runtime_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,9 @@ def build_runtime_image(

# Scenario 1: If we already have an image with the exact same hash, then it means the image is already built
# with the exact same source code and Dockerfile, so we will reuse it. Building it is not required.
if not force_rebuild and runtime_builder.image_exists(hash_runtime_image_name, False):
if not force_rebuild and runtime_builder.image_exists(
hash_runtime_image_name, False
):
logger.info(
f'Image [{hash_runtime_image_name}] already exists so we will reuse it.'
)
Expand Down
7 changes: 4 additions & 3 deletions openhands/runtime/utils/shutdown_listener.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This module monitors the app for shutdown signals
"""

import asyncio
import signal
import time
Expand All @@ -16,7 +17,7 @@ def _register_signal_handler(sig: signal.Signals):

def handler(sig_: int, frame: FrameType | None):
global _should_exit
_should_exit = True
_should_exit = True
if original_handler:
original_handler(sig_, frame) # type: ignore[unreachable]

Expand All @@ -43,7 +44,7 @@ def should_continue() -> bool:


def sleep_if_should_continue(timeout: float):
if(timeout <= 1):
if timeout <= 1:
time.sleep(timeout)
return
start_time = time.time()
Expand All @@ -52,7 +53,7 @@ def sleep_if_should_continue(timeout: float):


async def async_sleep_if_should_continue(timeout: float):
if(timeout <= 1):
if timeout <= 1:
await asyncio.sleep(timeout)
return
start_time = time.time()
Expand Down
4 changes: 3 additions & 1 deletion openhands/server/session/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ async def dispatch(self, data: dict):
'Model does not support image upload, change to a different model or try without an image.'
)
return
asyncio.run_coroutine_threadsafe(self._add_event(event, EventSource.USER), self.agent_session.loop) # type: ignore
asyncio.run_coroutine_threadsafe(
self._add_event(event, EventSource.USER), self.agent_session.loop
) # type: ignore

async def _add_event(self, event, event_source):
self.agent_session.event_stream.add_event(event, EventSource.USER)
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ reportlab = "*"
[tool.coverage.run]
concurrency = ["gevent"]


[tool.poetry.group.runtime.dependencies]
jupyterlab = "*"
notebook = "*"
Expand Down Expand Up @@ -115,6 +116,7 @@ ignore = ["D1"]
[tool.ruff.lint.pydocstyle]
convention = "google"


[tool.poetry.group.evaluation.dependencies]
streamlit = "*"
whatthepatch = "*"
Expand Down
23 changes: 14 additions & 9 deletions tests/unit/linters/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest


@pytest.fixture
def syntax_error_py_file(tmp_path):
file_content = """
Expand All @@ -8,7 +9,7 @@ def foo():
print("Wrong indent")
foo(
"""
file_path = tmp_path / "test_file.py"
file_path = tmp_path / 'test_file.py'
file_path.write_text(file_content)
return str(file_path)

Expand All @@ -19,52 +20,56 @@ def wrongly_indented_py_file(tmp_path):
def foo():
print("Hello, World!")
"""
file_path = tmp_path / "test_file.py"
file_path = tmp_path / 'test_file.py'
file_path.write_text(file_content)
return str(file_path)


@pytest.fixture
def simple_correct_py_file(tmp_path):
file_content = 'print("Hello, World!")\n'
file_path = tmp_path / "test_file.py"
file_path = tmp_path / 'test_file.py'
file_path.write_text(file_content)
return str(file_path)


@pytest.fixture
def simple_correct_py_func_def(tmp_path):
file_content = """def foo():
print("Hello, World!")
foo()
"""
file_path = tmp_path / "test_file.py"
file_path = tmp_path / 'test_file.py'
file_path.write_text(file_content)
return str(file_path)


@pytest.fixture
def simple_correct_ruby_file(tmp_path):
file_content ="""def foo
file_content = """def foo
puts "Hello, World!"
end
foo
"""
file_path = tmp_path / "test_file.rb"
file_path = tmp_path / 'test_file.rb'
file_path.write_text(file_content)
return str(file_path)


@pytest.fixture
def simple_incorrect_ruby_file(tmp_path):
file_content ="""def foo():
file_content = """def foo():
print("Hello, World!")
foo()
"""
file_path = tmp_path / "test_file.rb"
file_path = tmp_path / 'test_file.rb'
file_path.write_text(file_content)
return str(file_path)


@pytest.fixture
def parenthesis_incorrect_ruby_file(tmp_path):
file_content = """def print_hello_world()\n puts 'Hello World'\n"""
file_path = tmp_path / "test_file.rb"
file_path = tmp_path / 'test_file.rb'
file_path.write_text(file_content)
return str(file_path)

0 comments on commit 178dbfa

Please sign in to comment.