Skip to content

Commit 376bca2

Browse files
authored
fix(protocols): export Protocol from pcapkit.protocols.protocol (#1770) (#1778)
- protocol.py: __all__ listed only ProtocolBase, so a star-import did not bind Protocol, the module's user-facing class. It now lists both. ProtocolMeta stays out, as protocol.rst files it under Internal Definitions. - The registry error still names Protocol while the guard tests ProtocolBase. That gap is deliberate: the message's reader is a third party, who should subclass the public class (#1016). - New test_protocol_module_exports_1770_unit.py pins __all__ and the star-import, and that a non-Protocol built-in passes the guard. Narrow runs and tests/project (930 passed, 1 skipped) pass. Closes #1770
1 parent 7c3ed8a commit 376bca2

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

‎pcapkit/protocols/protocol.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575

7676
from pcapkit.corekit.context import ProtocolContext
7777

78-
__all__ = ['ProtocolBase']
78+
__all__ = ['Protocol', 'ProtocolBase']
7979

8080
_PT = TypeVar('_PT', bound='Data')
8181
_ST = TypeVar('_ST', bound='Schema')
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# -*- coding: utf-8 -*-
2+
"""GitHub issue #1770: :mod:`pcapkit.protocols.protocol` exports ``Protocol``.
3+
4+
The module's :attr:`__all__` was ``['ProtocolBase']``, so ``from
5+
pcapkit.protocols.protocol import *`` did not bind
6+
:class:`~pcapkit.protocols.protocol.Protocol`, the class its docstring and
7+
:file:`protocol.rst` present as the module's user-facing one. It is listed now.
8+
9+
"""
10+
11+
import importlib.util
12+
import unittest
13+
from unittest import mock
14+
15+
RUNTIME_DEPS = ('tbtrim', 'aenum', 'chardet', 'dictdumper')
16+
HAS_RUNTIME = all(importlib.util.find_spec(name) is not None for name in RUNTIME_DEPS)
17+
18+
19+
@unittest.skipUnless(HAS_RUNTIME, 'runtime dependencies not installed')
20+
class RegistryGuardWidthTests(unittest.TestCase):
21+
"""The registry guard is wider than its error message, on purpose."""
22+
23+
def test_the_guard_accepts_a_protocol_base_that_is_not_a_protocol(self) -> None:
24+
# The guard tests ProtocolBase because every built-in derives from it
25+
# and none from Protocol. The message still names Protocol, since its
26+
# reader is a third party who should subclass the public class (#1016).
27+
from pcapkit.protocols.misc.raw import Raw
28+
from pcapkit.protocols.protocol import Protocol, ProtocolBase
29+
30+
self.assertFalse(issubclass(Raw, Protocol))
31+
with mock.patch.dict(ProtocolBase.__proto__):
32+
ProtocolBase.register(1, Raw)
33+
self.assertIs(ProtocolBase.__proto__[1], Raw)
34+
35+
36+
@unittest.skipUnless(HAS_RUNTIME, 'runtime dependencies not installed')
37+
class ProtocolModuleAllTests(unittest.TestCase):
38+
"""``import *`` of :mod:`pcapkit.protocols.protocol` binds ``Protocol``."""
39+
40+
def test_protocol_is_in_all(self) -> None:
41+
from pcapkit.protocols import protocol
42+
43+
self.assertIn('Protocol', protocol.__all__)
44+
self.assertIn('ProtocolBase', protocol.__all__)
45+
46+
def test_star_import_binds_protocol(self) -> None:
47+
from pcapkit.protocols import protocol
48+
49+
namespace = {} # type: dict[str, object]
50+
exec('from pcapkit.protocols.protocol import *', namespace) # pylint: disable=exec-used
51+
self.assertIs(namespace.get('Protocol'), protocol.Protocol)
52+
self.assertIs(namespace.get('ProtocolBase'), protocol.ProtocolBase)
53+
54+
55+
if __name__ == '__main__':
56+
unittest.main()

0 commit comments

Comments
 (0)