Skip to content

Commit 73170eb

Browse files
authored
Merge pull request #64 from kbsriram/add-types
Add type annotations for library and tests.
2 parents 41a7218 + 3e6a8bc commit 73170eb

9 files changed

+48
-40
lines changed

Diff for: adafruit_pioasm.py

+15-14
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
* Author(s): Scott Shawcroft
1212
"""
1313

14+
try:
15+
from typing import List, MutableSequence
16+
except ImportError:
17+
pass
18+
1419
import array
1520
import re
1621

@@ -40,14 +45,14 @@ class Program: # pylint: disable=too-few-public-methods
4045
4146
"""
4247

43-
def __init__(self, text_program: str, *, build_debuginfo=False) -> None:
48+
def __init__(self, text_program: str, *, build_debuginfo: bool = False) -> None:
4449
"""Converts pioasm text to encoded instruction bytes"""
4550
# pylint: disable=too-many-branches,too-many-statements,too-many-locals
46-
assembled = []
51+
assembled: List[int] = []
4752
program_name = None
4853
labels = {}
4954
linemap = []
50-
instructions = []
55+
instructions: List[str] = []
5156
sideset_count = 0
5257
sideset_enable = 0
5358
wrap = None
@@ -86,9 +91,8 @@ def __init__(self, text_program: str, *, build_debuginfo=False) -> None:
8691

8792
max_delay = 2 ** (5 - sideset_count - sideset_enable) - 1
8893
assembled = []
89-
for instruction in instructions:
90-
# print(instruction)
91-
instruction = splitter(instruction.strip())
94+
for line in instructions:
95+
instruction = splitter(line.strip())
9296
delay = 0
9397
if instruction[-1].endswith("]"): # Delay
9498
delay = int(instruction[-1].strip("[]"), 0)
@@ -253,16 +257,13 @@ def __init__(self, text_program: str, *, build_debuginfo=False) -> None:
253257

254258
self.assembled = array.array("H", assembled)
255259

256-
if build_debuginfo:
257-
self.debuginfo = (linemap, text_program)
258-
else:
259-
self.debuginfo = None
260+
self.debuginfo = (linemap, text_program) if build_debuginfo else None
260261

261-
def print_c_program(self, name, qualifier="const"):
262+
def print_c_program(self, name: str, qualifier: str = "const") -> None:
262263
"""Print the program into a C program snippet"""
263264
if self.debuginfo is None:
264-
linemap = None
265-
program_lines = None
265+
linemap = []
266+
program_lines = []
266267
else:
267268
linemap = self.debuginfo[0][:] # Use a copy since we destroy it
268269
program_lines = self.debuginfo[1].split("\n")
@@ -306,7 +307,7 @@ def print_c_program(self, name, qualifier="const"):
306307
print()
307308

308309

309-
def assemble(program_text: str) -> array.array:
310+
def assemble(program_text: str) -> MutableSequence[int]:
310311
"""Converts pioasm text to encoded instruction bytes
311312
312313
In new code, prefer to use the `Program` class so that the extra arguments

Diff for: tests/pytest_helpers.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,22 @@
66
Pytest helper functions
77
"""
88

9+
try:
10+
from typing import Any, List, Optional, Type
11+
except ImportError:
12+
pass
13+
914
import pytest
1015

1116
import adafruit_pioasm
1217

1318

14-
def nice_opcode(opcode):
15-
opcode = f"{opcode:016b}"
16-
return opcode[:3] + "_" + opcode[3:8] + "_" + opcode[8:]
19+
def nice_opcode(opcode: int) -> str:
20+
nice = f"{opcode:016b}"
21+
return nice[:3] + "_" + nice[3:8] + "_" + nice[8:]
1722

1823

19-
def assert_assembles_to(source, expected):
24+
def assert_assembles_to(source: str, expected: List[int]) -> None:
2025
actual = adafruit_pioasm.assemble(source)
2126
expected_bin = [nice_opcode(x) for x in expected]
2227
actual_bin = [nice_opcode(x) for x in actual]
@@ -25,7 +30,9 @@ def assert_assembles_to(source, expected):
2530
), f"Assembling {source!r}: Expected {expected_bin}, got {actual_bin}"
2631

2732

28-
def assert_assembly_fails(source, match=None, errtype=RuntimeError):
33+
def assert_assembly_fails(
34+
source: str, match: Optional[str] = None, errtype: Type[Exception] = RuntimeError
35+
) -> None:
2936
with pytest.raises(errtype, match=match):
3037
adafruit_pioasm.assemble(source)
3138
# if match:
@@ -36,6 +43,6 @@ def assert_assembly_fails(source, match=None, errtype=RuntimeError):
3643
# adafruit_pioasm.assemble(source)
3744

3845

39-
def assert_pio_kwargs(source, **kw):
46+
def assert_pio_kwargs(source: str, **kw: Any) -> None:
4047
program = adafruit_pioasm.Program(source)
4148
assert kw == program.pio_kwargs

Diff for: tests/test_in.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ def test_in_delay_with_sideset() -> None:
5757
assert_assembles_to("\n".join(source), [0b010_10_101_000_10001])
5858

5959

60-
def test_in_bad_source():
60+
def test_in_bad_source() -> None:
6161
assert_assembly_fails(
6262
"in bad, 17", match="Invalid in source 'bad'", errtype=ValueError
6363
)
6464

6565

66-
def test_in_bad_bitcount():
66+
def test_in_bad_bitcount() -> None:
6767
assert_assembly_fails(
6868
"in pins, 0", match="Count out of range", errtype=RuntimeError
6969
)

Diff for: tests/test_mov.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@
99
from pytest_helpers import assert_assembles_to, assert_assembly_fails
1010

1111

12-
def test_mov_non_happy():
12+
def test_mov_non_happy() -> None:
1313
# non happy path
1414
assert_assembly_fails(
1515
"mov x, blah", match="Invalid mov source 'blah'", errtype=ValueError
1616
)
1717

1818

19-
def test_mov_invert():
19+
def test_mov_invert() -> None:
2020
# test moving and inverting
2121
assert_assembles_to("mov x, ~ x", [0b101_00000_001_01_001])
2222
assert_assembles_to("mov x, ~x", [0b101_00000_001_01_001])
2323
assert_assembles_to("mov x, !x", [0b101_00000_001_01_001])
2424

2525

26-
def test_mov_reverse():
26+
def test_mov_reverse() -> None:
2727
# test moving and reversing bits
2828
assert_assembles_to("mov x, :: x", [0b101_00000_001_10_001])
2929
assert_assembles_to("mov x, ::x", [0b101_00000_001_10_001])

Diff for: tests/test_nop.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
from pytest_helpers import assert_assembles_to, assert_assembly_fails, assert_pio_kwargs
1010

1111

12-
def test_nonsense():
12+
def test_nonsense() -> None:
1313
assert_assembly_fails("nope")
1414

1515

16-
def test_nop():
16+
def test_nop() -> None:
1717
assert_assembles_to("nop", [0b101_00000_010_00_010])
1818
assert_assembles_to("nop\nnop", [0b101_00000_010_00_010, 0b101_00000_010_00_010])
1919
assert_assembles_to("nop [1]", [0b101_00001_010_00_010])
@@ -22,7 +22,7 @@ def test_nop():
2222
assert_assembles_to(".side_set 1\nnop side 1 [15]", [0b101_11111_010_00_010])
2323

2424

25-
def test_sideset_opt():
25+
def test_sideset_opt() -> None:
2626
assert_assembles_to(".side_set 1 opt\nnop side 1", [0b101_11000_010_00_010])
2727
assert_assembles_to(".side_set 1 opt\nnop side 0", [0b101_10000_010_00_010])
2828
assert_assembles_to(".side_set 1 opt\nnop side 0 [1]", [0b101_10001_010_00_010])
@@ -32,14 +32,14 @@ def test_sideset_opt():
3232
assert_assembles_to(".side_set 1 opt\nnop side 0 [7]", [0b101_10111_010_00_010])
3333

3434

35-
def test_set():
35+
def test_set() -> None:
3636
# non happy path
3737
assert_assembly_fails(
3838
"set isr, 1", match="Invalid set destination 'isr'", errtype=ValueError
3939
)
4040

4141

42-
def test_jmp():
42+
def test_jmp() -> None:
4343
assert_assembles_to("l:\njmp l", [0b000_00000_000_00000])
4444
assert_assembles_to("l:\njmp 7", [0b000_00000_000_00111])
4545
assert_assembles_to("jmp l\nl:", [0b000_00000_000_00001])
@@ -56,7 +56,7 @@ def test_jmp():
5656
)
5757

5858

59-
def test_wait():
59+
def test_wait() -> None:
6060
assert_assembles_to("wait 0 gpio 0", [0b001_00000_0_00_00000])
6161
assert_assembles_to("wait 0 gpio 1", [0b001_00000_0_00_00001])
6262
assert_assembles_to("wait 1 gpio 2", [0b001_00000_1_00_00010])
@@ -69,15 +69,15 @@ def test_wait():
6969
assert_assembles_to("wait 0 irq 1 rel", [0b001_00000_0_10_10001])
7070

7171

72-
def test_limits():
72+
def test_limits() -> None:
7373
assert_assembly_fails(".side_set 1\nnop side 2")
7474
assert_assembly_fails(".side_set 1\nnop side 2 [1]")
7575
assert_assembly_fails("nop [32]")
7676
assert_assembly_fails(".side_set 1\nnop side 0 [16]")
7777
assert_assembly_fails(".side_set 1 opt\nnop side 0 [8]")
7878

7979

80-
def test_cls():
80+
def test_cls() -> None:
8181
assert_pio_kwargs("", sideset_enable=False)
8282
assert_pio_kwargs(".side_set 1", sideset_pin_count=1, sideset_enable=False)
8383
assert_pio_kwargs(".side_set 3 opt", sideset_pin_count=3, sideset_enable=True)

Diff for: tests/test_out.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ def test_out_delay_with_sideset() -> None:
5959
assert_assembles_to("\n".join(source), [0b011_10_101_000_10001])
6060

6161

62-
def test_out_bad_destination():
62+
def test_out_bad_destination() -> None:
6363
assert_assembly_fails(
6464
"out bad, 17", match="Invalid out destination 'bad'", errtype=ValueError
6565
)
6666

6767

68-
def test_out_bad_bitcount():
68+
def test_out_bad_bitcount() -> None:
6969
assert_assembly_fails(
7070
"out pins, 0", match="Count out of range", errtype=RuntimeError
7171
)

Diff for: tests/test_pseudo.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
from pytest_helpers import assert_pio_kwargs
1010

1111

12-
def test_offset():
12+
def test_offset() -> None:
1313
assert_pio_kwargs(".origin 7", offset=7, sideset_enable=False)

Diff for: tests/test_radix.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
from pytest_helpers import assert_assembles_to
1010

1111

12-
def test_octal():
12+
def test_octal() -> None:
1313
assert_assembles_to(".side_set 0o1\nset x, 0o11", [0b111_00000_001_01001])
1414

1515

16-
def test_binary():
16+
def test_binary() -> None:
1717
assert_assembles_to(".side_set 0b101\nnop side 0b10001", [0b101_10001_010_00_010])
1818

1919

20-
def test_hex():
20+
def test_hex() -> None:
2121
assert_assembles_to(".side_set 0x0\nnop [0x10]", [0b101_10000_010_00_010])

Diff for: tests/test_wrap.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from pytest_helpers import assert_assembly_fails, assert_pio_kwargs
1010

1111

12-
def test_wrap():
12+
def test_wrap() -> None:
1313
assert_assembly_fails(".wrap")
1414
assert_pio_kwargs(
1515
"nop\n.wrap_target\nnop\nnop\n.wrap",

0 commit comments

Comments
 (0)