Skip to content

Commit fe5f73f

Browse files
committed
complete type-checking
and include py.typed in the published packages so that consumers can make use of the types
1 parent 01043da commit fe5f73f

13 files changed

Lines changed: 92 additions & 25 deletions

File tree

baudot/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
21
"""
32
Baudot – Tools for handling stateful 5-bit encoding
43
"""
54

6-
from .core import encode, encode_str, decode, decode_to_str
5+
from .core import decode, decode_to_str, encode, encode_str
6+
7+
__all__ = [
8+
"encode",
9+
"encode_str",
10+
"decode",
11+
"decode_to_str",
12+
]

baudot/codecs/__init__.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1-
21
"""
32
Codecs are the tools used to convert encoded-data (5-bit digits)
43
into Unicode characters and back.
54
"""
65

7-
from .core import BaudotCodec, SimpleTabledCodec, Shift
6+
from .core import BaudotCodec, Shift, SimpleTabledCodec
87
from .ita1_baudot import ITA1_CONTINENTAL, ITA1_UK
98
from .ita2_baudot_murray import ITA2_STANDARD, ITA2_US
9+
10+
__all__ = [
11+
"BaudotCodec",
12+
"SimpleTabledCodec",
13+
"Shift",
14+
"ITA1_CONTINENTAL",
15+
"ITA1_UK",
16+
"ITA2_STANDARD",
17+
"ITA2_US",
18+
]

baudot/codecs/core.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,17 @@ class BaudotCodec(ABC):
2828
"""
2929

3030
@abstractmethod
31-
def encode(self, value: Value, state: Shift) -> Tuple[int, Shift]:
31+
def encode(
32+
self,
33+
value: Value,
34+
state: Optional[Shift]
35+
) -> Tuple[int, Optional[Shift]]:
3236
"""
3337
Abstract method for encoding a single character or state shift
3438
"""
3539

3640
@abstractmethod
37-
def decode(self, code: int, state: Shift) -> Union[str, Shift]:
41+
def decode(self, code: int, state: Optional[Shift]) -> Value:
3842
"""
3943
Abstract method for decoding a single code.
4044
"""
@@ -71,7 +75,11 @@ def __init__(self, name: str, tables: Dict[Shift, Table]):
7175
self.encoding_any: Dict[Value, int] = enc_any
7276
self.encoding_others: Dict[Value, Set[Tuple[int, Shift]]] = enc_others
7377

74-
def encode(self, value: Value, state: Shift) -> Tuple[int, Shift]:
78+
def encode(
79+
self,
80+
value: Value,
81+
state: Optional[Shift]
82+
) -> Tuple[int, Optional[Shift]]:
7583
"""
7684
Get the code of the given character of Shift for this codec.
7785
@@ -134,7 +142,7 @@ def decode(self, code: int, state: Optional[Shift]) -> Value:
134142
return self.decoding_table[state][code]
135143

136144

137-
def _verify_tables(tables: Dict[Shift, Table]):
145+
def _verify_tables(tables: Dict[Shift, Table]) -> None:
138146
"""
139147
Function for verifying that a given input table is correct
140148
"""
@@ -155,7 +163,13 @@ def _verify_tables(tables: Dict[Shift, Table]):
155163
raise IncoherentTable("Shifts in the tables don't match their keys")
156164

157165

158-
def _make_simple_encoding_table(tables: Dict[Shift, Table]):
166+
def _make_simple_encoding_table(
167+
tables: Dict[Shift, Table]
168+
) -> Tuple[
169+
Dict[Value, Tuple[int, Shift]],
170+
Dict[Value, int],
171+
Dict[Value, Set[Tuple[int, Shift]]],
172+
]:
159173
"""
160174
Generates the encoding tables by reversing the decoding table
161175
"""

baudot/codecs/ita1_baudot.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22
Codec definitions for first-generation Baudot codes (a.k.a. ITA1)
33
"""
44
# pylint: disable=invalid-name
5+
from __future__ import annotations
6+
7+
from typing import Dict, List, TYPE_CHECKING
58

69
from .core import Shift, SimpleTabledCodec
710

11+
if TYPE_CHECKING:
12+
from .core import Value
13+
814
Figures = Shift('Figures')
915
Letters = Shift('Letters')
1016

11-
CONTINENTAL_TABLE = {
17+
CONTINENTAL_TABLE: Dict[Shift, List[Value]] = {
1218
Letters: [
1319
' ', 'A', 'E', 'É', 'Y', 'U', 'I', 'O',
1420
Figures, 'J', 'G', 'H', 'B', 'C', 'F', 'D',
@@ -23,7 +29,7 @@
2329
],
2430
}
2531

26-
UK_TABLE = {
32+
UK_TABLE: Dict[Shift, List[Value]] = {
2733
Letters: [
2834
' ', 'A', 'E', '/', 'Y', 'U', 'I', 'O',
2935
Figures, 'J', 'G', 'H', 'B', 'C', 'F', 'D',

baudot/codecs/ita2_baudot_murray.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@
33
(a.k.a. Baudot-Murray or ITA2)
44
"""
55
# pylint: disable=invalid-name
6+
from __future__ import annotations
7+
8+
from typing import Dict, List, TYPE_CHECKING
69

710
from .core import Shift, SimpleTabledCodec
811

12+
if TYPE_CHECKING:
13+
from .core import Value
14+
915
Letters = Shift('Letters')
1016
Figures = Shift('Figures')
1117

12-
STANDARD_TABLE = {
18+
STANDARD_TABLE: Dict[Shift, List[Value]] = {
1319
Letters: [
1420
'\x00', 'E', '\n', 'A', ' ', 'S', 'I', 'U',
1521
'\r', 'D', 'R', 'J', 'N', 'F', 'C', 'K',
@@ -24,7 +30,7 @@
2430
],
2531
}
2632

27-
US_TABLE = {
33+
US_TABLE: Dict[Shift, List[Value]] = {
2834
Letters: STANDARD_TABLE[Letters].copy(),
2935
Figures: [
3036
'\x00', '3', '\n', '-', ' ', '\x07', '8', '7',

baudot/core.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from .codecs import BaudotCodec, Shift
1010

1111

12-
def encode(stream: TextIOBase, codec: BaudotCodec, writer: BaudotWriter):
12+
def encode(stream: TextIOBase, codec: BaudotCodec, writer: BaudotWriter) -> None:
1313
"""
1414
Encode unicode characters from an input stream to an output writer,
1515
using the given codec.
@@ -30,6 +30,7 @@ def encode(stream: TextIOBase, codec: BaudotCodec, writer: BaudotWriter):
3030
buffer.append(code)
3131

3232
if new_state != state:
33+
assert new_state is not None
3334
state_code, _ = codec.encode(new_state, None)
3435
buffer.append(state_code)
3536
state = new_state
@@ -38,7 +39,7 @@ def encode(stream: TextIOBase, codec: BaudotCodec, writer: BaudotWriter):
3839
writer.write(buffer.pop(-1))
3940

4041

41-
def encode_str(chars: str, codec: BaudotCodec, writer: BaudotWriter):
42+
def encode_str(chars: str, codec: BaudotCodec, writer: BaudotWriter) -> None:
4243
"""
4344
Encode unicode characters from an input string to an output writer,
4445
using the given codec.
@@ -51,7 +52,7 @@ def encode_str(chars: str, codec: BaudotCodec, writer: BaudotWriter):
5152
encode(stream, codec, writer)
5253

5354

54-
def decode(reader: BaudotReader, codec: BaudotCodec, stream: TextIOBase):
55+
def decode(reader: BaudotReader, codec: BaudotCodec, stream: TextIOBase) -> None:
5556
"""
5657
Decode a baudot code stream from a reader to a unicode stream,
5758
using a given codec.

baudot/handlers/__init__.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1-
21
"""
32
The handlers are interfaces to read and write 5-bit data
43
from a variety of formats.
54
"""
65

7-
from .core import BaudotWriter, BaudotReader
8-
from .tape import TapeReader, TapeWriter, TapeConfig
6+
from .core import BaudotReader, BaudotWriter
97
from .hexbytes import HexBytesReader, HexBytesWriter
8+
from .tape import TapeConfig, TapeReader, TapeWriter
9+
10+
__all__ = [
11+
"BaudotWriter",
12+
"BaudotReader",
13+
"TapeReader",
14+
"TapeWriter",
15+
"TapeConfig",
16+
"HexBytesReader",
17+
"HexBytesWriter",
18+
]

baudot/handlers/core.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
"""
22
Core utilities for Baudot input/output handlers
33
"""
4+
from __future__ import annotations
45

56
from abc import ABC, abstractmethod
7+
from typing import TYPE_CHECKING
8+
9+
if TYPE_CHECKING:
10+
from collections.abc import Iterator
611

712

813
class BaudotReader(ABC):
914
"""Abstract Base Class for a reader"""
1015

11-
def __iter__(self):
16+
def __iter__(self) -> Iterator[int]:
1217
return self
1318

1419
@abstractmethod
15-
def __next__(self):
20+
def __next__(self) -> int:
1621
pass
1722

1823

1924
class BaudotWriter(ABC):
2025
"""Abstract Base Class for a writer"""
2126

2227
@abstractmethod
23-
def write(self, code: int):
28+
def write(self, code: int) -> None:
2429
"""Write a single code to the output"""

baudot/handlers/hexbytes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class HexBytesReader(BaudotReader):
1616
def __init__(self, stream: BufferedIOBase):
1717
self.stream = stream
1818

19-
def __next__(self):
19+
def __next__(self) -> int:
2020
hex_byte = self.stream.read(2)
2121
if not hex_byte:
2222
raise StopIteration()
@@ -38,7 +38,7 @@ class HexBytesWriter(BaudotWriter):
3838
def __init__(self, stream: BufferedIOBase):
3939
self.stream = stream
4040

41-
def write(self, code: int):
41+
def write(self, code: int) -> None:
4242
"""Writes a code as an hexadecimal value"""
4343
if not 0 <= code < 32:
4444
raise WriteError('Invalid 5-bit character code')

baudot/handlers/tape.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def __init__(self, stream: TextIOBase, config: TapeConfig = DEFAULT_TAPE):
6161
self.stream = stream
6262
self.config = config
6363

64-
def write(self, code: int):
64+
def write(self, code: int) -> None:
6565
"""Writes a code to tape"""
6666
if not 0 <= code < 32:
6767
raise WriteError('Invalid 5-bit character code')

0 commit comments

Comments
 (0)