Skip to content

Commit 4048ddc

Browse files
Stop importing Dict, List, Tuple, Type from Typing
Since Python 3.9 one can simply use the build-in version
1 parent b8d6d52 commit 4048ddc

File tree

14 files changed

+44
-47
lines changed

14 files changed

+44
-47
lines changed

python/nav/Snmp/pynetsnmp.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
c_ulong,
3232
c_uint64,
3333
)
34-
from typing import Union, Optional
34+
from typing import Optional, Union
3535

3636
from IPy import IP
3737
from pynetsnmp import netsnmp

python/nav/eventengine/severity.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ class Expression(typing.NamedTuple):
5858

5959

6060
SeverityModifier = typing.Callable[[int], int]
61-
Expressions = typing.Union[typing.Tuple[Expression], typing.Tuple[()]]
62-
Rule = typing.Tuple[Expressions, SeverityModifier]
61+
Expressions = typing.Union[tuple[Expression], tuple]
62+
Rule = tuple[Expressions, SeverityModifier]
6363
AlertObject = typing.Union[event.EventQueue, event.AlertQueue, event.AlertHistory]
6464

6565
#
@@ -155,7 +155,7 @@ def _parse_raw_severity_rules(
155155

156156
@classmethod
157157
def _parse_rule_sublist(
158-
cls, current: Expressions, definitions: typing.List[dict]
158+
cls, current: Expressions, definitions: list[dict]
159159
) -> typing.Generator[Rule, None, None]:
160160
"""Generator that parses a nested list of severity rule definitions and their
161161
corresponding severity modifiers, yielding a list of tuples describing the rules

python/nav/ipdevpoll/plugins/modules.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
the interface will have its module set to be whatever the ancestor
2626
module of the physical entity is.
2727
"""
28-
from typing import List
2928
import configparser
3029
import re
3130

@@ -164,7 +163,7 @@ def _process_entities(self, result):
164163
self._process_ports(entities, module_containers)
165164

166165

167-
def get_ignored_serials(config: configparser.ConfigParser) -> List[str]:
166+
def get_ignored_serials(config: configparser.ConfigParser) -> list[str]:
168167
"""Returns a list of ignored serial numbers from a ConfigParser instance"""
169168
if config is None:
170169
return []

python/nav/ipdevpoll/snmp/common.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import logging
1919
from dataclasses import dataclass
2020
from functools import wraps
21-
from typing import Optional, Any, Dict
21+
from typing import Any, Optional
2222

2323
from twisted.internet import reactor
2424
from twisted.internet.defer import succeed
@@ -218,7 +218,7 @@ def factory(
218218
return cls(**kwargs_out)
219219

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

237237
return params
238238

239-
def as_agentproxy_args(self) -> Dict[str, Any]:
239+
def as_agentproxy_args(self) -> dict[str, Any]:
240240
"""Returns the SNMP session parameters in a dict format compatible with
241241
pynetsnmp.twistedsnmp.AgentProxy() keyword arguments.
242242
"""

python/nav/jwtconf.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from os.path import join
33
from functools import partial
44
import configparser
5-
from typing import Dict, Any
5+
from typing import Any
66

77
from nav.config import ConfigurationError, NAVConfigParser
88

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

18-
def get_issuers_setting(self) -> Dict[str, Any]:
18+
def get_issuers_setting(self) -> dict[str, Any]:
1919
"""Parses the webfront/jwt.conf config file and returns a dictionary that can
2020
be used as settings for the `drf-oidc-auth` django extension.
2121
If the parsing fails, an empty dict is returned.

python/nav/models/manage.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import logging
2929
import math
3030
import re
31-
from typing import Set, Optional
31+
from typing import Optional
3232

3333
import IPy
3434
from django.conf import settings
@@ -595,7 +595,7 @@ def get_environment_sensors(self):
595595
)
596596

597597
@property
598-
def mac_addresses(self) -> Set[str]:
598+
def mac_addresses(self) -> set[str]:
599599
"""Returns a set of collected chassis MAC addresses for this Netbox"""
600600
macinfo_match = (Q(key="bridge_info") & Q(variable="base_address")) | (
601601
Q(key="lldp") & Q(variable="chassis_mac")

python/nav/napalm.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"""This module contains NAPALM connectivity interfaces for NAV"""
1717
import weakref
1818
from tempfile import NamedTemporaryFile
19-
from typing import TypeVar, Type
19+
from typing import TypeVar
2020
import logging
2121
import napalm
2222
from napalm.base import NetworkDriver
@@ -71,7 +71,7 @@ def _write_key_to_temporary_file(config: dict, optional_args: dict):
7171

7272
def get_driver(
7373
profile: manage.ManagementProfile,
74-
) -> Type[napalm.base.base.NetworkDriver]:
74+
) -> type[napalm.base.base.NetworkDriver]:
7575
"""Returns a NAPALM NetworkDriver based on a management profile config"""
7676
if profile.protocol != profile.PROTOCOL_NAPALM:
7777
raise NapalmError("Management profile is not a NAPALM profile")

python/nav/portadmin/handlers.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#
1616
"""Interface definition for PortAdmin management handlers"""
1717
import time
18-
from typing import List, Tuple, Dict, Any, Sequence, Union, Optional
18+
from typing import Any, Optional, Sequence, Union
1919
import logging
2020
from dataclasses import dataclass
2121

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

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

141141
def _filter_oper_up_interfaces(
142142
self, interfaces: Sequence[manage.Interface]
143-
) -> List[manage.Interface]:
143+
) -> list[manage.Interface]:
144144
"""Filters a list of Interface objects, returning only those that are
145145
currently operationally up.
146146
@@ -183,7 +183,7 @@ def get_interface_admin_status(self, interface: manage.Interface) -> int:
183183
"""
184184
raise NotImplementedError
185185

186-
def get_netbox_vlans(self) -> List[FantasyVlan]:
186+
def get_netbox_vlans(self) -> list[FantasyVlan]:
187187
"""Returns a list of FantasyVlan objects representing the enabled VLANs on
188188
this netbox.
189189
@@ -193,7 +193,7 @@ def get_netbox_vlans(self) -> List[FantasyVlan]:
193193
"""
194194
raise NotImplementedError
195195

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

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

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

303303
def get_poe_states(
304304
self, interfaces: Optional[Sequence[manage.Interface]] = None
305-
) -> Dict[str, Optional[PoeState]]:
305+
) -> dict[str, Optional[PoeState]]:
306306
"""Retrieves current PoE state for interfaces on this device.
307307
308308
:param interfaces: Optional sequence of interfaces to filter for, as fetching

python/nav/portadmin/napalm/juniper.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@
2626
so the underlying Juniper PyEZ library is utilized directly in most cases.
2727
2828
"""
29-
from __future__ import annotations
3029
from operator import attrgetter
31-
from typing import List, Any, Dict, Tuple, Sequence, Optional
30+
from typing import Any, Optional, Sequence
3231

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

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

214-
def get_netbox_vlans(self) -> List[FantasyVlan]:
213+
def get_netbox_vlans(self) -> list[FantasyVlan]:
215214
vlan_objects = manage.Vlan.objects.filter(
216215
swport_vlans__interface__netbox=self.netbox
217216
).distinct()
@@ -234,7 +233,7 @@ def _make_vlan(vlan):
234233
}
235234
return sorted(result, key=attrgetter("vlan"))
236235

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

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

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

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

521520
def _get_poe_states_bulk(
522521
self, interfaces: Sequence[manage.Interface]
523-
) -> Dict[str, Optional[PoeState]]:
522+
) -> dict[str, Optional[PoeState]]:
524523
tree = self._get_all_poe_interface_information()
525524
interface_information_elements = tree.findall(".//interface-information")
526525
ifname_to_state_dict = {}
@@ -558,7 +557,7 @@ def _poe_string_to_state(self, state_str: str) -> PoeState:
558557
# dot1x authentication configuration fetchers aren't implemented yet, for lack
559558
# of configured devices to test on
560559
# def is_dot1x_enabled(self, interface: manage.Interface) -> bool:
561-
# def get_dot1x_enabled_interfaces(self) -> Dict[str, bool]:
560+
# def get_dot1x_enabled_interfaces(self) -> dict[str, bool]:
562561
# def is_port_access_control_enabled(self) -> bool:
563562

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

580579

581-
def split_master_unit(name: str) -> Tuple[str, str]:
580+
def split_master_unit(name: str) -> tuple[str, str]:
582581
"""Splits an interface name into master and unit parts. If the name doesn't
583582
already refer to a unit, unit 0 will be assumed.
584583
"""

python/nav/portadmin/snmp/base.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from functools import wraps
1818
from operator import attrgetter
1919
import logging
20-
from typing import Dict, Sequence, List, Any
20+
from typing import Any, Sequence
2121

2222
from nav.Snmp.profile import get_snmp_session_for_profile
2323
from nav.Snmp import safestring, OID
@@ -250,7 +250,7 @@ def test_write(self):
250250
@translate_protocol_errors
251251
def get_interfaces(
252252
self, interfaces: Sequence[manage.Interface] = None
253-
) -> List[Dict[str, Any]]:
253+
) -> list[dict[str, Any]]:
254254
names = self._get_interface_names()
255255
aliases = self._get_all_ifaliases()
256256
oper = dict(self._get_all_interfaces_oper_status())
@@ -270,7 +270,7 @@ def get_interfaces(
270270
]
271271
return result
272272

273-
def _get_interface_names(self) -> Dict[int, str]:
273+
def _get_interface_names(self) -> dict[int, str]:
274274
"""Returns a mapping of interface indexes to ifName values"""
275275
return {
276276
OID(index)[-1]: safestring(value)

python/nav/portadmin/snmp/cisco.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#
1717
"""Cisco specific PortAdmin SNMP handling"""
1818
import logging
19-
from typing import Sequence, Dict, Optional, Tuple
19+
from typing import Optional, Sequence
2020

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

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

360360
def get_poe_states(
361361
self, interfaces: Optional[Sequence[manage.Interface]] = None
362-
) -> Dict[str, Optional[PoeState]]:
362+
) -> dict[str, Optional[PoeState]]:
363363
"""Retrieves current PoE state for interfaces on this device.
364364
365365
:param interfaces: Optional sequence of interfaces to filter for, as fetching

python/nav/web/alertprofiles/forms.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
# pylint: disable=R0903
2020

21-
from typing import Any, Dict
21+
from typing import Any
2222

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

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

648648
match_field = validated_data["match_field"]

python/nav/web/portadmin/utils.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#
1616
"""Util functions for the PortAdmin"""
1717
from __future__ import unicode_literals
18-
from typing import List, Sequence, Dict, Any, Optional
18+
from typing import Any, Optional, Sequence
1919
import re
2020
import logging
2121
from operator import attrgetter
@@ -45,7 +45,7 @@ def get_and_populate_livedata(netbox, interfaces):
4545

4646

4747
def update_interfaces_with_collected_data(
48-
interfaces: Sequence[manage.Interface], livedata: Sequence[Dict[str, Any]]
48+
interfaces: Sequence[manage.Interface], livedata: Sequence[dict[str, Any]]
4949
):
5050
"""Updates the list of Interface objects with data gathered via
5151
ManagementHandler.get_interfaces().
@@ -83,7 +83,7 @@ def find_and_populate_allowed_vlans(
8383

8484
def find_allowed_vlans_for_user_on_netbox(
8585
account: profiles.Account, netbox: manage.Netbox, handler: ManagementHandler = None
86-
) -> List[FantasyVlan]:
86+
) -> list[FantasyVlan]:
8787
"""Finds allowed vlans for this user on this netbox"""
8888
netbox_vlans = find_vlans_on_netbox(netbox, handler=handler)
8989

@@ -101,7 +101,7 @@ def find_allowed_vlans_for_user_on_netbox(
101101

102102
def find_vlans_on_netbox(
103103
netbox: manage.Netbox, handler: ManagementHandler = None
104-
) -> List[FantasyVlan]:
104+
) -> list[FantasyVlan]:
105105
"""Find all the available vlans on this netbox
106106
107107
:param netbox: The Netbox whose available VLANs you want to find.

0 commit comments

Comments
 (0)