Skip to content

change to ruff #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# SPDX-FileCopyrightText: 2024 Justin Myers for Adafruit Industries
#
# SPDX-License-Identifier: Unlicense

.py text eol=lf
.rst text eol=lf
.txt text eol=lf
.yaml text eol=lf
.toml text eol=lf
.license text eol=lf
.md text eol=lf
43 changes: 11 additions & 32 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,42 +1,21 @@
# SPDX-FileCopyrightText: 2020 Diego Elio Pettenò
# SPDX-FileCopyrightText: 2024 Justin Myers for Adafruit Industries
#
# SPDX-License-Identifier: Unlicense

repos:
- repo: https://github.com/python/black
rev: 23.3.0
hooks:
- id: black
- repo: https://github.com/fsfe/reuse-tool
rev: v1.1.2
hooks:
- id: reuse
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/pycqa/pylint
rev: v3.2.7
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.4
hooks:
- id: pylint
name: pylint (library code)
types: [python]
args:
- --disable=consider-using-f-string
exclude: "^(docs/|examples/|tests/|setup.py$)"
- id: pylint
name: pylint (example code)
description: Run pylint rules on "examples/*.py" files
types: [python]
files: "^examples/"
args:
- --disable=missing-docstring,invalid-name,consider-using-f-string,duplicate-code
- id: pylint
name: pylint (test code)
description: Run pylint rules on "tests/*.py" files
types: [python]
files: "^tests/"
args:
- --disable=missing-docstring,consider-using-f-string,duplicate-code
- id: ruff-format
- id: ruff
args: ["--fix"]
- repo: https://github.com/fsfe/reuse-tool
rev: v3.0.1
hooks:
- id: reuse
399 changes: 0 additions & 399 deletions .pylintrc

This file was deleted.

6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
@@ -14,9 +14,9 @@ Introduction
:target: https://github.com/adafruit/Adafruit_CircuitPython_HID/actions/
:alt: Build Status

.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
:target: https://github.com/psf/black
:alt: Code Style: Black
.. image:: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json
:target: https://github.com/astral-sh/ruff
:alt: Code Style: Ruff


This driver simulates USB HID devices. Currently keyboard and mouse are implemented.
7 changes: 2 additions & 5 deletions adafruit_hid/__init__.py
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@

# imports
from __future__ import annotations

import time

try:
@@ -55,11 +56,7 @@ def find_device(
devices = [devices] # type: ignore
device = None
for dev in devices:
if (
dev.usage_page == usage_page
and dev.usage == usage
and hasattr(dev, "send_report")
):
if dev.usage_page == usage_page and dev.usage == usage and hasattr(dev, "send_report"):
device = dev
break
if device is None:
11 changes: 4 additions & 7 deletions adafruit_hid/consumer_control.py
Original file line number Diff line number Diff line change
@@ -12,16 +12,15 @@
import sys

if sys.implementation.version[0] < 3:
raise ImportError(
"{0} is not supported in CircuitPython 2.x or lower".format(__name__)
)
raise ImportError(f"{__name__} is not supported in CircuitPython 2.x or lower")

# pylint: disable=wrong-import-position
import struct

from . import find_device

try:
from typing import Sequence

import usb_hid
except ImportError:
pass
@@ -40,9 +39,7 @@ def __init__(self, devices: Sequence[usb_hid.Device], timeout: int = None) -> No
itself. A device is any object that implements ``send_report()``, ``usage_page`` and
``usage``.
"""
self._consumer_device = find_device(
devices, usage_page=0x0C, usage=0x01, timeout=timeout
)
self._consumer_device = find_device(devices, usage_page=0x0C, usage=0x01, timeout=timeout)

# Reuse this bytearray to send consumer reports.
self._report = bytearray(2)
2 changes: 0 additions & 2 deletions adafruit_hid/consumer_control_code.py
Original file line number Diff line number Diff line change
@@ -17,8 +17,6 @@ class ConsumerControlCode:
https://www.usb.org/sites/default/files/hut1_21_0.pdf#page=118.
"""

# pylint: disable-msg=too-few-public-methods

RECORD = 0xB2
"""Record"""
FAST_FORWARD = 0xB3
8 changes: 3 additions & 5 deletions adafruit_hid/keyboard.py
Original file line number Diff line number Diff line change
@@ -11,12 +11,12 @@

from micropython import const

from .keycode import Keycode

from . import find_device
from .keycode import Keycode

try:
from typing import Sequence

import usb_hid
except ImportError:
pass
@@ -48,9 +48,7 @@ def __init__(self, devices: Sequence[usb_hid.Device], timeout: int = None) -> No
itself. A device is any object that implements ``send_report()``, ``usage_page`` and
``usage``.
"""
self._keyboard_device = find_device(
devices, usage_page=0x1, usage=0x06, timeout=timeout
)
self._keyboard_device = find_device(devices, usage_page=0x1, usage=0x06, timeout=timeout)

# Reuse this bytearray to send keyboard reports.
self.report = bytearray(8)
2 changes: 1 addition & 1 deletion adafruit_hid/keyboard_layout_base.py
Original file line number Diff line number Diff line change
@@ -9,9 +9,9 @@
* Author(s): Dan Halbert, AngainorDev, Neradoc
"""


try:
from typing import Tuple

from .keyboard import Keyboard
except ImportError:
pass
6 changes: 1 addition & 5 deletions adafruit_hid/keycode.py
Original file line number Diff line number Diff line change
@@ -27,7 +27,6 @@ class Keycode:
different variations of a keyboard.
"""

# pylint: disable-msg=invalid-name
A = 0x04
"""``a`` and ``A``"""
B = 0x05
@@ -297,11 +296,8 @@ class Keycode:
RIGHT_GUI = 0xE7
"""GUI modifier right of the spacebar"""

# pylint: enable-msg=invalid-name
@classmethod
def modifier_bit(cls, keycode: int) -> int:
"""Return the modifer bit to be set in an HID keycode report if this is a
modifier key; otherwise return 0."""
return (
1 << (keycode - 0xE0) if cls.LEFT_CONTROL <= keycode <= cls.RIGHT_GUI else 0
)
return 1 << (keycode - 0xE0) if cls.LEFT_CONTROL <= keycode <= cls.RIGHT_GUI else 0
6 changes: 3 additions & 3 deletions adafruit_hid/mouse.py
Original file line number Diff line number Diff line change
@@ -8,10 +8,12 @@
* Author(s): Dan Halbert
"""

from . import find_device

try:
from typing import Sequence

import usb_hid
except ImportError:
pass
@@ -41,9 +43,7 @@ def __init__(self, devices: Sequence[usb_hid.Device], timeout: int = None) -> No
itself. A device is any object that implements ``send_report()``, ``usage_page`` and
``usage``.
"""
self._mouse_device = find_device(
devices, usage_page=0x1, usage=0x02, timeout=timeout
)
self._mouse_device = find_device(devices, usage_page=0x1, usage=0x02, timeout=timeout)

# Reuse this bytearray to send mouse reports.
# report[0] buttons pressed (LEFT, MIDDLE, RIGHT)
3 changes: 3 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

.. If you created a package, create one automodule per module in the package.
API Reference
#############

.. automodule:: adafruit_hid
:members:

8 changes: 2 additions & 6 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# -*- coding: utf-8 -*-

# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
#
# SPDX-License-Identifier: MIT

import datetime
import os
import sys
import datetime

sys.path.insert(0, os.path.abspath(".."))

@@ -53,9 +51,7 @@
creation_year = "2017"
current_year = str(datetime.datetime.now().year)
year_duration = (
current_year
if current_year == creation_year
else creation_year + " - " + current_year
current_year if current_year == creation_year else creation_year + " - " + current_year
)
copyright = year_duration + " Scott Shawcroft"
author = "Scott Shawcroft"
2 changes: 2 additions & 0 deletions examples/hid_consumer_control_brightness.py
Original file line number Diff line number Diff line change
@@ -2,9 +2,11 @@
# SPDX-License-Identifier: MIT

import time

import board
import digitalio
import usb_hid

from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode

4 changes: 2 additions & 2 deletions examples/hid_joywing_gamepad.py
Original file line number Diff line number Diff line change
@@ -14,10 +14,10 @@

import board
import busio
from micropython import const
from adafruit_seesaw.seesaw import Seesaw
import usb_hid
from adafruit_seesaw.seesaw import Seesaw
from hid_gamepad import Gamepad
from micropython import const


def range_map(value, in_min, in_max, out_min, out_max):
1 change: 1 addition & 0 deletions examples/hid_keyboard_layout.py
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@
# SPDX-License-Identifier: MIT

import time

import board
import digitalio
import usb_hid
2 changes: 2 additions & 0 deletions examples/hid_keyboard_shortcuts.py
Original file line number Diff line number Diff line change
@@ -2,9 +2,11 @@
# SPDX-License-Identifier: MIT

import time

import board
import digitalio
import usb_hid

from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode

3 changes: 1 addition & 2 deletions examples/hid_simple_gamepad.py
Original file line number Diff line number Diff line change
@@ -6,11 +6,10 @@
# See this Learn Guide for details:
# https://learn.adafruit.com/customizing-usb-devices-in-circuitpython/hid-devices#custom-hid-devices-3096614-9

import analogio
import board
import digitalio
import analogio
import usb_hid

from hid_gamepad import Gamepad

gp = Gamepad(usb_hid.devices)
2 changes: 2 additions & 0 deletions examples/hid_simpletest.py
Original file line number Diff line number Diff line change
@@ -2,9 +2,11 @@
# SPDX-License-Identifier: MIT

import time

import board
import digitalio
import usb_hid

from adafruit_hid.mouse import Mouse

mouse = Mouse(usb_hid.devices)
109 changes: 109 additions & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries
#
# SPDX-License-Identifier: MIT

target-version = "py38"
line-length = 100

[lint]
preview = true
select = ["I", "PL", "UP"]

extend-select = [
"D419", # empty-docstring
"E501", # line-too-long
"W291", # trailing-whitespace
"PLC0414", # useless-import-alias
"PLC2401", # non-ascii-name
"PLC2801", # unnecessary-dunder-call
"PLC3002", # unnecessary-direct-lambda-call
"E999", # syntax-error
"PLE0101", # return-in-init
"F706", # return-outside-function
"F704", # yield-outside-function
"PLE0116", # continue-in-finally
"PLE0117", # nonlocal-without-binding
"PLE0241", # duplicate-bases
"PLE0302", # unexpected-special-method-signature
"PLE0604", # invalid-all-object
"PLE0605", # invalid-all-format
"PLE0643", # potential-index-error
"PLE0704", # misplaced-bare-raise
"PLE1141", # dict-iter-missing-items
"PLE1142", # await-outside-async
"PLE1205", # logging-too-many-args
"PLE1206", # logging-too-few-args
"PLE1307", # bad-string-format-type
"PLE1310", # bad-str-strip-call
"PLE1507", # invalid-envvar-value
"PLE2502", # bidirectional-unicode
"PLE2510", # invalid-character-backspace
"PLE2512", # invalid-character-sub
"PLE2513", # invalid-character-esc
"PLE2514", # invalid-character-nul
"PLE2515", # invalid-character-zero-width-space
"PLR0124", # comparison-with-itself
"PLR0202", # no-classmethod-decorator
"PLR0203", # no-staticmethod-decorator
"UP004", # useless-object-inheritance
"PLR0206", # property-with-parameters
"PLR0904", # too-many-public-methods
"PLR0911", # too-many-return-statements
"PLR0912", # too-many-branches
"PLR0913", # too-many-arguments
"PLR0914", # too-many-locals
"PLR0915", # too-many-statements
"PLR0916", # too-many-boolean-expressions
"PLR1702", # too-many-nested-blocks
"PLR1704", # redefined-argument-from-local
"PLR1711", # useless-return
"C416", # unnecessary-comprehension
"PLR1733", # unnecessary-dict-index-lookup
"PLR1736", # unnecessary-list-index-lookup

# ruff reports this rule is unstable
#"PLR6301", # no-self-use

"PLW0108", # unnecessary-lambda
"PLW0120", # useless-else-on-loop
"PLW0127", # self-assigning-variable
"PLW0129", # assert-on-string-literal
"B033", # duplicate-value
"PLW0131", # named-expr-without-context
"PLW0245", # super-without-brackets
"PLW0406", # import-self
"PLW0602", # global-variable-not-assigned
"PLW0603", # global-statement
"PLW0604", # global-at-module-level

# fails on the try: import typing used by libraries
#"F401", # unused-import

"F841", # unused-variable
"E722", # bare-except
"PLW0711", # binary-op-exception
"PLW1501", # bad-open-mode
"PLW1508", # invalid-envvar-default
"PLW1509", # subprocess-popen-preexec-fn
"PLW2101", # useless-with-lock
"PLW3301", # nested-min-max
]

ignore = [
"PLR2004", # magic-value-comparison
"UP030", # format literals
"PLW1514", # unspecified-encoding
"PLR0913", # too-many-arguments
"PLR0915", # too-many-statements
"PLR0917", # too-many-positional-arguments
"PLR0904", # too-many-public-methods
"PLR0912", # too-many-branches
"PLR0916", # too-many-boolean-expressions
"PLR6301", # could-be-static no-self-use
"PLC0415", # import outside toplevel
"PLC2701", # private import
"PLW2901", # loop var overwrite
]

[format]
line-ending = "lf"