Merge pull request 'fix: NotRequired keys optional at runtime (#144); feat: PackageDict.architecture, 4.59.0 (NetOrk/netork#2907)' (#149) from fix/kernel-facts-optional-keys into main
driver / lint (push) Successful in 16s
driver / standard (push) Successful in 19s
driver / types (push) Successful in 1m6s
driver / test (3.12) (push) Successful in 1m31s
driver / lowest (push) Successful in 45s
driver / test (3.13) (push) Successful in 1m23s
driver / build (push) Successful in 33s
driver / security (push) Successful in 1m9s
driver / publish (push) Successful in 32s
CI / driver (push) Successful in 3m6s

This commit was merged in pull request #149.
This commit is contained in:
christianmanivong committed 2026-12-05 06:07:49 +00:00
commit 85119a770e
15 files changed
+167 -31

No files matched your search

+20
View File
@@ -7,6 +7,26 @@ recorded here; their history is the git log.
## [Unreleased]
## [4.59.0] - 2026-12-05
### Added
- `PackageDict`: optional `architecture`, the build's architecture in the package
manager's own words (`x64`, `x86`, `arm64` on Windows; `amd64`, `i386`, `all` from
dpkg; `x86_64`, `noarch` from rpm). It tells apart two packages of one name, such as a
multiarch host's `libc6` for amd64 and for i386 (NetOrk/netork#2907). A driver that
cannot tell leaves it out. `PackageDict` moves to `package_models`; `models`
re-exports it, so every import keeps working.
### Fixed
- A key declared `NotRequired` is optional at runtime too: `__optional_keys__` of
`KernelFactsDict`, `HostRepairResultDict`, `LldpNeighborDict`, `LogSourceDict`,
`ScheduledJobDict`, `ScheduledJobSpecDict`, `NASShareDict`, `UpdateHistoryPackageDict`
and `ZabbixAgentSettingsDict` was empty, and their optional keys read as required. Their
modules postponed annotations (`from __future__ import annotations`), and TypedDict
sees `NotRequired` only in an evaluated annotation. Type checking was not affected. A
test now walks every TypedDict in the package for an annotation it could not read
(#144).
## [4.58.1] - 2026-12-03
### Fixed
+2 -2
View File
@@ -21,8 +21,8 @@ Declared under ``if TYPE_CHECKING``: a contract, not a placeholder (see the pack
docstring). Not mixed into any role base.
"""
from __future__ import annotations
# No ``from __future__ import annotations``: TypedDict tells a ``NotRequired`` key only
# from an evaluated annotation (``HostRepairResultDict.__optional_keys__``).
from collections.abc import Callable, Sequence
from typing import TYPE_CHECKING, NotRequired
+2 -2
View File
@@ -5,8 +5,8 @@ it: the reading grew its live patches (NetOrk/netork#2701), and ``models`` is at
recorded size.
"""
from __future__ import annotations
# No ``from __future__ import annotations``: TypedDict tells a ``NotRequired`` key only
# from an evaluated annotation (``KernelFactsDict.__optional_keys__``).
from typing import NotRequired
from typing_extensions import TypedDict
+2 -2
View File
@@ -8,8 +8,8 @@ phone and the PC behind it, an unmanaged switch) is several rows, and stays
several entries in both shapes.
"""
from __future__ import annotations
# No ``from __future__ import annotations``: TypedDict tells a ``NotRequired`` key only
# from an evaluated annotation (``LldpNeighborDict.__optional_keys__``).
import re
from collections.abc import Iterable
from typing import NotRequired, TypedDict
+2 -2
View File
@@ -40,8 +40,8 @@ short must never read as logs that went away, and so must a host whose
:class:`LogSourcesUnavailable`.
"""
from __future__ import annotations
# No ``from __future__ import annotations``: TypedDict tells a ``NotRequired`` key only
# from an evaluated annotation (``LogSourceDict.__optional_keys__``).
import re
from datetime import UTC, datetime
from shlex import quote
+3 -13
View File
@@ -10,8 +10,8 @@ from typing_extensions import TypedDict
# The SNMP health profile shapes live in snmp_health_profile.py, those of the access
# point getters in access_point_models.py, an OS's accounts and processes in
# os_models.py, the host firewall's in host_firewall_models.py; re-exported here,
# where they were.
# os_models.py, the host firewall's in host_firewall_models.py, an installed package's
# in package_models.py; re-exported here, where they were.
from napalm_device_types.access_point_models import ChannelScanEntryDict as ChannelScanEntryDict
from napalm_device_types.access_point_models import (
FastTransitionConfigDict as FastTransitionConfigDict,
@@ -30,6 +30,7 @@ from napalm_device_types.host_firewall_models import NetfilterRuleDict as Netfil
from napalm_device_types.kernel_models import KernelFactsDict as KernelFactsDict
from napalm_device_types.os_models import ProcessDict as ProcessDict
from napalm_device_types.os_models import UserDict as UserDict
from napalm_device_types.package_models import PackageDict as PackageDict
from napalm_device_types.share_models import NASShareDict as NASShareDict
from napalm_device_types.share_models import ShareAccessDict as ShareAccessDict
from napalm_device_types.snmp_health_profile import SnmpHealthProfile as SnmpHealthProfile
@@ -67,17 +68,6 @@ class MACACLDict(TypedDict):
entries: list[MACACLEntryDict]
class PackageDict(TypedDict):
"""A software package / plugin installed on the device."""
name: str
version: str
installed: bool
description: str
size: int
source: str
#: A service's last run ended as it should, or it has not ended (systemd's ``Result``).
SERVICE_RESULT_SUCCESS = "success"
#: A service's last run ended with an exit code that is not success.
+31
View File
@@ -0,0 +1,31 @@
"""The shape of an installed package (``PackageManagementMixin.get_packages``).
A module of its own rather than in :mod:`napalm_device_types.models`, which re-exports
it: the package grew its architecture (NetOrk/netork#2907), and ``models`` is at its
recorded size.
"""
# No ``from __future__ import annotations``: TypedDict tells a ``NotRequired`` key only
# from an evaluated annotation (``PackageDict.__optional_keys__``).
from typing import NotRequired
from typing_extensions import TypedDict
class PackageDict(TypedDict):
"""A software package / plugin installed on the device.
``architecture`` is the build's architecture in the package manager's own words:
``x64``, ``x86`` or ``arm64`` on Windows, ``amd64``, ``i386`` or ``all`` from dpkg,
``x86_64`` or ``noarch`` from rpm. It tells apart two packages of one name, as a
Debian multiarch host's ``libc6`` for amd64 and for i386 or a 32-bit and a 64-bit
install of one Windows program. A driver that cannot tell leaves it out.
"""
name: str
version: str
installed: bool
description: str
size: int
source: str
architecture: NotRequired[str]
+2 -2
View File
@@ -23,8 +23,8 @@ saw. The driver reads the job again right before it writes and raises
:class:`ScheduledJobChanged` when the definition differs.
"""
from __future__ import annotations
# No ``from __future__ import annotations``: TypedDict tells a ``NotRequired`` key only
# from an evaluated annotation (``ScheduledJobDict.__optional_keys__``).
import hashlib
import json
import re
+2 -2
View File
@@ -4,8 +4,8 @@ A storage driver's ``get_shares`` and a Linux host's (``PosixSharesMixin``) answ
:class:`NASShareDict`; ``models`` re-exports both names, where they used to live.
"""
from __future__ import annotations
# No ``from __future__ import annotations``: TypedDict tells a ``NotRequired`` key only
# from an evaluated annotation (``NASShareDict.__optional_keys__``).
from typing import NotRequired
from typing_extensions import TypedDict
+2 -2
View File
@@ -10,8 +10,8 @@ Nothing exists at runtime until a concrete driver implements it, so mixing
this class in can never shadow a working implementation from a sibling base.
"""
from __future__ import annotations
# No ``from __future__ import annotations``: TypedDict tells a ``NotRequired`` key only
# from an evaluated annotation (``UpdateHistoryPackageDict.__optional_keys__``).
from collections.abc import Callable
from typing import TYPE_CHECKING, Any, Literal, NotRequired
+2 -2
View File
@@ -26,8 +26,8 @@ with the rpm in an ``x86_64`` directory. The release line is the server's: an ag
may be older than its server, not newer.
"""
from __future__ import annotations
# No ``from __future__ import annotations``: TypedDict tells a ``NotRequired`` key only
# from an evaluated annotation (``ZabbixAgentSettingsDict.__optional_keys__``).
import re
import shlex
from typing import NotRequired, TypedDict
+1 -1
View File
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "napalm-device-types"
version = "4.58.1"
version = "4.59.0"
description = "Abstract device-type base classes for NAPALM drivers"
readme = "README.md"
requires-python = ">=3.12"
+1 -1
View File
@@ -31,7 +31,7 @@
},
"mypy_relaxed": [],
"python_file_lines": {
"napalm_device_types/models.py": 1286
"napalm_device_types/models.py": 1276
},
"untyped_public": []
}
+24
View File
@@ -0,0 +1,24 @@
"""PackageDict: one installed package, told apart from its namesakes by architecture.
A host can list one name twice: Debian multiarch installs ``libc6`` for amd64 and for
i386, Windows a 32-bit and a 64-bit build of one program (NetOrk/netork#2907).
"""
from napalm_device_types import models
from napalm_device_types.package_models import PackageDict
def test_architecture_is_the_one_optional_key() -> None:
assert PackageDict.__optional_keys__ == {"architecture"}
assert PackageDict.__required_keys__ == {
"name",
"version",
"installed",
"description",
"size",
"source",
}
def test_models_re_exports_it() -> None:
assert models.PackageDict is PackageDict
+71
View File
@@ -0,0 +1,71 @@
"""A ``NotRequired`` key is optional at runtime too.
TypedDict tells a ``NotRequired`` key only from an evaluated annotation. Under
``from __future__ import annotations`` every annotation is a string, the wrapper goes
unseen, and the key lands in ``__required_keys__``. mypy reads the source and is not
fooled; runtime introspection (``__optional_keys__``) is.
"""
import importlib
import pkgutil
from typing import Any, ForwardRef
import pytest
import typing_extensions
import napalm_device_types
from napalm_device_types.host_repairs import HostRepairResultDict
from napalm_device_types.kernel_models import KernelFactsDict
from napalm_device_types.lldp import LldpNeighborDict
from napalm_device_types.log_sources import LogSourceDict
from napalm_device_types.scheduled_jobs import ScheduledJobDict, ScheduledJobSpecDict
from napalm_device_types.share_models import NASShareDict
from napalm_device_types.updates import UpdateHistoryPackageDict
from napalm_device_types.zabbix_agent import ZabbixAgentSettingsDict
OPTIONAL_KEYS: dict[Any, set[str]] = {
KernelFactsDict: {"live_patches"},
HostRepairResultDict: {"error"},
LldpNeighborDict: {"mac"},
LogSourceDict: {"channel"},
ScheduledJobDict: {"name", "description", "last_run", "last_result", "next_run"},
ScheduledJobSpecDict: {"description"},
NASShareDict: {"service", "export", "access", "guest"},
UpdateHistoryPackageDict: {"title"},
ZabbixAgentSettingsDict: {"hostname", "release", "active_only"},
}
@pytest.mark.parametrize("typed_dict", list(OPTIONAL_KEYS), ids=lambda value: value.__name__)
def test_not_required_keys_are_optional_at_runtime(typed_dict: Any) -> None:
optional = OPTIONAL_KEYS[typed_dict]
assert typed_dict.__optional_keys__ == optional
assert not optional & typed_dict.__required_keys__
def _typed_dicts() -> list[Any]:
found = []
for info in pkgutil.iter_modules(napalm_device_types.__path__):
module = importlib.import_module(f"napalm_device_types.{info.name}")
for value in vars(module).values():
if (
isinstance(value, type)
and typing_extensions.is_typeddict(value)
and value.__module__ == module.__name__
):
found.append(value)
return found
def test_no_typed_dict_keeps_not_required_as_a_string() -> None:
"""Every module: a ``NotRequired`` the TypedDict could not see is a required key."""
unseen = [
f"{typed_dict.__module__}.{typed_dict.__qualname__}.{key}"
for typed_dict in _typed_dicts()
for key, annotation in typed_dict.__dict__.get("__annotations__", {}).items()
if isinstance(annotation, str | ForwardRef)
and str(getattr(annotation, "__forward_arg__", annotation)).startswith(
("NotRequired[", "Required[")
)
]
assert unseen == []