Skip to content

Commit 027f7d5

Browse files
authored
fix(hip): resolve a str enum name in list-argument makers too (#2401)
Follow-up to #2356/#2393. That fix covered scalar _make_index arguments whose own default is None, so the namespace couldn't be inferred. A separate 12-site family in hip.py has the same symptom but a different shape: the str name is a loop variable over a list parameter (e.g. _make_param_dh_group_list(groups=['Group_384_bit_group'])), so the member and int forms of the same call already worked while the str-name form raised ProtocolNotImplemented. Each of the 12 makers gets `if X_namespace is None: X_namespace = Enum_Y` hoisted above its build loop, next to the existing `if X is None: X = []` -- one decision per parameter rather than one per item, and every original _make_index call line stays unchanged (an inline conditional at that indentation pushed 10 of 12 lines past 120 columns). _make_param_transport_format_list's list holds parameter codes, so its namespace is Enum_Parameter -- the same enum its own code argument carries, not a namespace of its own. _make_param_reg_failed's scalar sibling failure_type already resolved by inference (its default is a real enum member); left alone, since the two arguments there now resolve against different enums. 5 of the 9 enums these makers need (Enum_Suite, Enum_NATTraversal, Enum_Registration, Enum_ESPTransformSuite, Enum_Transport) were imported only under TYPE_CHECKING, next to Enum_RegistrationFailure from the same pcapkit.const.hip package already imported at runtime. Promoted all 5: pcapkit/const/hip/*.py never imports from pcapkit/protocols, so this cannot create a cycle -- verified with a cold import of pcapkit.protocols.internet.hip as the very first import, not just `import pcapkit`. Tests: tests/protocols/internet/test_hip_list_argument_str_names_2394_unit.py, 7 tests / 17 subtests -- str/member/int parity for all 12 sites, a mixed list, the scalar sibling's inference, an explicit namespace override, an unknown-name raise, the 5 promoted enums, and an AST walk asserting every list-argument _make_index call in hip.py has a covering case.
1 parent 4a14097 commit 027f7d5

2 files changed

Lines changed: 421 additions & 5 deletions

File tree

‎pcapkit/protocols/internet/hip.py‎

Lines changed: 92 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,18 @@
4343
from pcapkit.const.hip.ecdsa_curve import ECDSACurve as Enum_ECDSACurve
4444
from pcapkit.const.hip.ecdsa_low_curve import ECDSALowCurve as Enum_ECDSALowCurve
4545
from pcapkit.const.hip.eddsa_curve import EdDSACurve as Enum_EdDSACurve
46+
from pcapkit.const.hip.esp_transform_suite import ESPTransformSuite as Enum_ESPTransformSuite
4647
from pcapkit.const.hip.group import Group as Enum_Group
4748
from pcapkit.const.hip.hi_algorithm import HIAlgorithm as Enum_HIAlgorithm
4849
from pcapkit.const.hip.hit_suite import HITSuite as Enum_HITSuite
50+
from pcapkit.const.hip.nat_traversal import NATTraversal as Enum_NATTraversal
4951
from pcapkit.const.hip.notify_message import NotifyMessage as Enum_NotifyMessage
5052
from pcapkit.const.hip.packet import Packet as Enum_Packet
5153
from pcapkit.const.hip.parameter import Parameter as Enum_Parameter
54+
from pcapkit.const.hip.registration import Registration as Enum_Registration
5255
from pcapkit.const.hip.registration_failure import RegistrationFailure as Enum_RegistrationFailure
56+
from pcapkit.const.hip.suite import Suite as Enum_Suite
57+
from pcapkit.const.hip.transport import Transport as Enum_Transport
5358
from pcapkit.const.reg.transtype import TransType as Enum_TransType
5459
from pcapkit.corekit.fields.ipaddress import parse_ip_address
5560
from pcapkit.corekit.multidict import OrderedMultiDict
@@ -216,11 +221,6 @@
216221
from mypy_extensions import DefaultArg, KwArg, NamedArg
217222
from typing_extensions import Literal, NotRequired
218223

219-
from pcapkit.const.hip.esp_transform_suite import ESPTransformSuite as Enum_ESPTransformSuite
220-
from pcapkit.const.hip.nat_traversal import NATTraversal as Enum_NATTraversal
221-
from pcapkit.const.hip.registration import Registration as Enum_Registration
222-
from pcapkit.const.hip.suite import Suite as Enum_Suite
223-
from pcapkit.const.hip.transport import Transport as Enum_Transport
224224
from pcapkit.corekit.protochain import ProtoChain
225225
from pcapkit.protocols.protocol import ProtocolBase
226226
from pcapkit.protocols.schema.internet.hip import Parameter as Schema_Parameter
@@ -3893,6 +3893,13 @@ def _make_param_dh_group_list(self, code: 'Enum_Parameter', param: 'Optional[Dat
38933893
if groups is None:
38943894
groups = []
38953895

3896+
# NOTE: ``group`` is a loop variable rather than an argument of this
3897+
# maker, so ``_make_index`` has no argument default to infer the
3898+
# namespace of a :obj:`str` member name from; name it here rather than
3899+
# leave the name unresolvable (#2394).
3900+
if group_namespace is None:
3901+
group_namespace = Enum_Group
3902+
38963903
group_id = []
38973904
for group in groups:
38983905
group_id.append(self._make_index(group, group_default, namespace=group_namespace, # type: ignore[arg-type]
@@ -3986,6 +3993,13 @@ def _make_param_hip_transform(self, code: 'Enum_Parameter', param: 'Optional[Dat
39863993
if suites is None:
39873994
suites = []
39883995

3996+
# NOTE: ``suite`` is a loop variable rather than an argument of this
3997+
# maker, so ``_make_index`` has no argument default to infer the
3998+
# namespace of a :obj:`str` member name from; name it here rather than
3999+
# leave the name unresolvable (#2394).
4000+
if suite_namespace is None:
4001+
suite_namespace = Enum_Suite
4002+
39894003
suite_id = []
39904004
for suite in suites:
39914005
suite_id.append(self._make_index(suite, suite_default, namespace=suite_namespace, # type: ignore[arg-type]
@@ -4032,6 +4046,13 @@ def _make_param_hip_cipher(self, code: 'Enum_Parameter', param: 'Optional[Data_H
40324046
if ciphers is None:
40334047
ciphers = []
40344048

4049+
# NOTE: ``cipher`` is a loop variable rather than an argument of this
4050+
# maker, so ``_make_index`` has no argument default to infer the
4051+
# namespace of a :obj:`str` member name from; name it here rather than
4052+
# leave the name unresolvable (#2394).
4053+
if cipher_namespace is None:
4054+
cipher_namespace = Enum_Cipher
4055+
40354056
cipher_id = []
40364057
for cipher in ciphers:
40374058
cipher_id.append(self._make_index(cipher, cipher_default, namespace=cipher_namespace, # type: ignore[arg-type]
@@ -4076,6 +4097,13 @@ def _make_param_nat_traversal_mode(self, code: 'Enum_Parameter', param: 'Optiona
40764097
if modes is None:
40774098
modes = []
40784099

4100+
# NOTE: ``mode`` is a loop variable rather than an argument of this
4101+
# maker, so ``_make_index`` has no argument default to infer the
4102+
# namespace of a :obj:`str` member name from; name it here rather than
4103+
# leave the name unresolvable (#2394).
4104+
if mode_namespace is None:
4105+
mode_namespace = Enum_NATTraversal
4106+
40794107
mode_id = []
40804108
for mode in modes:
40814109
mode_id.append(self._make_index(mode, mode_default, namespace=mode_namespace, # type: ignore[arg-type]
@@ -4343,6 +4371,13 @@ def _make_param_hit_suite_list(self, code: 'Enum_Parameter', param: 'Optional[Da
43434371
if suites is None:
43444372
suites = []
43454373

4374+
# NOTE: ``suite`` is a loop variable rather than an argument of this
4375+
# maker, so ``_make_index`` has no argument default to infer the
4376+
# namespace of a :obj:`str` member name from; name it here rather than
4377+
# leave the name unresolvable (#2394).
4378+
if suite_namespace is None:
4379+
suite_namespace = Enum_HITSuite
4380+
43464381
suite_id = []
43474382
for suite in suites:
43484383
suite_id.append(self._make_index(suite, suite_default, namespace=suite_namespace, # type: ignore[arg-type]
@@ -4546,6 +4581,13 @@ def _make_param_reg_info(self, code: 'Enum_Parameter', param: 'Optional[Data_Reg
45464581
if reg_info is None:
45474582
reg_info = []
45484583

4584+
# NOTE: ``reg`` is a loop variable rather than an argument of this
4585+
# maker, so ``_make_index`` has no argument default to infer the
4586+
# namespace of a :obj:`str` member name from; name it here rather than
4587+
# leave the name unresolvable (#2394).
4588+
if reg_info_namespace is None:
4589+
reg_info_namespace = Enum_Registration
4590+
45494591
reg_type = []
45504592
for reg in reg_info:
45514593
reg_type.append(self._make_index(reg, reg_info_default, namespace=reg_info_namespace, # type: ignore[arg-type]
@@ -4610,6 +4652,13 @@ def _make_param_reg_request(self, code: 'Enum_Parameter', param: 'Optional[Data_
46104652
if reg_request is None:
46114653
reg_request = []
46124654

4655+
# NOTE: ``reg`` is a loop variable rather than an argument of this
4656+
# maker, so ``_make_index`` has no argument default to infer the
4657+
# namespace of a :obj:`str` member name from; name it here rather than
4658+
# leave the name unresolvable (#2394).
4659+
if reg_request_namespace is None:
4660+
reg_request_namespace = Enum_Registration
4661+
46134662
reg_type = []
46144663
for reg in reg_request:
46154664
reg_type.append(self._make_index(reg, reg_request_default, namespace=reg_request_namespace, # type: ignore[arg-type]
@@ -4672,6 +4721,13 @@ def _make_param_reg_response(self, code: 'Enum_Parameter', param: 'Optional[Data
46724721
if reg_response is None:
46734722
reg_response = []
46744723

4724+
# NOTE: ``reg`` is a loop variable rather than an argument of this
4725+
# maker, so ``_make_index`` has no argument default to infer the
4726+
# namespace of a :obj:`str` member name from; name it here rather than
4727+
# leave the name unresolvable (#2394).
4728+
if reg_response_namespace is None:
4729+
reg_response_namespace = Enum_Registration
4730+
46754731
reg_type = []
46764732
for reg in reg_response:
46774733
reg_type.append(self._make_index(reg, reg_response_default, namespace=reg_response_namespace, # type: ignore[arg-type]
@@ -4733,6 +4789,15 @@ def _make_param_reg_failed(self, code: 'Enum_Parameter', param: 'Optional[Data_R
47334789
if reg_failed is None:
47344790
reg_failed = []
47354791

4792+
# NOTE: ``reg`` is a loop variable rather than an argument of this
4793+
# maker, so ``_make_index`` has no argument default to infer the
4794+
# namespace of a :obj:`str` member name from; name it here rather than
4795+
# leave the name unresolvable (#2394). ``failure_type`` above is a
4796+
# scalar whose own default is an enumeration member, so it has been
4797+
# resolving a member name by inference all along.
4798+
if reg_failed_namespace is None:
4799+
reg_failed_namespace = Enum_Registration
4800+
47364801
reg_type = []
47374802
for reg in reg_failed:
47384803
reg_type.append(self._make_index(reg, reg_failed_default, namespace=reg_failed_namespace, # type: ignore[arg-type]
@@ -4854,6 +4919,14 @@ def _make_param_transport_format_list(self, code: 'Enum_Parameter', param: 'Opti
48544919
if formats is None:
48554920
formats = []
48564921

4922+
# NOTE: ``tf`` is a loop variable rather than an argument of this
4923+
# maker, so ``_make_index`` has no argument default to infer the
4924+
# namespace of a :obj:`str` member name from; name it here rather than
4925+
# leave the name unresolvable (#2394). A transport format is a
4926+
# parameter code, so the namespace is ``Enum_Parameter`` itself.
4927+
if format_namespace is None:
4928+
format_namespace = Enum_Parameter
4929+
48574930
tf_type = []
48584931
for tf in formats:
48594932
tf_type.append(self._make_index(tf, format_default, namespace=format_namespace, # type: ignore[arg-type]
@@ -4898,6 +4971,13 @@ def _make_param_esp_transform(self, code: 'Enum_Parameter', param: 'Optional[Dat
48984971
if suites is None:
48994972
suites = []
49004973

4974+
# NOTE: ``suite`` is a loop variable rather than an argument of this
4975+
# maker, so ``_make_index`` has no argument default to infer the
4976+
# namespace of a :obj:`str` member name from; name it here rather than
4977+
# leave the name unresolvable (#2394).
4978+
if suite_namespace is None:
4979+
suite_namespace = Enum_ESPTransformSuite
4980+
49014981
suite_id = []
49024982
for suite in suites:
49034983
suite_id.append(self._make_index(suite, suite_default, namespace=suite_namespace, # type: ignore[arg-type]
@@ -5228,6 +5308,13 @@ def _make_param_hip_transport_mode(self, code: 'Enum_Parameter', param: 'Optiona
52285308
if modes is None:
52295309
modes = []
52305310

5311+
# NOTE: ``mode`` is a loop variable rather than an argument of this
5312+
# maker, so ``_make_index`` has no argument default to infer the
5313+
# namespace of a :obj:`str` member name from; name it here rather than
5314+
# leave the name unresolvable (#2394).
5315+
if mode_namespace is None:
5316+
mode_namespace = Enum_Transport
5317+
52315318
mode_id = []
52325319
for mode in modes:
52335320
mode_id.append(self._make_index(mode, mode_default, namespace=mode_namespace, # type: ignore[arg-type]

0 commit comments

Comments
 (0)