Skip to content

Commit

Permalink
Stop importing Dict, List, Tuple, Type from Typing
Browse files Browse the repository at this point in the history
Since Python 3.9 one can simply use the build-in version
  • Loading branch information
johannaengland committed Aug 8, 2024
1 parent d766fd1 commit 86adc05
Show file tree
Hide file tree
Showing 17 changed files with 48 additions and 54 deletions.
2 changes: 1 addition & 1 deletion python/nav/Snmp/pynetsnmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
c_ulong,
c_uint64,
)
from typing import Union, Optional
from typing import Optional, Union

from IPy import IP
from pynetsnmp import netsnmp
Expand Down
3 changes: 1 addition & 2 deletions python/nav/bin/navclean.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"""Cleans old data from the NAV database"""
import argparse
import sys
from typing import List

from nav.bootstrap import bootstrap_django

Expand Down Expand Up @@ -171,7 +170,7 @@ def postgresql_interval(value):

def get_selected_cleaners(
args: argparse.Namespace, connection
) -> List["RecordCleaner"]:
) -> list["RecordCleaner"]:
"""Returns a list of RecordCleaner instances for each of the tables
selected in the supplied ArgumentParser.
"""
Expand Down
6 changes: 3 additions & 3 deletions python/nav/eventengine/severity.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class Expression(typing.NamedTuple):


SeverityModifier = typing.Callable[[int], int]
Expressions = typing.Union[typing.Tuple[Expression], typing.Tuple[()]]
Rule = typing.Tuple[Expressions, SeverityModifier]
Expressions = typing.Union[tuple[Expression], tuple]
Rule = tuple[Expressions, SeverityModifier]
AlertObject = typing.Union[event.EventQueue, event.AlertQueue, event.AlertHistory]

#
Expand Down Expand Up @@ -155,7 +155,7 @@ def _parse_raw_severity_rules(

@classmethod
def _parse_rule_sublist(
cls, current: Expressions, definitions: typing.List[dict]
cls, current: Expressions, definitions: list[dict]
) -> typing.Generator[Rule, None, None]:
"""Generator that parses a nested list of severity rule definitions and their
corresponding severity modifiers, yielding a list of tuples describing the rules
Expand Down
3 changes: 1 addition & 2 deletions python/nav/ipdevpoll/plugins/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
the interface will have its module set to be whatever the ancestor
module of the physical entity is.
"""
from typing import List
import configparser
import re

Expand Down Expand Up @@ -164,7 +163,7 @@ def _process_entities(self, result):
self._process_ports(entities, module_containers)


def get_ignored_serials(config: configparser.ConfigParser) -> List[str]:
def get_ignored_serials(config: configparser.ConfigParser) -> list[str]:
"""Returns a list of ignored serial numbers from a ConfigParser instance"""
if config is None:
return []
Expand Down
3 changes: 1 addition & 2 deletions python/nav/ipdevpoll/plugins/paloaltoarp.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"""

import xml.etree.ElementTree as ET
from typing import Dict

from IPy import IP
from twisted.internet import defer, reactor, ssl
Expand All @@ -40,7 +39,7 @@


class PaloaltoArp(Arp):
configured_devices: Dict[str, str] = {}
configured_devices: dict[str, str] = {}

@classmethod
def on_plugin_load(cls):
Expand Down
6 changes: 3 additions & 3 deletions python/nav/ipdevpoll/snmp/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import logging
from dataclasses import dataclass
from functools import wraps
from typing import Optional, Any, Dict
from typing import Any, Optional

from twisted.internet import reactor
from twisted.internet.defer import succeed
Expand Down Expand Up @@ -218,7 +218,7 @@ def factory(
return cls(**kwargs_out)

@classmethod
def get_params_from_ipdevpoll_config(cls, section: str = "snmp") -> Dict[str, Any]:
def get_params_from_ipdevpoll_config(cls, section: str = "snmp") -> dict[str, Any]:
"""Reads and returns global SNMP parameters from ipdevpoll configuration as a
simple dict.
"""
Expand All @@ -236,7 +236,7 @@ def get_params_from_ipdevpoll_config(cls, section: str = "snmp") -> Dict[str, An

return params

def as_agentproxy_args(self) -> Dict[str, Any]:
def as_agentproxy_args(self) -> dict[str, Any]:
"""Returns the SNMP session parameters in a dict format compatible with
pynetsnmp.twistedsnmp.AgentProxy() keyword arguments.
"""
Expand Down
4 changes: 2 additions & 2 deletions python/nav/jwtconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from os.path import join
from functools import partial
import configparser
from typing import Dict, Any
from typing import Any

from nav.config import ConfigurationError, NAVConfigParser

Expand All @@ -15,7 +15,7 @@ class JWTConf(NAVConfigParser):
DEFAULT_CONFIG_FILES = [join('webfront', 'jwt.conf')]
NAV_SECTION = "nav"

def get_issuers_setting(self) -> Dict[str, Any]:
def get_issuers_setting(self) -> dict[str, Any]:
"""Parses the webfront/jwt.conf config file and returns a dictionary that can
be used as settings for the `drf-oidc-auth` django extension.
If the parsing fails, an empty dict is returned.
Expand Down
4 changes: 2 additions & 2 deletions python/nav/models/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import logging
import math
import re
from typing import Set, Optional
from typing import Optional

import IPy
from django.conf import settings
Expand Down Expand Up @@ -592,7 +592,7 @@ def get_environment_sensors(self):
)

@property
def mac_addresses(self) -> Set[str]:
def mac_addresses(self) -> set[str]:
"""Returns a set of collected chassis MAC addresses for this Netbox"""
macinfo_match = (Q(key="bridge_info") & Q(variable="base_address")) | (
Q(key="lldp") & Q(variable="chassis_mac")
Expand Down
4 changes: 2 additions & 2 deletions python/nav/napalm.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"""This module contains NAPALM connectivity interfaces for NAV"""
import weakref
from tempfile import NamedTemporaryFile
from typing import TypeVar, Type
from typing import TypeVar
import logging
import napalm
from napalm.base import NetworkDriver
Expand Down Expand Up @@ -71,7 +71,7 @@ def _write_key_to_temporary_file(config: dict, optional_args: dict):

def get_driver(
profile: manage.ManagementProfile,
) -> Type[napalm.base.base.NetworkDriver]:
) -> type[napalm.base.base.NetworkDriver]:
"""Returns a NAPALM NetworkDriver based on a management profile config"""
if profile.protocol != profile.PROTOCOL_NAPALM:
raise NapalmError("Management profile is not a NAPALM profile")
Expand Down
16 changes: 8 additions & 8 deletions python/nav/portadmin/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#
"""Interface definition for PortAdmin management handlers"""
import time
from typing import List, Tuple, Dict, Any, Sequence, Union, Optional
from typing import Any, Optional, Sequence, Union
import logging
from dataclasses import dataclass

Expand Down Expand Up @@ -58,7 +58,7 @@ def get_interface_native_vlan(self, interface: manage.Interface) -> int:

def get_interfaces(
self, interfaces: Sequence[manage.Interface] = None
) -> List[Dict[str, Any]]:
) -> list[dict[str, Any]]:
"""Retrieves running configuration switch ports on the device.
:param interfaces: Optional list of interfaces to filter for, as fetching
Expand Down Expand Up @@ -140,7 +140,7 @@ def cycle_interfaces(

def _filter_oper_up_interfaces(
self, interfaces: Sequence[manage.Interface]
) -> List[manage.Interface]:
) -> list[manage.Interface]:
"""Filters a list of Interface objects, returning only those that are
currently operationally up.
Expand Down Expand Up @@ -183,7 +183,7 @@ def get_interface_admin_status(self, interface: manage.Interface) -> int:
"""
raise NotImplementedError

def get_netbox_vlans(self) -> List[FantasyVlan]:
def get_netbox_vlans(self) -> list[FantasyVlan]:
"""Returns a list of FantasyVlan objects representing the enabled VLANs on
this netbox.
Expand All @@ -193,7 +193,7 @@ def get_netbox_vlans(self) -> List[FantasyVlan]:
"""
raise NotImplementedError

def get_netbox_vlan_tags(self) -> List[int]:
def get_netbox_vlan_tags(self) -> list[int]:
"""Returns a list of enabled VLANs on this netbox.
:returns: A list of VLAN tags (integers)
Expand Down Expand Up @@ -232,7 +232,7 @@ def disable_cisco_cdp(self, interface):
"""Should not be implemented on anything else than Cisco"""
raise NotImplementedError

def get_native_and_trunked_vlans(self, interface) -> Tuple[int, List[int]]:
def get_native_and_trunked_vlans(self, interface) -> tuple[int, list[int]]:
"""Get the trunked vlans on this interface
:returns: (native_vlan_tag, list_of_trunked_vlan_tags)
Expand Down Expand Up @@ -264,7 +264,7 @@ def is_dot1x_enabled(self, interface: manage.Interface) -> bool:
"""Returns True if 802.1X authentication is is enabled on interface"""
raise NotImplementedError

def get_dot1x_enabled_interfaces(self) -> Dict[str, bool]:
def get_dot1x_enabled_interfaces(self) -> dict[str, bool]:
"""Fetches the 802.1X enabled state of every interface.
:returns: A dict mapping each interface name to a "802.1X enabled" value
Expand Down Expand Up @@ -302,7 +302,7 @@ def set_poe_state(self, interface: manage.Interface, state: PoeState):

def get_poe_states(
self, interfaces: Optional[Sequence[manage.Interface]] = None
) -> Dict[str, Optional[PoeState]]:
) -> dict[str, Optional[PoeState]]:
"""Retrieves current PoE state for interfaces on this device.
:param interfaces: Optional sequence of interfaces to filter for, as fetching
Expand Down
19 changes: 9 additions & 10 deletions python/nav/portadmin/napalm/juniper.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@
so the underlying Juniper PyEZ library is utilized directly in most cases.
"""
from __future__ import annotations
from operator import attrgetter
from typing import List, Any, Dict, Tuple, Sequence, Optional
from typing import Any, Optional, Sequence

from django.template.loader import get_template
from napalm.base.exceptions import ConnectAuthError, ConnectionException
Expand Down Expand Up @@ -165,7 +164,7 @@ def junos_major_version(self) -> int:

def get_interfaces(
self, interfaces: Sequence[manage.Interface] = None
) -> List[Dict[str, Any]]:
) -> list[dict[str, Any]]:
vlan_map = self._get_untagged_vlans()
if interfaces and len(interfaces) == 1:
# we can use a filter if only a single interface was specified
Expand Down Expand Up @@ -211,7 +210,7 @@ def _get_untagged_vlans(self):
switching.get()
return {port.ifname: port.tag for port in switching if not port.tagged}

def get_netbox_vlans(self) -> List[FantasyVlan]:
def get_netbox_vlans(self) -> list[FantasyVlan]:
vlan_objects = manage.Vlan.objects.filter(
swport_vlans__interface__netbox=self.netbox
).distinct()
Expand All @@ -234,7 +233,7 @@ def _make_vlan(vlan):
}
return sorted(result, key=attrgetter("vlan"))

def get_netbox_vlan_tags(self) -> List[int]:
def get_netbox_vlan_tags(self) -> list[int]:
return [vlan.tag for vlan in self.vlans]

def get_interface_native_vlan(self, interface: manage.Interface) -> int:
Expand All @@ -244,7 +243,7 @@ def get_interface_native_vlan(self, interface: manage.Interface) -> int:
def set_native_vlan(self, interface: manage.Interface, vlan: int):
raise NotImplementedError # This is in fact never used on Juniper!

def get_native_and_trunked_vlans(self, interface) -> Tuple[int, List[int]]:
def get_native_and_trunked_vlans(self, interface) -> tuple[int, list[int]]:
if not self.is_els:
switching = EthernetSwitchingInterfaceTable(self.device.device)
switching.get(interface_name=interface.ifname)
Expand Down Expand Up @@ -472,7 +471,7 @@ def set_poe_state(self, interface: manage.Interface, state: PoeState):

def get_poe_states(
self, interfaces: Optional[Sequence[manage.Interface]] = None
) -> Dict[str, Optional[PoeState]]:
) -> dict[str, Optional[PoeState]]:
"""Retrieves current PoE state for interfaces on this device.
:param interfaces: Optional sequence of interfaces to filter for, as fetching
Expand Down Expand Up @@ -520,7 +519,7 @@ def _get_single_poe_state(self, interface: manage.Interface) -> PoeState:

def _get_poe_states_bulk(
self, interfaces: Sequence[manage.Interface]
) -> Dict[str, Optional[PoeState]]:
) -> dict[str, Optional[PoeState]]:
tree = self._get_all_poe_interface_information()
interface_information_elements = tree.findall(".//interface-information")
ifname_to_state_dict = {}
Expand Down Expand Up @@ -558,7 +557,7 @@ def _poe_string_to_state(self, state_str: str) -> PoeState:
# dot1x authentication configuration fetchers aren't implemented yet, for lack
# of configured devices to test on
# def is_dot1x_enabled(self, interface: manage.Interface) -> bool:
# def get_dot1x_enabled_interfaces(self) -> Dict[str, bool]:
# def get_dot1x_enabled_interfaces(self) -> dict[str, bool]:
# def is_port_access_control_enabled(self) -> bool:

# These are not relevant for Juniper
Expand All @@ -578,7 +577,7 @@ def is_unit(name: str) -> bool:
return len(names) == 2


def split_master_unit(name: str) -> Tuple[str, str]:
def split_master_unit(name: str) -> tuple[str, str]:
"""Splits an interface name into master and unit parts. If the name doesn't
already refer to a unit, unit 0 will be assumed.
"""
Expand Down
6 changes: 3 additions & 3 deletions python/nav/portadmin/snmp/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from functools import wraps
from operator import attrgetter
import logging
from typing import Dict, Sequence, List, Any
from typing import Any, Sequence

from nav.Snmp.profile import get_snmp_session_for_profile
from nav.Snmp import safestring, OID
Expand Down Expand Up @@ -250,7 +250,7 @@ def test_write(self):
@translate_protocol_errors
def get_interfaces(
self, interfaces: Sequence[manage.Interface] = None
) -> List[Dict[str, Any]]:
) -> list[dict[str, Any]]:
names = self._get_interface_names()
aliases = self._get_all_ifaliases()
oper = dict(self._get_all_interfaces_oper_status())
Expand All @@ -270,7 +270,7 @@ def get_interfaces(
]
return result

def _get_interface_names(self) -> Dict[int, str]:
def _get_interface_names(self) -> dict[int, str]:
"""Returns a mapping of interface indexes to ifName values"""
return {
OID(index)[-1]: safestring(value)
Expand Down
6 changes: 3 additions & 3 deletions python/nav/portadmin/snmp/cisco.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#
"""Cisco specific PortAdmin SNMP handling"""
import logging
from typing import Sequence, Dict, Optional, Tuple
from typing import Optional, Sequence

from nav.Snmp.errors import SnmpError
from nav.bitvector import BitVector
Expand Down Expand Up @@ -345,7 +345,7 @@ def set_poe_state(self, interface: manage.Interface, state: PoeState):

def _get_poe_indexes_for_interface(
self, interface: manage.Interface
) -> Tuple[int, int]:
) -> tuple[int, int]:
"""Returns the unit number and interface number for the given interface"""
try:
poeport = manage.POEPort.objects.get(interface=interface)
Expand All @@ -359,7 +359,7 @@ def _get_poe_indexes_for_interface(

def get_poe_states(
self, interfaces: Optional[Sequence[manage.Interface]] = None
) -> Dict[str, Optional[PoeState]]:
) -> dict[str, Optional[PoeState]]:
"""Retrieves current PoE state for interfaces on this device.
:param interfaces: Optional sequence of interfaces to filter for, as fetching
Expand Down
4 changes: 2 additions & 2 deletions python/nav/web/alertprofiles/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

# pylint: disable=R0903

from typing import Any, Dict
from typing import Any

from django import forms
from django.db.models import Q
Expand Down Expand Up @@ -642,7 +642,7 @@ def __init__(self, *args, **kwargs):
else:
self.fields['value'] = forms.CharField(required=True)

def clean(self) -> Dict[str, Any]:
def clean(self) -> dict[str, Any]:
validated_data = super().clean()

match_field = validated_data["match_field"]
Expand Down
Loading

0 comments on commit 86adc05

Please sign in to comment.