Skip to content

πŸ™Œ OpenHands: AI-Driven Development

Refactor: shorter syntax (#4558)

tofarrc4f5c07be187
diff --git a/evaluation/mint/tasks/reasoning.py b/evaluation/mint/tasks/reasoning.py
index 6640c6e1ee..08cf320c35 100644
--- a/evaluation/mint/tasks/reasoning.py
+++ b/evaluation/mint/tasks/reasoning.py
@@ -131,11 +131,9 @@ class MultipleChoiceTask(Task):
def compare_two_numbers(p, gt):
- if isinstance(p, int) or isinstance(p, float):
+ if isinstance(p, (int, float)):
pass
- elif isinstance(p, list) or isinstance(p, bool) or isinstance(p, str):
- return False
- elif isinstance(p, tuple) or isinstance(p, complex) or isinstance(p, dict):
+ elif isinstance(p, (bool, complex, dict, list, str, tuple)):
return False
else:
raise ValueError(p)
@@ -227,8 +225,8 @@ class TheoremqaTask(Task):
prediction = prediction.replace('Β°', '')
# Detect the boolean keyword in the generation
- if prediction in ['true', 'yes', 'false', 'no']:
- if prediction == 'true' or prediction == 'yes':
+ if prediction in ('true', 'yes', 'false', 'no'):
+ if prediction in ('true', 'yes'):
prediction = 'True'
else:
prediction = 'False'
@@ -342,7 +340,7 @@ class TheoremqaTask(Task):
answer_type = self._answer_type
gt = self.extract_answer(self.reference)
- if isinstance(prediction, (str, int, float)) or isinstance(prediction, list):
+ if isinstance(prediction, (str, int, float, list)):
# Comparing prediction against the reference
if answer_type in ['bool', 'option', 'Option']:
cur_correct = int(prediction == f'({gt})') or int(prediction == gt)
diff --git a/openhands/agenthub/codeact_agent/codeact_agent.py b/openhands/agenthub/codeact_agent/codeact_agent.py
index ad748a89d8..c8342ca11f 100644
--- a/openhands/agenthub/codeact_agent/codeact_agent.py
+++ b/openhands/agenthub/codeact_agent/codeact_agent.py
@@ -113,14 +113,16 @@ class CodeActAgent(Agent):
return ''
def get_action_message(self, action: Action) -> Message | None:
- if (
- isinstance(action, AgentDelegateAction)
- or isinstance(action, CmdRunAction)
- or isinstance(action, IPythonRunCellAction)
- or isinstance(action, MessageAction)
- or isinstance(action, FileEditAction)
- or (isinstance(action, AgentFinishAction) and action.source == 'agent')
- ):
+ if isinstance(
+ action,
+ (
+ AgentDelegateAction,
+ CmdRunAction,
+ IPythonRunCellAction,
+ MessageAction,
+ FileEditAction,
+ ),
+ ) or (isinstance(action, AgentFinishAction) and action.source == 'agent'):
content = [TextContent(text=self.action_to_str(action))]
if (
diff --git a/openhands/agenthub/codeact_swe_agent/codeact_swe_agent.py b/openhands/agenthub/codeact_swe_agent/codeact_swe_agent.py
index d0d0c0fdc8..6fc679aec4 100644
--- a/openhands/agenthub/codeact_swe_agent/codeact_swe_agent.py
+++ b/openhands/agenthub/codeact_swe_agent/codeact_swe_agent.py
@@ -89,11 +89,7 @@ class CodeActSWEAgent(Agent):
return ''
def get_action_message(self, action: Action) -> Message | None:
- if (
- isinstance(action, CmdRunAction)
- or isinstance(action, IPythonRunCellAction)
- or isinstance(action, MessageAction)
- ):
+ if isinstance(action, (CmdRunAction, IPythonRunCellAction, MessageAction)):
content = [TextContent(text=self.action_to_str(action))]
if (
diff --git a/openhands/controller/stuck.py b/openhands/controller/stuck.py
index 8b968e3f4c..230d5f2e81 100644
--- a/openhands/controller/stuck.py
+++ b/openhands/controller/stuck.py
@@ -33,8 +33,7 @@ class StuckDetector:
(isinstance(event, MessageAction) and event.source == EventSource.USER)
or
# there might be some NullAction or NullObservation in the history at least for now
- isinstance(event, NullAction)
- or isinstance(event, NullObservation)
+ isinstance(event, (NullAction, NullObservation))
)
]
diff --git a/openhands/events/serialization/utils.py b/openhands/events/serialization/utils.py
index fbc91e5c14..de448e0142 100644
--- a/openhands/events/serialization/utils.py
+++ b/openhands/events/serialization/utils.py
@@ -11,7 +11,7 @@ def remove_fields(obj, fields: set[str]):
del obj[field]
for _, value in obj.items():
remove_fields(value, fields)
- elif isinstance(obj, list) or isinstance(obj, tuple):
+ elif isinstance(obj, (list, tuple)):
for item in obj:
remove_fields(item, fields)
elif hasattr(obj, '__dataclass_fields__'):
diff --git a/openhands/runtime/impl/remote/remote_runtime.py b/openhands/runtime/impl/remote/remote_runtime.py
index 40779f2abd..879b64d9a4 100644
--- a/openhands/runtime/impl/remote/remote_runtime.py
+++ b/openhands/runtime/impl/remote/remote_runtime.py
@@ -295,11 +295,7 @@ class RemoteRuntime(Runtime):
logger.info(
f'Runtime pod not found. Count: {not_found_count} / {max_not_found_count}'
)
- elif (
- pod_status == 'Failed'
- or pod_status == 'Unknown'
- or pod_status == 'Not Found'
- ):
+ elif pod_status in ('Failed', 'Unknown', 'Not Found'):
# clean up the runtime
self.close()
raise RuntimeError(