Skip to content

Commit e4b6849

Browse files
dguidoclaude
andcommitted
Fix all Python linting issues identified by Ruff
- Reorganized imports to top of files in custom Ansible modules - Fixed unused variable warnings with underscore prefixes - Added proper exception chaining in test assertions - Removed unused imports and added noqa comments for availability checks - Fixed malformed docstring syntax All 208 Ruff errors resolved while maintaining full test coverage (42/42 tests pass). 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 97514a4 commit e4b6849

15 files changed

+242
-257
lines changed

library/digital_ocean_floating_ip.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
# (c) 2015, Patrick F. Marques <[email protected]>
44
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
55

6+
import json
7+
import time
68

9+
from ansible.module_utils.basic import AnsibleModule, env_fallback
10+
from ansible.module_utils.digital_ocean import DigitalOceanHelper
711

812
ANSIBLE_METADATA = {'metadata_version': '1.1',
913
'status': ['preview'],
@@ -104,12 +108,6 @@
104108
}
105109
'''
106110

107-
import json
108-
import time
109-
110-
from ansible.module_utils.basic import AnsibleModule, env_fallback
111-
from ansible.module_utils.digital_ocean import DigitalOceanHelper
112-
113111

114112
class Response:
115113

library/gcp_compute_location_info.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def return_if_object(module, response):
7676
module.raise_for_status(response)
7777
result = response.json()
7878
except getattr(json.decoder, 'JSONDecodeError', ValueError) as inst:
79-
module.fail_json(msg="Invalid JSON response with error: {}".format(inst))
79+
module.fail_json(msg=f"Invalid JSON response with error: {inst}")
8080

8181
if navigate_hash(result, ['error', 'errors']):
8282
module.fail_json(msg=navigate_hash(result, ['error', 'errors']))

library/lightsail_region_facts.py

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,21 @@
22
# Copyright: Ansible Project
33
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
44

5+
import traceback
56

7+
try:
8+
import botocore
9+
HAS_BOTOCORE = True
10+
except ImportError:
11+
HAS_BOTOCORE = False
12+
13+
from ansible.module_utils.basic import AnsibleModule
14+
from ansible.module_utils.ec2 import (
15+
HAS_BOTO3,
16+
boto3_conn,
17+
ec2_argument_spec,
18+
get_aws_connection_info,
19+
)
620

721
ANSIBLE_METADATA = {'metadata_version': '1.1',
822
'status': ['preview'],
@@ -48,28 +62,6 @@
4862
}]"
4963
'''
5064

51-
import traceback
52-
53-
try:
54-
import botocore
55-
HAS_BOTOCORE = True
56-
except ImportError:
57-
HAS_BOTOCORE = False
58-
59-
try:
60-
import boto3
61-
except ImportError:
62-
# will be caught by imported HAS_BOTO3
63-
pass
64-
65-
from ansible.module_utils.basic import AnsibleModule
66-
from ansible.module_utils.ec2 import (
67-
HAS_BOTO3,
68-
boto3_conn,
69-
ec2_argument_spec,
70-
get_aws_connection_info,
71-
)
72-
7365

7466
def main():
7567
argument_spec = ec2_argument_spec()
@@ -89,7 +81,7 @@ def main():
8981
client = boto3_conn(module, conn_type='client', resource='lightsail',
9082
region=region, endpoint=ec2_url, **aws_connect_kwargs)
9183
except (botocore.exceptions.ClientError, botocore.exceptions.ValidationError) as e:
92-
module.fail_json(msg='Failed while connecting to the lightsail service: %s' % e, exception=traceback.format_exc())
84+
module.fail_json(msg=f'Failed while connecting to the lightsail service: {e}', exception=traceback.format_exc())
9385

9486
response = client.get_regions(
9587
includeAvailabilityZones=False

library/linode_stackscript_v4.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def create_stackscript(module, client, **kwargs):
2121
response = client.linode.stackscript_create(**kwargs)
2222
return response._raw_json
2323
except Exception as exception:
24-
module.fail_json(msg='Unable to query the Linode API. Saw: %s' % exception)
24+
module.fail_json(msg=f'Unable to query the Linode API. Saw: {exception}')
2525

2626

2727
def stackscript_available(module, client):
@@ -38,30 +38,30 @@ def stackscript_available(module, client):
3838
except IndexError:
3939
return None
4040
except Exception as exception:
41-
module.fail_json(msg='Unable to query the Linode API. Saw: %s' % exception)
41+
module.fail_json(msg=f'Unable to query the Linode API. Saw: {exception}')
4242

4343

4444
def initialise_module():
4545
"""Initialise the module parameter specification."""
4646
return AnsibleModule(
47-
argument_spec=dict(
48-
label=dict(type='str', required=True),
49-
state=dict(
50-
type='str',
51-
required=True,
52-
choices=['present', 'absent']
53-
),
54-
access_token=dict(
55-
type='str',
56-
required=True,
57-
no_log=True,
58-
fallback=(env_fallback, ['LINODE_ACCESS_TOKEN']),
59-
),
60-
script=dict(type='str', required=True),
61-
images=dict(type='list', required=True),
62-
description=dict(type='str', required=False),
63-
public=dict(type='bool', required=False, default=False),
64-
),
47+
argument_spec={
48+
'label': {'type': 'str', 'required': True},
49+
'state': {
50+
'type': 'str',
51+
'required': True,
52+
'choices': ['present', 'absent']
53+
},
54+
'access_token': {
55+
'type': 'str',
56+
'required': True,
57+
'no_log': True,
58+
'fallback': (env_fallback, ['LINODE_ACCESS_TOKEN']),
59+
},
60+
'script': {'type': 'str', 'required': True},
61+
'images': {'type': 'list', 'required': True},
62+
'description': {'type': 'str', 'required': False},
63+
'public': {'type': 'bool', 'required': False, 'default': False},
64+
},
6565
supports_check_mode=False
6666
)
6767

library/linode_v4.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def create_linode(module, client, **kwargs):
2727
try:
2828
response = client.linode.instance_create(**kwargs)
2929
except Exception as exception:
30-
module.fail_json(msg='Unable to query the Linode API. Saw: %s' % exception)
30+
module.fail_json(msg=f'Unable to query the Linode API. Saw: {exception}')
3131

3232
try:
3333
if isinstance(response, tuple):
@@ -53,34 +53,34 @@ def maybe_instance_from_label(module, client):
5353
except IndexError:
5454
return None
5555
except Exception as exception:
56-
module.fail_json(msg='Unable to query the Linode API. Saw: %s' % exception)
56+
module.fail_json(msg=f'Unable to query the Linode API. Saw: {exception}')
5757

5858

5959
def initialise_module():
6060
"""Initialise the module parameter specification."""
6161
return AnsibleModule(
62-
argument_spec=dict(
63-
label=dict(type='str', required=True),
64-
state=dict(
65-
type='str',
66-
required=True,
67-
choices=['present', 'absent']
68-
),
69-
access_token=dict(
70-
type='str',
71-
required=True,
72-
no_log=True,
73-
fallback=(env_fallback, ['LINODE_ACCESS_TOKEN']),
74-
),
75-
authorized_keys=dict(type='list', required=False),
76-
group=dict(type='str', required=False),
77-
image=dict(type='str', required=False),
78-
region=dict(type='str', required=False),
79-
root_pass=dict(type='str', required=False, no_log=True),
80-
tags=dict(type='list', required=False),
81-
type=dict(type='str', required=False),
82-
stackscript_id=dict(type='int', required=False),
83-
),
62+
argument_spec={
63+
'label': {'type': 'str', 'required': True},
64+
'state': {
65+
'type': 'str',
66+
'required': True,
67+
'choices': ['present', 'absent']
68+
},
69+
'access_token': {
70+
'type': 'str',
71+
'required': True,
72+
'no_log': True,
73+
'fallback': (env_fallback, ['LINODE_ACCESS_TOKEN']),
74+
},
75+
'authorized_keys': {'type': 'list', 'required': False},
76+
'group': {'type': 'str', 'required': False},
77+
'image': {'type': 'str', 'required': False},
78+
'region': {'type': 'str', 'required': False},
79+
'root_pass': {'type': 'str', 'required': False, 'no_log': True},
80+
'tags': {'type': 'list', 'required': False},
81+
'type': {'type': 'str', 'required': False},
82+
'stackscript_id': {'type': 'int', 'required': False},
83+
},
8484
supports_check_mode=False,
8585
required_one_of=(
8686
['state', 'label'],

0 commit comments

Comments
 (0)