Skip to content

Commit 41fb914

Browse files
committed
typing: Add hints to image commands
Change-Id: I8cd238465819be19f698d40aa3d17fd0d8cd4c74 Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
1 parent 054db28 commit 41fb914

11 files changed

Lines changed: 228 additions & 123 deletions

openstackclient/image/client.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15+
import argparse
1516
import logging
17+
from typing import Any
1618

1719
from osc_lib import utils
1820

@@ -27,7 +29,7 @@
2729
API_VERSIONS = ('1', '2')
2830

2931

30-
def make_client(instance):
32+
def make_client(instance: Any) -> Any:
3133
"""Returns an image service client."""
3234
LOG.debug(
3335
'Image client initialized using OpenStack SDK: %s',
@@ -36,7 +38,9 @@ def make_client(instance):
3638
return instance.sdk_connection.image
3739

3840

39-
def build_option_parser(parser):
41+
def build_option_parser(
42+
parser: argparse.ArgumentParser,
43+
) -> argparse.ArgumentParser:
4044
"""Hook to add global options"""
4145
parser.add_argument(
4246
'--os-image-api-version',
@@ -48,6 +52,6 @@ def build_option_parser(parser):
4852
return parser
4953

5054

51-
def check_api_version(check_version):
55+
def check_api_version(check_version: str) -> bool:
5256
# SDK supports auto-negotiation for us: always return True
5357
return True

openstackclient/image/v1/image.py

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import logging
2020
import os
2121
import sys
22+
from collections.abc import Iterable, Sequence
2223
from typing import Any
2324

2425
from cliff import columns as cliff_columns
@@ -51,7 +52,7 @@
5152
LOG = logging.getLogger(__name__)
5253

5354

54-
def _get_columns(item):
55+
def _get_columns(item: Any) -> tuple[tuple[str, ...], tuple[str, ...]]:
5556
column_map = {'is_protected': 'protected', 'owner_id': 'owner'}
5657
hidden_columns = [
5758
'location',
@@ -69,7 +70,7 @@ def _get_columns(item):
6970

7071

7172
class HumanReadableSizeColumn(cliff_columns.FormattableColumn[int]):
72-
def human_readable(self):
73+
def human_readable(self) -> str:
7374
"""Return a formatted visibility string
7475
7576
:rtype:
@@ -83,7 +84,7 @@ def human_readable(self):
8384

8485

8586
class VisibilityColumn(cliff_columns.FormattableColumn[bool]):
86-
def human_readable(self):
87+
def human_readable(self) -> str:
8788
"""Return a formatted visibility string
8889
8990
:rtype:
@@ -99,7 +100,7 @@ def human_readable(self):
99100
class CreateImage(command.ShowOne):
100101
_description = _("Create/upload an image")
101102

102-
def get_parser(self, prog_name):
103+
def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
103104
parser = super().get_parser(prog_name)
104105
parser.add_argument(
105106
"name",
@@ -239,7 +240,9 @@ def get_parser(self, prog_name):
239240
)
240241
return parser
241242

242-
def take_action(self, parsed_args):
243+
def take_action(
244+
self, parsed_args: argparse.Namespace
245+
) -> tuple[Sequence[str], Iterable[Any]]:
243246
image_client = self.app.client_manager.image
244247

245248
# Build an attribute dict from the parsed args, only include
@@ -351,13 +354,15 @@ def take_action(self, parsed_args):
351354
info['properties'] = format_columns.DictColumn(
352355
info.get('properties', {})
353356
)
354-
return zip(*sorted(info.items()))
357+
col_headers, col_data = zip(*sorted(info.items()))
358+
return col_headers, col_data
359+
return ((), ())
355360

356361

357362
class DeleteImage(command.Command):
358363
_description = _("Delete image(s)")
359364

360-
def get_parser(self, prog_name):
365+
def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
361366
parser = super().get_parser(prog_name)
362367
parser.add_argument(
363368
"images",
@@ -367,7 +372,7 @@ def get_parser(self, prog_name):
367372
)
368373
return parser
369374

370-
def take_action(self, parsed_args):
375+
def take_action(self, parsed_args: argparse.Namespace) -> None:
371376
result = 0
372377
image_client = self.app.client_manager.image
373378
for image in parsed_args.images:
@@ -396,7 +401,7 @@ def take_action(self, parsed_args):
396401
class ListImage(command.Lister):
397402
_description = _("List available images")
398403

399-
def get_parser(self, prog_name):
404+
def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
400405
parser = super().get_parser(prog_name)
401406
public_group = parser.add_mutually_exclusive_group()
402407
public_group.add_argument(
@@ -453,7 +458,9 @@ def get_parser(self, prog_name):
453458
)
454459
return parser
455460

456-
def take_action(self, parsed_args):
461+
def take_action(
462+
self, parsed_args: argparse.Namespace
463+
) -> tuple[Sequence[str], Iterable[tuple[Any, ...]]]:
457464
image_client = self.app.client_manager.image
458465

459466
kwargs = {}
@@ -527,7 +534,7 @@ def take_action(self, parsed_args):
527534
class SaveImage(command.Command):
528535
_description = _("Save an image locally")
529536

530-
def get_parser(self, prog_name):
537+
def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
531538
parser = super().get_parser(prog_name)
532539
parser.add_argument(
533540
"--chunk-size",
@@ -551,7 +558,7 @@ def get_parser(self, prog_name):
551558
)
552559
return parser
553560

554-
def take_action(self, parsed_args):
561+
def take_action(self, parsed_args: argparse.Namespace) -> None:
555562
image_client = self.app.client_manager.image
556563
image = image_client.find_image(
557564
parsed_args.image, ignore_missing=False
@@ -572,7 +579,7 @@ def take_action(self, parsed_args):
572579
class SetImage(command.Command):
573580
_description = _("Set image properties")
574581

575-
def get_parser(self, prog_name):
582+
def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
576583
parser = super().get_parser(prog_name)
577584
parser.add_argument(
578585
"image",
@@ -702,7 +709,7 @@ def get_parser(self, prog_name):
702709
)
703710
return parser
704711

705-
def take_action(self, parsed_args):
712+
def take_action(self, parsed_args: argparse.Namespace) -> None:
706713
image_client = self.app.client_manager.image
707714

708715
kwargs = {}
@@ -819,7 +826,7 @@ def take_action(self, parsed_args):
819826
class ShowImage(command.ShowOne):
820827
_description = _("Display image details")
821828

822-
def get_parser(self, prog_name):
829+
def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
823830
parser = super().get_parser(prog_name)
824831
parser.add_argument(
825832
"--human-readable",
@@ -834,7 +841,9 @@ def get_parser(self, prog_name):
834841
)
835842
return parser
836843

837-
def take_action(self, parsed_args):
844+
def take_action(
845+
self, parsed_args: argparse.Namespace
846+
) -> tuple[Sequence[str], Iterable[Any]]:
838847
image_client = self.app.client_manager.image
839848
image = image_client.find_image(
840849
parsed_args.image, ignore_missing=False

openstackclient/image/v2/cache.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
# License for the specific language governing permissions and limitations
1414
# under the License.
1515

16+
import argparse
17+
from collections.abc import Iterable, Sequence
1618
import copy
1719
import datetime
1820
import logging
21+
from typing import Any
1922

2023
from osc_lib import exceptions
2124
from osc_lib import utils
@@ -27,7 +30,7 @@
2730
LOG = logging.getLogger(__name__)
2831

2932

30-
def _format_image_cache(cached_images):
33+
def _format_image_cache(cached_images: dict[str, Any]) -> list[dict[str, Any]]:
3134
"""Format image cache to make it more consistent with OSC operations."""
3235

3336
image_list = []
@@ -70,11 +73,13 @@ def _format_image_cache(cached_images):
7073
class ListCachedImage(command.Lister):
7174
_description = _("Get Cache State")
7275

73-
def get_parser(self, prog_name):
76+
def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
7477
parser = super().get_parser(prog_name)
7578
return parser
7679

77-
def take_action(self, parsed_args):
80+
def take_action(
81+
self, parsed_args: argparse.Namespace
82+
) -> tuple[Sequence[str], Iterable[tuple[Any, ...]]]:
7883
image_client = self.app.client_manager.image
7984

8085
# List of Cache data received
@@ -111,7 +116,7 @@ def take_action(self, parsed_args):
111116
class QueueCachedImage(command.Command):
112117
_description = _("Queue image(s) for caching.")
113118

114-
def get_parser(self, prog_name):
119+
def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
115120
parser = super().get_parser(prog_name)
116121
parser.add_argument(
117122
"images",
@@ -121,7 +126,7 @@ def get_parser(self, prog_name):
121126
)
122127
return parser
123128

124-
def take_action(self, parsed_args):
129+
def take_action(self, parsed_args: argparse.Namespace) -> None:
125130
image_client = self.app.client_manager.image
126131

127132
failures = 0
@@ -151,7 +156,7 @@ def take_action(self, parsed_args):
151156
class DeleteCachedImage(command.Command):
152157
_description = _("Delete image(s) from cache")
153158

154-
def get_parser(self, prog_name):
159+
def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
155160
parser = super().get_parser(prog_name)
156161
parser.add_argument(
157162
"images",
@@ -161,7 +166,7 @@ def get_parser(self, prog_name):
161166
)
162167
return parser
163168

164-
def take_action(self, parsed_args):
169+
def take_action(self, parsed_args: argparse.Namespace) -> None:
165170
failures = 0
166171
image_client = self.app.client_manager.image
167172
for image in parsed_args.images:
@@ -190,7 +195,7 @@ def take_action(self, parsed_args):
190195
class ClearCachedImage(command.Command):
191196
_description = _("Clear all images from cache, queue or both")
192197

193-
def get_parser(self, prog_name):
198+
def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
194199
parser = super().get_parser(prog_name)
195200
parser.add_argument(
196201
"--cache",
@@ -210,7 +215,7 @@ def get_parser(self, prog_name):
210215
)
211216
return parser
212217

213-
def take_action(self, parsed_args):
218+
def take_action(self, parsed_args: argparse.Namespace) -> None:
214219
image_client = self.app.client_manager.image
215220

216221
target = parsed_args.target

0 commit comments

Comments
 (0)