diff --git a/docs/source/contributing/conventions/registry-protocol.rst b/docs/source/contributing/conventions/registry-protocol.rst index 51d9ae929a..79477709a0 100644 --- a/docs/source/contributing/conventions/registry-protocol.rst +++ b/docs/source/contributing/conventions/registry-protocol.rst @@ -74,6 +74,23 @@ generated registries spell by hand in ``_missing_``: if not (isinstance(value, int) and 0 <= value <= 0xFF): raise EnumValueError(f'{value!r} is not a valid {cls.__name__}') +**Every registry overrides it**, so that ``register`` refuses what ``get`` refuses +(:issue:`1816`). Each vendor generator emits the override from the check it already +writes at the top of ``_missing_``, with the same message. That is the ``FLAG`` of most +crawlers, but a bespoke template's own range where its ``_missing_`` checks something +else: ``0``-``999`` for :class:`~pcapkit.const.http.status_code.StatusCode` and +:class:`~pcapkit.const.ftp.return_code.ReturnCode`, and :class:`str` for the +string-valued registries. +:class:`~pcapkit.const.pcapng.tls_key_label.TLSKeyLabel` and +:class:`~pcapkit.const.ipv6.extension_header.ExtensionHeader` have no ``_missing_`` to +copy from, because their constructor refuses every value no member carries, so the +first checks its member type and the second the 8-bit Next Header field the type is +sent in. The ``AppType`` transports inherit ``AppType``'s own. + +The check is the *range*, not everything ``_missing_`` decides. A value inside the range +that matches neither a member nor an unassigned range -- ``CauseCode(0)``, say -- still +fails to resolve through ``get``, and ``register`` may mint it. + Three things about it are easy to get wrong: * **It guards, it does not convert.** The return type is :obj:`None` deliberately, so diff --git a/pcapkit/const/arp/hardware.py b/pcapkit/const/arp/hardware.py index 638c3f8150..68e2303f96 100644 --- a/pcapkit/const/arp/hardware.py +++ b/pcapkit/const/arp/hardware.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['Hardware'] @@ -148,6 +154,28 @@ class Hardware(EnumRegistry, IntEnum): #: Reserved [:rfc:`5494`] Reserved_65535 = 65535 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 65535): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'Hardware': """Lookup function used when value is not found. diff --git a/pcapkit/const/arp/operation.py b/pcapkit/const/arp/operation.py index cf85c52bcd..a9688006c8 100644 --- a/pcapkit/const/arp/operation.py +++ b/pcapkit/const/arp/operation.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['Operation'] @@ -101,6 +107,28 @@ class Operation(EnumRegistry, IntEnum): #: Reserved [:rfc:`5494`] Reserved_65535 = 65535 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 65535): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'Operation': """Lookup function used when value is not found. diff --git a/pcapkit/const/esp/cipher.py b/pcapkit/const/esp/cipher.py index 5e76db1f85..07c9039538 100644 --- a/pcapkit/const/esp/cipher.py +++ b/pcapkit/const/esp/cipher.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['Cipher'] @@ -218,6 +224,28 @@ class Cipher(EnumRegistry, IntEnum): #: Alias of :attr:`Cipher.ENCR_MAGMA_MGM_MAC_KTREE`. MAGMA_MGM_MAC_KTREE = 35 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 65535): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'Cipher': """Lookup function used when value is not found. diff --git a/pcapkit/const/esp/integrity.py b/pcapkit/const/esp/integrity.py index 950ec45354..00ff03d3d1 100644 --- a/pcapkit/const/esp/integrity.py +++ b/pcapkit/const/esp/integrity.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['Integrity'] @@ -107,6 +113,28 @@ class Integrity(EnumRegistry, IntEnum): #: Alias of :attr:`Integrity.AUTH_HMAC_SHA2_512_256`. HMAC_SHA2_512_256 = 14 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 65535): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'Integrity': """Lookup function used when value is not found. diff --git a/pcapkit/const/ftp/command.py b/pcapkit/const/ftp/command.py index 3d038947a0..f50baca742 100644 --- a/pcapkit/const/ftp/command.py +++ b/pcapkit/const/ftp/command.py @@ -17,6 +17,7 @@ from pcapkit.corekit.enum import EnumLookup, EnumRegistry from pcapkit.corekit.sentinels import NO_DEFAULT +from pcapkit.utilities.exceptions import EnumValueError if TYPE_CHECKING: from typing import Any, Optional, Type @@ -175,6 +176,28 @@ def get(cls, key: 'Any', default: 'Any' = NO_DEFAULT) -> 'FEATCode': return member return super().get(key, default) + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it is a :class:`str`, as :meth:`_missing_` does. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with the check :meth:`_missing_` makes, and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`get` refuses, in the same words (:issue:`1816`). Any + :class:`str` passes, since :meth:`_missing_` resolves every one. + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` is not a :class:`str`. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not isinstance(value, str): + raise EnumValueError(f'{value!r} is not a valid {cls.__name__}') + @classmethod def _missing_(cls, value: 'str') -> 'FEATCode': """Lookup function used when value is not found. @@ -575,6 +598,28 @@ def get(key: 'str', default: 'Optional[str]' = None) -> 'Command': return Command._unregistered_member(default if default is not None else key, name) return Command[name] # type: ignore[misc] + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it is a :class:`str`, as :meth:`_missing_` does. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with the check :meth:`_missing_` makes, and the message it raises, so + :meth:`register` refuses a value the constructor refuses, in the same + words (:issue:`1816`). Any :class:`str` passes, since + :meth:`_missing_` resolves every one. + + Args: + value: Value about to be minted. + + Raises: + EnumValueError: If ``value`` is not a :class:`str`. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not isinstance(value, str): + raise EnumValueError(f'{value!r} is not a valid {cls.__name__}') + @classmethod def _missing_(cls, value: 'str') -> 'Command': """Lookup function used when value is not found. diff --git a/pcapkit/const/ftp/return_code.py b/pcapkit/const/ftp/return_code.py index 8f6f21ee6a..39c164bd5f 100644 --- a/pcapkit/const/ftp/return_code.py +++ b/pcapkit/const/ftp/return_code.py @@ -16,9 +16,10 @@ from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError if TYPE_CHECKING: - from typing import Optional, Type + from typing import Any, Optional, Type __all__ = ['ReturnCode'] @@ -43,6 +44,29 @@ class ResponseKind(EnumRegistry, IntEnum): PermanentNegativeCompletion = 5 Protected = 6 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it is a digit, which :meth:`_missing_` accepts. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with the range :meth:`_missing_` resolves, and the message the + constructor raises outside it, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` is not an :class:`int` in ``0``-``9``. + A :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 9): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'ResponseKind': """Lookup function used when value is not found. @@ -67,6 +91,29 @@ class GroupingInformation(EnumRegistry, IntEnum): Unspecified = 4 FileSystem = 5 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it is a digit, which :meth:`_missing_` accepts. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with the range :meth:`_missing_` resolves, and the message the + constructor raises outside it, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` is not an :class:`int` in ``0``-``9``. + A :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 9): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'GroupingInformation': """Lookup function used when value is not found. @@ -308,6 +355,30 @@ def _unregistered_member(cls, value: 'int', name: 'str') -> 'ReturnCode': obj.group = GroupingInformation(int(code[1])) return obj + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it is a three-digit reply code. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with the range check :meth:`_missing_` makes, and the message it + raises, so :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses + exactly the values :meth:`~pcapkit.corekit.enum.EnumLookup.get` + refuses, in the same words (:issue:`1816`). That range is ``0``-``999`` + rather than the registry's own, since :meth:`_missing_` resolves a code + outside the registry to ``Unknown`` rather than refusing it. + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` is not an :class:`int` in + ``0``-``999``. A :exc:`ValueError`, so an ``except ValueError`` + caller is unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 999): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'ReturnCode': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/certificate.py b/pcapkit/const/hip/certificate.py index 1225b4dbda..98e546f26d 100644 --- a/pcapkit/const/hip/certificate.py +++ b/pcapkit/const/hip/certificate.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['Certificate'] @@ -47,6 +53,28 @@ class Certificate(EnumRegistry, IntEnum): #: Obsoleted [:rfc:`8002`] Obsoleted_8 = 8 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'Certificate': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/cipher.py b/pcapkit/const/hip/cipher.py index 0159b834fc..96a0436720 100644 --- a/pcapkit/const/hip/cipher.py +++ b/pcapkit/const/hip/cipher.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['Cipher'] @@ -35,6 +41,28 @@ class Cipher(EnumRegistry, IntEnum): #: AES-256-CBC [:rfc:`7401`] AES_256_CBC = 4 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 65535): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'Cipher': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/di.py b/pcapkit/const/hip/di.py index b3bb92e2ed..bec843c220 100644 --- a/pcapkit/const/hip/di.py +++ b/pcapkit/const/hip/di.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['DITypes'] @@ -29,6 +35,28 @@ class DITypes(EnumRegistry, IntEnum): #: NAI [:rfc:`7401`] NAI = 2 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 15): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'DITypes': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/ecdsa_curve.py b/pcapkit/const/hip/ecdsa_curve.py index 5e79a051da..180c626006 100644 --- a/pcapkit/const/hip/ecdsa_curve.py +++ b/pcapkit/const/hip/ecdsa_curve.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['ECDSACurve'] @@ -29,6 +35,28 @@ class ECDSACurve(EnumRegistry, IntEnum): #: NIST P-384 [:rfc:`7401`] NIST_P_384 = 2 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 65535): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'ECDSACurve': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/ecdsa_low_curve.py b/pcapkit/const/hip/ecdsa_low_curve.py index a132829ade..5ddcbb6ad0 100644 --- a/pcapkit/const/hip/ecdsa_low_curve.py +++ b/pcapkit/const/hip/ecdsa_low_curve.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['ECDSALowCurve'] @@ -26,6 +32,28 @@ class ECDSALowCurve(EnumRegistry, IntEnum): #: SECP160R1 [:rfc:`7401`] SECP160R1 = 1 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 65535): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'ECDSALowCurve': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/eddsa_curve.py b/pcapkit/const/hip/eddsa_curve.py index f27b3e9849..ddb2c31cfb 100644 --- a/pcapkit/const/hip/eddsa_curve.py +++ b/pcapkit/const/hip/eddsa_curve.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['EdDSACurve'] @@ -35,6 +41,28 @@ class EdDSACurve(EnumRegistry, IntEnum): #: EdDSA448ph [:rfc:`8032`] EdDSA448ph = 4 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 65535): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'EdDSACurve': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/esp_transform_suite.py b/pcapkit/const/hip/esp_transform_suite.py index a7f4077bdb..5261fe5864 100644 --- a/pcapkit/const/hip/esp_transform_suite.py +++ b/pcapkit/const/hip/esp_transform_suite.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['ESPTransformSuite'] @@ -68,6 +74,28 @@ class ESPTransformSuite(EnumRegistry, IntEnum): #: AES-GMAC [:rfc:`4543`] AES_GMAC = 15 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 65535): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'ESPTransformSuite': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/group.py b/pcapkit/const/hip/group.py index 918ae43d5f..e0c4bd9ea1 100644 --- a/pcapkit/const/hip/group.py +++ b/pcapkit/const/hip/group.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['Group'] @@ -56,6 +62,28 @@ class Group(EnumRegistry, IntEnum): #: 2048-bit MODP group [:rfc:`7401`] Group_2048_bit_MODP_group = 11 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'Group': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/hi_algorithm.py b/pcapkit/const/hip/hi_algorithm.py index d49bcd7748..be7fac303c 100644 --- a/pcapkit/const/hip/hi_algorithm.py +++ b/pcapkit/const/hip/hi_algorithm.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['HIAlgorithm'] @@ -53,6 +59,28 @@ class HIAlgorithm(EnumRegistry, IntEnum): #: EdDSA [:rfc:`8032`] EdDSA = 13 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 65535): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'HIAlgorithm': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/hit_suite.py b/pcapkit/const/hip/hit_suite.py index c9c5960453..60b596bc1e 100644 --- a/pcapkit/const/hip/hit_suite.py +++ b/pcapkit/const/hip/hit_suite.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['HITSuite'] @@ -38,6 +44,28 @@ class HITSuite(EnumRegistry, IntEnum): #: EdDSA/cSHAKE128 [:rfc:`9374`] EdDSA_cSHAKE128 = 5 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 15): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'HITSuite': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/nat_traversal.py b/pcapkit/const/hip/nat_traversal.py index 6fb3289aa6..f2793ddfe2 100644 --- a/pcapkit/const/hip/nat_traversal.py +++ b/pcapkit/const/hip/nat_traversal.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['NATTraversal'] @@ -32,6 +38,28 @@ class NATTraversal(EnumRegistry, IntEnum): #: ICE-HIP-UDP [:rfc:`9028`] ICE_HIP_UDP = 3 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 65535): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'NATTraversal': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/notify_message.py b/pcapkit/const/hip/notify_message.py index eafba59527..d45442a4b9 100644 --- a/pcapkit/const/hip/notify_message.py +++ b/pcapkit/const/hip/notify_message.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['NotifyMessage'] @@ -134,6 +140,28 @@ class NotifyMessage(EnumRegistry, IntEnum): #: NAT_KEEPALIVE [:rfc:`9028`] NAT_KEEPALIVE = 16385 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 65535): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'NotifyMessage': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/packet.py b/pcapkit/const/hip/packet.py index 12ddbcf871..edded56785 100644 --- a/pcapkit/const/hip/packet.py +++ b/pcapkit/const/hip/packet.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['Packet'] @@ -53,6 +59,28 @@ class Packet(EnumRegistry, IntEnum): #: HIP_DATA [:rfc:`6078`] HIP_DATA = 32 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 127): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'Packet': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/parameter.py b/pcapkit/const/hip/parameter.py index 5d52057de7..cf3da5e3de 100644 --- a/pcapkit/const/hip/parameter.py +++ b/pcapkit/const/hip/parameter.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['Parameter'] @@ -206,6 +212,28 @@ class Parameter(EnumRegistry, IntEnum): #: RELAY_HMAC [:rfc:`5770`] RELAY_HMAC = 65520 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 65535): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'Parameter': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/registration.py b/pcapkit/const/hip/registration.py index d9c3f3695d..7c60ca09f9 100644 --- a/pcapkit/const/hip/registration.py +++ b/pcapkit/const/hip/registration.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['Registration'] @@ -35,6 +41,28 @@ class Registration(EnumRegistry, IntEnum): #: CANDIDATE_DISCOVERY [:rfc:`9028`] CANDIDATE_DISCOVERY = 4 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'Registration': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/registration_failure.py b/pcapkit/const/hip/registration_failure.py index d5e148c814..ab380f7289 100644 --- a/pcapkit/const/hip/registration_failure.py +++ b/pcapkit/const/hip/registration_failure.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['RegistrationFailure'] @@ -51,6 +57,28 @@ class RegistrationFailure(EnumRegistry, IntEnum): #: [:rfc:`9028`] Simultaneous_Rendezvous_and_Control_Relay_Service_usage_prohibited = 9 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'RegistrationFailure': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/suite.py b/pcapkit/const/hip/suite.py index 33463de8d9..3257e208d4 100644 --- a/pcapkit/const/hip/suite.py +++ b/pcapkit/const/hip/suite.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['Suite'] @@ -41,6 +47,28 @@ class Suite(EnumRegistry, IntEnum): #: NULL-ENCRYPT with HMAC-MD5 [:rfc:`5201`] NULL_ENCRYPT_with_HMAC_MD5 = 6 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 65535): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'Suite': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/transport.py b/pcapkit/const/hip/transport.py index 6ced8d0327..c77d5cbecf 100644 --- a/pcapkit/const/hip/transport.py +++ b/pcapkit/const/hip/transport.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['Transport'] @@ -32,6 +38,28 @@ class Transport(EnumRegistry, IntEnum): #: ESP-TCP [:rfc:`6261`] ESP_TCP = 3 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 3): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'Transport': """Lookup function used when value is not found. diff --git a/pcapkit/const/http/error_code.py b/pcapkit/const/http/error_code.py index f20155fa64..164d6cda64 100644 --- a/pcapkit/const/http/error_code.py +++ b/pcapkit/const/http/error_code.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['ErrorCode'] @@ -64,6 +70,28 @@ class ErrorCode(EnumRegistry, IntEnum): #: HTTP_1_1_REQUIRED, Use HTTP/1.1 for the request [:rfc:`9113#section-7`] HTTP_1_1_REQUIRED = 0x0000000D + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0x00000000 <= value <= 0xFFFFFFFF): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'ErrorCode': """Lookup function used when value is not found. diff --git a/pcapkit/const/http/frame.py b/pcapkit/const/http/frame.py index 5ef1e8a78c..f9f446af78 100644 --- a/pcapkit/const/http/frame.py +++ b/pcapkit/const/http/frame.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['Frame'] @@ -62,6 +68,28 @@ class Frame(EnumRegistry, IntEnum): #: ``PRIORITY_UPDATE`` [:rfc:`9218`] PRIORITY_UPDATE = 0x10 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0x00 <= value <= 0xFF): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'Frame': """Lookup function used when value is not found. diff --git a/pcapkit/const/http/method.py b/pcapkit/const/http/method.py index b648bab3eb..821df8e999 100644 --- a/pcapkit/const/http/method.py +++ b/pcapkit/const/http/method.py @@ -15,9 +15,10 @@ from aenum import StrEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError if TYPE_CHECKING: - from typing import Optional, Type + from typing import Any, Optional, Type __all__ = ['Method'] @@ -268,8 +269,9 @@ def get(cls, key: 'str', default: 'Optional[str]' = None) -> 'Method': Raises: ValueError: If ``key`` is not a :class:`str`. This reaches the - caller from the base's non-``str`` branch, which calls - ``cls(key)`` and so ``_missing_``, and is **not** caught by + caller from the base's non-``str`` branch, where + :meth:`_validate_value` refuses it before ``cls(key)`` could + reach ``_missing_``, and is **not** caught by the ``except KeyError`` fallback below -- only a failed *name* lookup is. Raising is the intended behaviour for ``get(b'GET')`` as well: a member whose name and @@ -294,6 +296,29 @@ def get(cls, key: 'str', default: 'Optional[str]' = None) -> 'Method': # passed, per GitHub issue #860's conversion away from minting. return cls._unregistered_member(default if default is not None else key, key.upper()) + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it is a :class:`str`, as :meth:`_missing_` does. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with the check :meth:`_missing_` makes, and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`get` refuses, in the same words (:issue:`1816`). Any + :class:`str` passes, since :meth:`get` and :meth:`_missing_` resolve + every one. + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` is not a :class:`str`. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not isinstance(value, str): + raise EnumValueError(f'{value!r} is not a valid {cls.__name__}') + @classmethod def _missing_(cls, value: 'str') -> 'Method': """Lookup function used when value is not found. diff --git a/pcapkit/const/http/setting.py b/pcapkit/const/http/setting.py index 8b70c7ce77..ba0ace2529 100644 --- a/pcapkit/const/http/setting.py +++ b/pcapkit/const/http/setting.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['Setting'] @@ -60,6 +66,28 @@ class Setting(EnumRegistry, IntEnum): #: Value: 0) SETTINGS_ENABLE_METADATA = 0x4D44 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0x0000 <= value <= 0xFFFF): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'Setting': """Lookup function used when value is not found. diff --git a/pcapkit/const/http/status_code.py b/pcapkit/const/http/status_code.py index 8cb122a02a..5577b13f2b 100644 --- a/pcapkit/const/http/status_code.py +++ b/pcapkit/const/http/status_code.py @@ -16,9 +16,10 @@ from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError if TYPE_CHECKING: - from typing import Type + from typing import Any, Type __all__ = ['StatusCode'] @@ -270,6 +271,30 @@ def _unregistered_member(cls, value: 'int', name: 'str') -> 'StatusCode': obj.message = name return obj + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it is a three-digit status code. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with the range check :meth:`_missing_` makes, and the message it + raises, so :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses + exactly the values :meth:`~pcapkit.corekit.enum.EnumLookup.get` + refuses, in the same words (:issue:`1816`). That range is ``0``-``999`` + rather than the registry's own, since :meth:`_missing_` resolves a code + outside the registry to ``Unknown`` rather than refusing it. + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` is not an :class:`int` in + ``0``-``999``. A :exc:`ValueError`, so an ``except ValueError`` + caller is unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 999): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'StatusCode': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv4/classification_level.py b/pcapkit/const/ipv4/classification_level.py index 16d425b22f..c7d7dba5ab 100644 --- a/pcapkit/const/ipv4/classification_level.py +++ b/pcapkit/const/ipv4/classification_level.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['ClassificationLevel'] @@ -36,6 +42,28 @@ class ClassificationLevel(EnumRegistry, IntEnum): Reserved_1 = 0b11110001 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0b00000000 <= value <= 0b11111111): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'ClassificationLevel': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv4/option_class.py b/pcapkit/const/ipv4/option_class.py index 8b399171b9..bad2e2d68a 100644 --- a/pcapkit/const/ipv4/option_class.py +++ b/pcapkit/const/ipv4/option_class.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['OptionClass'] @@ -28,6 +34,28 @@ class OptionClass(EnumRegistry, IntEnum): reserved_for_future_use_3 = 3 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 3): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'OptionClass': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv4/option_number.py b/pcapkit/const/ipv4/option_number.py index 7c167206ac..79712a3664 100644 --- a/pcapkit/const/ipv4/option_number.py +++ b/pcapkit/const/ipv4/option_number.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['OptionNumber'] @@ -110,6 +116,28 @@ class OptionNumber(EnumRegistry, IntEnum): #: ``EXP``, RFC3692-style Experiment [:rfc:`4727`] EXP_222 = 222 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'OptionNumber': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv4/protection_authority.py b/pcapkit/const/ipv4/protection_authority.py index 635d693bc1..c5dd6625b7 100644 --- a/pcapkit/const/ipv4/protection_authority.py +++ b/pcapkit/const/ipv4/protection_authority.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['ProtectionAuthority'] @@ -36,6 +42,28 @@ class ProtectionAuthority(EnumRegistry, IntEnum): Field_Termination_Indicator = 7 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and value >= 0): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'ProtectionAuthority': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv4/qs_function.py b/pcapkit/const/ipv4/qs_function.py index 2bb798c4e8..b132406b6f 100644 --- a/pcapkit/const/ipv4/qs_function.py +++ b/pcapkit/const/ipv4/qs_function.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['QSFunction'] @@ -24,6 +30,28 @@ class QSFunction(EnumRegistry, IntEnum): Report_of_Approved_Rate = 8 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 15): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'QSFunction': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv4/router_alert.py b/pcapkit/const/ipv4/router_alert.py index 1f6768601a..d5babf4d70 100644 --- a/pcapkit/const/ipv4/router_alert.py +++ b/pcapkit/const/ipv4/router_alert.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['RouterAlert'] @@ -221,6 +227,28 @@ class RouterAlert(EnumRegistry, IntEnum): #: Reserved [:rfc:`5350`] Reserved_65535 = 65535 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 65535): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'RouterAlert': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv4/tos_del.py b/pcapkit/const/ipv4/tos_del.py index 8d2d634f11..7cda2fcc13 100644 --- a/pcapkit/const/ipv4/tos_del.py +++ b/pcapkit/const/ipv4/tos_del.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['ToSDelay'] @@ -24,6 +30,28 @@ class ToSDelay(EnumRegistry, IntEnum): LOW = 1 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 1): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'ToSDelay': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv4/tos_ecn.py b/pcapkit/const/ipv4/tos_ecn.py index 34afff8934..c5f9107931 100644 --- a/pcapkit/const/ipv4/tos_ecn.py +++ b/pcapkit/const/ipv4/tos_ecn.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['ToSECN'] @@ -28,6 +34,28 @@ class ToSECN(EnumRegistry, IntEnum): CE = 0b11 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0b00 <= value <= 0b11): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'ToSECN': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv4/tos_pre.py b/pcapkit/const/ipv4/tos_pre.py index 9cffeb9280..284a9d57e5 100644 --- a/pcapkit/const/ipv4/tos_pre.py +++ b/pcapkit/const/ipv4/tos_pre.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['ToSPrecedence'] @@ -36,6 +42,28 @@ class ToSPrecedence(EnumRegistry, IntEnum): Routine = 0 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0b000 <= value <= 0b111): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'ToSPrecedence': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv4/tos_rel.py b/pcapkit/const/ipv4/tos_rel.py index dc00762b78..a66e42e7ac 100644 --- a/pcapkit/const/ipv4/tos_rel.py +++ b/pcapkit/const/ipv4/tos_rel.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['ToSReliability'] @@ -24,6 +30,28 @@ class ToSReliability(EnumRegistry, IntEnum): HIGH = 1 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 1): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'ToSReliability': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv4/tos_thr.py b/pcapkit/const/ipv4/tos_thr.py index 91cb71629e..e11d5720d6 100644 --- a/pcapkit/const/ipv4/tos_thr.py +++ b/pcapkit/const/ipv4/tos_thr.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['ToSThroughput'] @@ -24,6 +30,28 @@ class ToSThroughput(EnumRegistry, IntEnum): HIGH = 1 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 1): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'ToSThroughput': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv4/ts_flag.py b/pcapkit/const/ipv4/ts_flag.py index f89ca8aa55..3cdfebe0e0 100644 --- a/pcapkit/const/ipv4/ts_flag.py +++ b/pcapkit/const/ipv4/ts_flag.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['TSFlag'] @@ -26,6 +32,28 @@ class TSFlag(EnumRegistry, IntEnum): Prespecified_IP_with_Timestamp = 3 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0b0000 <= value <= 0b1111): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'TSFlag': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv6/extension_header.py b/pcapkit/const/ipv6/extension_header.py index 3c391ae0ee..af093ad423 100644 --- a/pcapkit/const/ipv6/extension_header.py +++ b/pcapkit/const/ipv6/extension_header.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['ExtensionHeader'] @@ -59,3 +65,29 @@ class ExtensionHeader(EnumRegistry, IntEnum): #: Use for experimentation and testing [:rfc:`3692`][:rfc:`4727`] Use_for_experimentation_and_testing_254 = 254 + + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it fits the 8-bit Next Header field. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` so + that :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses a + value no extension header can carry, as + :meth:`~pcapkit.corekit.enum.EnumLookup.get` does, in the same words + (:issue:`1816`). There is no ``_missing_`` here to take a range from + -- the constructor refuses every value no member carries -- so the + range is that of the Next Header field an extension header type is + sent in (:rfc:`8200#section-3`), and an unassigned type within it may + still be minted. + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` is not an :class:`int` in + ``0``-``255``. A :exc:`ValueError`, so an ``except + ValueError`` caller is unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) diff --git a/pcapkit/const/ipv6/option.py b/pcapkit/const/ipv6/option.py index a543f982ad..a123726f1e 100644 --- a/pcapkit/const/ipv6/option.py +++ b/pcapkit/const/ipv6/option.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['Option'] @@ -110,6 +116,28 @@ class Option(EnumRegistry, IntEnum): #: RFC3692-style Experiment [:rfc:`4727`] RFC3692_style_Experiment_0xFE = 0xFE + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0x00 <= value <= 0xFF): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'Option': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv6/option_action.py b/pcapkit/const/ipv6/option_action.py index 7d5ac9671b..88ad2f731c 100644 --- a/pcapkit/const/ipv6/option_action.py +++ b/pcapkit/const/ipv6/option_action.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['OptionAction'] @@ -28,6 +34,28 @@ class OptionAction(EnumRegistry, IntEnum): discard_icmp_unicast = 3 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 3): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'OptionAction': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv6/qs_function.py b/pcapkit/const/ipv6/qs_function.py index caf2ab9b5d..f957d2f89e 100644 --- a/pcapkit/const/ipv6/qs_function.py +++ b/pcapkit/const/ipv6/qs_function.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['QSFunction'] @@ -24,6 +30,28 @@ class QSFunction(EnumRegistry, IntEnum): Report_of_Approved_Rate = 8 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 15): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'QSFunction': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv6/router_alert.py b/pcapkit/const/ipv6/router_alert.py index ecc993129e..4445bb704a 100644 --- a/pcapkit/const/ipv6/router_alert.py +++ b/pcapkit/const/ipv6/router_alert.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['RouterAlert'] @@ -233,6 +239,28 @@ class RouterAlert(EnumRegistry, IntEnum): #: Reserved [The Internet Assigned Numbers Authority] Reserved_65535 = 65535 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 65535): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'RouterAlert': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv6/routing.py b/pcapkit/const/ipv6/routing.py index 611ae25a92..69c9ae29fd 100644 --- a/pcapkit/const/ipv6/routing.py +++ b/pcapkit/const/ipv6/routing.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['Routing'] @@ -50,6 +56,28 @@ class Routing(EnumRegistry, IntEnum): #: Reserved Reserved_255 = 255 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'Routing': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv6/seed_id.py b/pcapkit/const/ipv6/seed_id.py index d7333613c7..bc6c9afe71 100644 --- a/pcapkit/const/ipv6/seed_id.py +++ b/pcapkit/const/ipv6/seed_id.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['SeedID'] @@ -28,6 +34,28 @@ class SeedID(EnumRegistry, IntEnum): SEEDID_128_BIT_UNSIGNED_INTEGER = 0b11 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0b00 <= value <= 0b11): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'SeedID': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv6/smf_dpd_mode.py b/pcapkit/const/ipv6/smf_dpd_mode.py index 11ff5a2ff2..f9eac68f91 100644 --- a/pcapkit/const/ipv6/smf_dpd_mode.py +++ b/pcapkit/const/ipv6/smf_dpd_mode.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['SMFDPDMode'] @@ -24,6 +30,28 @@ class SMFDPDMode(EnumRegistry, IntEnum): H_DPD = 1 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 1): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'SMFDPDMode': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv6/tagger_id.py b/pcapkit/const/ipv6/tagger_id.py index 966030508c..74fccbf2bd 100644 --- a/pcapkit/const/ipv6/tagger_id.py +++ b/pcapkit/const/ipv6/tagger_id.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['TaggerID'] @@ -32,6 +38,28 @@ class TaggerID(EnumRegistry, IntEnum): #: IPv6 [:rfc:`6621`] IPv6 = 3 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 7): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'TaggerID': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipx/packet.py b/pcapkit/const/ipx/packet.py index d697d9508c..ca8e68e820 100644 --- a/pcapkit/const/ipx/packet.py +++ b/pcapkit/const/ipx/packet.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['Packet'] @@ -45,6 +51,28 @@ class Packet(EnumRegistry, IntEnum): #: Broadcast[4] Broadcast_4 = 20 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'Packet': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipx/socket.py b/pcapkit/const/ipx/socket.py index 649dd12678..96c7d30b0b 100644 --- a/pcapkit/const/ipx/socket.py +++ b/pcapkit/const/ipx/socket.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['Socket'] @@ -65,6 +71,28 @@ class Socket(EnumRegistry, IntEnum): #: IPXF, IPX Fragmentation Protocol IPXF = 0x9093 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0x0000 <= value <= 0xFFFF): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'Socket': """Lookup function used when value is not found. diff --git a/pcapkit/const/l2tp/type.py b/pcapkit/const/l2tp/type.py index c2a7880e63..bac457c63d 100644 --- a/pcapkit/const/l2tp/type.py +++ b/pcapkit/const/l2tp/type.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['Type'] @@ -24,6 +30,28 @@ class Type(EnumRegistry, IntEnum): Control = 1 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 1): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'Type': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/access_type.py b/pcapkit/const/mh/access_type.py index dfb524e63c..4c84ab8292 100644 --- a/pcapkit/const/mh/access_type.py +++ b/pcapkit/const/mh/access_type.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['AccessType'] @@ -62,6 +68,28 @@ class AccessType(EnumRegistry, IntEnum): #: 3GPP NB-IOT [3GPP TS 29.275][Kimmo Kymalainen] AccessType_3GPP_NB_IOT = 13 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'AccessType': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/ack_status_code.py b/pcapkit/const/mh/ack_status_code.py index e5cf91ab69..38cccb4060 100644 --- a/pcapkit/const/mh/ack_status_code.py +++ b/pcapkit/const/mh/ack_status_code.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['ACKStatusCode'] @@ -38,6 +44,28 @@ class ACKStatusCode(EnumRegistry, IntEnum): #: Dynamic pseudo home address assignment not available [:rfc:`5726`] Dynamic_pseudo_home_address_assignment_not_available = 132 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'ACKStatusCode': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/ani_suboption.py b/pcapkit/const/mh/ani_suboption.py index d6ee122194..8807f44516 100644 --- a/pcapkit/const/mh/ani_suboption.py +++ b/pcapkit/const/mh/ani_suboption.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['ANISuboption'] @@ -44,6 +50,28 @@ class ANISuboption(EnumRegistry, IntEnum): #: Reserved [:rfc:`6757`] Reserved_255 = 255 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'ANISuboption': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/auth_subtype.py b/pcapkit/const/mh/auth_subtype.py index 6e18e0c4a6..0fe7cb543c 100644 --- a/pcapkit/const/mh/auth_subtype.py +++ b/pcapkit/const/mh/auth_subtype.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['AuthSubtype'] @@ -26,6 +32,28 @@ class AuthSubtype(EnumRegistry, IntEnum): #: MN-AAA authentication mobility option [:rfc:`4285`] MN_AAA = 2 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'AuthSubtype': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/binding_ack_flag.py b/pcapkit/const/mh/binding_ack_flag.py index bb34ce85cc..dee9433168 100644 --- a/pcapkit/const/mh/binding_ack_flag.py +++ b/pcapkit/const/mh/binding_ack_flag.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntFlag from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['BindingACKFlag'] @@ -41,6 +47,28 @@ class BindingACKFlag(EnumRegistry, IntFlag): #: D [:rfc:`8885`] D = 0x02 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 0xFF): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'BindingACKFlag': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/binding_error.py b/pcapkit/const/mh/binding_error.py index ba7460695a..7c9246b7b5 100644 --- a/pcapkit/const/mh/binding_error.py +++ b/pcapkit/const/mh/binding_error.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['BindingError'] @@ -24,6 +30,28 @@ class BindingError(EnumRegistry, IntEnum): Unrecognized_MH_Type_value = 2 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'BindingError': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/binding_revocation.py b/pcapkit/const/mh/binding_revocation.py index 2ecafa3186..db0fba4229 100644 --- a/pcapkit/const/mh/binding_revocation.py +++ b/pcapkit/const/mh/binding_revocation.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['BindingRevocation'] @@ -29,6 +35,28 @@ class BindingRevocation(EnumRegistry, IntEnum): #: Binding Revocation Acknowledgement [:rfc:`5846`] Binding_Revocation_Acknowledgement = 2 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'BindingRevocation': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/binding_update_flag.py b/pcapkit/const/mh/binding_update_flag.py index 4535b87d8d..af5537758d 100644 --- a/pcapkit/const/mh/binding_update_flag.py +++ b/pcapkit/const/mh/binding_update_flag.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntFlag from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['BindingUpdateFlag'] @@ -56,6 +62,28 @@ class BindingUpdateFlag(EnumRegistry, IntFlag): #: D [:rfc:`8885`] D = 0x0010 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 0xFFFF): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'BindingUpdateFlag': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/cga_extension.py b/pcapkit/const/mh/cga_extension.py index e9e95ef1e7..a54adefb5e 100644 --- a/pcapkit/const/mh/cga_extension.py +++ b/pcapkit/const/mh/cga_extension.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['CGAExtension'] @@ -32,6 +38,28 @@ class CGAExtension(EnumRegistry, IntEnum): #: Exp_FFFF (experimental) [:rfc:`4581`] Exp_FFFF = 0xFFFF + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 0xFFFF): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'CGAExtension': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/cga_sec.py b/pcapkit/const/mh/cga_sec.py index ea08f79957..e55502b3e5 100644 --- a/pcapkit/const/mh/cga_sec.py +++ b/pcapkit/const/mh/cga_sec.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['CGASec'] @@ -29,6 +35,28 @@ class CGASec(EnumRegistry, IntEnum): #: SHA-1_32hash2bits [:rfc:`4982`] SHA_1_32hash2bits = 0b010 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 0b111): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'CGASec': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/cga_type.py b/pcapkit/const/mh/cga_type.py index 03510ee152..d4e7ac55ca 100644 --- a/pcapkit/const/mh/cga_type.py +++ b/pcapkit/const/mh/cga_type.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum, extend_enum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['CGAType'] @@ -41,6 +47,28 @@ class CGAType(EnumRegistry, IntEnum): #: 0x00B5 A69C 795D F5D5 F008 7F56 843F 2C40 [:rfc:`9374`] Tag_00B5_A69C_795D_F5D5_F008_7F56_843F_2C40 = 0x00B5_A69C_795D_F5D5_F008_7F56_843F_2C40 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 0xFFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'CGAType': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/dhcp_support_mode.py b/pcapkit/const/mh/dhcp_support_mode.py index d76fdd411f..c3721fcfd2 100644 --- a/pcapkit/const/mh/dhcp_support_mode.py +++ b/pcapkit/const/mh/dhcp_support_mode.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['DHCPSupportMode'] @@ -26,6 +32,28 @@ class DHCPSupportMode(EnumRegistry, IntEnum): #: (S) flag [:rfc:`5844`] S_flag = 0x1 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 1): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'DHCPSupportMode': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/dns_status_code.py b/pcapkit/const/mh/dns_status_code.py index c68779019d..2ef478d691 100644 --- a/pcapkit/const/mh/dns_status_code.py +++ b/pcapkit/const/mh/dns_status_code.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['DNSStatusCode'] @@ -32,6 +38,28 @@ class DNSStatusCode(EnumRegistry, IntEnum): #: DNS Update Failed [:rfc:`5026`] DNS_Update_Failed = 130 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'DNSStatusCode': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/dsmip6_tls_packet.py b/pcapkit/const/mh/dsmip6_tls_packet.py index 85cde08ab8..935092a092 100644 --- a/pcapkit/const/mh/dsmip6_tls_packet.py +++ b/pcapkit/const/mh/dsmip6_tls_packet.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['DSMIP6TLSPacket'] @@ -29,6 +35,28 @@ class DSMIP6TLSPacket(EnumRegistry, IntEnum): #: mobility header [:rfc:`6618`] mobility_header = 8 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 15): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'DSMIP6TLSPacket': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/dsmipv6_home_address.py b/pcapkit/const/mh/dsmipv6_home_address.py index 16a490dd3c..0e0b77f0b0 100644 --- a/pcapkit/const/mh/dsmipv6_home_address.py +++ b/pcapkit/const/mh/dsmipv6_home_address.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['DSMIPv6HomeAddress'] @@ -41,6 +47,28 @@ class DSMIPv6HomeAddress(EnumRegistry, IntEnum): #: Prefix allocation unauthorized [:rfc:`5555`] Prefix_allocation_unauthorized = 133 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'DSMIPv6HomeAddress': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/enumerating_algorithm.py b/pcapkit/const/mh/enumerating_algorithm.py index 0f6c815c67..d20f999d3d 100644 --- a/pcapkit/const/mh/enumerating_algorithm.py +++ b/pcapkit/const/mh/enumerating_algorithm.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['EnumeratingAlgorithm'] @@ -29,6 +35,28 @@ class EnumeratingAlgorithm(EnumRegistry, IntEnum): #: Reserved for use by 3GPP2 [:rfc:`4285`] Reserved_for_use_by_3GPP2 = 5 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'EnumeratingAlgorithm': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/fb_ack_status.py b/pcapkit/const/mh/fb_ack_status.py index 2fc317e34c..08cac4edb9 100644 --- a/pcapkit/const/mh/fb_ack_status.py +++ b/pcapkit/const/mh/fb_ack_status.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['FlowBindingACKStatus'] @@ -29,6 +35,28 @@ class FlowBindingACKStatus(EnumRegistry, IntEnum): #: Action NOT Authorized [:rfc:`7109`] Action_NOT_Authorized = 129 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'FlowBindingACKStatus': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/fb_action.py b/pcapkit/const/mh/fb_action.py index 0fd35f936c..f98c0d0071 100644 --- a/pcapkit/const/mh/fb_action.py +++ b/pcapkit/const/mh/fb_action.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['FlowBindingAction'] @@ -38,6 +44,28 @@ class FlowBindingAction(EnumRegistry, IntEnum): #: Revoke a flow binding [:rfc:`7109`] Revoke = 16 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'FlowBindingAction': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/fb_indication_trigger.py b/pcapkit/const/mh/fb_indication_trigger.py index 51d7134f06..2a2b249e33 100644 --- a/pcapkit/const/mh/fb_indication_trigger.py +++ b/pcapkit/const/mh/fb_indication_trigger.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['FlowBindingIndicationTrigger'] @@ -32,6 +38,28 @@ class FlowBindingIndicationTrigger(EnumRegistry, IntEnum): #: Possible Out-of-Sync BCE State [:rfc:`7109`] Possible_Out_of_Sync_BCE_State = 3 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'FlowBindingIndicationTrigger': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/fb_type.py b/pcapkit/const/mh/fb_type.py index a016e6a733..731332faa3 100644 --- a/pcapkit/const/mh/fb_type.py +++ b/pcapkit/const/mh/fb_type.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['FlowBindingType'] @@ -29,6 +35,28 @@ class FlowBindingType(EnumRegistry, IntEnum): #: Flow Binding Acknowledgement [:rfc:`7109`] Acknowledgement = 2 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'FlowBindingType': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/flow_id_status.py b/pcapkit/const/mh/flow_id_status.py index e9b5b89b6b..19b5fdf39d 100644 --- a/pcapkit/const/mh/flow_id_status.py +++ b/pcapkit/const/mh/flow_id_status.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['FlowIDStatus'] @@ -41,6 +47,28 @@ class FlowIDStatus(EnumRegistry, IntEnum): #: Traffic selector format not supported [:rfc:`6089`] Traffic_selector_format_not_supported = 133 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'FlowIDStatus': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/flow_id_suboption.py b/pcapkit/const/mh/flow_id_suboption.py index f0c6ca09e1..c501acd4c8 100644 --- a/pcapkit/const/mh/flow_id_suboption.py +++ b/pcapkit/const/mh/flow_id_suboption.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['FlowIDSuboption'] @@ -38,6 +44,28 @@ class FlowIDSuboption(EnumRegistry, IntEnum): #: Target Care-of Address [:rfc:`7109`] Target_Care_of_Address = 5 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'FlowIDSuboption': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/handoff_type.py b/pcapkit/const/mh/handoff_type.py index bf6036f84e..211a9d4527 100644 --- a/pcapkit/const/mh/handoff_type.py +++ b/pcapkit/const/mh/handoff_type.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['HandoffType'] @@ -41,6 +47,28 @@ class HandoffType(EnumRegistry, IntEnum): #: Attachment over a new interface sharing prefixes [:rfc:`7864`] Attachment_over_a_new_interface_sharing_prefixes = 6 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'HandoffType': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/handover_ack_flag.py b/pcapkit/const/mh/handover_ack_flag.py index 0ea3834e1a..1adfbd069a 100644 --- a/pcapkit/const/mh/handover_ack_flag.py +++ b/pcapkit/const/mh/handover_ack_flag.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntFlag from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['HandoverACKFlag'] @@ -29,6 +35,28 @@ class HandoverACKFlag(EnumRegistry, IntFlag): #: Forwarding flag [:rfc:`5949`] F = 0x20 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 0xFF): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'HandoverACKFlag': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/handover_ack_status.py b/pcapkit/const/mh/handover_ack_status.py index ab75f40bac..3ce76b71e5 100644 --- a/pcapkit/const/mh/handover_ack_status.py +++ b/pcapkit/const/mh/handover_ack_status.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['HandoverACKStatus'] @@ -59,6 +65,28 @@ class HandoverACKStatus(EnumRegistry, IntEnum): #: Forwarding Not Available [:rfc:`5949`] Forwarding_Not_Available = 132 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'HandoverACKStatus': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/handover_initiate_flag.py b/pcapkit/const/mh/handover_initiate_flag.py index d4ecc19972..2de24549d1 100644 --- a/pcapkit/const/mh/handover_initiate_flag.py +++ b/pcapkit/const/mh/handover_initiate_flag.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntFlag from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['HandoverInitiateFlag'] @@ -32,6 +38,28 @@ class HandoverInitiateFlag(EnumRegistry, IntFlag): #: Forwarding flag [:rfc:`5949`] F = 0x10 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 0xFF): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'HandoverInitiateFlag': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/handover_initiate_status.py b/pcapkit/const/mh/handover_initiate_status.py index 3c0a305f82..1183002ecf 100644 --- a/pcapkit/const/mh/handover_initiate_status.py +++ b/pcapkit/const/mh/handover_initiate_status.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['HandoverInitiateStatus'] @@ -32,6 +38,28 @@ class HandoverInitiateStatus(EnumRegistry, IntEnum): #: All available context transferred [:rfc:`5949`] All_available_context_transferred = 3 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'HandoverInitiateStatus': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/home_address_reply.py b/pcapkit/const/mh/home_address_reply.py index 189c420aad..692f5b9d81 100644 --- a/pcapkit/const/mh/home_address_reply.py +++ b/pcapkit/const/mh/home_address_reply.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['HomeAddressReply'] @@ -38,6 +44,28 @@ class HomeAddressReply(EnumRegistry, IntEnum): #: Dynamic IPv4 home address assignment not available [:rfc:`5844`] Dynamic_IPv4_home_address_assignment_not_available = 132 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'HomeAddressReply': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/lla_code.py b/pcapkit/const/mh/lla_code.py index f74b67822e..8006842ea1 100644 --- a/pcapkit/const/mh/lla_code.py +++ b/pcapkit/const/mh/lla_code.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['LLACode'] @@ -36,6 +42,28 @@ class LLACode(EnumRegistry, IntEnum): no_fast_handover_support = 7 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'LLACode': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/lma_mag_suboption.py b/pcapkit/const/mh/lma_mag_suboption.py index 4f171e86a3..fa0f168862 100644 --- a/pcapkit/const/mh/lma_mag_suboption.py +++ b/pcapkit/const/mh/lma_mag_suboption.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['LMAControlledMAGSuboption'] @@ -29,6 +35,28 @@ class LMAControlledMAGSuboption(EnumRegistry, IntEnum): #: Heartbeat Control Sub-Option [:rfc:`8127`] Heartbeat_Control = 2 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'LMAControlledMAGSuboption': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/mn_group_id.py b/pcapkit/const/mh/mn_group_id.py index c05c34880c..173d213971 100644 --- a/pcapkit/const/mh/mn_group_id.py +++ b/pcapkit/const/mh/mn_group_id.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['MNGroupID'] @@ -29,6 +35,28 @@ class MNGroupID(EnumRegistry, IntEnum): #: Reserved [:rfc:`6602`] Reserved_255 = 255 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'MNGroupID': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/mn_id_subtype.py b/pcapkit/const/mh/mn_id_subtype.py index 345436c703..37a030aa8e 100644 --- a/pcapkit/const/mh/mn_id_subtype.py +++ b/pcapkit/const/mh/mn_id_subtype.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['MNIDSubtype'] @@ -44,6 +50,28 @@ class MNIDSubtype(EnumRegistry, IntEnum): #: DUID [:rfc:`8371`] DUID = 8 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'MNIDSubtype': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/operator_id.py b/pcapkit/const/mh/operator_id.py index 9c2c906a56..ec8d6a2dac 100644 --- a/pcapkit/const/mh/operator_id.py +++ b/pcapkit/const/mh/operator_id.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['OperatorID'] @@ -33,6 +39,28 @@ class OperatorID(EnumRegistry, IntEnum): #: Reserved [:rfc:`6757`] Reserved_255 = 255 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'OperatorID': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/option.py b/pcapkit/const/mh/option.py index e9d529f65c..30c4234295 100644 --- a/pcapkit/const/mh/option.py +++ b/pcapkit/const/mh/option.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['Option'] @@ -233,6 +239,28 @@ class Option(EnumRegistry, IntEnum): #: DLIF Link-Layer Address [:rfc:`8885`] DLIF_Link_Layer_Address = 70 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'Option': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/packet.py b/pcapkit/const/mh/packet.py index bfe7db38ea..d553414edf 100644 --- a/pcapkit/const/mh/packet.py +++ b/pcapkit/const/mh/packet.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['Packet'] @@ -92,6 +98,28 @@ class Packet(EnumRegistry, IntEnum): #: Subscription Response [:rfc:`7161`] Subscription_Response = 23 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'Packet': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/qos_attribute.py b/pcapkit/const/mh/qos_attribute.py index 2d9ca0938d..45cd55d254 100644 --- a/pcapkit/const/mh/qos_attribute.py +++ b/pcapkit/const/mh/qos_attribute.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['QoSAttribute'] @@ -59,6 +65,28 @@ class QoSAttribute(EnumRegistry, IntEnum): #: Reserved [:rfc:`7222`] Reserved_255 = 255 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'QoSAttribute': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/revocation_status_code.py b/pcapkit/const/mh/revocation_status_code.py index 3e8734b53e..2a6fc1dab2 100644 --- a/pcapkit/const/mh/revocation_status_code.py +++ b/pcapkit/const/mh/revocation_status_code.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['RevocationStatusCode'] @@ -50,6 +56,28 @@ class RevocationStatusCode(EnumRegistry, IntEnum): #: Proxy Binding Revocation NOT Supported [:rfc:`5846`] Proxy_Binding_Revocation_NOT_Supported = 135 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'RevocationStatusCode': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/revocation_trigger.py b/pcapkit/const/mh/revocation_trigger.py index 09c6beb892..c32198fdb0 100644 --- a/pcapkit/const/mh/revocation_trigger.py +++ b/pcapkit/const/mh/revocation_trigger.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['RevocationTrigger'] @@ -50,6 +56,28 @@ class RevocationTrigger(EnumRegistry, IntEnum): #: Revoking Mobility Node Local Policy [:rfc:`5846`] Revoking_Mobility_Node_Local_Policy = 129 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'RevocationTrigger': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/status_code.py b/pcapkit/const/mh/status_code.py index 55b8ae43ae..f756b5a8d0 100644 --- a/pcapkit/const/mh/status_code.py +++ b/pcapkit/const/mh/status_code.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['StatusCode'] @@ -201,6 +207,28 @@ class StatusCode(EnumRegistry, IntEnum): #: CANNOT_SUPPORT_MULTIPATH_BINDING [:rfc:`8278`] CANNOT_SUPPORT_MULTIPATH_BINDING = 180 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'StatusCode': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/traffic_selector.py b/pcapkit/const/mh/traffic_selector.py index 01a07e996d..28ab3e5faf 100644 --- a/pcapkit/const/mh/traffic_selector.py +++ b/pcapkit/const/mh/traffic_selector.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['TrafficSelector'] @@ -29,6 +35,28 @@ class TrafficSelector(EnumRegistry, IntEnum): #: IPv6 Binary Traffic Selector [:rfc:`6088`] IPv6_Binary_Traffic_Selector = 2 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'TrafficSelector': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/upa_status.py b/pcapkit/const/mh/upa_status.py index ec982bfaea..248baa9442 100644 --- a/pcapkit/const/mh/upa_status.py +++ b/pcapkit/const/mh/upa_status.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['UpdateNotificationACKStatus'] @@ -38,6 +44,28 @@ class UpdateNotificationACKStatus(EnumRegistry, IntEnum): #: MN not attached. [:rfc:`7864`] MN_not_attached = 132 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'UpdateNotificationACKStatus': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/upn_reason.py b/pcapkit/const/mh/upn_reason.py index aa508989b8..22c4c0e5b4 100644 --- a/pcapkit/const/mh/upn_reason.py +++ b/pcapkit/const/mh/upn_reason.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['UpdateNotificationReason'] @@ -50,6 +56,28 @@ class UpdateNotificationReason(EnumRegistry, IntEnum): #: Reserved [:rfc:`7077`] Reserved_255 = 255 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'UpdateNotificationReason': """Lookup function used when value is not found. diff --git a/pcapkit/const/ngap/procedure_code.py b/pcapkit/const/ngap/procedure_code.py index d01f7e84ed..ef6bed228b 100644 --- a/pcapkit/const/ngap/procedure_code.py +++ b/pcapkit/const/ngap/procedure_code.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['ProcedureCode'] @@ -182,6 +188,28 @@ class ProcedureCode(EnumRegistry, IntEnum): BroadcastSessionTransport = 80 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'ProcedureCode': """Lookup function used when value is not found. diff --git a/pcapkit/const/ngap/protocol_ie.py b/pcapkit/const/ngap/protocol_ie.py index 3d63bfd502..0a1d9bebe1 100644 --- a/pcapkit/const/ngap/protocol_ie.py +++ b/pcapkit/const/ngap/protocol_ie.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['ProtocolIE'] @@ -896,6 +902,28 @@ class ProtocolIE(EnumRegistry, IntEnum): ExtendedOldAMF = 443 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 65535): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'ProtocolIE': """Lookup function used when value is not found. diff --git a/pcapkit/const/ospf/authentication.py b/pcapkit/const/ospf/authentication.py index b55c3d5745..fe4b453063 100644 --- a/pcapkit/const/ospf/authentication.py +++ b/pcapkit/const/ospf/authentication.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['Authentication'] @@ -32,6 +38,28 @@ class Authentication(EnumRegistry, IntEnum): #: Cryptographic Authentication with Extended Sequence Numbers [:rfc:`7474`] Cryptographic_Authentication_with_Extended_Sequence_Numbers = 3 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 65535): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'Authentication': """Lookup function used when value is not found. diff --git a/pcapkit/const/ospf/packet.py b/pcapkit/const/ospf/packet.py index e6683e4b58..7ac8d13281 100644 --- a/pcapkit/const/ospf/packet.py +++ b/pcapkit/const/ospf/packet.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['Packet'] @@ -38,6 +44,28 @@ class Packet(EnumRegistry, IntEnum): #: Link State Ack [:rfc:`2328`] Link_State_Ack = 5 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 65535): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'Packet': """Lookup function used when value is not found. diff --git a/pcapkit/const/pcapng/block_type.py b/pcapkit/const/pcapng/block_type.py index f29582d982..3ff54a561c 100644 --- a/pcapkit/const/pcapng/block_type.py +++ b/pcapkit/const/pcapng/block_type.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['BlockType'] @@ -110,6 +116,28 @@ class BlockType(EnumRegistry, IntEnum): #: Section Header Block ( Section 4.1 ) Section_Header_Block = 0x0a0d0d0a + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 0xFFFFFFFF): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'BlockType': """Lookup function used when value is not found. diff --git a/pcapkit/const/pcapng/filter_type.py b/pcapkit/const/pcapng/filter_type.py index d004ad8d8b..9d49289073 100644 --- a/pcapkit/const/pcapng/filter_type.py +++ b/pcapkit/const/pcapng/filter_type.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['FilterType'] @@ -21,6 +27,28 @@ class FilterType(EnumRegistry, IntEnum): """[FilterType] Filter Types""" + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0x00<= value <= 0xFF): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'FilterType': """Lookup function used when value is not found. diff --git a/pcapkit/const/pcapng/hash_algorithm.py b/pcapkit/const/pcapng/hash_algorithm.py index 637039e0aa..bdf34c0b04 100644 --- a/pcapkit/const/pcapng/hash_algorithm.py +++ b/pcapkit/const/pcapng/hash_algorithm.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['HashAlgorithm'] @@ -32,6 +38,28 @@ class HashAlgorithm(EnumRegistry, IntEnum): Toeplitz = 5 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0x00 <= value <= 0xFF): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'HashAlgorithm': """Lookup function used when value is not found. diff --git a/pcapkit/const/pcapng/option_type.py b/pcapkit/const/pcapng/option_type.py index f2c42aeebb..af3ccf4d14 100644 --- a/pcapkit/const/pcapng/option_type.py +++ b/pcapkit/const/pcapng/option_type.py @@ -16,6 +16,7 @@ from aenum import StrEnum, extend_enum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError __all__ = ['OptionType'] @@ -271,6 +272,10 @@ def register(cls, value: 'int', name: 'str') -> 'OptionType': Raises: ValueError: If ``value`` is already assigned in ``name``'s namespace or in ``opt``, or if ``name`` is already taken. + EnumValueError: If ``value`` is outside the range + :meth:`_missing_` accepts, from :meth:`_validate_value`. A + :exc:`ValueError`, carrying the message the constructor raises + for the same code. """ namespace = name.split('_', maxsplit=1)[0] @@ -375,6 +380,29 @@ def get(key: 'int | str', default: 'int' = -1, *, namespace: 'str' = 'opt') -> ' # pseudo-member with the same attributes such a mint would set. return OptionType._unregistered_member(default, key) + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so :meth:`register` + refuses exactly the option codes the constructor refuses, in the same + words (:issue:`1816`). :meth:`get` never calls it: that override + answers every :class:`int` code, out of range or not, with an + unregistered member. + + Args: + value: Option code about to be minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 0xFFFF): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'OptionType': """Lookup function used when value is not found. diff --git a/pcapkit/const/pcapng/record_type.py b/pcapkit/const/pcapng/record_type.py index 1a21f8f81c..33de400c4e 100644 --- a/pcapkit/const/pcapng/record_type.py +++ b/pcapkit/const/pcapng/record_type.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['RecordType'] @@ -29,6 +35,28 @@ class RecordType(EnumRegistry, IntEnum): #: nrb_record_ipv6 nrb_record_ipv6 = 0x0002 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 0xFFFF): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'RecordType': """Lookup function used when value is not found. diff --git a/pcapkit/const/pcapng/secrets_type.py b/pcapkit/const/pcapng/secrets_type.py index 6a85b0ac28..3f76af9b7e 100644 --- a/pcapkit/const/pcapng/secrets_type.py +++ b/pcapkit/const/pcapng/secrets_type.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['SecretsType'] @@ -28,6 +34,28 @@ class SecretsType(EnumRegistry, IntEnum): ZigBee_APS_Key = 0x5a415053 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0x00000000 <= value <= 0xFFFFFFFF): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'SecretsType': """Lookup function used when value is not found. diff --git a/pcapkit/const/pcapng/tls_key_label.py b/pcapkit/const/pcapng/tls_key_label.py index 50aab2d67b..6ce78fbd72 100644 --- a/pcapkit/const/pcapng/tls_key_label.py +++ b/pcapkit/const/pcapng/tls_key_label.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import StrEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['TLSKeyLabel'] @@ -63,3 +69,27 @@ class TLSKeyLabel(EnumRegistry, StrEnum): #: ECHConfig used for construction of the ECH, c.f., :rfc:`9850#section-4.2`. ECH_CONFIG = 'ECH_CONFIG' + + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it is a :class:`str`, this registry's member type. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` so + that :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses a + value :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). There is no ``_missing_`` here to take a + range from -- the constructor refuses every value no member carries -- + so the member type is the whole check, and any :class:`str` label may + still be minted. + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` is not a :class:`str`. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not isinstance(value, str): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) diff --git a/pcapkit/const/pcapng/verdict_type.py b/pcapkit/const/pcapng/verdict_type.py index 7dd997bbd4..fa4a858df7 100644 --- a/pcapkit/const/pcapng/verdict_type.py +++ b/pcapkit/const/pcapng/verdict_type.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['VerdictType'] @@ -26,6 +32,28 @@ class VerdictType(EnumRegistry, IntEnum): Linux_eBPF_XDP = 2 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0x00 <= value <= 0xFF): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'VerdictType': """Lookup function used when value is not found. diff --git a/pcapkit/const/reg/ethertype.py b/pcapkit/const/reg/ethertype.py index 2fc74eef9a..b430cafe1e 100644 --- a/pcapkit/const/reg/ethertype.py +++ b/pcapkit/const/reg/ethertype.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['EtherType'] @@ -518,6 +524,28 @@ class EtherType(EnumRegistry, IntEnum): #: Reserved [:rfc:`1701`] Reserved = 0xFFFF + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0x0000 <= value <= 0xFFFF): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'EtherType': """Lookup function used when value is not found. diff --git a/pcapkit/const/reg/linktype.py b/pcapkit/const/reg/linktype.py index 1274f8b662..7cb663bea1 100644 --- a/pcapkit/const/reg/linktype.py +++ b/pcapkit/const/reg/linktype.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['LinkType'] @@ -752,6 +758,28 @@ class LinkType(EnumRegistry, IntEnum): #: [``DLT_IPMB_LINUX``] Legacy names (do not use) for Linux I2C below. IPMB_LINUX = 209 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0x00000000 <= value <= 0xFFFFFFFF): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'LinkType': """Lookup function used when value is not found. diff --git a/pcapkit/const/reg/transtype.py b/pcapkit/const/reg/transtype.py index cd6a700c9d..e1924c1fe9 100644 --- a/pcapkit/const/reg/transtype.py +++ b/pcapkit/const/reg/transtype.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['TransType'] @@ -496,6 +502,28 @@ class TransType(EnumRegistry, IntEnum): #: [Internet Assigned Numbers Authority] Reserved_255 = 255 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'TransType': """Lookup function used when value is not found. diff --git a/pcapkit/const/sctp/cause_code.py b/pcapkit/const/sctp/cause_code.py index dc6f09356c..a90f5e6c91 100644 --- a/pcapkit/const/sctp/cause_code.py +++ b/pcapkit/const/sctp/cause_code.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['CauseCode'] @@ -93,6 +99,28 @@ class CauseCode(EnumRegistry, IntEnum): #: Unsupported HMAC Identifier [:rfc:`4895`] Unsupported_HMAC_Identifier = 261 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 65535): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'CauseCode': """Lookup function used when value is not found. diff --git a/pcapkit/const/sctp/chunk.py b/pcapkit/const/sctp/chunk.py index 10f964f3a9..4671078e8b 100644 --- a/pcapkit/const/sctp/chunk.py +++ b/pcapkit/const/sctp/chunk.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['Chunk'] @@ -111,6 +117,28 @@ class Chunk(EnumRegistry, IntEnum): #: Reserved for IETF-defined Chunk Extensions [:rfc:`9260`] Reserved_for_IETF_defined_Chunk_Extensions_255 = 255 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'Chunk': """Lookup function used when value is not found. diff --git a/pcapkit/const/sctp/parameter.py b/pcapkit/const/sctp/parameter.py index f78d63f57f..7999c486e2 100644 --- a/pcapkit/const/sctp/parameter.py +++ b/pcapkit/const/sctp/parameter.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['Parameter'] @@ -117,6 +123,28 @@ class Parameter(EnumRegistry, IntEnum): #: Reserved for IETF-defined Chunk Extensions [:rfc:`9260`] Reserved_for_IETF_defined_Chunk_Extensions = 65535 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 65535): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'Parameter': """Lookup function used when value is not found. diff --git a/pcapkit/const/sctp/payload_protocol_identifier.py b/pcapkit/const/sctp/payload_protocol_identifier.py index 016a326ad4..ce1c95f993 100644 --- a/pcapkit/const/sctp/payload_protocol_identifier.py +++ b/pcapkit/const/sctp/payload_protocol_identifier.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['PayloadProtocolIdentifier'] @@ -266,6 +272,28 @@ class PayloadProtocolIdentifier(EnumRegistry, IntEnum): #: chunk-01] DTLS_Chunk_Key_Management_Messages = 4242 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 4294967295): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'PayloadProtocolIdentifier': """Lookup function used when value is not found. diff --git a/pcapkit/const/tcp/checksum.py b/pcapkit/const/tcp/checksum.py index 043c3ec5e1..0083156a78 100644 --- a/pcapkit/const/tcp/checksum.py +++ b/pcapkit/const/tcp/checksum.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['Checksum'] @@ -28,6 +34,28 @@ class Checksum(EnumRegistry, IntEnum): Redundant_Checksum_Avoidance = 3 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'Checksum': """Lookup function used when value is not found. diff --git a/pcapkit/const/tcp/flags.py b/pcapkit/const/tcp/flags.py index 7285a760bf..5dfa4b3691 100644 --- a/pcapkit/const/tcp/flags.py +++ b/pcapkit/const/tcp/flags.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntFlag from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['Flags'] @@ -62,6 +68,28 @@ class Flags(EnumRegistry, IntFlag): #: No more data from sender (FIN) [:rfc:`9293`] FIN = 1 << 15 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 0xFFFF): + raise EnumValueError(f'{value!r} is not a valid {cls.__name__}') + @classmethod def _missing_(cls, value: 'int') -> 'Flags': """Lookup function used when value is not found. diff --git a/pcapkit/const/tcp/mp_tcp_option.py b/pcapkit/const/tcp/mp_tcp_option.py index e2208382bf..fc5621d0f4 100644 --- a/pcapkit/const/tcp/mp_tcp_option.py +++ b/pcapkit/const/tcp/mp_tcp_option.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['MPTCPOption'] @@ -51,6 +57,28 @@ class MPTCPOption(EnumRegistry, IntEnum): #: [:rfc:`8684`] Reserved_for_Private_Use = 0xf + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'MPTCPOption': """Lookup function used when value is not found. diff --git a/pcapkit/const/tcp/option.py b/pcapkit/const/tcp/option.py index 380eebdc95..d505db6853 100644 --- a/pcapkit/const/tcp/option.py +++ b/pcapkit/const/tcp/option.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['Option'] @@ -157,6 +163,28 @@ class Option(EnumRegistry, IntEnum): #: products) [:rfc:`4727`] RFC3692_style_Experiment_2 = 254 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'Option': """Lookup function used when value is not found. diff --git a/pcapkit/const/vlan/priority_level.py b/pcapkit/const/vlan/priority_level.py index ac2038b770..81382ad84c 100644 --- a/pcapkit/const/vlan/priority_level.py +++ b/pcapkit/const/vlan/priority_level.py @@ -10,9 +10,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['PriorityLevel'] @@ -44,6 +50,28 @@ class PriorityLevel(EnumRegistry, IntEnum): #: Network control (highest) NC = 0b111 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0b000 <= value <= 0b111): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'PriorityLevel': """Lookup function used when value is not found. diff --git a/pcapkit/corekit/enum.py b/pcapkit/corekit/enum.py index 8c9b4ea37a..a11165f6fe 100644 --- a/pcapkit/corekit/enum.py +++ b/pcapkit/corekit/enum.py @@ -170,8 +170,10 @@ def _validate_value(cls, value: 'Any') -> 'None': one is only an open registry's concern. The base implementation accepts everything, because a base cannot know - any subclass's range. Overriding it is how a subclass states one -- the - shape ``_missing_`` spells by hand across the generated registries:: + any subclass's range. Overriding it is how a subclass states one, and + every registry under :mod:`pcapkit.const` does, its vendor generator + emitting the check its ``_missing_`` makes (:issue:`1816`) -- in the + shape:: @classmethod def _validate_value(cls, value: 'Any') -> 'None': diff --git a/pcapkit/vendor/default.py b/pcapkit/vendor/default.py index 2a2d73539b..7f8d609dd3 100644 --- a/pcapkit/vendor/default.py +++ b/pcapkit/vendor/default.py @@ -84,9 +84,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum{', extend_enum' if 'extend_enum(' in MISS else ''} from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['{NAME}'] @@ -96,6 +102,28 @@ class {NAME}(EnumRegistry, IntEnum): {ENUM} + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not ({FLAG}): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> '{NAME}': """Lookup function used when value is not found. diff --git a/pcapkit/vendor/ftp/command.py b/pcapkit/vendor/ftp/command.py index b31942fce9..cdfb5f194d 100644 --- a/pcapkit/vendor/ftp/command.py +++ b/pcapkit/vendor/ftp/command.py @@ -57,6 +57,7 @@ from pcapkit.corekit.enum import EnumLookup, EnumRegistry from pcapkit.corekit.sentinels import NO_DEFAULT +from pcapkit.utilities.exceptions import EnumValueError if TYPE_CHECKING: from typing import Any, Optional, Type @@ -187,6 +188,28 @@ def get(cls, key: 'Any', default: 'Any' = NO_DEFAULT) -> 'FEATCode': return member return super().get(key, default) + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it is a :class:`str`, as :meth:`_missing_` does. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with the check :meth:`_missing_` makes, and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`get` refuses, in the same words (:issue:`1816`). Any + :class:`str` passes, since :meth:`_missing_` resolves every one. + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` is not a :class:`str`. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not isinstance(value, str): + raise EnumValueError(f'{{value!r}} is not a valid {{cls.__name__}}') + @classmethod def _missing_(cls, value: 'str') -> 'FEATCode': """Lookup function used when value is not found. @@ -409,6 +432,28 @@ def get(key: 'str', default: 'Optional[str]' = None) -> '{NAME}': return {NAME}._unregistered_member(default if default is not None else key, name) return {NAME}[name] # type: ignore[misc] + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it is a :class:`str`, as :meth:`_missing_` does. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with the check :meth:`_missing_` makes, and the message it raises, so + :meth:`register` refuses a value the constructor refuses, in the same + words (:issue:`1816`). Any :class:`str` passes, since + :meth:`_missing_` resolves every one. + + Args: + value: Value about to be minted. + + Raises: + EnumValueError: If ``value`` is not a :class:`str`. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not isinstance(value, str): + raise EnumValueError(f'{{value!r}} is not a valid {{cls.__name__}}') + @classmethod def _missing_(cls, value: 'str') -> '{NAME}': """Lookup function used when value is not found. diff --git a/pcapkit/vendor/ftp/return_code.py b/pcapkit/vendor/ftp/return_code.py index 99e14d6654..d1da31cdef 100644 --- a/pcapkit/vendor/ftp/return_code.py +++ b/pcapkit/vendor/ftp/return_code.py @@ -45,9 +45,10 @@ from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError if TYPE_CHECKING: - from typing import Optional, Type + from typing import Any, Optional, Type __all__ = ['{NAME}'] @@ -72,6 +73,29 @@ class ResponseKind(EnumRegistry, IntEnum): PermanentNegativeCompletion = 5 Protected = 6 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it is a digit, which :meth:`_missing_` accepts. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with the range :meth:`_missing_` resolves, and the message the + constructor raises outside it, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` is not an :class:`int` in ``0``-``9``. + A :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 9): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'ResponseKind': """Lookup function used when value is not found. @@ -96,6 +120,29 @@ class GroupingInformation(EnumRegistry, IntEnum): Unspecified = 4 FileSystem = 5 + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it is a digit, which :meth:`_missing_` accepts. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with the range :meth:`_missing_` resolves, and the message the + constructor raises outside it, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` is not an :class:`int` in ``0``-``9``. + A :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 9): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> 'GroupingInformation': """Lookup function used when value is not found. @@ -164,6 +211,30 @@ def _unregistered_member(cls, value: 'int', name: 'str') -> '{NAME}': obj.group = GroupingInformation(int(code[1])) return obj + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it is a three-digit reply code. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with the range check :meth:`_missing_` makes, and the message it + raises, so :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses + exactly the values :meth:`~pcapkit.corekit.enum.EnumLookup.get` + refuses, in the same words (:issue:`1816`). That range is ``0``-``999`` + rather than the registry's own, since :meth:`_missing_` resolves a code + outside the registry to ``Unknown`` rather than refusing it. + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` is not an :class:`int` in + ``0``-``999``. A :exc:`ValueError`, so an ``except ValueError`` + caller is unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 999): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> '{NAME}': """Lookup function used when value is not found. diff --git a/pcapkit/vendor/http/method.py b/pcapkit/vendor/http/method.py index 0b746a6522..13f657b12b 100644 --- a/pcapkit/vendor/http/method.py +++ b/pcapkit/vendor/http/method.py @@ -39,9 +39,10 @@ from aenum import StrEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError if TYPE_CHECKING: - from typing import Optional, Type + from typing import Any, Optional, Type __all__ = ['{NAME}'] @@ -173,8 +174,9 @@ def get(cls, key: 'str', default: 'Optional[str]' = None) -> '{NAME}': Raises: ValueError: If ``key`` is not a :class:`str`. This reaches the - caller from the base's non-``str`` branch, which calls - ``cls(key)`` and so ``_missing_``, and is **not** caught by + caller from the base's non-``str`` branch, where + :meth:`_validate_value` refuses it before ``cls(key)`` could + reach ``_missing_``, and is **not** caught by the ``except KeyError`` fallback below -- only a failed *name* lookup is. Raising is the intended behaviour for ``get(b'GET')`` as well: a member whose name and @@ -199,6 +201,29 @@ def get(cls, key: 'str', default: 'Optional[str]' = None) -> '{NAME}': # passed, per GitHub issue #860's conversion away from minting. return cls._unregistered_member(default if default is not None else key, key.upper()) + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it is a :class:`str`, as :meth:`_missing_` does. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with the check :meth:`_missing_` makes, and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`get` refuses, in the same words (:issue:`1816`). Any + :class:`str` passes, since :meth:`get` and :meth:`_missing_` resolve + every one. + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` is not a :class:`str`. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not isinstance(value, str): + raise EnumValueError(f'{{value!r}} is not a valid {{cls.__name__}}') + @classmethod def _missing_(cls, value: 'str') -> '{NAME}': """Lookup function used when value is not found. diff --git a/pcapkit/vendor/http/status_code.py b/pcapkit/vendor/http/status_code.py index 004356ebbd..007f382300 100644 --- a/pcapkit/vendor/http/status_code.py +++ b/pcapkit/vendor/http/status_code.py @@ -41,9 +41,10 @@ from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError if TYPE_CHECKING: - from typing import Type + from typing import Any, Type __all__ = ['{NAME}'] @@ -93,6 +94,30 @@ def _unregistered_member(cls, value: 'int', name: 'str') -> '{NAME}': obj.message = name return obj + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it is a three-digit status code. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with the range check :meth:`_missing_` makes, and the message it + raises, so :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses + exactly the values :meth:`~pcapkit.corekit.enum.EnumLookup.get` + refuses, in the same words (:issue:`1816`). That range is ``0``-``999`` + rather than the registry's own, since :meth:`_missing_` resolves a code + outside the registry to ``Unknown`` rather than refusing it. + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` is not an :class:`int` in + ``0``-``999``. A :exc:`ValueError`, so an ``except ValueError`` + caller is unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 999): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> '{NAME}': """Lookup function used when value is not found. diff --git a/pcapkit/vendor/ipv6/extension_header.py b/pcapkit/vendor/ipv6/extension_header.py index 7fbaa01446..15c0e6eeca 100644 --- a/pcapkit/vendor/ipv6/extension_header.py +++ b/pcapkit/vendor/ipv6/extension_header.py @@ -36,9 +36,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['{NAME}'] @@ -54,6 +60,32 @@ class {NAME}(EnumRegistry, IntEnum): """ {ENUM} + + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it fits the 8-bit Next Header field. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` so + that :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses a + value no extension header can carry, as + :meth:`~pcapkit.corekit.enum.EnumLookup.get` does, in the same words + (:issue:`1816`). There is no ``_missing_`` here to take a range from + -- the constructor refuses every value no member carries -- so the + range is that of the Next Header field an extension header type is + sent in (:rfc:`8200#section-3`), and an unassigned type within it may + still be minted. + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` is not an :class:`int` in + ``0``-``255``. A :exc:`ValueError`, so an ``except + ValueError`` caller is unaffected. + + """ + if not (isinstance(value, int) and 0 <= value <= 255): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) ''' # type: Callable[[str, str, str, str], str] diff --git a/pcapkit/vendor/mh/binding_ack_flag.py b/pcapkit/vendor/mh/binding_ack_flag.py index c240c69d87..924ac41a76 100644 --- a/pcapkit/vendor/mh/binding_ack_flag.py +++ b/pcapkit/vendor/mh/binding_ack_flag.py @@ -35,9 +35,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntFlag from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['{NAME}'] @@ -47,6 +53,28 @@ class {NAME}(EnumRegistry, IntFlag): {ENUM} + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not ({FLAG}): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> '{NAME}': """Lookup function used when value is not found. diff --git a/pcapkit/vendor/mh/binding_update_flag.py b/pcapkit/vendor/mh/binding_update_flag.py index 4ac8055084..88e49a0755 100644 --- a/pcapkit/vendor/mh/binding_update_flag.py +++ b/pcapkit/vendor/mh/binding_update_flag.py @@ -34,9 +34,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntFlag from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['{NAME}'] @@ -46,6 +52,28 @@ class {NAME}(EnumRegistry, IntFlag): {ENUM} + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not ({FLAG}): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> '{NAME}': """Lookup function used when value is not found. diff --git a/pcapkit/vendor/mh/handover_ack_flag.py b/pcapkit/vendor/mh/handover_ack_flag.py index 971cdc0dc0..d2311ee496 100644 --- a/pcapkit/vendor/mh/handover_ack_flag.py +++ b/pcapkit/vendor/mh/handover_ack_flag.py @@ -35,9 +35,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntFlag from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['{NAME}'] @@ -47,6 +53,28 @@ class {NAME}(EnumRegistry, IntFlag): {ENUM} + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not ({FLAG}): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> '{NAME}': """Lookup function used when value is not found. diff --git a/pcapkit/vendor/mh/handover_initiate_flag.py b/pcapkit/vendor/mh/handover_initiate_flag.py index dd29e54f36..f312a54914 100644 --- a/pcapkit/vendor/mh/handover_initiate_flag.py +++ b/pcapkit/vendor/mh/handover_initiate_flag.py @@ -35,9 +35,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntFlag from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['{NAME}'] @@ -47,6 +53,28 @@ class {NAME}(EnumRegistry, IntFlag): {ENUM} + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not ({FLAG}): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> '{NAME}': """Lookup function used when value is not found. diff --git a/pcapkit/vendor/ngap/procedure_code.py b/pcapkit/vendor/ngap/procedure_code.py index bce363429b..d8998d5018 100644 --- a/pcapkit/vendor/ngap/procedure_code.py +++ b/pcapkit/vendor/ngap/procedure_code.py @@ -70,9 +70,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['{NAME}'] @@ -82,6 +88,28 @@ class {NAME}(EnumRegistry, IntEnum): {ENUM} + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not ({FLAG}): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> '{NAME}': """Lookup function used when value is not found. diff --git a/pcapkit/vendor/ngap/protocol_ie.py b/pcapkit/vendor/ngap/protocol_ie.py index 398f70ab78..4f09de4a34 100644 --- a/pcapkit/vendor/ngap/protocol_ie.py +++ b/pcapkit/vendor/ngap/protocol_ie.py @@ -54,9 +54,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['{NAME}'] @@ -66,6 +72,28 @@ class {NAME}(EnumRegistry, IntEnum): {ENUM} + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not ({FLAG}): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> '{NAME}': """Lookup function used when value is not found. diff --git a/pcapkit/vendor/pcapng/option_type.py b/pcapkit/vendor/pcapng/option_type.py index 4b32647bbf..fcf1dd67a3 100644 --- a/pcapkit/vendor/pcapng/option_type.py +++ b/pcapkit/vendor/pcapng/option_type.py @@ -44,6 +44,7 @@ from aenum import StrEnum, extend_enum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError __all__ = ['{NAME}'] @@ -181,6 +182,10 @@ def register(cls, value: 'int', name: 'str') -> '{NAME}': Raises: ValueError: If ``value`` is already assigned in ``name``'s namespace or in ``opt``, or if ``name`` is already taken. + EnumValueError: If ``value`` is outside the range + :meth:`_missing_` accepts, from :meth:`_validate_value`. A + :exc:`ValueError`, carrying the message the constructor raises + for the same code. """ namespace = name.split('_', maxsplit=1)[0] @@ -285,6 +290,29 @@ def get(key: 'int | str', default: 'int' = -1, *, namespace: 'str' = 'opt') -> ' # pseudo-member with the same attributes such a mint would set. return {NAME}._unregistered_member(default, key) + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so :meth:`register` + refuses exactly the option codes the constructor refuses, in the same + words (:issue:`1816`). :meth:`get` never calls it: that override + answers every :class:`int` code, out of range or not, with an + unregistered member. + + Args: + value: Option code about to be minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not ({FLAG}): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) + @classmethod def _missing_(cls, value: 'int') -> '{NAME}': """Lookup function used when value is not found. diff --git a/pcapkit/vendor/pcapng/tls_key_label.py b/pcapkit/vendor/pcapng/tls_key_label.py index da9841be99..4fa2970953 100644 --- a/pcapkit/vendor/pcapng/tls_key_label.py +++ b/pcapkit/vendor/pcapng/tls_key_label.py @@ -50,9 +50,15 @@ """ +from typing import TYPE_CHECKING + from aenum import StrEnum from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['{NAME}'] @@ -67,6 +73,30 @@ class {NAME}(EnumRegistry, StrEnum): """ {ENUM} + + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it is a :class:`str`, this registry's member type. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` so + that :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses a + value :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). There is no ``_missing_`` here to take a + range from -- the constructor refuses every value no member carries -- + so the member type is the whole check, and any :class:`str` label may + still be minted. + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` is not a :class:`str`. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not isinstance(value, str): + raise EnumValueError('%r is not a valid %s' % (value, cls.__name__)) ''' # type: Callable[[str, str, str, str], str] diff --git a/pcapkit/vendor/tcp/flags.py b/pcapkit/vendor/tcp/flags.py index c3862ebec3..f3c7e6fe5a 100644 --- a/pcapkit/vendor/tcp/flags.py +++ b/pcapkit/vendor/tcp/flags.py @@ -47,9 +47,15 @@ """ +from typing import TYPE_CHECKING + from aenum import IntFlag from pcapkit.corekit.enum import EnumRegistry +from pcapkit.utilities.exceptions import EnumValueError + +if TYPE_CHECKING: + from typing import Any __all__ = ['{NAME}'] @@ -65,6 +71,28 @@ class {NAME}(EnumRegistry, IntFlag): {ENUM} + @classmethod + def _validate_value(cls, value: 'Any') -> 'None': + """Reject ``value`` unless it passes the range check :meth:`_missing_` makes. + + Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` + with that check and the message it raises, so + :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the + values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the + same words (:issue:`1816`). + + Args: + value: Value about to be looked up or minted. + + Raises: + EnumValueError: If ``value`` fails that check. A + :exc:`ValueError`, so an ``except ValueError`` caller is + unaffected. + + """ + if not ({FLAG}): + raise EnumValueError(f'{{value!r}} is not a valid {{cls.__name__}}') + @classmethod def _missing_(cls, value: 'int') -> '{NAME}': """Lookup function used when value is not found. diff --git a/tests/const/test_const_enum_no_mint.py b/tests/const/test_const_enum_no_mint.py index e79e4b7247..d12f40860c 100644 --- a/tests/const/test_const_enum_no_mint.py +++ b/tests/const/test_const_enum_no_mint.py @@ -698,6 +698,36 @@ def _purge_member(cls: 'type', name: 'str', value: 'int') -> 'None': cls._value2member_map_.pop(value, None) # type: ignore[attr-defined] +def _mintable_value(cls: 'type', preferred: 'int') -> 'Optional[int]': + """``preferred`` if ``cls.register`` may mint it, else the highest value it may. + + ``register`` refuses what ``_validate_value`` refuses (#1816). Every + registry this is used on checks a range ``0`` to some top, so the top is + found by bisection on that hook, then stepped down past any member. + :obj:`None` if every value in range already has a member. + + """ + def legal(value: 'int') -> 'bool': + try: + cls._validate_value(value) # type: ignore[attr-defined] + except ValueError: + return False + return True + + if legal(preferred) and preferred not in cls._value2member_map_: # type: ignore[attr-defined] + return preferred + low, high = 0, preferred + while low < high: + mid = (low + high + 1) // 2 + if legal(mid): + low = mid + else: + high = mid - 1 + while low in cls._value2member_map_: # type: ignore[attr-defined] + low -= 1 + return low if legal(low) else None + + class UnassignedRangeDoesNotMintTests(unittest.TestCase): """A bounded-but-unassigned value must not grow the registry.""" @@ -845,17 +875,32 @@ def test_register_actually_mints_on_every_one_of_the_105_registries(self) -> Non on every one of tier 1's 105 registries, the full set the shared template now reaches (:data:`ALL_105_REGISTRIES`), not only the 22 with a bounded-unassigned range. A high, fixed value is deliberately - outside any registry's assigned codes -- most bound themselves to 16 - bits or fewer -- so this cannot collide with anything either the CSV - seeded or a sibling test minted. + outside any registry's assigned codes, so this cannot collide with + anything either the CSV seeded or a sibling test minted. + + Since #1816, ``register`` refuses a value outside the range a + registry's ``_missing_`` checks, and most bound themselves to 16 bits + or fewer. So the fixed value is used where the range reaches it, and + elsewhere the highest value in range that no member carries. A + registry whose whole range is assigned has nothing left to mint, and + must refuse the fixed value instead. """ - value = 0x6E7A0001 # arbitrary, outside every registry's own domain + from pcapkit.utilities.exceptions import EnumValueError + name = 'PyPCAPKit_775_sweep' + preferred = 0x6E7A0001 # arbitrary, outside most registries' own domains for module_name, class_name in ALL_105_REGISTRIES: with self.subTest(registry=class_name): cls = getattr(importlib.import_module(module_name), class_name) + value = _mintable_value(cls, preferred) + if value is None: + before = len(cls.__members__) + with self.assertRaises(EnumValueError): + cls.register(preferred, name) + self.assertEqual(len(cls.__members__), before) + continue self.addCleanup(_purge_member, cls, name, value) before = len(cls.__members__) diff --git a/tests/const/test_const_register_validates_every_registry_1816_unit.py b/tests/const/test_const_register_validates_every_registry_1816_unit.py new file mode 100644 index 0000000000..11bfa5f813 --- /dev/null +++ b/tests/const/test_const_register_validates_every_registry_1816_unit.py @@ -0,0 +1,236 @@ +# -*- coding: utf-8 -*- +"""Every registry's ``register`` refuses what its ``get`` refuses. + +GitHub issue #1816. :meth:`~pcapkit.corekit.enum.EnumRegistry.register` routes a +new value through :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value`, but +only ``AppType`` (#1792) overrode that hook, so everywhere else ``register`` +minted a value ``get`` refuses: ``Hardware.register(70000, 'x')`` minted, and +``Hardware.get(70000)`` then returned the new member. Every registry now overrides +the hook. A generated one uses the check its ``_missing_`` makes; ``TLSKeyLabel`` +and ``ExtensionHeader`` have no ``_missing_``, so they check the member type and +the 8-bit Next Header field respectively. + +The registries are found at runtime, after importing every module under +:mod:`pcapkit.const` and :mod:`pcapkit.protocols`, so one added later is covered +without editing this file. + +What counts as out of range does not come from the override under test. It is a +negative number, a number wider than any field, a float, ``None`` and ``bytes`` +for every registry, plus a ``str`` for one that does not hold strings. +``ProtectionAuthority`` has no upper bound, so its ``get`` resolves the wide +number and ``register`` must mint it: the comparison runs in both directions. + +The oracle is ``get`` with no default, except where ``get`` is a +:class:`staticmethod` backport that bypasses the base lookup. ``OptionType``'s +answers every integer code with an unregistered member, and ``Command``'s calls +``key.upper()`` and so answers :class:`bytes` too. There the constructor is the +oracle instead; ``OptionType``'s ``_missing_`` refusing anything above +``0xFFFF`` is what the issue cites. + +Each test imports the registries after +:func:`~tests._support.reimport_once_per_class`, whose ``restore`` puts the class's +import back afterwards, so a member minted here does not leak. + +""" +from __future__ import annotations + +import importlib +import inspect +import itertools +import pkgutil +import unittest +from typing import TYPE_CHECKING + +from tests._support import reimport_once_per_class + +if TYPE_CHECKING: + from typing import Any + +#: Serial for probe names, so that no refusal is a name collision instead. +_SERIAL = itertools.count() + +#: Out of range for every registry: below zero, wider than any field, and not +#: an integer or string at all. +OUT_OF_RANGE = (-1, 2 ** 130, 1.5, None, b'\x01') + +#: Bounds of the hooks that do not copy a ``FLAG``, which nothing else here pins: +#: ``(module, registry, accepted, refused)``. ``ExtensionHeader`` and +#: ``TLSKeyLabel`` have no ``_missing_`` to compare against, and that of +#: ``ResponseKind`` and ``GroupingInformation`` falls through rather than +#: raising outside its range. +PINNED = ( + ('pcapkit.const.ipv6.extension_header', 'ExtensionHeader', (0, 255), (-1, 256, 1.5, '0')), + ('pcapkit.const.pcapng.tls_key_label', 'TLSKeyLabel', ('PYPCAPKIT_1816_LABEL', ''), + (0, -1, None, b'CLIENT_RANDOM')), + ('pcapkit.const.ftp.return_code', 'ResponseKind', (0, 9), (-1, 10, '9')), + ('pcapkit.const.ftp.return_code', 'GroupingInformation', (0, 9), (-1, 10, '9')), +) + +#: The registry whose ``_missing_`` refuses every value: it has no transport. +APPTYPE = 'pcapkit.const.reg.apptype.apptype.AppType' + +#: Out of range for a registry that does not hold strings. +NOT_AN_INT = 'pypcapkit-1816-probe' + +#: Around the edges of the field widths and digit counts the registries use. +BOUNDARIES = tuple(sorted({ + *(2 ** k + d for k in (1, 2, 3, 4, 7, 8, 16, 32, 128) for d in (-1, 0)), + -1, 0, 9, 10, 599, 600, 659, 660, 999, 1000, +})) + + +def _registries() -> 'list[type]': + """Every :class:`~pcapkit.corekit.enum.EnumRegistry` subclass, found at runtime.""" + for name in ('pcapkit.const', 'pcapkit.protocols'): + package = importlib.import_module(name) + for info in pkgutil.walk_packages(package.__path__, f'{name}.'): + importlib.import_module(info.name) + + from pcapkit.corekit.enum import EnumRegistry + + found = [] # type: list[type] + stack = [EnumRegistry] + while stack: + for sub in stack.pop().__subclasses__(): + if sub not in found: + found.append(sub) + stack.append(sub) + return sorted(found, key=lambda cls: (cls.__module__, cls.__qualname__)) + + +def _members(cls: 'type') -> 'dict[str, object]': + return dict(cls.__members__) # type: ignore[attr-defined] + + +def _label(cls: 'type') -> 'str': + return f'{cls.__module__}.{cls.__qualname__}' + + +def _raises(call: 'Any', *args: 'Any') -> 'BaseException | None': + try: + call(*args) + except Exception as error: # pylint: disable=broad-exception-caught + return error + return None + + +def _oracle(cls: 'type', value: 'object') -> 'BaseException | None': + """Whether a lookup of ``value`` on ``cls`` is refused, and with what.""" + if isinstance(inspect.getattr_static(cls, 'get'), staticmethod): + return _raises(cls, value) + return _raises(cls.get, value) # type: ignore[attr-defined] + + +def _resolves_through_missing(cls: 'type') -> 'bool': + """Whether ``cls`` declares a ``_missing_``, on itself or a registry base.""" + from pcapkit.corekit.enum import EnumRegistry + + return any('_missing_' in vars(base) for base in cls.__mro__[:cls.__mro__.index(EnumRegistry)]) + + +class RegisterValidatesEveryRegistry1816Tests(unittest.TestCase): + """``register`` and ``get`` agree on every registry about what is out of range.""" + + def setUp(self) -> None: + reimport_once_per_class(self, restore=True) + + def test_the_walk_finds_the_registries(self) -> None: + labels = {_label(cls) for cls in _registries()} + for expected in ('pcapkit.const.arp.hardware.Hardware', + 'pcapkit.const.pcapng.option_type.OptionType', + 'pcapkit.const.ftp.return_code.ResponseKind', + 'pcapkit.const.reg.apptype.tcp.TCP'): + self.assertIn(expected, labels) + + def test_register_refuses_an_out_of_range_value_iff_get_does(self) -> None: + for cls in _registries(): + probes = OUT_OF_RANGE if issubclass(cls, str) else (*OUT_OF_RANGE, NOT_AN_INT) + for value in probes: + if value in cls._value2member_map_: # type: ignore[attr-defined] + continue + with self.subTest(registry=_label(cls), value=value): + refused = _oracle(cls, value) + members = _members(cls) + error = _raises(cls.register, # type: ignore[attr-defined] + value, f'pypcapkit_1816_probe_{next(_SERIAL)}') + self.assertEqual(error is not None, refused is not None, + f'get {"refused" if refused else "accepted"} {value!r} ' + f'but register {"refused" if error else "minted"} it') + if error is not None: + self.assertIsInstance(error, ValueError) + self.assertEqual(_members(cls), members) + self.assertIsNotNone(_oracle(cls, value)) + + def test_validate_value_mirrors_the_range_check_of_missing(self) -> None: + # NOTE: one-way checks, each direction on its own, rather than an + # equivalence. A value inside the range that matches neither a member + # nor an unassigned range -- ``CauseCode(0)``, ``MPTCPOption(16)`` -- + # falls through ``_missing_``, so the constructor refuses it although the + # range check passed; such a value is ``register``'s to mint, and is not + # out of range. ``ResponseKind`` and ``GroupingInformation`` fall through + # the same way outside their range rather than raising, so the second + # check never reaches them; the pinned bounds below do. ``AppType`` + # itself is skipped: with no transport, its ``_missing_`` and its + # ``register`` both refuse every value, which the test above covers. + from pcapkit.utilities.exceptions import EnumValueError + + for cls in _registries(): + if not _resolves_through_missing(cls) or _label(cls) == APPTYPE: + continue + for value in (*BOUNDARIES, NOT_AN_INT, 1.5, None, b'\x01'): + if value in cls._value2member_map_: # type: ignore[attr-defined] + continue + with self.subTest(registry=_label(cls), value=value): + checked = _raises(cls._validate_value, value) # type: ignore[attr-defined] + if checked is None: + self.assertIsNone(cls._validate_value(value)) # type: ignore[attr-defined] + else: + self.assertIs(type(checked), EnumValueError) + self.assertEqual(str(checked), f'{value!r} is not a valid {cls.__name__}') + self.assertIsNotNone(_raises(cls, value), + 'the hook refuses a value the constructor resolves') + missing = _raises(cls._missing_, value) # type: ignore[attr-defined] + if missing is not None: + self.assertIsNotNone(checked, + '_missing_ refuses a value the hook lets through') + self.assertEqual(str(checked), str(missing)) + + def test_every_registered_value_passes_validate_value(self) -> None: + for cls in _registries(): + for member in cls: # type: ignore[attr-defined] + # NOTE: ``OptionType`` and ``AppType`` hold a formatted or service + # string as the value; what their hook guards is the option code + # or port the member carries beside it. + code = getattr(member, 'opt_value', getattr(member, 'port', member._value_)) + hook = cls._validate_value # type: ignore[attr-defined] + with self.subTest(registry=_label(cls), member=member._name_): + # NOTE: called directly rather than through ``_raises``, so + # that a hook returning anything but ``None`` fails as well. + self.assertIsNone(hook(code)) + + def test_the_bounds_no_flag_pins(self) -> None: + from pcapkit.utilities.exceptions import EnumValueError + + for module, name, accepted, refused in PINNED: + cls = getattr(importlib.import_module(module), name) + for value in accepted: + with self.subTest(registry=name, value=value, expect='accepted'): + self.assertIsNone(cls._validate_value(value)) + if value not in cls._value2member_map_: + member = cls.register(value, f'pypcapkit_1816_pin_{next(_SERIAL)}') + self.assertIs(cls.get(value), member) + for value in refused: + with self.subTest(registry=name, value=value, expect='refused'): + with self.assertRaises(EnumValueError) as caught: + cls._validate_value(value) + self.assertIs(type(caught.exception), EnumValueError) + self.assertEqual(str(caught.exception), f'{value!r} is not a valid {name}') + members = _members(cls) + with self.assertRaises(EnumValueError) as minted: + cls.register(value, f'pypcapkit_1816_pin_{next(_SERIAL)}') + self.assertIs(type(minted.exception), EnumValueError) + self.assertEqual(_members(cls), members) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/project/test_rfc_anchor_fragments.py b/tests/project/test_rfc_anchor_fragments.py index 670a0715a0..27ec828537 100644 --- a/tests/project/test_rfc_anchor_fragments.py +++ b/tests/project/test_rfc_anchor_fragments.py @@ -26,8 +26,8 @@ The sweep this test is built on also turned up a **second** instance of the same defect: ``:rfc:`5797#secion-2.2``` in the ``feat`` attribute docstring of the ``Command`` class (not ``FEATCode``), at -:file:`pcapkit/vendor/ftp/command.py:273` and its generated copy -:file:`pcapkit/const/ftp/command.py:261`. Same missing ``t``, same two files, +:file:`pcapkit/vendor/ftp/command.py:296` and its generated copy +:file:`pcapkit/const/ftp/command.py:284`. Same missing ``t``, same two files, same one-character fix -- fixed alongside #942's rather than carried as a documented exception. diff --git a/tests/protocols/internet/test_mh_unit.py b/tests/protocols/internet/test_mh_unit.py index eed1841068..238cfe8d91 100644 --- a/tests/protocols/internet/test_mh_unit.py +++ b/tests/protocols/internet/test_mh_unit.py @@ -1853,7 +1853,7 @@ def wrap_in_ipv4(mh_payload: bytes) -> IPv4: with self.subTest('over IPv6, the raise now costs only the MH slot (GitHub issue #891)'): # Mobility Header is *also* one of IPv6's own chained extension # headers (``Enum_ExtensionHeader.Mobility_Header``, value 135 -- - # ``pcapkit/const/ipv6/extension_header.py:49``), so IPv6 never + # ``pcapkit/const/ipv6/extension_header.py:55``), so IPv6 never # reaches the fallback through the base ``_import_next_layer`` at # all -- it walks its own ``@beholder``-decorated override # (``pcapkit/protocols/internet/ipv6.py:_import_next_layer``). diff --git a/tests/test_docstring_contract.py b/tests/test_docstring_contract.py index fad63c0248..d26df326e6 100644 --- a/tests/test_docstring_contract.py +++ b/tests/test_docstring_contract.py @@ -231,7 +231,7 @@ class Known(NamedTuple): "and holds CSV rows. Nothing was renamed here despite the " '``# pylint: disable=arguments-renamed`` pragma -- the parameter ' 'matches the base ``Vendor.context(self, data)`` in ' - 'pcapkit/vendor/default.py:417, which documents it as ' + 'pcapkit/vendor/default.py:445, which documents it as ' "'data: CSV data.'. The line was copy-pasted from a sibling whose " "process()/context() really does take 'soup', so both the name and " 'the description are wrong'), diff --git a/tests/vendor/test_re_sub_positional_flag_unit.py b/tests/vendor/test_re_sub_positional_flag_unit.py index 40ac281fb0..380ad26cb3 100644 --- a/tests/vendor/test_re_sub_positional_flag_unit.py +++ b/tests/vendor/test_re_sub_positional_flag_unit.py @@ -379,7 +379,7 @@ def _assert_no_deprecation(self, module_name: 'str', class_name: 'str', instance.process(rows) # type: ignore[attr-defined] def test_default_process_no_deprecation(self) -> None: - # pcapkit/vendor/default.py:348 -- the shared base implementation, + # pcapkit/vendor/default.py:376 -- the shared base implementation, # inherited by every crawler that does not override process(), e.g. # pcapkit.vendor.arp.hardware.Hardware. self._assert_no_deprecation( diff --git a/tests/vendor/test_vendor_missing_body_unit.py b/tests/vendor/test_vendor_missing_body_unit.py index c3031e9e78..443ca0e612 100644 --- a/tests/vendor/test_vendor_missing_body_unit.py +++ b/tests/vendor/test_vendor_missing_body_unit.py @@ -22,7 +22,7 @@ The invariant is deliberately not "every emitted line returns": the base crawler itself emits legitimate non-returning lines for a range-guarded body. :meth:`pcapkit.vendor.default.Vendor.process` (see -:file:`pcapkit/vendor/default.py:397-399`) renders such a body as three +:file:`pcapkit/vendor/default.py:425-427`) renders such a body as three lines -- an ``if`` header, a ``#:`` comment, then an indented ``return`` -- and only the third of the three returns::