Skip to content

Commit 95b0aba

Browse files
committed
Refactor nxos ntp for mypy typing
Making NTPPeerDict and NTPServerDict non-identical caused issues with typing since we wre unable to assert the type of the return dictionary easily. This refactor allows us to explicitly state the return type for each function.
1 parent 053e899 commit 95b0aba

File tree

2 files changed

+17
-22
lines changed

2 files changed

+17
-22
lines changed

napalm/base/models.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -223,17 +223,7 @@
223223

224224
NTPPeerDict = TypedDict(
225225
"NTPPeerDict",
226-
{
227-
"address": str,
228-
"port": int,
229-
"version": int,
230-
"association_type": str,
231-
"iburst": bool,
232-
"prefer": bool,
233-
"network_instance": str,
234-
"source_address": str,
235-
"key_id": int,
236-
},
226+
{},
237227
total=False,
238228
)
239229

napalm/nxos/nxos.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,27 +1204,32 @@ def get_arp_table(self, vrf: str = "") -> List[models.ARPTableDict]:
12041204
)
12051205
return arp_table
12061206

1207-
def _get_ntp_entity(
1208-
self, peer_type: str
1209-
) -> Dict[str, Union[models.NTPPeerDict, models.NTPServerDict]]:
1210-
ntp_entities: Dict[str, Union[models.NTPPeerDict, models.NTPServerDict]] = {}
1207+
def _filter_ntp_table(self, peer_type: str) -> List[str]:
1208+
ret = []
12111209
command = "show ntp peers"
12121210
ntp_peers_table = self._get_command_table(command, "TABLE_peers", "ROW_peers")
1213-
12141211
for ntp_peer in ntp_peers_table:
12151212
if ntp_peer.get("serv_peer", "").strip() != peer_type:
12161213
continue
12171214
peer_addr = napalm.base.helpers.ip(ntp_peer.get("PeerIPAddress").strip())
1218-
# Ignore the type of the following line until NTP data is modelled
1219-
ntp_entities[peer_addr] = {} # type: ignore
1220-
1221-
return ntp_entities
1215+
ret.append(peer_addr)
1216+
return ret
12221217

12231218
def get_ntp_peers(self) -> Dict[str, models.NTPPeerDict]:
1224-
return self._get_ntp_entity("Peer")
1219+
ntp_entities: Dict[str, models.NTPPeerDict] = {}
1220+
peers = self._filter_ntp_table("Peer")
1221+
for peer_addr in peers:
1222+
ntp_entities[peer_addr] = {}
1223+
1224+
return ntp_entities
12251225

12261226
def get_ntp_servers(self) -> Dict[str, models.NTPServerDict]:
1227-
return self._get_ntp_entity("Server")
1227+
ntp_entities: Dict[str, models.NTPServerDict] = {}
1228+
peers = self._filter_ntp_table("Server")
1229+
for peer_addr in peers:
1230+
ntp_entities[peer_addr] = {}
1231+
1232+
return ntp_entities
12281233

12291234
def get_ntp_stats(self) -> List[models.NTPStats]:
12301235
ntp_stats: List[models.NTPStats] = []

0 commit comments

Comments
 (0)