Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add from __future__ import annotations lint rule #216

Merged
merged 2 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mreg_cli/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Entry point for the mreg_cli application."""
from __future__ import annotations

from . import main

Expand Down
1 change: 1 addition & 0 deletions mreg_cli/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
fix that by using pydantic models to validate incoming data so the client code has
guarantees about the data it is working with.
"""
from __future__ import annotations

import re
from ipaddress import ip_address
Expand Down
1 change: 1 addition & 0 deletions mreg_cli/api/abstracts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Abstract models for the API."""
from __future__ import annotations

from abc import ABC, abstractmethod
from datetime import datetime
Expand Down
1 change: 1 addition & 0 deletions mreg_cli/api/endpoints.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""API endpoints for mreg."""
from __future__ import annotations

from enum import Enum
from urllib.parse import quote
Expand Down
1 change: 1 addition & 0 deletions mreg_cli/api/fields.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Fields for models of the API."""
from __future__ import annotations

import ipaddress
import re
Expand Down
42 changes: 22 additions & 20 deletions mreg_cli/api/models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""Pydantic models for the mreg_cli package."""

from __future__ import annotations

import ipaddress
import re
from datetime import datetime
from typing import Any, Optional, Union
from typing import Any

from pydantic import AliasChoices, BaseModel, Field, field_validator, model_validator

Expand Down Expand Up @@ -62,7 +64,7 @@ class WithHost(BaseModel):

host: int

def resolve_host(self) -> Union["Host", None]:
def resolve_host(self) -> Host | None:
"""Resolve the host ID to a Host object.

Notes
Expand All @@ -84,7 +86,7 @@ class WithZone(BaseModel):

zone: int

def resolve_zone(self) -> Union["Zone", None]:
def resolve_zone(self) -> Zone | None:
"""Resolve the zone ID to a (Forward)Zone object.

Notes
Expand Down Expand Up @@ -131,7 +133,7 @@ def is_delegated(self) -> bool:
return False

@classmethod
def get_from_hostname(cls, hostname: HostT) -> Union["Delegation", "Zone", None]:
def get_from_hostname(cls, hostname: HostT) -> Delegation | Zone | None:
"""Get the zone from a hostname.

Note: This method may return either a Delegation or a Zone object.
Expand Down Expand Up @@ -205,7 +207,7 @@ def output(self, padding: int = 14) -> None:
OutputManager().add_line(f"{'Role:':<{padding}}{self.name} ({self.description})")

@classmethod
def output_multiple(cls, roles: list["Role"], padding: int = 14) -> None:
def output_multiple(cls, roles: list[Role], padding: int = 14) -> None:
"""Output multiple roles to the console.

:param roles: List of roles to output.
Expand Down Expand Up @@ -239,7 +241,7 @@ def endpoint(cls) -> Endpoint:
return Endpoint.Networks

@classmethod
def get_by_ip(cls, ip: IP_AddressT) -> "Network":
def get_by_ip(cls, ip: IP_AddressT) -> Network:
"""Get a network by IP address.

:param ip: The IP address to search for.
Expand All @@ -249,7 +251,7 @@ def get_by_ip(cls, ip: IP_AddressT) -> "Network":
return Network(**data.json())

@classmethod
def get_by_netmask(cls, netmask: str) -> "Network":
def get_by_netmask(cls, netmask: str) -> Network:
"""Get a network by netmask.

:param netmask: The netmask to search for.
Expand Down Expand Up @@ -294,7 +296,7 @@ def convert_ip_address(cls, values: Any):
return values

@classmethod
def get_by_ip(cls, ip: IP_AddressT) -> Union["IPAddress", None]:
def get_by_ip(cls, ip: IP_AddressT) -> IPAddress | None:
"""Get an IP address object by IP address.

:param ip: The IP address to search for.
Expand Down Expand Up @@ -335,7 +337,7 @@ def ip(self) -> IP_AddressT:
"""Return the IP address."""
return self.ipaddress.address

def associate_mac(self, mac: MACAddressField | str, force: bool = False) -> "IPAddress":
def associate_mac(self, mac: MACAddressField | str, force: bool = False) -> IPAddress:
"""Associate a MAC address with the IP address.

:param mac: The MAC address to associate.
Expand Down Expand Up @@ -365,7 +367,7 @@ def output(self, len_ip: int, len_names: int, names: bool = False):
OutputManager().add_line(f"{name:<{len_names}}{ip:<{len_ip}}{mac}")

@classmethod
def output_multiple(cls, ips: list["IPAddress"], padding: int = 14, names: bool = False):
def output_multiple(cls, ips: list[IPAddress], padding: int = 14, names: bool = False):
"""Output IP addresses to the console."""
output_manager = OutputManager()
len_ip = max(padding, max([len(str(ip.ipaddress)) for ip in ips], default=0) + 2)
Expand Down Expand Up @@ -435,7 +437,7 @@ def output(self, padding: int = 14) -> None:
OutputManager().add_line(f"{'Cname:':<{padding}}{self.name} -> {host}")

@classmethod
def output_multiple(cls, cnames: list["CNAME"], padding: int = 14) -> None:
def output_multiple(cls, cnames: list[CNAME], padding: int = 14) -> None:
"""Output multiple CNAME records to the console.

:param cnames: List of CNAME records to output.
Expand All @@ -461,7 +463,7 @@ def output(self, padding: int = 14) -> None:
OutputManager().add_line(f"{'TXT:':<{padding}}{self.txt}")

@classmethod
def output_multiple(cls, txts: list["TXT"], padding: int = 14) -> None:
def output_multiple(cls, txts: list[TXT], padding: int = 14) -> None:
"""Output multiple TXT records to the console.

:param txts: List of TXT records to output.
Expand Down Expand Up @@ -491,7 +493,7 @@ def output(self, padding: int = 14) -> None:
)

@classmethod
def output_multiple(cls, mxs: list["MX"], padding: int = 14) -> None:
def output_multiple(cls, mxs: list[MX], padding: int = 14) -> None:
"""Output MX records to the console."""
if not mxs:
return
Expand Down Expand Up @@ -549,7 +551,7 @@ def headers(cls) -> list[str]:
]

@classmethod
def output_multiple(cls, naptrs: list["NAPTR"], padding: int = 14) -> None:
def output_multiple(cls, naptrs: list[NAPTR], padding: int = 14) -> None:
"""Output multiple NAPTR records to the console."""
headers = cls.headers()
row_format = f"{{:<{padding}}}" * len(headers)
Expand Down Expand Up @@ -609,7 +611,7 @@ def output(self, padding: int = 14, host_id_name_map: dict[int, str] | None = No
)

@classmethod
def output_multiple(cls, srvs: list["Srv"], padding: int = 14) -> None:
def output_multiple(cls, srvs: list[Srv], padding: int = 14) -> None:
"""Output multiple SRV records.

This method adjusts the padding dynamically based on the longest record name.
Expand Down Expand Up @@ -645,7 +647,7 @@ class PTR_override(FrozenModelWithTimestamps, WithHost):
ipaddress: str # For now, should be an IP address

@classmethod
def output_multiple(cls, ptrs: list["PTR_override"], padding: int = 14):
def output_multiple(cls, ptrs: list[PTR_override], padding: int = 14):
"""Output multiple PTR override records to the console.

:param ptrs: List of PTR override records to output.
Expand Down Expand Up @@ -683,7 +685,7 @@ def endpoint(cls) -> Endpoint:
return Endpoint.Sshfps

@classmethod
def output_multiple(cls, sshfps: list["SSHFP"], padding: int = 14):
def output_multiple(cls, sshfps: list[SSHFP], padding: int = 14):
"""Output multiple SSHFP records to the console.

:param sshfps: List of SSHFP records to output.
Expand Down Expand Up @@ -753,7 +755,7 @@ def endpoint(cls) -> Endpoint:
@classmethod
def get_by_any_means(
cls, identifier: str | HostT, inform_as_cname: bool = True
) -> Optional["Host"]:
) -> Host | None:
"""Get a host by the given identifier.

- If the identifier is numeric, it will be treated as an ID.
Expand Down Expand Up @@ -859,7 +861,7 @@ def ipv6_addresses(self):

def associate_mac_to_ip(
self, mac: MACAddressField | str, ip: IPAddressField | str, force: bool = False
) -> "Host":
) -> Host:
"""Associate a MAC address to an IP address.

:param mac: The MAC address to associate.
Expand Down Expand Up @@ -1092,7 +1094,7 @@ def endpoint(cls) -> Endpoint:
return Endpoint.Hosts

@classmethod
def get(cls, params: dict[str, Any] | None = None) -> "HostList":
def get(cls, params: dict[str, Any] | None = None) -> HostList:
"""Get a list of hosts.

:param params: Optional parameters to pass to the API.
Expand Down
8 changes: 5 additions & 3 deletions mreg_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
This file contains the main CLI class and the top level parser.
"""

from __future__ import annotations

import argparse
import html
import os
import shlex
import sys
from collections.abc import Generator
from typing import TYPE_CHECKING, Any, NoReturn, Optional
from typing import TYPE_CHECKING, Any, NoReturn

from prompt_toolkit import HTML, document, print_formatted_text
from prompt_toolkit.completion import CompleteEvent, Completer, Completion
Expand Down Expand Up @@ -44,7 +46,7 @@ class CliExit(Exception):
pass


def _create_command_group(parent: argparse.ArgumentParser) -> "SubparserType":
def _create_command_group(parent: argparse.ArgumentParser) -> SubparserType:
"""Create a sub parser for a command."""
parent_name = parent.prog.strip()

Expand Down Expand Up @@ -79,7 +81,7 @@ def __init__(self, parser: argparse.ArgumentParser, flags: list[Flag], short_des
self.parser = parser
# sub is an object used for creating sub parser for this command. A
# command/ArgParser can only have one of this object.
self.sub: Optional["SubparserType"] = None
self.sub: SubparserType | None = None

self.short_desc = short_desc
self.children: dict[str, Command] = {}
Expand Down
1 change: 1 addition & 0 deletions mreg_cli/commands/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
This module provides the :py:class:`BaseCommands` class, which is used as the
base class for all CLI command classes.
"""
from __future__ import annotations

from typing import Any

Expand Down
1 change: 1 addition & 0 deletions mreg_cli/commands/dhcp.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""DHCP commands for mreg_cli."""
from __future__ import annotations

import argparse
from typing import Any
Expand Down
1 change: 1 addition & 0 deletions mreg_cli/commands/group.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Hostgroups commands for mreg_cli."""
from __future__ import annotations

import argparse
from itertools import chain
Expand Down
1 change: 1 addition & 0 deletions mreg_cli/commands/help.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Help command for the CLI."""
from __future__ import annotations

import argparse
from typing import Any
Expand Down
1 change: 1 addition & 0 deletions mreg_cli/commands/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
submodules. This base module contains the registry that the submodules
add their commands to.
"""
from __future__ import annotations

import importlib
import os
Expand Down
1 change: 1 addition & 0 deletions mreg_cli/commands/host_submodules/a_aaaa.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- aaaa_remove
- aaaa_show
"""
from __future__ import annotations

import argparse

Expand Down
1 change: 1 addition & 0 deletions mreg_cli/commands/host_submodules/bacnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- bacnetid_remove
- bacnetid_list
"""
from __future__ import annotations

import argparse

Expand Down
1 change: 1 addition & 0 deletions mreg_cli/commands/host_submodules/cname.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Sub module for the 'host' command handling CNAME records."""
from __future__ import annotations

import argparse

Expand Down
1 change: 1 addition & 0 deletions mreg_cli/commands/host_submodules/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- set_comment
- set_contact
"""
from __future__ import annotations

import argparse
import ipaddress
Expand Down
1 change: 1 addition & 0 deletions mreg_cli/commands/host_submodules/rr.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
- cname_replace
- cname_show
"""
from __future__ import annotations

import argparse
from typing import Any
Expand Down
1 change: 1 addition & 0 deletions mreg_cli/commands/label.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Label-related commands for mreg_cli."""
from __future__ import annotations

import argparse
from typing import Any
Expand Down
1 change: 1 addition & 0 deletions mreg_cli/commands/network.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Network commands for mreg_cli."""
from __future__ import annotations

import argparse
import ipaddress
Expand Down
1 change: 1 addition & 0 deletions mreg_cli/commands/permission.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Permission commands for mreg_cli."""
from __future__ import annotations

import argparse
import ipaddress
Expand Down
1 change: 1 addition & 0 deletions mreg_cli/commands/policy.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Policy commands for mreg_cli."""
from __future__ import annotations

import argparse
from typing import Any
Expand Down
1 change: 1 addition & 0 deletions mreg_cli/commands/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
This module provides the :py:class:`CommandWrapper` class, which is used to wrap
a command and its attributes, so it may be used as a subcommand of mreg-cli.
"""
from __future__ import annotations

from collections.abc import Callable

Expand Down
1 change: 1 addition & 0 deletions mreg_cli/commands/zone.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Zone commands for mreg_cli."""
from __future__ import annotations

import argparse
from typing import Any
Expand Down
9 changes: 4 additions & 5 deletions mreg_cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
2. :py:const:`logging.INFO`
3. :py:const:`logging.DEBUG`
"""
from __future__ import annotations

import configparser
import logging
Expand Down Expand Up @@ -64,7 +65,7 @@ class MregCliConfig:
_config_file: dict[str, str]
_config_env: dict[str, str]

def __new__(cls) -> "MregCliConfig":
def __new__(cls) -> MregCliConfig:
"""Create a new instance of the configuration class.

This ensures that only one instance of the configuration class is created.
Expand All @@ -89,12 +90,10 @@ def _load_env_config() -> dict[str, str]:
}

@overload
def get(self, key: str) -> str | None:
...
def get(self, key: str) -> str | None: ...

@overload
def get(self, key: str, default: DefaultType = ...) -> str | DefaultType:
...
def get(self, key: str, default: DefaultType = ...) -> str | DefaultType: ...

def get(self, key: str, default: DefaultType | None = None) -> str | DefaultType | None:
"""Get a configuration value with priority: cmdline, env, file.
Expand Down
Loading
Loading