π OpenHands: AI-Driven Development
refactor: Renamed variables to resolve naming conflicts and eliminate warnings (#2732)
Leo5e6fb6131f10
diff --git a/opendevin/controller/agent_controller.py b/opendevin/controller/agent_controller.pyindex c3c2108639..e2f776e39e 100644--- a/opendevin/controller/agent_controller.py+++ b/opendevin/controller/agent_controller.py@@ -86,7 +86,7 @@ class AgentController:)# state from the previous session, state from a parent agent, or a fresh state- self._set_initial_state(+ self.set_initial_state(state=initial_state,max_iterations=max_iterations,)@@ -225,8 +225,8 @@ class AgentController:return self.state.agent_stateasync def start_delegate(self, action: AgentDelegateAction):- AgentCls: Type[Agent] = Agent.get_cls(action.agent)- agent = AgentCls(llm=self.agent.llm)+ agent_cls: Type[Agent] = Agent.get_cls(action.agent)+ agent = agent_cls(llm=self.agent.llm)state = State(inputs=action.inputs or {},iteration=0,@@ -368,7 +368,7 @@ class AgentController:def get_state(self):return self.state- def _set_initial_state(+ def set_initial_state(self, state: State | None, max_iterations: int = MAX_ITERATIONS):# state from the previous session, state from a parent agent, or a new statediff --git a/opendevin/core/config.py b/opendevin/core/config.pyindex b2d85e9be8..e31c8b6bda 100644--- a/opendevin/core/config.py+++ b/opendevin/core/config.py@@ -7,7 +7,7 @@ import uuidfrom dataclasses import dataclass, field, fields, is_dataclassfrom enum import Enumfrom types import UnionType-from typing import Any, ClassVar, get_args, get_origin+from typing import Any, ClassVar, MutableMapping, get_args, get_originimport tomlfrom dotenv import load_dotenv@@ -76,10 +76,10 @@ class LLMConfig(metaclass=Singleton):"""Serialize fields to a dict for the frontend, including type hints, defaults, and whether it's optional."""- dict = {}+ result = {}for f in fields(self):- dict[f.name] = get_field_info(f)- return dict+ result[f.name] = get_field_info(f)+ return resultdef __str__(self):attr_str = []@@ -117,10 +117,10 @@ class AgentConfig(metaclass=Singleton):"""Serialize fields to a dict for the frontend, including type hints, defaults, and whether it's optional."""- dict = {}+ result = {}for f in fields(self):- dict[f.name] = get_field_info(f)- return dict+ result[f.name] = get_field_info(f)+ return resultclass UndefinedString(str, Enum):@@ -215,16 +215,16 @@ class AppConfig(metaclass=Singleton):"""Serialize fields to a dict for the frontend, including type hints, defaults, and whether it's optional."""- dict = {}+ result = {}for f in fields(self):field_value = getattr(self, f.name)# dataclasses compute their defaults themselvesif is_dataclass(type(field_value)):- dict[f.name] = field_value.defaults_to_dict()+ result[f.name] = field_value.defaults_to_dict()else:- dict[f.name] = get_field_info(f)- return dict+ result[f.name] = get_field_info(f)+ return resultdef __str__(self):attr_str = []@@ -248,16 +248,16 @@ class AppConfig(metaclass=Singleton):return self.__str__()-def get_field_info(field):+def get_field_info(f):"""Extract information about a dataclass field: type, optional, and default.Args:- field: The field to extract information from.+ f: The field to extract information from.Returns: A dict with the field's type, whether it's optional, and its default value."""- field_type = field.type+ field_type = f.typeoptional = False# for types like str | None, find the non-None type and set optional to True@@ -277,23 +277,23 @@ def get_field_info(field):)# default is always present- default = field.default+ default = f.default# return a schema with the useful info for frontendreturn {'type': type_name.lower(), 'optional': optional, 'default': default}-def load_from_env(config: AppConfig, env_or_toml_dict: dict | os._Environ):+def load_from_env(cfg: AppConfig, env_or_toml_dict: dict | MutableMapping[str, str]):"""Reads the env-style vars and sets config attributes based on env vars or a config.toml dict.Compatibility with vars like LLM_BASE_URL, AGENT_MEMORY_ENABLED and others.Args:- config: The AppConfig object to set attributes on.+ cfg: The AppConfig object to set attributes on.env_or_toml_dict: The environment variables or a config.toml dict."""def get_optional_type(union_type: UnionType) -> Any:- """Returns the non-None type from an Union."""+ """Returns the non-None type from a Union."""types = get_args(union_type)return next((t for t in types if t is not type(None)), None)@@ -334,19 +334,18 @@ def load_from_env(config: AppConfig, env_or_toml_dict: dict | os._Environ):)# Start processing from the root of the config object- set_attr_from_env(config)+ set_attr_from_env(cfg)-def load_from_toml(config: AppConfig, toml_file: str = 'config.toml'):+def load_from_toml(cfg: AppConfig, toml_file: str = 'config.toml'):"""Load the config from the toml file. Supports both styles of config vars.Args:- config: The AppConfig object to update attributes of.+ cfg: The AppConfig object to update attributes of.+ toml_file: The path to the toml file. Defaults to 'config.toml'."""# try to read the config.toml file into the config object- toml_config = {}-try:with open(toml_file, 'r', encoding='utf-8') as toml_contents:toml_config = toml.load(toml_contents)@@ -363,24 +362,24 @@ def load_from_toml(config: AppConfig, toml_file: str = 'config.toml'):# if there was an exception or core is not in the toml, try to use the old-style tomlif 'core' not in toml_config:# re-use the env loader to set the config from env-style vars- load_from_env(config, toml_config)+ load_from_env(cfg, toml_config)returncore_config = toml_config['core']try:# set llm config from the toml file- llm_config = config.llm+ llm_config = cfg.llmif 'llm' in toml_config:llm_config = LLMConfig(**toml_config['llm'])# set agent config from the toml file- agent_config = config.agent+ agent_config = cfg.agentif 'agent' in toml_config:agent_config = AgentConfig(**toml_config['agent'])# update the config object with the new values- config = AppConfig(llm=llm_config, agent=agent_config, **core_config)+ AppConfig(llm=llm_config, agent=agent_config, **core_config)except (TypeError, KeyError) as e:logger.warning(f'Cannot parse config from toml, toml values have not been applied.\nError: {e}',@@ -388,38 +387,38 @@ def load_from_toml(config: AppConfig, toml_file: str = 'config.toml'):)-def finalize_config(config: AppConfig):+def finalize_config(cfg: AppConfig):"""More tweaks to the config after it's been loaded."""# Set workspace_mount_path if not set by the user- if config.workspace_mount_path is UndefinedString.UNDEFINED:- config.workspace_mount_path = os.path.abspath(config.workspace_base)- config.workspace_base = os.path.abspath(config.workspace_base)+ if cfg.workspace_mount_path is UndefinedString.UNDEFINED:+ cfg.workspace_mount_path = os.path.abspath(cfg.workspace_base)+ cfg.workspace_base = os.path.abspath(cfg.workspace_base)# In local there is no sandbox, the workspace will have the same pwd as the host- if config.sandbox_type == 'local':- config.workspace_mount_path_in_sandbox = config.workspace_mount_path+ if cfg.sandbox_type == 'local':+ cfg.workspace_mount_path_in_sandbox = cfg.workspace_mount_path- if config.workspace_mount_rewrite: # and not config.workspace_mount_path:+ if cfg.workspace_mount_rewrite: # and not config.workspace_mount_path:# TODO why do we need to check if workspace_mount_path is None?- base = config.workspace_base or os.getcwd()- parts = config.workspace_mount_rewrite.split(':')- config.workspace_mount_path = base.replace(parts[0], parts[1])+ base = cfg.workspace_base or os.getcwd()+ parts = cfg.workspace_mount_rewrite.split(':')+ cfg.workspace_mount_path = base.replace(parts[0], parts[1])- if config.llm.embedding_base_url is None:- config.llm.embedding_base_url = config.llm.base_url+ if cfg.llm.embedding_base_url is None:+ cfg.llm.embedding_base_url = cfg.llm.base_url- if config.use_host_network and platform.system() == 'Darwin':+ if cfg.use_host_network and platform.system() == 'Darwin':logger.warning('Please upgrade to Docker Desktop 4.29.0 or later to use host network mode on macOS. ''See https://github.com/docker/roadmap/issues/238#issuecomment-2044688144 for more information.')# make sure cache dir exists- if config.cache_dir:- pathlib.Path(config.cache_dir).mkdir(parents=True, exist_ok=True)+ if cfg.cache_dir:+ pathlib.Path(cfg.cache_dir).mkdir(parents=True, exist_ok=True)config = AppConfig()@@ -565,11 +564,11 @@ def parse_arguments():Parse the command line arguments."""parser = get_parser()- args, _ = parser.parse_known_args()- if args.directory:- config.workspace_base = os.path.abspath(args.directory)+ parsed_args, _ = parser.parse_known_args()+ if parsed_args.directory:+ config.workspace_base = os.path.abspath(parsed_args.directory)print(f'Setting workspace base to {config.workspace_base}')- return args+ return parsed_argsargs = parse_arguments()diff --git a/opendevin/server/listen.py b/opendevin/server/listen.pyindex baae6771eb..265049ed78 100644--- a/opendevin/server/listen.py+++ b/opendevin/server/listen.py@@ -67,18 +67,13 @@ def load_file_upload_config() -> tuple[int, bool, list[str]]:restrict_file_types = config.file_uploads_restrict_file_typesallowed_extensions = config.file_uploads_allowed_extensions- # Sanity check for max_file_size_mb- MAX_ALLOWED_SIZE = 1024 # Maximum allowed file size 1 GB- if not isinstance(max_file_size_mb, int) or max_file_size_mb < 0:- logger.warning(- f'Invalid max_file_size_mb: {max_file_size_mb}. Setting to 0 (no limit).'- )- max_file_size_mb = 0- elif max_file_size_mb > MAX_ALLOWED_SIZE:- logger.warning(- f'max_file_size_mb exceeds maximum allowed size. Capping at {MAX_ALLOWED_SIZE}MB.'- )- max_file_size_mb = MAX_ALLOWED_SIZE+ max_file_size_mb = (+ max_file_size_mb+ if (isinstance(max_file_size_mb, int) and max_file_size_mb >= 0)+ else 0+ )+ if max_file_size_mb == 0:+ logger.warning('Using unlimited (0) file size for uploads.')# Sanity check for allowed_extensionsif not isinstance(allowed_extensions, (list, set)) or not allowed_extensions:@@ -371,7 +366,7 @@ def list_files(request: Request, path: str = '/'):filtered_entries = [entryfor entry in entries- if not spec.match_file(os.path.relpath(entry, full_path))+ if not spec.match_file(os.path.relpath(entry, str(full_path)))]# Separate directories and files@@ -391,8 +386,8 @@ def list_files(request: Request, path: str = '/'):files.append(entry)# Sort directories and files separately- directories.sort(key=str.lower)- files.sort(key=str.lower)+ directories.sort(key=lambda s: s.lower())+ files.sort(key=lambda s: s.lower())# Combine sorted directories and filessorted_entries = directories + filesdiff --git a/opendevin/server/session/agent.py b/opendevin/server/session/agent.pyindex 15731716e6..14f189329b 100644--- a/opendevin/server/session/agent.py+++ b/opendevin/server/session/agent.py@@ -98,7 +98,8 @@ class AgentSession:if isinstance(agent, CodeActAgent):if not self.runtime or not isinstance(self.runtime.sandbox, DockerSSHBox):logger.warning(- 'CodeActAgent requires DockerSSHBox as sandbox! Using other sandbox that are not stateful (LocalBox, DockerExecBox) will not work properly.'+ 'CodeActAgent requires DockerSSHBox as sandbox! Using other sandbox that are not stateful'+ ' (LocalBox, DockerExecBox) will not work properly.')self.runtime.init_sandbox_plugins(agent.sandbox_plugins)self.runtime.init_runtime_tools(agent.runtime_tools)@@ -111,7 +112,7 @@ class AgentSession:)try:agent_state = State.restore_from_session(self.sid)- self.controller._set_initial_state(agent_state)+ self.controller.set_initial_state(agent_state)logger.info(f'Restored agent state from session, sid: {self.sid}')except Exception as e:print('Error restoring state', e)