Skip to content

Commit

Permalink
Merge pull request #105 from robberwick/mypy
Browse files Browse the repository at this point in the history
Add mypy as a dev dependency
  • Loading branch information
robberwick authored Nov 17, 2024
2 parents 3a47472 + ba981c3 commit 1799286
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 25 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/mypy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Mypy Type Check

on: [push, pull_request]

jobs:
mypy:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install .[dev,test]
- name: Run mypy
run: mypy src tests
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ dependencies = [
dynamic = ["version"]

[project.optional-dependencies]
dev = ["black", "isort"]
dev = ["black", "isort", "mypy"]
test = ["coverage", "pytest", "pytest-cov", "pytest-mock"]

[project.scripts]
Expand Down
3 changes: 2 additions & 1 deletion src/blinkstick/backends/unix_like.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def find_blinksticks(find_all: bool = True) -> list[usb.core.Device] | None:

@staticmethod
def find_by_serial(serial: str) -> list[usb.core.Device] | None:
for d in UnixLikeBackend.find_blinksticks():
found_devices = UnixLikeBackend.find_blinksticks() or []
for d in found_devices:
try:
if usb.util.get_string(d, 3, 1033) == serial:
devices = [d]
Expand Down
5 changes: 2 additions & 3 deletions src/blinkstick/backends/win32.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ def __init__(self, device=None):

@staticmethod
def find_by_serial(serial: str) -> list[hid.HidDevice] | None:
devices = [
d for d in Win32Backend.find_blinksticks() if d.serial_number == serial
]
found_devices = Win32Backend.find_blinksticks() or []
devices = [d for d in found_devices if d.serial_number == serial]

if len(devices) > 0:
return devices
Expand Down
42 changes: 25 additions & 17 deletions src/blinkstick/blinkstick.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import time
import warnings
from importlib.metadata import version
from typing import Callable

from blinkstick.colors import (
hex_to_rgb,
Expand All @@ -18,11 +19,11 @@

if sys.platform == "win32":
from blinkstick.backends.win32 import Win32Backend as USBBackend
import pywinusb.hid as hid
import pywinusb.hid as hid # type: ignore
else:
from blinkstick.backends.unix_like import UnixLikeBackend as USBBackend
import usb.core
import usb.util
import usb.core # type: ignore
import usb.util # type: ignore

from random import randint

Expand Down Expand Up @@ -222,13 +223,13 @@ def _get_color_rgb(self, index: int = 0) -> tuple[int, int, int]:
0x80 | 0x20, 0x1, 0x0001, 0, 33
)
if self.inverse:
return [
return (
255 - device_bytes[1],
255 - device_bytes[2],
255 - device_bytes[3],
]
)
else:
return [device_bytes[1], device_bytes[2], device_bytes[3]]
return device_bytes[1], device_bytes[2], device_bytes[3]
else:
data = self.get_led_data((index + 1) * 3)

Expand All @@ -242,7 +243,7 @@ def get_color(
self,
index: int = 0,
color_mode: ColorFormat = ColorFormat.RGB,
color_format: str = None,
color_format: str | None = None,
) -> tuple[int, int, int] | str:
"""
Get the current backend color in the defined format.
Expand Down Expand Up @@ -282,12 +283,12 @@ def get_color(
except ValueError:
color_mode = ColorFormat.RGB

color_funcs = {
color_funcs: dict[ColorFormat, Callable[[int], tuple[int, int, int] | str]] = {
ColorFormat.RGB: self._get_color_rgb,
ColorFormat.HEX: self._get_color_hex,
}

return color_funcs.get(color_mode, ColorFormat.RGB)(index)
return color_funcs.get(color_mode, self._get_color_rgb)(index)

def _determine_report_id(self, led_count: int) -> tuple[int, int]:
report_id = 9
Expand Down Expand Up @@ -453,7 +454,7 @@ def get_info_block2(self) -> str:
result += chr(i)
return result

def _data_to_message(self, data) -> bytes:
def _data_to_message(self, data: str) -> bytes:
"""
Helper method to convert a string to byte array of 32 bytes.
Expand All @@ -463,14 +464,14 @@ def _data_to_message(self, data) -> bytes:
@rtype: byte[32]
@return: It fills the rest of bytes with zeros.
"""
bytes = [1]
byte_array = bytearray([1])
for c in data:
bytes.append(ord(c))
byte_array.append(ord(c))

for i in range(32 - len(data)):
bytes.append(0)
byte_array.append(0)

return bytes
return bytes(byte_array)

def set_info_block1(self, data: str) -> None:
"""
Expand Down Expand Up @@ -686,7 +687,7 @@ def morph(
)

for grad in gradient:
grad_r, grad_g, grad_b = grad
grad_r, grad_g, grad_b = map(int, grad)

self.set_color(
channel=channel, index=index, red=grad_r, green=grad_g, blue=grad_b
Expand Down Expand Up @@ -729,7 +730,7 @@ def set_inverse(self, value: bool) -> None:
@param value: True/False to set the inverse mode
"""
if type(value) is str:
value = value.lower() == "true"
value = value.lower() == "true" # type: ignore
self.inverse = bool(value)

def set_max_rgb_value(self, value: int) -> None:
Expand Down Expand Up @@ -917,6 +918,9 @@ def send_data(self, channel: int) -> None:
- 1 - G pin on BlinkStick Pro board
- 2 - B pin on BlinkStick Pro board
"""
if self.bstick is None:
return

packet_data = [item for sublist in self.data[channel] for item in sublist]

try:
Expand Down Expand Up @@ -1433,8 +1437,10 @@ def find_first() -> BlinkStick | None:
if d:
return BlinkStick(device=d)

return None


def find_by_serial(serial: str | None = None) -> BlinkStick | None:
def find_by_serial(serial: str = "") -> BlinkStick | None:
"""
Find BlinkStick backend based on serial number.
Expand All @@ -1447,6 +1453,8 @@ def find_by_serial(serial: str | None = None) -> BlinkStick | None:
if devices:
return BlinkStick(device=devices[0])

return None


def get_blinkstick_package_version() -> str:
return version("blinkstick")
5 changes: 4 additions & 1 deletion src/blinkstick/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,11 @@ def normalize_hex(hex_value: str) -> str:
ValueError: '0099cc' is not a valid hexadecimal color value.
"""
invalid_hex_value_msg = "'%s' is not a valid hexadecimal color value."
if not (hex_match := HEX_COLOR_RE.match(hex_value)):
raise ValueError(invalid_hex_value_msg % hex_value)
try:
hex_digits = HEX_COLOR_RE.match(hex_value).groups()[0]
hex_digits = hex_match.groups()[0]
except AttributeError:
raise ValueError("'%s' is not a valid hexadecimal color value." % hex_value)
if len(hex_digits) == 3:
Expand Down
4 changes: 2 additions & 2 deletions tests/clients/test_blinkstick.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def test_set_inverse_type_checking(make_blinkstick, input_value, expected_result
(0, 255, 0, 0),
(255, 0, 0),
False,
[255, 0, 0],
(255, 0, 0),
id="RGB, NoInverse",
),
pytest.param(
Expand All @@ -258,7 +258,7 @@ def test_set_inverse_type_checking(make_blinkstick, input_value, expected_result
(0, 255, 0, 0),
(255, 0, 0),
True,
[0, 255, 255],
(0, 255, 255),
id="RGB, Inverse",
),
pytest.param(
Expand Down

0 comments on commit 1799286

Please sign in to comment.