diff --git a/openstackclient/common/project_cleanup.py b/openstackclient/common/project_cleanup.py index d8dc5ac9d0..84a07353d7 100644 --- a/openstackclient/common/project_cleanup.py +++ b/openstackclient/common/project_cleanup.py @@ -36,7 +36,7 @@ def ask_user_yesno(msg): :return bool: User choice """ while True: - answer = getpass._raw_input('{} [{}]: '.format(msg, 'y/n')) + answer = getpass.getpass('{} [{}]: '.format(msg, 'y/n')) if answer in ('y', 'Y', 'yes'): return True elif answer in ('n', 'N', 'no'): diff --git a/openstackclient/common/quota.py b/openstackclient/common/quota.py index 1ac50f80d8..03a4f09086 100644 --- a/openstackclient/common/quota.py +++ b/openstackclient/common/quota.py @@ -18,6 +18,7 @@ import argparse import itertools import logging import sys +import typing as ty from openstack import exceptions as sdk_exceptions from osc_lib.command import command @@ -199,7 +200,7 @@ def get_network_quotas( # # so we need to make conversion to have data in same format from # all of the services - result = {"usage": {}, "reservation": {}} + result: dict[str, ty.Any] = {"usage": {}, "reservation": {}} for key, values in dict_quota.items(): if values is None: continue diff --git a/openstackclient/compute/v2/keypair.py b/openstackclient/compute/v2/keypair.py index 45a3b90216..52fe0b5911 100644 --- a/openstackclient/compute/v2/keypair.py +++ b/openstackclient/compute/v2/keypair.py @@ -126,7 +126,6 @@ class CreateKeypair(command.ShowOne): kwargs = {'name': parsed_args.name} if parsed_args.public_key: - generated_keypair = None try: with open(os.path.expanduser(parsed_args.public_key)) as p: public_key = p.read() diff --git a/openstackclient/compute/v2/server.py b/openstackclient/compute/v2/server.py index 4c978da30b..8e88d8e90c 100644 --- a/openstackclient/compute/v2/server.py +++ b/openstackclient/compute/v2/server.py @@ -1970,7 +1970,7 @@ class CreateServer(command.ShowOne): # convert from the novaclient-derived "NIC" view to the actual # "network" view - network = {} + network: dict[str, str] = {} if nic['net-id']: network['uuid'] = nic['net-id'] @@ -1986,7 +1986,7 @@ class CreateServer(command.ShowOne): if nic.get('tag'): # tags are optional network['tag'] = nic['tag'] - networks.append(network) + networks.append(network) # type: ignore[union-attr] if not parsed_args.nics and sdk_utils.supports_microversion( compute_client, '2.37' diff --git a/openstackclient/identity/v2_0/user.py b/openstackclient/identity/v2_0/user.py index 2262d264d4..c4cd52b8d1 100644 --- a/openstackclient/identity/v2_0/user.py +++ b/openstackclient/identity/v2_0/user.py @@ -249,10 +249,7 @@ class ListUser(command.Lister): data = identity_client.users.list(tenant_id=project) if parsed_args.project: - d = {} - for s in data: - d[s.id] = s - data = d.values() + data = {s.id: s for s in data}.values() if parsed_args.long: # FIXME(dtroyer): Sometimes user objects have 'tenant_id' instead diff --git a/openstackclient/identity/v3/user.py b/openstackclient/identity/v3/user.py index fc727e9576..bbf3f1df59 100644 --- a/openstackclient/identity/v3/user.py +++ b/openstackclient/identity/v3/user.py @@ -17,6 +17,7 @@ import copy import logging +import typing as ty from openstack import exceptions as sdk_exc from osc_lib.command import command @@ -58,7 +59,7 @@ def _format_user(user): def _get_options_for_user(identity_client, parsed_args): - options = {} + options: dict[str, ty.Any] = {} if parsed_args.ignore_lockout_failure_attempts: options['ignore_lockout_failure_attempts'] = True if parsed_args.no_ignore_lockout_failure_attempts: diff --git a/openstackclient/image/v2/image.py b/openstackclient/image/v2/image.py index a45f3c39eb..57f278dcd7 100644 --- a/openstackclient/image/v2/image.py +++ b/openstackclient/image/v2/image.py @@ -428,7 +428,7 @@ class CreateImage(command.ShowOne): # Build an attribute dict from the parsed args, only include # attributes that were actually set on the command line - kwargs = {'allow_duplicates': True} + kwargs: dict[str, ty.Any] = {'allow_duplicates': True} copy_attrs = ( 'name', 'id', @@ -611,7 +611,7 @@ class CreateImage(command.ShowOne): volume_client.volumes, parsed_args.volume, ) - kwargs = { + kwargs: dict[str, ty.Any] = { 'visibility': None, 'protected': None, } @@ -1575,7 +1575,7 @@ class StageImage(command.Command): else: fp = get_data_from_stdin() - kwargs = {} + kwargs: dict[str, ty.Any] = {} if parsed_args.progress and parsed_args.filename: # NOTE(stephenfin): we only show a progress bar if the user diff --git a/openstackclient/network/common.py b/openstackclient/network/common.py index 568d893d28..c5fad534ff 100644 --- a/openstackclient/network/common.py +++ b/openstackclient/network/common.py @@ -12,10 +12,12 @@ # import abc +import argparse import contextlib import logging import typing as ty +import cliff.app import openstack.exceptions from osc_lib.cli import parseractions from osc_lib.command import command @@ -66,6 +68,8 @@ class NetDetectionMixin(metaclass=abc.ABCMeta): present the options for both network types, often qualified accordingly. """ + app: cliff.app.App + @property def _network_type(self): """Discover whether the running cloud is using neutron or nova-network. @@ -132,9 +136,9 @@ class NetDetectionMixin(metaclass=abc.ABCMeta): ) ) - def get_parser(self, prog_name): + def get_parser(self, prog_name: str) -> argparse.ArgumentParser: LOG.debug('get_parser(%s)', prog_name) - parser = super().get_parser(prog_name) + parser = super().get_parser(prog_name) # type: ignore parser = self.update_parser_common(parser) LOG.debug('common parser: %s', parser) if self.is_neutron or self.is_docs_build: diff --git a/openstackclient/network/v2/network_meter_rule.py b/openstackclient/network/v2/network_meter_rule.py index f884b2f6d3..c7551543ea 100644 --- a/openstackclient/network/v2/network_meter_rule.py +++ b/openstackclient/network/v2/network_meter_rule.py @@ -14,6 +14,7 @@ """Meter Rule Implementations""" import logging +import typing as ty from osc_lib.command import command from osc_lib import exceptions @@ -34,7 +35,7 @@ def _get_columns(item): def _get_attrs(client_manager, parsed_args): - attrs = {} + attrs: dict[str, ty.Any] = {} if parsed_args.exclude: attrs['excluded'] = True diff --git a/openstackclient/network/v2/router.py b/openstackclient/network/v2/router.py index 2794df43aa..0f6b622cdf 100644 --- a/openstackclient/network/v2/router.py +++ b/openstackclient/network/v2/router.py @@ -18,6 +18,7 @@ import collections import copy import json import logging +import typing as ty from cliff import columns as cliff_columns from osc_lib.cli import format_columns @@ -104,21 +105,21 @@ def _passed_multiple_gateways(extension_supported, external_gateways): def _get_external_gateway_attrs(client_manager, parsed_args): - attrs = {} + attrs: dict[str, ty.Any] = {} if parsed_args.external_gateways: external_gateways: collections.defaultdict[str, list[dict]] = ( collections.defaultdict(list) ) n_client = client_manager.network - first_network_id = None + first_network_id = '' for gw_net_name_or_id in parsed_args.external_gateways: gateway_info = {} gw_net = n_client.find_network( gw_net_name_or_id, ignore_missing=False ) - if first_network_id is None: + if not first_network_id: first_network_id = gw_net.id gateway_info['network_id'] = gw_net.id if 'disable_snat' in parsed_args and parsed_args.disable_snat: @@ -146,7 +147,7 @@ def _get_external_gateway_attrs(client_manager, parsed_args): for ip_spec in parsed_args.fixed_ips: # If there is only one gateway, this value will represent the # network ID for it, otherwise it will be overridden. - ip_net_id = first_network_id + ip_net_id: str = first_network_id if ip_spec.get('subnet', False): subnet_name_id = ip_spec.pop('subnet') diff --git a/openstackclient/tests/functional/network/v2/common.py b/openstackclient/tests/functional/network/v2/common.py index 715dab38e2..248758a843 100644 --- a/openstackclient/tests/functional/network/v2/common.py +++ b/openstackclient/tests/functional/network/v2/common.py @@ -33,7 +33,7 @@ class NetworkTests(base.TestCase): class NetworkTagTests(NetworkTests): """Functional tests with tag operation""" - base_command = None + base_command: str def test_tag_operation(self): # Get project IDs @@ -56,7 +56,7 @@ class NetworkTagTests(NetworkTests): 'set', name1, '--tag red --tag green', ['red', 'green'] ) - list_expected: tuple[tuple[str, list[str]]] = ( + list_expected: tuple[tuple[str, list[str]], ...] = ( (name1, ['red', 'green']), (name2, ['red', 'blue']), (name3, []), @@ -93,7 +93,11 @@ class NetworkTagTests(NetworkTests): parse_output=True, ) - def _create_resource_and_tag_check(self, args, expected): + def _create_resource_and_tag_check( + self, + args: str, + expected: list[str], + ) -> str: name = uuid.uuid4().hex cmd_output = self._create_resource_for_tag_test(name, args) self.addCleanup(self.openstack, f'{self.base_command} delete {name}') diff --git a/openstackclient/tests/unit/common/test_project_cleanup.py b/openstackclient/tests/unit/common/test_project_cleanup.py index 50d4e6487c..42020646cf 100644 --- a/openstackclient/tests/unit/common/test_project_cleanup.py +++ b/openstackclient/tests/unit/common/test_project_cleanup.py @@ -10,7 +10,6 @@ # License for the specific language governing permissions and limitations # under the License. -from io import StringIO from unittest import mock from openstackclient.common import project_cleanup @@ -70,7 +69,7 @@ class TestProjectCleanup(test_utils.TestCommand): parsed_args = self.check_parser(self.cmd, arglist, verifylist) result = None - with mock.patch('sys.stdin', StringIO('y')): + with mock.patch('getpass.getpass', return_value='y'): result = self.cmd.take_action(parsed_args) self.sdk_connect_as_project_mock.assert_called_with(self.project) @@ -143,7 +142,7 @@ class TestProjectCleanup(test_utils.TestCommand): parsed_args = self.check_parser(self.cmd, arglist, verifylist) result = None - with mock.patch('sys.stdin', StringIO('y')): + with mock.patch('getpass.getpass', return_value='y'): result = self.cmd.take_action(parsed_args) self.sdk_connect_as_project_mock.assert_called_with(self.project) @@ -178,7 +177,7 @@ class TestProjectCleanup(test_utils.TestCommand): parsed_args = self.check_parser(self.cmd, arglist, verifylist) result = None - with mock.patch('sys.stdin', StringIO('n')): + with mock.patch('getpass.getpass', return_value='y'): result = self.cmd.take_action(parsed_args) self.sdk_connect_as_project_mock.assert_called_with(self.project) @@ -234,7 +233,7 @@ class TestProjectCleanup(test_utils.TestCommand): parsed_args = self.check_parser(self.cmd, arglist, verifylist) result = None - with mock.patch('sys.stdin', StringIO('y')): + with mock.patch('getpass.getpass', return_value='y'): result = self.cmd.take_action(parsed_args) self.sdk_connect_as_project_mock.assert_not_called() @@ -268,7 +267,7 @@ class TestProjectCleanup(test_utils.TestCommand): parsed_args = self.check_parser(self.cmd, arglist, verifylist) result = None - with mock.patch('sys.stdin', StringIO('y')): + with mock.patch('getpass.getpass', return_value='y'): result = self.cmd.take_action(parsed_args) self.sdk_connect_as_project_mock.assert_called_with(self.project)