Skip to content

Commit d133789

Browse files
hinogiburtenshaw
andauthored
Refactor/typings auto discovery (#831)
* refactor: substituting Option for union None type * refactor: replaced Dict with dict * refactor: added missing generic types and type annotation for dicts that infer to unknown --------- Co-authored-by: burtenshaw <ben.burtenshaw@gmail.com>
1 parent f9ab472 commit d133789

1 file changed

Lines changed: 20 additions & 18 deletions

File tree

src/openenv/auto/_discovery.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
This enables AutoEnv to work without coupling to src/envs/ directory.
1818
"""
1919

20+
from __future__ import annotations
21+
2022
import importlib
2123
import importlib.metadata
2224
import importlib.resources
@@ -26,7 +28,7 @@
2628
import tempfile
2729
from dataclasses import asdict, dataclass
2830
from pathlib import Path
29-
from typing import Any, Dict, Optional, Type
31+
from typing import Any, Type
3032

3133
import yaml
3234

@@ -63,10 +65,10 @@ class EnvironmentInfo:
6365
action_class_name: str
6466
observation_class_name: str
6567
default_image: str
66-
spec_version: Optional[int] = None
67-
manifest: Optional[Dict[str, Any]] = None
68+
spec_version: int | None = None
69+
manifest: dict[str, Any] | None = None
6870

69-
def get_client_class(self) -> Type:
71+
def get_client_class(self) -> Type[Any]:
7072
"""
7173
Dynamically import and return the client class.
7274
@@ -90,7 +92,7 @@ def get_client_class(self) -> Type:
9092
f"Class {self.client_class_name} not found in {self.client_module_path}: {e}"
9193
) from e
9294

93-
def get_action_class(self) -> Type:
95+
def get_action_class(self) -> Type[Any]:
9496
"""
9597
Dynamically import and return the action class.
9698
@@ -114,7 +116,7 @@ def get_action_class(self) -> Type:
114116
f"Class {self.action_class_name} not found in {self.client_module_path}: {e}"
115117
) from e
116118

117-
def get_observation_class(self) -> Type:
119+
def get_observation_class(self) -> Type[Any]:
118120
"""
119121
Dynamically import and return the observation class.
120122
@@ -225,7 +227,7 @@ def _infer_class_name(env_name: str, class_type: str) -> str:
225227

226228
def _load_manifest_from_package(
227229
package_name: str, module_name: str
228-
) -> Optional[Dict[str, Any]]:
230+
) -> dict[str, Any] | None:
229231
"""
230232
Load openenv.yaml manifest from an installed package.
231233
@@ -259,7 +261,7 @@ def _load_manifest_from_package(
259261

260262
def _create_env_info_from_package(
261263
package_name: str, module_name: str, version: str
262-
) -> Optional[EnvironmentInfo]:
264+
) -> EnvironmentInfo | None:
263265
"""
264266
Create EnvironmentInfo from an installed package.
265267
@@ -347,17 +349,17 @@ class EnvironmentDiscovery:
347349

348350
def __init__(self):
349351
"""Initialize discovery system."""
350-
self._cache: Optional[Dict[str, EnvironmentInfo]] = None
352+
self._cache: dict[str, EnvironmentInfo] | None = None
351353
self._cache_file = Path(tempfile.gettempdir()) / "openenv_discovery_cache.json"
352354

353-
def _discover_installed_packages(self) -> Dict[str, EnvironmentInfo]:
355+
def _discover_installed_packages(self) -> dict[str, EnvironmentInfo]:
354356
"""
355357
Discover all installed openenv-* packages.
356358
357359
Returns:
358360
Dictionary mapping env_key to EnvironmentInfo
359361
"""
360-
environments = {}
362+
environments: dict[str, EnvironmentInfo] = {}
361363

362364
# Invalidate import caches to ensure we pick up newly installed packages
363365
importlib.invalidate_caches()
@@ -403,7 +405,7 @@ def _discover_installed_packages(self) -> Dict[str, EnvironmentInfo]:
403405

404406
return environments
405407

406-
def _load_cache(self) -> Optional[Dict[str, EnvironmentInfo]]:
408+
def _load_cache(self) -> dict[str, EnvironmentInfo] | None:
407409
"""
408410
Load cached discovery results.
409411
@@ -418,7 +420,7 @@ def _load_cache(self) -> Optional[Dict[str, EnvironmentInfo]]:
418420
cache_data = json.load(f)
419421

420422
# Reconstruct EnvironmentInfo objects
421-
cache = {}
423+
cache: dict[str, EnvironmentInfo] = {}
422424
for env_key, env_data in cache_data.items():
423425
cache[env_key] = EnvironmentInfo(**env_data)
424426

@@ -427,7 +429,7 @@ def _load_cache(self) -> Optional[Dict[str, EnvironmentInfo]]:
427429
logger.warning(f"Failed to load discovery cache: {e}")
428430
return None
429431

430-
def _save_cache(self, environments: Dict[str, EnvironmentInfo]) -> None:
432+
def _save_cache(self, environments: dict[str, EnvironmentInfo]) -> None:
431433
"""
432434
Save discovery results to cache.
433435
@@ -445,7 +447,7 @@ def _save_cache(self, environments: Dict[str, EnvironmentInfo]) -> None:
445447
except Exception as e:
446448
logger.warning(f"Failed to save discovery cache: {e}")
447449

448-
def discover(self, use_cache: bool = True) -> Dict[str, EnvironmentInfo]:
450+
def discover(self, use_cache: bool = True) -> dict[str, EnvironmentInfo]:
449451
"""
450452
Discover all installed OpenEnv environments.
451453
@@ -481,7 +483,7 @@ def discover(self, use_cache: bool = True) -> Dict[str, EnvironmentInfo]:
481483

482484
return environments
483485

484-
def get_environment(self, env_key: str) -> Optional[EnvironmentInfo]:
486+
def get_environment(self, env_key: str) -> EnvironmentInfo | None:
485487
"""
486488
Get information about a specific environment.
487489
@@ -500,7 +502,7 @@ def get_environment(self, env_key: str) -> Optional[EnvironmentInfo]:
500502
environments = self.discover()
501503
return environments.get(env_key)
502504

503-
def get_environment_by_name(self, name: str) -> Optional[EnvironmentInfo]:
505+
def get_environment_by_name(self, name: str) -> EnvironmentInfo | None:
504506
"""
505507
Get environment info by flexible name matching.
506508
@@ -554,7 +556,7 @@ def clear_cache(self) -> None:
554556

555557

556558
# Global discovery instance
557-
_global_discovery: Optional[EnvironmentDiscovery] = None
559+
_global_discovery: EnvironmentDiscovery | None = None
558560

559561

560562
def get_discovery() -> EnvironmentDiscovery:

0 commit comments

Comments
 (0)