Skip to content

Commit

Permalink
refactor: add future annotations imports (ansible-collections#398)
Browse files Browse the repository at this point in the history
##### SUMMARY

This adds future annotations imports to benefit from the modern python
typing system.
  • Loading branch information
jooola authored Nov 23, 2023
1 parent 9905bd0 commit df8c3b6
Show file tree
Hide file tree
Showing 43 changed files with 275 additions and 126 deletions.
1 change: 1 addition & 0 deletions .ansible-lint
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ exclude_paths:

warn_list:
- internal-error
- sanity[cannot-ignore]
1 change: 1 addition & 0 deletions .azure-pipelines/scripts/combine-coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
It is up to pipeline authors to avoid name collisions when deviating from the recommended format.
"""

from __future__ import annotations

import os
import re
Expand Down
1 change: 1 addition & 0 deletions .azure-pipelines/scripts/time-command.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python
"""Prepends a relative timestamp to each input line from stdin and writes it to stdout."""

from __future__ import annotations

import sys
import time
Expand Down
3 changes: 3 additions & 0 deletions plugins/doc_fragments/hcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)


from __future__ import annotations


class ModuleDocFragment:
DOCUMENTATION = """
options:
Expand Down
7 changes: 4 additions & 3 deletions plugins/inventory/hcloud.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Copyright (c) 2019 Hetzner Cloud GmbH <[email protected]>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import annotations

DOCUMENTATION = r"""
name: hcloud
short_description: Ansible dynamic inventory plugin for the Hetzner Cloud.
Expand Down Expand Up @@ -135,7 +137,6 @@
import os
import sys
from ipaddress import IPv6Network
from typing import List, Optional, Tuple

from ansible.errors import AnsibleError
from ansible.inventory.manager import InventoryData
Expand Down Expand Up @@ -391,7 +392,7 @@ def verify_file(self, path):
"""Return the possibly of a file being consumable by this plugin."""
return super().verify_file(path) and path.endswith(("hcloud.yaml", "hcloud.yml"))

def _get_cached_result(self, path, cache) -> Tuple[List[Optional[InventoryServer]], bool]:
def _get_cached_result(self, path, cache) -> tuple[list[InventoryServer | None], bool]:
# false when refresh_cache or --flush-cache is used
if not cache:
return None, False
Expand All @@ -409,7 +410,7 @@ def _get_cached_result(self, path, cache) -> Tuple[List[Optional[InventoryServer

return cached_result, True

def _update_cached_result(self, path, cache, result: List[InventoryServer]):
def _update_cached_result(self, path, cache, result: list[InventoryServer]):
if not self.get_option("cache"):
return

Expand Down
12 changes: 7 additions & 5 deletions plugins/module_utils/hcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause)


from __future__ import annotations

import traceback
from typing import Any, Dict, Optional, Union
from typing import Any

from ansible.module_utils.basic import (
AnsibleModule as AnsibleModuleBase,
Expand Down Expand Up @@ -56,7 +58,7 @@ def __init__(self, module: AnsibleModule):
def fail_json_hcloud(
self,
exception: HCloudException,
msg: Optional[str] = None,
msg: str | None = None,
params: Any = None,
**kwargs,
) -> None:
Expand Down Expand Up @@ -91,7 +93,7 @@ def _build_client(self) -> None:
application_version=version,
)

def _client_get_by_name_or_id(self, resource: str, param: Union[str, int]):
def _client_get_by_name_or_id(self, resource: str, param: str | int):
"""
Get a resource by name, and if not found by its ID.
Expand Down Expand Up @@ -132,11 +134,11 @@ def base_module_arguments(cls):
},
}

def _prepare_result(self) -> Dict[str, Any]:
def _prepare_result(self) -> dict[str, Any]:
"""Prepare the result for every module"""
return {}

def get_result(self) -> Dict[str, Any]:
def get_result(self) -> dict[str, Any]:
if getattr(self, self.represent) is not None:
self.result[self.represent] = self._prepare_result()
return self.result
2 changes: 2 additions & 0 deletions plugins/module_utils/version.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from __future__ import annotations

version = "2.3.0" # x-release-please-version
8 changes: 4 additions & 4 deletions plugins/modules/certificate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)


from __future__ import annotations

DOCUMENTATION = """
---
module: certificate
Expand Down Expand Up @@ -143,8 +145,6 @@
type: dict
"""

from typing import Optional

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native

Expand All @@ -156,7 +156,7 @@
class AnsibleHCloudCertificate(AnsibleHCloud):
represent = "hcloud_certificate"

hcloud_certificate: Optional[BoundCertificate] = None
hcloud_certificate: BoundCertificate | None = None

def _prepare_result(self):
return {
Expand Down Expand Up @@ -264,7 +264,7 @@ def define_module(cls):
"choices": ["absent", "present"],
"default": "present",
},
**super().base_module_arguments()
**super().base_module_arguments(),
),
required_one_of=[["id", "name"]],
required_if=[["state", "present", ["name"]]],
Expand Down
7 changes: 4 additions & 3 deletions plugins/modules/certificate_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)


from __future__ import annotations

DOCUMENTATION = """
---
module: certificate_info
Expand Down Expand Up @@ -83,7 +85,6 @@
returned: always
type: dict
"""
from typing import List, Optional

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native
Expand All @@ -96,7 +97,7 @@
class AnsibleHCloudCertificateInfo(AnsibleHCloud):
represent = "hcloud_certificate_info"

hcloud_certificate_info: Optional[List[BoundCertificate]] = None
hcloud_certificate_info: list[BoundCertificate] | None = None

def _prepare_result(self):
certificates = []
Expand Down Expand Up @@ -140,7 +141,7 @@ def define_module(cls):
id={"type": "int"},
name={"type": "str"},
label_selector={"type": "str"},
**super().base_module_arguments()
**super().base_module_arguments(),
),
supports_check_mode=True,
)
Expand Down
6 changes: 3 additions & 3 deletions plugins/modules/datacenter_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)


from __future__ import annotations

DOCUMENTATION = """
---
module: datacenter_info
Expand Down Expand Up @@ -115,8 +117,6 @@
sample: [1, 2, 3]
"""

from typing import List, Optional

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native

Expand All @@ -128,7 +128,7 @@
class AnsibleHCloudDatacenterInfo(AnsibleHCloud):
represent = "hcloud_datacenter_info"

hcloud_datacenter_info: Optional[List[BoundDatacenter]] = None
hcloud_datacenter_info: list[BoundDatacenter] | None = None

def _prepare_result(self):
tmp = []
Expand Down
7 changes: 4 additions & 3 deletions plugins/modules/firewall.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)


from __future__ import annotations

DOCUMENTATION = """
---
module: firewall
Expand Down Expand Up @@ -167,7 +169,6 @@
"""

import time
from typing import Optional

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native
Expand All @@ -180,7 +181,7 @@
class AnsibleHCloudFirewall(AnsibleHCloud):
represent = "hcloud_firewall"

hcloud_firewall: Optional[BoundFirewall] = None
hcloud_firewall: BoundFirewall | None = None

def _prepare_result(self):
return {
Expand Down Expand Up @@ -324,7 +325,7 @@ def define_module(cls):
"choices": ["absent", "present"],
"default": "present",
},
**super().base_module_arguments()
**super().base_module_arguments(),
),
required_one_of=[["id", "name"]],
required_if=[["state", "present", ["name"]]],
Expand Down
6 changes: 3 additions & 3 deletions plugins/modules/floating_ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)


from __future__ import annotations

DOCUMENTATION = """
---
module: floating_ip
Expand Down Expand Up @@ -157,8 +159,6 @@
mylabel: 123
"""

from typing import Optional

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native

Expand All @@ -170,7 +170,7 @@
class AnsibleHCloudFloatingIP(AnsibleHCloud):
represent = "hcloud_floating_ip"

hcloud_floating_ip: Optional[BoundFloatingIP] = None
hcloud_floating_ip: BoundFloatingIP | None = None

def _prepare_result(self):
server = None
Expand Down
6 changes: 3 additions & 3 deletions plugins/modules/floating_ip_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)


from __future__ import annotations

DOCUMENTATION = """
---
module: floating_ip_info
Expand Down Expand Up @@ -97,8 +99,6 @@
type: dict
"""

from typing import List, Optional

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native

Expand All @@ -110,7 +110,7 @@
class AnsibleHCloudFloatingIPInfo(AnsibleHCloud):
represent = "hcloud_floating_ip_info"

hcloud_floating_ip_info: Optional[List[BoundFloatingIP]] = None
hcloud_floating_ip_info: list[BoundFloatingIP] | None = None

def _prepare_result(self):
tmp = []
Expand Down
8 changes: 4 additions & 4 deletions plugins/modules/image_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)


from __future__ import annotations

DOCUMENTATION = """
---
module: image_info
Expand Down Expand Up @@ -109,8 +111,6 @@
type: dict
"""

from typing import List, Optional

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native

Expand All @@ -122,7 +122,7 @@
class AnsibleHCloudImageInfo(AnsibleHCloud):
represent = "hcloud_image_info"

hcloud_image_info: Optional[List[BoundImage]] = None
hcloud_image_info: list[BoundImage] | None = None

def _prepare_result(self):
tmp = []
Expand Down Expand Up @@ -188,7 +188,7 @@ def define_module(cls):
label_selector={"type": "str"},
type={"choices": ["system", "snapshot", "backup"], "default": "system", "type": "str"},
architecture={"choices": ["x86", "arm"], "type": "str"},
**super().base_module_arguments()
**super().base_module_arguments(),
),
supports_check_mode=True,
)
Expand Down
6 changes: 3 additions & 3 deletions plugins/modules/iso_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)


from __future__ import annotations

DOCUMENTATION = """
---
module: iso_info
Expand Down Expand Up @@ -119,8 +121,6 @@
sample: "2021-12-01T00:00:00+00:00"
"""

from typing import List, Optional

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native

Expand All @@ -132,7 +132,7 @@
class AnsibleHCloudIsoInfo(AnsibleHCloud):
represent = "hcloud_iso_info"

hcloud_iso_info: Optional[List[BoundIso]] = None
hcloud_iso_info: list[BoundIso] | None = None

def _prepare_result(self):
tmp = []
Expand Down
8 changes: 4 additions & 4 deletions plugins/modules/load_balancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)


from __future__ import annotations

DOCUMENTATION = """
---
module: load_balancer
Expand Down Expand Up @@ -149,8 +151,6 @@
sample: false
"""

from typing import Optional

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native

Expand All @@ -165,7 +165,7 @@
class AnsibleHCloudLoadBalancer(AnsibleHCloud):
represent = "hcloud_load_balancer"

hcloud_load_balancer: Optional[BoundLoadBalancer] = None
hcloud_load_balancer: BoundLoadBalancer | None = None

def _prepare_result(self):
private_ipv4_address = (
Expand Down Expand Up @@ -315,7 +315,7 @@ def define_module(cls):
"choices": ["absent", "present"],
"default": "present",
},
**super().base_module_arguments()
**super().base_module_arguments(),
),
required_one_of=[["id", "name"]],
mutually_exclusive=[["location", "network_zone"]],
Expand Down
Loading

0 comments on commit df8c3b6

Please sign in to comment.