Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e954e8b

Browse files
authoredMay 16, 2025··
Merge pull request #134 from adafruit/use_ruff
change to ruff
2 parents d26db69 + 3a44c26 commit e954e8b

20 files changed

+163
-472
lines changed
 

‎.gitattributes

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# SPDX-FileCopyrightText: 2024 Justin Myers for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
5+
.py text eol=lf
6+
.rst text eol=lf
7+
.txt text eol=lf
8+
.yaml text eol=lf
9+
.toml text eol=lf
10+
.license text eol=lf
11+
.md text eol=lf

‎.pre-commit-config.yaml

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,21 @@
1-
# SPDX-FileCopyrightText: 2020 Diego Elio Pettenò
1+
# SPDX-FileCopyrightText: 2024 Justin Myers for Adafruit Industries
22
#
33
# SPDX-License-Identifier: Unlicense
44

55
repos:
6-
- repo: https://github.com/python/black
7-
rev: 23.3.0
8-
hooks:
9-
- id: black
10-
- repo: https://github.com/fsfe/reuse-tool
11-
rev: v1.1.2
12-
hooks:
13-
- id: reuse
146
- repo: https://github.com/pre-commit/pre-commit-hooks
15-
rev: v4.4.0
7+
rev: v4.5.0
168
hooks:
179
- id: check-yaml
1810
- id: end-of-file-fixer
1911
- id: trailing-whitespace
20-
- repo: https://github.com/pycqa/pylint
21-
rev: v3.2.7
12+
- repo: https://github.com/astral-sh/ruff-pre-commit
13+
rev: v0.3.4
2214
hooks:
23-
- id: pylint
24-
name: pylint (library code)
25-
types: [python]
26-
args:
27-
- --disable=consider-using-f-string
28-
exclude: "^(docs/|examples/|tests/|setup.py$)"
29-
- id: pylint
30-
name: pylint (example code)
31-
description: Run pylint rules on "examples/*.py" files
32-
types: [python]
33-
files: "^examples/"
34-
args:
35-
- --disable=missing-docstring,invalid-name,consider-using-f-string,duplicate-code
36-
- id: pylint
37-
name: pylint (test code)
38-
description: Run pylint rules on "tests/*.py" files
39-
types: [python]
40-
files: "^tests/"
41-
args:
42-
- --disable=missing-docstring,consider-using-f-string,duplicate-code
15+
- id: ruff-format
16+
- id: ruff
17+
args: ["--fix"]
18+
- repo: https://github.com/fsfe/reuse-tool
19+
rev: v3.0.1
20+
hooks:
21+
- id: reuse

‎.pylintrc

Lines changed: 0 additions & 399 deletions
This file was deleted.

‎README.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ Introduction
1414
:target: https://github.com/adafruit/Adafruit_CircuitPython_HID/actions/
1515
:alt: Build Status
1616

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

2121

2222
This driver simulates USB HID devices. Currently keyboard and mouse are implemented.

‎adafruit_hid/__init__.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
# imports
1515
from __future__ import annotations
16+
1617
import time
1718

1819
try:
@@ -55,11 +56,7 @@ def find_device(
5556
devices = [devices] # type: ignore
5657
device = None
5758
for dev in devices:
58-
if (
59-
dev.usage_page == usage_page
60-
and dev.usage == usage
61-
and hasattr(dev, "send_report")
62-
):
59+
if dev.usage_page == usage_page and dev.usage == usage and hasattr(dev, "send_report"):
6360
device = dev
6461
break
6562
if device is None:

‎adafruit_hid/consumer_control.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,15 @@
1212
import sys
1313

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

19-
# pylint: disable=wrong-import-position
2017
import struct
18+
2119
from . import find_device
2220

2321
try:
2422
from typing import Sequence
23+
2524
import usb_hid
2625
except ImportError:
2726
pass
@@ -40,9 +39,7 @@ def __init__(self, devices: Sequence[usb_hid.Device], timeout: int = None) -> No
4039
itself. A device is any object that implements ``send_report()``, ``usage_page`` and
4140
``usage``.
4241
"""
43-
self._consumer_device = find_device(
44-
devices, usage_page=0x0C, usage=0x01, timeout=timeout
45-
)
42+
self._consumer_device = find_device(devices, usage_page=0x0C, usage=0x01, timeout=timeout)
4643

4744
# Reuse this bytearray to send consumer reports.
4845
self._report = bytearray(2)

‎adafruit_hid/consumer_control_code.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ class ConsumerControlCode:
1717
https://www.usb.org/sites/default/files/hut1_21_0.pdf#page=118.
1818
"""
1919

20-
# pylint: disable-msg=too-few-public-methods
21-
2220
RECORD = 0xB2
2321
"""Record"""
2422
FAST_FORWARD = 0xB3

‎adafruit_hid/keyboard.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111

1212
from micropython import const
1313

14-
from .keycode import Keycode
15-
1614
from . import find_device
15+
from .keycode import Keycode
1716

1817
try:
1918
from typing import Sequence
19+
2020
import usb_hid
2121
except ImportError:
2222
pass
@@ -48,9 +48,7 @@ def __init__(self, devices: Sequence[usb_hid.Device], timeout: int = None) -> No
4848
itself. A device is any object that implements ``send_report()``, ``usage_page`` and
4949
``usage``.
5050
"""
51-
self._keyboard_device = find_device(
52-
devices, usage_page=0x1, usage=0x06, timeout=timeout
53-
)
51+
self._keyboard_device = find_device(devices, usage_page=0x1, usage=0x06, timeout=timeout)
5452

5553
# Reuse this bytearray to send keyboard reports.
5654
self.report = bytearray(8)

‎adafruit_hid/keyboard_layout_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
* Author(s): Dan Halbert, AngainorDev, Neradoc
1010
"""
1111

12-
1312
try:
1413
from typing import Tuple
14+
1515
from .keyboard import Keyboard
1616
except ImportError:
1717
pass

‎adafruit_hid/keycode.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class Keycode:
2727
different variations of a keyboard.
2828
"""
2929

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

300-
# pylint: enable-msg=invalid-name
301299
@classmethod
302300
def modifier_bit(cls, keycode: int) -> int:
303301
"""Return the modifer bit to be set in an HID keycode report if this is a
304302
modifier key; otherwise return 0."""
305-
return (
306-
1 << (keycode - 0xE0) if cls.LEFT_CONTROL <= keycode <= cls.RIGHT_GUI else 0
307-
)
303+
return 1 << (keycode - 0xE0) if cls.LEFT_CONTROL <= keycode <= cls.RIGHT_GUI else 0

‎adafruit_hid/mouse.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
99
* Author(s): Dan Halbert
1010
"""
11+
1112
from . import find_device
1213

1314
try:
1415
from typing import Sequence
16+
1517
import usb_hid
1618
except ImportError:
1719
pass
@@ -41,9 +43,7 @@ def __init__(self, devices: Sequence[usb_hid.Device], timeout: int = None) -> No
4143
itself. A device is any object that implements ``send_report()``, ``usage_page`` and
4244
``usage``.
4345
"""
44-
self._mouse_device = find_device(
45-
devices, usage_page=0x1, usage=0x02, timeout=timeout
46-
)
46+
self._mouse_device = find_device(devices, usage_page=0x1, usage=0x02, timeout=timeout)
4747

4848
# Reuse this bytearray to send mouse reports.
4949
# report[0] buttons pressed (LEFT, MIDDLE, RIGHT)

‎docs/api.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11

22
.. If you created a package, create one automodule per module in the package.
33
4+
API Reference
5+
#############
6+
47
.. automodule:: adafruit_hid
58
:members:
69

‎docs/conf.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
# -*- coding: utf-8 -*-
2-
31
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
42
#
53
# SPDX-License-Identifier: MIT
64

5+
import datetime
76
import os
87
import sys
9-
import datetime
108

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

@@ -53,9 +51,7 @@
5351
creation_year = "2017"
5452
current_year = str(datetime.datetime.now().year)
5553
year_duration = (
56-
current_year
57-
if current_year == creation_year
58-
else creation_year + " - " + current_year
54+
current_year if current_year == creation_year else creation_year + " - " + current_year
5955
)
6056
copyright = year_duration + " Scott Shawcroft"
6157
author = "Scott Shawcroft"

‎examples/hid_consumer_control_brightness.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
# SPDX-License-Identifier: MIT
33

44
import time
5+
56
import board
67
import digitalio
78
import usb_hid
9+
810
from adafruit_hid.consumer_control import ConsumerControl
911
from adafruit_hid.consumer_control_code import ConsumerControlCode
1012

‎examples/hid_joywing_gamepad.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414

1515
import board
1616
import busio
17-
from micropython import const
18-
from adafruit_seesaw.seesaw import Seesaw
1917
import usb_hid
18+
from adafruit_seesaw.seesaw import Seesaw
2019
from hid_gamepad import Gamepad
20+
from micropython import const
2121

2222

2323
def range_map(value, in_min, in_max, out_min, out_max):

‎examples/hid_keyboard_layout.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# SPDX-License-Identifier: MIT
33

44
import time
5+
56
import board
67
import digitalio
78
import usb_hid

‎examples/hid_keyboard_shortcuts.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
# SPDX-License-Identifier: MIT
33

44
import time
5+
56
import board
67
import digitalio
78
import usb_hid
9+
810
from adafruit_hid.keyboard import Keyboard
911
from adafruit_hid.keycode import Keycode
1012

‎examples/hid_simple_gamepad.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
# See this Learn Guide for details:
77
# https://learn.adafruit.com/customizing-usb-devices-in-circuitpython/hid-devices#custom-hid-devices-3096614-9
88

9+
import analogio
910
import board
1011
import digitalio
11-
import analogio
1212
import usb_hid
13-
1413
from hid_gamepad import Gamepad
1514

1615
gp = Gamepad(usb_hid.devices)

‎examples/hid_simpletest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
# SPDX-License-Identifier: MIT
33

44
import time
5+
56
import board
67
import digitalio
78
import usb_hid
9+
810
from adafruit_hid.mouse import Mouse
911

1012
mouse = Mouse(usb_hid.devices)

‎ruff.toml

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
target-version = "py38"
6+
line-length = 100
7+
8+
[lint]
9+
preview = true
10+
select = ["I", "PL", "UP"]
11+
12+
extend-select = [
13+
"D419", # empty-docstring
14+
"E501", # line-too-long
15+
"W291", # trailing-whitespace
16+
"PLC0414", # useless-import-alias
17+
"PLC2401", # non-ascii-name
18+
"PLC2801", # unnecessary-dunder-call
19+
"PLC3002", # unnecessary-direct-lambda-call
20+
"E999", # syntax-error
21+
"PLE0101", # return-in-init
22+
"F706", # return-outside-function
23+
"F704", # yield-outside-function
24+
"PLE0116", # continue-in-finally
25+
"PLE0117", # nonlocal-without-binding
26+
"PLE0241", # duplicate-bases
27+
"PLE0302", # unexpected-special-method-signature
28+
"PLE0604", # invalid-all-object
29+
"PLE0605", # invalid-all-format
30+
"PLE0643", # potential-index-error
31+
"PLE0704", # misplaced-bare-raise
32+
"PLE1141", # dict-iter-missing-items
33+
"PLE1142", # await-outside-async
34+
"PLE1205", # logging-too-many-args
35+
"PLE1206", # logging-too-few-args
36+
"PLE1307", # bad-string-format-type
37+
"PLE1310", # bad-str-strip-call
38+
"PLE1507", # invalid-envvar-value
39+
"PLE2502", # bidirectional-unicode
40+
"PLE2510", # invalid-character-backspace
41+
"PLE2512", # invalid-character-sub
42+
"PLE2513", # invalid-character-esc
43+
"PLE2514", # invalid-character-nul
44+
"PLE2515", # invalid-character-zero-width-space
45+
"PLR0124", # comparison-with-itself
46+
"PLR0202", # no-classmethod-decorator
47+
"PLR0203", # no-staticmethod-decorator
48+
"UP004", # useless-object-inheritance
49+
"PLR0206", # property-with-parameters
50+
"PLR0904", # too-many-public-methods
51+
"PLR0911", # too-many-return-statements
52+
"PLR0912", # too-many-branches
53+
"PLR0913", # too-many-arguments
54+
"PLR0914", # too-many-locals
55+
"PLR0915", # too-many-statements
56+
"PLR0916", # too-many-boolean-expressions
57+
"PLR1702", # too-many-nested-blocks
58+
"PLR1704", # redefined-argument-from-local
59+
"PLR1711", # useless-return
60+
"C416", # unnecessary-comprehension
61+
"PLR1733", # unnecessary-dict-index-lookup
62+
"PLR1736", # unnecessary-list-index-lookup
63+
64+
# ruff reports this rule is unstable
65+
#"PLR6301", # no-self-use
66+
67+
"PLW0108", # unnecessary-lambda
68+
"PLW0120", # useless-else-on-loop
69+
"PLW0127", # self-assigning-variable
70+
"PLW0129", # assert-on-string-literal
71+
"B033", # duplicate-value
72+
"PLW0131", # named-expr-without-context
73+
"PLW0245", # super-without-brackets
74+
"PLW0406", # import-self
75+
"PLW0602", # global-variable-not-assigned
76+
"PLW0603", # global-statement
77+
"PLW0604", # global-at-module-level
78+
79+
# fails on the try: import typing used by libraries
80+
#"F401", # unused-import
81+
82+
"F841", # unused-variable
83+
"E722", # bare-except
84+
"PLW0711", # binary-op-exception
85+
"PLW1501", # bad-open-mode
86+
"PLW1508", # invalid-envvar-default
87+
"PLW1509", # subprocess-popen-preexec-fn
88+
"PLW2101", # useless-with-lock
89+
"PLW3301", # nested-min-max
90+
]
91+
92+
ignore = [
93+
"PLR2004", # magic-value-comparison
94+
"UP030", # format literals
95+
"PLW1514", # unspecified-encoding
96+
"PLR0913", # too-many-arguments
97+
"PLR0915", # too-many-statements
98+
"PLR0917", # too-many-positional-arguments
99+
"PLR0904", # too-many-public-methods
100+
"PLR0912", # too-many-branches
101+
"PLR0916", # too-many-boolean-expressions
102+
"PLR6301", # could-be-static no-self-use
103+
"PLC0415", # import outside toplevel
104+
"PLC2701", # private import
105+
"PLW2901", # loop var overwrite
106+
]
107+
108+
[format]
109+
line-ending = "lf"

0 commit comments

Comments
 (0)
Please sign in to comment.