Skip to content

Commit ab125c7

Browse files
committed
remove is_isntallable_type
1 parent 795c1f1 commit ab125c7

4 files changed

Lines changed: 9 additions & 139 deletions

File tree

‎src/cloudai/_core/base_installer.py‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919
import shutil
2020
import subprocess
2121
from concurrent.futures import ThreadPoolExecutor, as_completed
22-
from typing import Iterable, Optional, TypeVar, final
23-
24-
from typing_extensions import TypeGuard
22+
from typing import Iterable, Optional, final
2523

2624
from cloudai.util import prepare_output_dir
2725

@@ -30,8 +28,6 @@
3028

3129
TASK_LIMIT_THRESHOLD = 256
3230

33-
T = TypeVar("T", bound=Installable)
34-
3531

3632
class BaseInstaller:
3733
"""
@@ -129,10 +125,6 @@ def all_items(self, items: Iterable[Installable], with_duplicates: bool = False)
129125
all_items = list(items) + self.system.system_installables()
130126
return list(set(all_items)) if not with_duplicates else all_items
131127

132-
@staticmethod
133-
def is_installable_type(item: Installable, expected_type: type[T]) -> TypeGuard[T]:
134-
return type(item) is expected_type
135-
136128
@final
137129
def is_installed(self, items: Iterable[Installable]) -> InstallStatusResult:
138130
"""

‎src/cloudai/systems/lsf/lsf_installer.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,21 @@ def _check_required_binaries(self) -> None:
5454
raise EnvironmentError(f"Required binary '{binary}' is not installed.")
5555

5656
def install_one(self, item: Installable) -> InstallStatusResult:
57-
if self.is_installable_type(item, DockerImage):
57+
if isinstance(item, DockerImage):
5858
return super().install_one(item)
5959
return self.slurm_installer.install_one(item)
6060

6161
def uninstall_one(self, item: Installable) -> InstallStatusResult:
62-
if self.is_installable_type(item, DockerImage):
62+
if isinstance(item, DockerImage):
6363
return super().uninstall_one(item)
6464
return self.slurm_installer.uninstall_one(item)
6565

6666
def is_installed_one(self, item: Installable) -> InstallStatusResult:
67-
if self.is_installable_type(item, DockerImage):
67+
if isinstance(item, DockerImage):
6868
return super().is_installed_one(item)
6969
return self.slurm_installer.is_installed_one(item)
7070

7171
def mark_as_installed_one(self, item: Installable) -> InstallStatusResult:
72-
if self.is_installable_type(item, DockerImage):
72+
if isinstance(item, DockerImage):
7373
return super().mark_as_installed_one(item)
7474
return self.slurm_installer.mark_as_installed_one(item)

‎src/cloudai/systems/slurm/slurm_installer.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,22 @@ def _check_srun_options(self) -> None:
7777

7878
def install_one(self, item: Installable) -> InstallStatusResult:
7979
logging.debug(f"Attempt to install {item}")
80-
if self.is_installable_type(item, DockerImage):
80+
if isinstance(item, DockerImage):
8181
res = self._install_docker_image(item)
8282
return InstallStatusResult(res.success, res.message)
8383

8484
return super().install_one(item)
8585

8686
def uninstall_one(self, item: Installable) -> InstallStatusResult:
8787
logging.debug(f"Attempt to uninstall {item!r}")
88-
if self.is_installable_type(item, DockerImage):
88+
if isinstance(item, DockerImage):
8989
res = self._uninstall_docker_image(item)
9090
return InstallStatusResult(res.success, res.message)
9191

9292
return super().uninstall_one(item)
9393

9494
def is_installed_one(self, item: Installable) -> InstallStatusResult:
95-
if self.is_installable_type(item, DockerImage):
95+
if isinstance(item, DockerImage):
9696
res = self.docker_image_cache_manager.check_docker_image_exists(item.url, item.cache_filename)
9797
if res.success and res.docker_image_path:
9898
item.installed_path = res.docker_image_path
@@ -101,7 +101,7 @@ def is_installed_one(self, item: Installable) -> InstallStatusResult:
101101
return super().is_installed_one(item)
102102

103103
def mark_as_installed_one(self, item: Installable) -> InstallStatusResult:
104-
if self.is_installable_type(item, DockerImage):
104+
if isinstance(item, DockerImage):
105105
if self.system.cache_docker_images_locally and not isinstance(item.installed_path, Path):
106106
item.installed_path = self.system.install_path / item.cache_filename
107107
return InstallStatusResult(True)

‎tests/test_base_installer.py‎

Lines changed: 0 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -310,125 +310,3 @@ def test_check_supported(installer: KubernetesInstaller | SlurmInstaller, monkey
310310

311311
res = installer.mark_as_installed_one(item)
312312
assert res.success, f"Failed to mark as installed {item} for {installer.__class__.__name__=} {res.message=}"
313-
314-
315-
class MyInstallable(Installable):
316-
def __eq__(self, other: object) -> bool:
317-
return True
318-
319-
def __hash__(self) -> int:
320-
return hash("MyInstallable")
321-
322-
323-
class CustomInstallable(Installable):
324-
def __init__(self):
325-
self.calls: list[tuple[str, BaseInstaller]] = []
326-
327-
def __eq__(self, other: object) -> bool:
328-
return isinstance(other, CustomInstallable)
329-
330-
def __hash__(self) -> int:
331-
return hash("CustomInstallable")
332-
333-
def install(self, installer: BaseInstaller) -> InstallStatusResult:
334-
self.calls.append(("install", installer))
335-
return InstallStatusResult(True, "custom installed")
336-
337-
def uninstall(self, installer: BaseInstaller) -> InstallStatusResult:
338-
self.calls.append(("uninstall", installer))
339-
return InstallStatusResult(True, "custom uninstalled")
340-
341-
def is_installed(self, installer: BaseInstaller) -> InstallStatusResult:
342-
self.calls.append(("is_installed", installer))
343-
return InstallStatusResult(True, "custom is installed")
344-
345-
def mark_as_installed(self, installer: BaseInstaller) -> InstallStatusResult:
346-
self.calls.append(("mark_as_installed", installer))
347-
return InstallStatusResult(True, "custom marked as installed")
348-
349-
350-
class CustomDockerImage(DockerImage):
351-
def __init__(self, url: str):
352-
super().__init__(url)
353-
self.calls: list[tuple[str, BaseInstaller]] = []
354-
355-
def __eq__(self, other: object) -> bool:
356-
return isinstance(other, CustomDockerImage) and self.url == other.url
357-
358-
def __hash__(self) -> int:
359-
return hash((CustomDockerImage, self.url))
360-
361-
def install(self, installer: BaseInstaller) -> InstallStatusResult:
362-
self.calls.append(("install", installer))
363-
return InstallStatusResult(True, "custom docker installed")
364-
365-
def uninstall(self, installer: BaseInstaller) -> InstallStatusResult:
366-
self.calls.append(("uninstall", installer))
367-
return InstallStatusResult(True, "custom docker uninstalled")
368-
369-
def is_installed(self, installer: BaseInstaller) -> InstallStatusResult:
370-
self.calls.append(("is_installed", installer))
371-
return InstallStatusResult(True, "custom docker is installed")
372-
373-
def mark_as_installed(self, installer: BaseInstaller) -> InstallStatusResult:
374-
self.calls.append(("mark_as_installed", installer))
375-
return InstallStatusResult(True, "custom docker marked as installed")
376-
377-
378-
def test_check_unsupported(installer: KubernetesInstaller | SlurmInstaller):
379-
unsupported = MyInstallable()
380-
for func in [
381-
installer.install_one,
382-
installer.uninstall_one,
383-
installer.is_installed_one,
384-
installer.mark_as_installed_one,
385-
]:
386-
res = func(unsupported)
387-
assert not res.success
388-
assert "Unsupported installable operation" in res.message
389-
assert type(unsupported).__name__ in res.message
390-
391-
392-
def test_custom_installable_operations_are_dispatched(slurm_system: SlurmSystem):
393-
installer = BaseInstaller(slurm_system)
394-
item = CustomInstallable()
395-
396-
install_result = installer.install_one(item)
397-
is_installed_result = installer.is_installed_one(item)
398-
uninstall_result = installer.uninstall_one(item)
399-
mark_result = installer.mark_as_installed_one(item)
400-
401-
assert install_result.success
402-
assert is_installed_result.success
403-
assert uninstall_result.success
404-
assert mark_result.success
405-
assert ("install", installer) in item.calls
406-
assert ("is_installed", installer) in item.calls
407-
assert ("uninstall", installer) in item.calls
408-
assert ("mark_as_installed", installer) in item.calls
409-
410-
411-
def test_builtin_installable_subclass_operations_are_dispatched(slurm_system: SlurmSystem):
412-
installer = SlurmInstaller(slurm_system)
413-
item = CustomDockerImage("fake_url/img")
414-
415-
results = [
416-
installer.install_one(item),
417-
installer.uninstall_one(item),
418-
installer.is_installed_one(item),
419-
installer.mark_as_installed_one(item),
420-
]
421-
422-
assert all(result.success for result in results)
423-
assert [result.message for result in results] == [
424-
"custom docker installed",
425-
"custom docker uninstalled",
426-
"custom docker is installed",
427-
"custom docker marked as installed",
428-
]
429-
assert item.calls == [
430-
("install", installer),
431-
("uninstall", installer),
432-
("is_installed", installer),
433-
("mark_as_installed", installer),
434-
]

0 commit comments

Comments
 (0)