Skip to content

Commit

Permalink
mypy: set no_implicit_optional
Browse files Browse the repository at this point in the history
The current version of PEP-484 indicates that
type checkers should not allow implicit optional
parameters.

- https://www.python.org/dev/peps/pep-0484/#union-types
- python/mypy#9091

Change-Id: I49e5729a4d754934e861d4d0fd25fd8b2a80602e
  • Loading branch information
eharney committed Mar 9, 2022
1 parent e8ad143 commit d3afa4d
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 83 deletions.
20 changes: 10 additions & 10 deletions cinder/backup/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ def delete(self,

def get_all(self,
context: context.RequestContext,
search_opts: dict = None,
marker: str = None,
limit: int = None,
offset: int = None,
sort_keys: List[str] = None,
sort_dirs: List[str] = None) -> 'objects.BackupList':
search_opts: Optional[dict] = None,
marker: Optional[str] = None,
limit: Optional[int] = None,
offset: Optional[int] = None,
sort_keys: Optional[List[str]] = None,
sort_dirs: Optional[List[str]] = None) -> 'objects.BackupList':
context.authorize(policy.GET_ALL_POLICY)

search_opts = search_opts or {}
Expand Down Expand Up @@ -223,10 +223,10 @@ def create(self,
volume_id: str,
container: str,
incremental: bool = False,
availability_zone: str = None,
availability_zone: Optional[str] = None,
force: bool = False,
snapshot_id: Optional[str] = None,
metadata: dict = None) -> 'objects.Backup':
metadata: Optional[dict] = None) -> 'objects.Backup':
"""Make the RPC call to create a volume backup."""
volume = self.volume_api.get(context, volume_id)
context.authorize(policy.CREATE_POLICY, target_obj=volume)
Expand Down Expand Up @@ -365,8 +365,8 @@ def create(self,
def restore(self,
context: context.RequestContext,
backup_id: str,
volume_id: str = None,
name: str = None) -> dict:
volume_id: Optional[str] = None,
name: Optional[str] = None) -> dict:
"""Make the RPC call to restore a volume backup."""
backup = self.get(context, backup_id)
context.authorize(policy.RESTORE_POLICY, target_obj=backup)
Expand Down
4 changes: 2 additions & 2 deletions cinder/coordination.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import os
import re
import sys
from typing import Callable
from typing import Callable, Optional # noqa: H301
import uuid

import decorator
Expand Down Expand Up @@ -56,7 +56,7 @@ class Coordinator(object):
meaningful prefix.
"""

def __init__(self, agent_id: str = None, prefix: str = ''):
def __init__(self, agent_id: Optional[str] = None, prefix: str = ''):
self.coordinator = None
self.agent_id = agent_id or str(uuid.uuid4())
self.started = False
Expand Down
4 changes: 2 additions & 2 deletions cinder/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"""

from typing import Union
from typing import Optional, Union # noqa: H301

from oslo_log import log as logging
from oslo_versionedobjects import exception as obj_exc
Expand Down Expand Up @@ -72,7 +72,7 @@ class CinderException(Exception):
headers: dict = {}
safe = False

def __init__(self, message: Union[str, tuple] = None, **kwargs):
def __init__(self, message: Optional[Union[str, tuple]] = None, **kwargs):
self.kwargs = kwargs
self.kwargs['message'] = message

Expand Down
44 changes: 23 additions & 21 deletions cinder/image/image_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,11 @@ def _get_qemu_convert_luks_cmd(src: str,
src_format: Optional[str] = None,
out_subformat: Optional[str] = None,
cache_mode: Optional[str] = None,
prefix: tuple = None,
prefix: Optional[tuple] = None,
cipher_spec: Optional[dict] = None,
passphrase_file: Optional[str] = None,
src_passphrase_file: str = None) -> List[str]:
src_passphrase_file: Optional[str] = None) \
-> List[str]:

cmd = ['qemu-img', 'convert']

Expand Down Expand Up @@ -214,14 +215,15 @@ def _get_qemu_convert_luks_cmd(src: str,
def _get_qemu_convert_cmd(src: str,
dest: str,
out_format: str,
src_format: str = None,
out_subformat: str = None,
cache_mode: str = None,
prefix: tuple = None,
cipher_spec: dict = None,
passphrase_file: str = None,
src_format: Optional[str] = None,
out_subformat: Optional[str] = None,
cache_mode: Optional[str] = None,
prefix: Optional[tuple] = None,
cipher_spec: Optional[dict] = None,
passphrase_file: Optional[str] = None,
compress: bool = False,
src_passphrase_file: str = None) -> List[str]:
src_passphrase_file: Optional[str] = None) \
-> List[str]:

if src_passphrase_file is not None:
if passphrase_file is None:
Expand Down Expand Up @@ -314,13 +316,13 @@ def _convert_image(prefix: tuple,
source: str,
dest: str,
out_format: str,
out_subformat: str = None,
src_format: str = None,
out_subformat: Optional[str] = None,
src_format: Optional[str] = None,
run_as_root: bool = True,
cipher_spec: dict = None,
passphrase_file: str = None,
cipher_spec: Optional[dict] = None,
passphrase_file: Optional[str] = None,
compress: bool = False,
src_passphrase_file: str = None) -> None:
src_passphrase_file: Optional[str] = None) -> None:
"""Convert image to other format.
NOTE: If the qemu-img convert command fails and this function raises an
Expand Down Expand Up @@ -421,14 +423,14 @@ def _convert_image(prefix: tuple,
def convert_image(source: str,
dest: str,
out_format: str,
out_subformat: str = None,
src_format: str = None,
out_subformat: Optional[str] = None,
src_format: Optional[str] = None,
run_as_root: bool = True,
throttle=None,
cipher_spec: dict = None,
passphrase_file: str = None,
cipher_spec: Optional[dict] = None,
passphrase_file: Optional[str] = None,
compress: bool = False,
src_passphrase_file: str = None) -> None:
src_passphrase_file: Optional[str] = None) -> None:
if not throttle:
throttle = throttling.Throttle.get_default()
with throttle.subcommand(source, dest) as throttle_cmd:
Expand All @@ -447,7 +449,7 @@ def convert_image(source: str,
def resize_image(source: str,
size: int,
run_as_root: bool = False,
file_format: str = None) -> None:
file_format: Optional[str] = None) -> None:
"""Changes the virtual size of the image."""
cmd: Tuple[str, ...]
if file_format:
Expand Down Expand Up @@ -805,7 +807,7 @@ def upload_volume(context: context.RequestContext,
volume_format: str = 'raw',
run_as_root: bool = True,
compress: bool = True,
store_id: str = None,
store_id: Optional[str] = None,
base_image_ref: Optional[str] = None) -> None:
# NOTE: You probably want to use volume_utils.upload_volume(),
# not this function.
Expand Down
4 changes: 2 additions & 2 deletions cinder/scheduler/base_weight.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@


def normalize(weight_list: List[float],
minval: float = None,
maxval: float = None) -> Iterable[float]:
minval: Optional[float] = None,
maxval: Optional[float] = None) -> Iterable[float]:
"""Normalize the values in a list between 0 and 1.0.
The normalization is made regarding the lower and upper values present in
Expand Down
3 changes: 2 additions & 1 deletion cinder/scheduler/filter_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,8 @@ def _find_valid_backends(self,

def _get_weighted_candidates_by_group_type(
self, context: context.RequestContext, group_spec: dict,
group_filter_properties: dict = None) -> List[WeighedHost]:
group_filter_properties: Optional[dict] = None) \
-> List[WeighedHost]:
"""Finds backends that supports the group type.
Returns a list of backends that meet the required specs,
Expand Down
2 changes: 1 addition & 1 deletion cinder/scheduler/host_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@

class ReadOnlyDict(abc.Mapping):
"""A read-only dict."""
def __init__(self, source: Union[dict, 'ReadOnlyDict'] = None):
def __init__(self, source: Optional[Union[dict, 'ReadOnlyDict']] = None):
self.data: dict
if source is not None:
self.data = dict(source)
Expand Down
15 changes: 9 additions & 6 deletions cinder/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def check_metadata_properties(
raise exception.InvalidVolumeMetadataSize(reason=msg)


def last_completed_audit_period(unit: str = None) -> \
def last_completed_audit_period(unit: Optional[str] = None) -> \
Tuple[Union[datetime.datetime, datetime.timedelta],
Union[datetime.datetime, datetime.timedelta]]:
"""This method gives you the most recently *completed* audit period.
Expand Down Expand Up @@ -374,7 +374,9 @@ def monkey_patch() -> None:
decorator("%s.%s" % (module, key), func))


def make_dev_path(dev: str, partition: str = None, base: str = '/dev') -> str:
def make_dev_path(dev: str,
partition: Optional[str] = None,
base: str = '/dev') -> str:
"""Return a path to a particular device.
>>> make_dev_path('xvdc')
Expand Down Expand Up @@ -434,7 +436,8 @@ def robust_file_write(directory: str, filename: str, data: str) -> None:


@contextlib.contextmanager
def temporary_chown(path: str, owner_uid: int = None) -> Iterator[None]:
def temporary_chown(path: str,
owner_uid: Optional[int] = None) -> Iterator[None]:
"""Temporarily chown a path.
:params owner_uid: UID of temporary owner (defaults to current user)
Expand Down Expand Up @@ -493,7 +496,7 @@ def get_file_size(path: str) -> int:

def _get_disk_of_partition(
devpath: str,
st: os.stat_result = None) -> Tuple[str, os.stat_result]:
st: Optional[os.stat_result] = None) -> Tuple[str, os.stat_result]:
"""Gets a disk device path and status from partition path.
Returns a disk device path from a partition device path, and stat for
Expand Down Expand Up @@ -558,7 +561,7 @@ def get_blkdev_major_minor(path: str,


def check_string_length(value: str, name: str, min_length: int = 0,
max_length: int = None,
max_length: Optional[int] = None,
allow_all_spaces: bool = True) -> None:
"""Check the length of specified string.
Expand Down Expand Up @@ -684,7 +687,7 @@ def convert_str(text: Union[str, bytes]) -> str:


def build_or_str(elements: Union[None, str, Iterable[str]],
str_format: str = None) -> str:
str_format: Optional[str] = None) -> str:
"""Builds a string of elements joined by 'or'.
Will join strings with the 'or' word and if a str_format is provided it
Expand Down
10 changes: 5 additions & 5 deletions cinder/volume/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,10 @@ def create(self,
name: Optional[str],
description: Optional[str],
snapshot: Optional[objects.Snapshot] = None,
image_id: str = None,
image_id: Optional[str] = None,
volume_type: Optional[objects.VolumeType] = None,
metadata: Optional[dict] = None,
availability_zone: str = None,
availability_zone: Optional[str] = None,
source_volume: Optional[objects.Volume] = None,
scheduler_hints=None,
source_replica=None,
Expand Down Expand Up @@ -1154,7 +1154,7 @@ def _create_snapshot_in_db_options(
name: str,
description: str,
cgsnapshot_id: str,
group_snapshot_id: str = None) -> Dict[str, Any]:
group_snapshot_id: Optional[str] = None) -> Dict[str, Any]:
options = {'volume_id': volume['id'],
'cgsnapshot_id': cgsnapshot_id,
'group_snapshot_id': group_snapshot_id,
Expand All @@ -1175,7 +1175,7 @@ def create_snapshot(
volume: objects.Volume,
name: str,
description: str,
metadata: Dict[str, Any] = None,
metadata: Optional[Dict[str, Any]] = None,
cgsnapshot_id: Optional[str] = None,
group_snapshot_id: Optional[str] = None,
allow_in_use: bool = False) -> objects.Snapshot:
Expand All @@ -1192,7 +1192,7 @@ def create_snapshot_force(
volume: objects.Volume,
name: str,
description: str,
metadata: Dict[str, Any] = None) -> objects.Snapshot:
metadata: Optional[Dict[str, Any]] = None) -> objects.Snapshot:
result = self._create_snapshot(context, volume, name, description,
True, metadata)
LOG.info("Snapshot force create request issued successfully.",
Expand Down
20 changes: 11 additions & 9 deletions cinder/volume/drivers/rbd.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,11 @@ class RBDVolumeProxy(object):
def __init__(self,
driver: 'RBDDriver',
name: str,
pool: str = None,
snapshot: str = None,
pool: Optional[str] = None,
snapshot: Optional[str] = None,
read_only: bool = False,
remote: Optional[Dict[str, str]] = None,
timeout: int = None,
timeout: Optional[int] = None,
client: 'rados.Rados' = None,
ioctx: 'rados.Ioctx' = None):
self._close_conn = not (client and ioctx)
Expand Down Expand Up @@ -205,7 +205,9 @@ def __getattr__(self, attrib: str):

class RADOSClient(object):
"""Context manager to simplify error handling for connecting to ceph."""
def __init__(self, driver: 'RBDDriver', pool: str = None) -> None:
def __init__(self,
driver: 'RBDDriver',
pool: Optional[str] = None) -> None:
self.driver = driver
self.cluster, self.ioctx = driver._connect_to_rados(pool)

Expand Down Expand Up @@ -246,7 +248,7 @@ class RBDDriver(driver.CloneableImageVD, driver.MigrateVD,
STORAGE_PROTOCOL = 'ceph'

def __init__(self,
active_backend_id: str = None,
active_backend_id: Optional[str] = None,
*args,
**kwargs) -> None:
super(RBDDriver, self).__init__(*args, **kwargs)
Expand Down Expand Up @@ -1531,9 +1533,9 @@ def _demote_volumes(self,
result.append(demoted)
return result

def _get_failover_target_config(self,
secondary_id: str = None) -> Tuple[str,
dict]:
def _get_failover_target_config(
self,
secondary_id: Optional[str] = None) -> Tuple[str, dict]:
if not secondary_id:
# In auto mode exclude failback and active
candidates = set(self._target_names).difference(
Expand Down Expand Up @@ -1587,7 +1589,7 @@ def failover_completed(self,
def failover_host(self,
context: context.RequestContext,
volumes: List[Volume],
secondary_id: str = None,
secondary_id: Optional[str] = None,
groups: Optional[List] = None) -> Tuple[str,
List[Volume],
List]:
Expand Down
3 changes: 2 additions & 1 deletion cinder/volume/flows/api/create_volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ def _extract_availability_zones(
snapshot,
source_volume,
group: Optional[dict],
volume_type: Dict[str, Any] = None) -> Tuple[List[str], bool]:
volume_type: Optional[Dict[str, Any]] = None) -> Tuple[List[str],
bool]:
"""Extracts and returns a validated availability zone list.
This function will extract the availability zone (if not provided) from
Expand Down
4 changes: 2 additions & 2 deletions cinder/volume/rpcapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ class VolumeAPI(rpc.RPCAPI):
BINARY = constants.VOLUME_BINARY

def _get_cctxt(self,
host: str = None,
version: Union[str, Tuple[str, ...]] = None,
host: Optional[str] = None,
version: Optional[Union[str, Tuple[str, ...]]] = None,
**kwargs) -> rpc.RPCAPI:
if host:
server = volume_utils.extract_host(host)
Expand Down
Loading

0 comments on commit d3afa4d

Please sign in to comment.