Commit 52432e27 authored by Carlos Ramos Carreño's avatar Carlos Ramos Carreño

Testcase module typed and formatted.

- Type hints for testcase module are completed.
- Type comments (to be compatible with Python 2) have been added to
  other parts of slapos.core only when necessary to achieve full type
  checking in the testcase module, with Pyright in strict mode.
- Docstrings added/completed for the testcase module.
- Source is formatted with ruff.

See merge request nexedi/slapos.core!693
parent 2e08776e
...@@ -3,4 +3,4 @@ include slapos/proxy/schema.sql ...@@ -3,4 +3,4 @@ include slapos/proxy/schema.sql
include slapos/slapos-client.cfg.example include slapos/slapos-client.cfg.example
include slapos/slapos-proxy.cfg.example include slapos/slapos-proxy.cfg.example
include slapos/slapos.cfg.example include slapos/slapos.cfg.example
recursive-include slapos *.in *.txt *.xsd *.rst recursive-include slapos *.in *.txt *.xsd *.rst py.typed
[tool.ruff]
line-length = 80
indent-width = 2
\ No newline at end of file
...@@ -211,6 +211,7 @@ class SlapPopen(subprocess.Popen): ...@@ -211,6 +211,7 @@ class SlapPopen(subprocess.Popen):
def md5digest(url): def md5digest(url):
# type: (str) -> str
return hashlib.md5(url.encode('utf-8')).hexdigest() return hashlib.md5(url.encode('utf-8')).hexdigest()
......
...@@ -45,6 +45,11 @@ import warnings ...@@ -45,6 +45,11 @@ import warnings
import json import json
import six import six
try:
from typing import Mapping, Sequence
except ImportError: # XXX to be removed once we depend on typing
pass
from .exception import ResourceNotReady, ServerError, NotFoundError, \ from .exception import ResourceNotReady, ServerError, NotFoundError, \
ConnectionError ConnectionError
from .hateoas import SlapHateoasNavigator, ConnectionHelper from .hateoas import SlapHateoasNavigator, ConnectionHelper
...@@ -178,6 +183,7 @@ class SoftwareRelease(SlapDocument): ...@@ -178,6 +183,7 @@ class SoftwareRelease(SlapDocument):
return self._computer_guid return self._computer_guid
def getURI(self): def getURI(self):
# type: () -> str
if not self._software_release: if not self._software_release:
raise NameError('software_release has not been defined.') raise NameError('software_release has not been defined.')
else: else:
...@@ -384,6 +390,7 @@ class Computer(SlapDocument): ...@@ -384,6 +390,7 @@ class Computer(SlapDocument):
@_syncComputerInformation @_syncComputerInformation
def getComputerPartitionList(self): def getComputerPartitionList(self):
# type: (...) -> Sequence[ComputerPartition]
for computer_partition in self._computer_partition_list: for computer_partition in self._computer_partition_list:
computer_partition._connection_helper = self._connection_helper computer_partition._connection_helper = self._connection_helper
computer_partition._hateoas_navigator = self._hateoas_navigator computer_partition._hateoas_navigator = self._hateoas_navigator
...@@ -596,6 +603,7 @@ class ComputerPartition(SlapRequester): ...@@ -596,6 +603,7 @@ class ComputerPartition(SlapRequester):
return software_instance return software_instance
def getId(self): def getId(self):
# type: (...) -> str
if not getattr(self, '_partition_id', None): if not getattr(self, '_partition_id', None):
raise ResourceNotReady() raise ResourceNotReady()
return self._partition_id return self._partition_id
...@@ -629,9 +637,11 @@ class ComputerPartition(SlapRequester): ...@@ -629,9 +637,11 @@ class ComputerPartition(SlapRequester):
return software_type return software_type
def getInstanceParameterDict(self): def getInstanceParameterDict(self):
# type: (...) -> Mapping[str, object]
return getattr(self, '_parameter_dict', None) or {} return getattr(self, '_parameter_dict', None) or {}
def getConnectionParameterDict(self): def getConnectionParameterDict(self):
# type: (...) -> Mapping[str, str]
connection_dict = getattr(self, '_connection_dict', None) connection_dict = getattr(self, '_connection_dict', None)
if connection_dict is None: if connection_dict is None:
# XXX Backward compatibility for older slapproxy (<= 1.0.0) # XXX Backward compatibility for older slapproxy (<= 1.0.0)
...@@ -640,6 +650,7 @@ class ComputerPartition(SlapRequester): ...@@ -640,6 +650,7 @@ class ComputerPartition(SlapRequester):
return connection_dict or {} return connection_dict or {}
def getSoftwareRelease(self): def getSoftwareRelease(self):
# type: (...) -> SoftwareRelease
""" """
Returns the software release associate to the computer partition. Returns the software release associate to the computer partition.
""" """
......
...@@ -44,7 +44,7 @@ except ImportError: ...@@ -44,7 +44,7 @@ except ImportError:
import subprocess import subprocess
try: try:
from typing import TYPE_CHECKING, Optional, Iterable, Dict, Union from typing import TYPE_CHECKING, Iterable, Mapping, Optional, Union
if TYPE_CHECKING: if TYPE_CHECKING:
import subprocess import subprocess
except ImportError: # XXX to be removed once we depend on typing except ImportError: # XXX to be removed once we depend on typing
...@@ -501,7 +501,13 @@ class StandaloneSlapOS(object): ...@@ -501,7 +501,13 @@ class StandaloneSlapOS(object):
self._initBaseDirectory(software_root, instance_root, shared_part_root) self._initBaseDirectory(software_root, instance_root, shared_part_root)
def _initBaseDirectory(self, software_root, instance_root, shared_part_root): def _initBaseDirectory(
self,
software_root, # type: str
instance_root, # type: str
shared_part_root, # type: str
):
# type: (...) -> None
"""Create the directory after checking it's not too deep. """Create the directory after checking it's not too deep.
""" """
base_directory = self._base_directory base_directory = self._base_directory
...@@ -619,10 +625,12 @@ class StandaloneSlapOS(object): ...@@ -619,10 +625,12 @@ class StandaloneSlapOS(object):
def format( def format(
self, self,
partition_count, partition_count, # type: int
ipv4_address, ipv4_address, # type: str
ipv6_address, ipv6_address, # type: str
partition_base_name="slappart"): partition_base_name="slappart", # type: str
):
# type: (...) -> None
"""Creates `partition_count` partitions. """Creates `partition_count` partitions.
All partitions have the same `ipv4_address` and use the current system All partitions have the same `ipv4_address` and use the current system
...@@ -728,7 +736,13 @@ class StandaloneSlapOS(object): ...@@ -728,7 +736,13 @@ class StandaloneSlapOS(object):
self._logger.error(e.output) self._logger.error(e.output)
raise raise
def supply(self, software_url, computer_guid=None, state="available"): def supply(
self,
software_url, # type: str
computer_guid=None, # type: str | None
state="available", # type: str
):
# type: (...) -> None
"""Supply a software, see ISupply.supply """Supply a software, see ISupply.supply
Software can only be supplied on this embedded computer. Software can only be supplied on this embedded computer.
...@@ -742,14 +756,15 @@ class StandaloneSlapOS(object): ...@@ -742,14 +756,15 @@ class StandaloneSlapOS(object):
) )
def request( def request(
self, self,
software_release, software_release, # type: str
partition_reference, partition_reference, # type: str
software_type=None, software_type=None, # type: str | None
shared=False, shared=False, # type: bool
partition_parameter_kw=None, partition_parameter_kw=None, # type: Mapping[str, object] | None
filter_kw=None, filter_kw=None, # type: Mapping[str, object] | None
state=None): state=None, # type: str | None
):
"""Request an instance, see IRequester.request """Request an instance, see IRequester.request
Instance can only be requested on this embedded computer. Instance can only be requested on this embedded computer.
......
This diff is collapsed.
...@@ -46,7 +46,7 @@ from ..grid.utils import getPythonExecutableFromSoftwarePath ...@@ -46,7 +46,7 @@ from ..grid.utils import getPythonExecutableFromSoftwarePath
try: try:
import typing import typing
if typing.TYPE_CHECKING: if typing.TYPE_CHECKING:
from PIL import Image # pylint:disable=unused-import from PIL.Image import Image # pylint:disable=unused-import
from .testcase import SlapOSInstanceTestCase from .testcase import SlapOSInstanceTestCase
except ImportError: except ImportError:
pass pass
......
...@@ -94,8 +94,12 @@ class SafeXMLUnmrshaller(Unmarshaller, object): ...@@ -94,8 +94,12 @@ class SafeXMLUnmrshaller(Unmarshaller, object):
loads = SafeXMLUnmrshaller().loads loads = SafeXMLUnmrshaller().loads
def mkdir_p(path, mode=0o700): def mkdir_p(
"""\ path, # type: str
mode=0o700, # type: int
):
# type: (...) -> None
"""
Creates a directory and its parents, if needed. Creates a directory and its parents, if needed.
NB: If the directory already exists, it does not change its permission. NB: If the directory already exists, it does not change its permission.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment