Procházet zdrojové kódy

Refactor: shorter syntax (#4558)

tofarr před 1 rokem
rodič
revize
c4f5c07be1

+ 5 - 7
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)

+ 10 - 8
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 (

+ 1 - 5
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 (

+ 1 - 2
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))
             )
         ]
 

+ 1 - 1
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__'):

+ 1 - 5
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(