Skip to content

Commit 2584e7f

Browse files
committed
Run mypy during pre-commit to verify typing information
1 parent 6cd4217 commit 2584e7f

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

.pre-commit-config.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,21 @@ repos:
4040
files: "^tests/"
4141
args:
4242
- --disable=missing-docstring,consider-using-f-string,duplicate-code
43+
- repo: local
44+
# We do not use pre-commit/mirrors-mypy,
45+
# as it comes with opinionated defaults
46+
# (like --ignore-missing-imports)
47+
# and is difficult to configure to run
48+
# with the dependencies correctly installed.
49+
hooks:
50+
- id: mypy
51+
name: mypy
52+
entry: "./run-mypy"
53+
language: python
54+
additional_dependencies: ["mypy==0.790"]
55+
types: [python]
56+
# use require_serial so that script
57+
# is only called once per commit
58+
require_serial: true
59+
# Print the number of files as a sanity-check
60+
verbose: true

adafruit_pioasm.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
import array
1515
import re
1616

17+
try:
18+
from typing import List, Tuple, Optional
19+
except: # pylint: disable=bare-except
20+
pass
21+
1722
splitter = re.compile(r",\s*|\s+(?:,\s*)?").split
1823
mov_splitter = re.compile("!|~|::").split
1924

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

43-
def __init__(self, text_program: str, *, build_debuginfo=False) -> None:
48+
debuginfo: Optional[Tuple[List[int], str]]
49+
50+
def __init__(self, text_program: str, *, build_debuginfo: bool = False) -> None:
4451
"""Converts pioasm text to encoded instruction bytes"""
4552
# pylint: disable=too-many-branches,too-many-statements,too-many-locals
46-
assembled = []
53+
assembled: List[int] = []
4754
program_name = None
4855
labels = {}
4956
linemap = []
50-
instructions = []
57+
instructions: List[str] = []
5158
sideset_count = 0
5259
sideset_enable = 0
5360
wrap = None
@@ -83,9 +90,9 @@ def __init__(self, text_program: str, *, build_debuginfo=False) -> None:
8390

8491
max_delay = 2 ** (5 - sideset_count - sideset_enable) - 1
8592
assembled = []
86-
for instruction in instructions:
93+
for instruction_str in instructions:
8794
# print(instruction)
88-
instruction = splitter(instruction.strip())
95+
instruction = splitter(instruction_str.strip())
8996
delay = 0
9097
if instruction[-1].endswith("]"): # Delay
9198
delay = int(instruction[-1].strip("[]"), 0)
@@ -242,14 +249,14 @@ def __init__(self, text_program: str, *, build_debuginfo=False) -> None:
242249
else:
243250
self.debuginfo = None
244251

245-
def print_c_program(self, name, qualifier="const"):
252+
def print_c_program(self, name: str, qualifier: str = "const") -> None:
246253
"""Print the program into a C program snippet"""
247-
if self.debuginfo is None:
248-
linemap = None
249-
program_lines = None
250-
else:
254+
if self.debuginfo:
251255
linemap = self.debuginfo[0][:] # Use a copy since we destroy it
252256
program_lines = self.debuginfo[1].split("\n")
257+
else:
258+
linemap = []
259+
program_lines = []
253260

254261
print(
255262
f"{qualifier} int {name}_wrap = {self.pio_kwargs.get('wrap', len(self.assembled)-1)};"
@@ -290,7 +297,7 @@ def print_c_program(self, name, qualifier="const"):
290297
print()
291298

292299

293-
def assemble(program_text: str) -> array.array:
300+
def assemble(program_text: str) -> "array.array[int]":
294301
"""Converts pioasm text to encoded instruction bytes
295302
296303
In new code, prefer to use the `Program` class so that the extra arguments

run-mypy

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
# SPDX-FileCopyrightText: 2022 Jeff Epler, written for Adafruit Industries
3+
#
4+
# SPDX-License-Identifier: Unlicense
5+
cd "$(dirname "$0")" &&
6+
pip install --no-input --quiet --editable . &&
7+
mypy --strict -m adafruit_pioasm

0 commit comments

Comments
 (0)