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 4014be2

Browse files
authoredMay 15, 2025··
Merge pull request #59 from adafruit/use_ruff
change to ruff
2 parents 988199f + 0c79575 commit 4014be2

File tree

9 files changed

+147
-465
lines changed

9 files changed

+147
-465
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: v2.17.4
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_SD/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
CircuitPython driver for SD cards. This implements the basic reading and writing
2222
block functionality needed to mount an SD card using `storage.VfsFat`.

‎adafruit_sdcard.py

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,16 @@
4141
"""
4242

4343
import time
44-
from micropython import const
44+
4545
from adafruit_bus_device import spi_device
46+
from micropython import const
4647

4748
try:
48-
from typing import Union, Optional
49+
from typing import Optional, Union
50+
4951
from busio import SPI
50-
from digitalio import DigitalInOut
5152
from circuitpython_typing import ReadableBuffer, WriteableBuffer
53+
from digitalio import DigitalInOut
5254
except ImportError:
5355
pass
5456

@@ -69,7 +71,6 @@
6971
_TOKEN_DATA = const(0xFE)
7072

7173

72-
# pylint: disable-msg=superfluous-parens
7374
class SDCard:
7475
"""Controls an SD card over SPI.
7576
@@ -97,7 +98,6 @@ class SDCard:
9798
9899
"""
99100

100-
# pylint: disable=invalid-name
101101
def __init__(self, spi: SPI, cs: DigitalInOut, baudrate: int = 1320000) -> None:
102102
# Create an SPIDevice running at a lower initialization baudrate first.
103103
self._spi = spi_device.SPIDevice(spi, cs, baudrate=250000, extra_clocks=8)
@@ -204,9 +204,6 @@ def _wait_for_ready(self, card: SPI, timeout: float = 0.3) -> None:
204204
while time.monotonic() - start_time < timeout and self._single_byte[0] != 0xFF:
205205
card.readinto(self._single_byte, write_value=0xFF)
206206

207-
# pylint: disable-msg=too-many-arguments
208-
# pylint: disable=no-member
209-
# pylint: disable-msg=too-many-branches
210207
# no-member disable should be reconsidered when it can be tested
211208
def _cmd(
212209
self,
@@ -253,7 +250,6 @@ def _cmd(
253250

254251
card.write(buf)
255252

256-
# pylint: disable-msg=too-many-nested-blocks
257253
# wait for the response (response[7] == 0)
258254
for _ in range(_CMD_TIMEOUT):
259255
card.readinto(buf, end=1, write_value=0xFF)
@@ -275,10 +271,6 @@ def _cmd(
275271
return buf[0]
276272
return -1
277273

278-
# pylint: enable-msg=too-many-branches
279-
# pylint: enable-msg=too-many-arguments
280-
281-
# pylint: disable-msg=too-many-arguments
282274
def _block_cmd(
283275
self,
284276
card: SPI,
@@ -327,15 +319,12 @@ def _block_cmd(
327319
result = buf[0]
328320
break
329321

330-
# pylint: disable=singleton-comparison
331322
# Disable should be removed when refactor can be tested.
332323
if response_buf != None and result == 0:
333324
self._readinto(card, response_buf)
334325

335326
return result
336327

337-
# pylint: enable-msg=too-many-arguments
338-
339328
def _cmd_nodata(self, card: SPI, cmd: int, response: int = 0xFF) -> int:
340329
"""
341330
Issue a command to the card with no argument.
@@ -379,7 +368,6 @@ def _readinto(
379368
# read checksum and throw it away
380369
card.readinto(self._cmdbuf, end=2, write_value=0xFF)
381370

382-
# pylint: disable-msg=too-many-arguments
383371
def _write(
384372
self,
385373
card: SPI,
@@ -412,7 +400,7 @@ def _write(
412400
card.write(cmd, end=2)
413401

414402
# check the response
415-
# pylint: disable=no-else-return
403+
416404
# Disable should be removed when refactor can be tested
417405
for _ in range(_CMD_TIMEOUT):
418406
card.readinto(cmd, end=1, write_value=0xFF)
@@ -429,8 +417,6 @@ def _write(
429417

430418
return 0 # worked
431419

432-
# pylint: enable-msg=too-many-arguments
433-
434420
def count(self) -> int:
435421
"""
436422
Returns the total number of sectors.
@@ -498,9 +484,7 @@ def writeblocks(self, start_block: int, buf: ReadableBuffer) -> int:
498484
# send the data
499485
offset = 0
500486
while nblocks:
501-
self._write(
502-
card, _TOKEN_CMD25, buf, start=offset, end=(offset + 512)
503-
)
487+
self._write(card, _TOKEN_CMD25, buf, start=offset, end=(offset + 512))
504488
offset += 512
505489
nblocks -= 1
506490
self._wait_for_ready(card)

‎docs/api.rst

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

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

‎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: 2021 ladyada 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

@@ -47,9 +45,7 @@
4745
creation_year = "2017"
4846
current_year = str(datetime.datetime.now().year)
4947
year_duration = (
50-
current_year
51-
if current_year == creation_year
52-
else creation_year + " - " + current_year
48+
current_year if current_year == creation_year else creation_year + " - " + current_year
5349
)
5450
copyright = year_duration + " Scott Shawcroft"
5551
author = "Scott Shawcroft"

‎examples/sd_read_simpletest.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
# SPDX-License-Identifier: MIT
33

44
import os
5+
6+
import board
57
import busio
68
import digitalio
7-
import board
89
import storage
10+
911
import adafruit_sdcard
1012

1113
# The SD_CS pin is the chip select line.
@@ -47,7 +49,7 @@ def print_directory(path, tabs=0):
4749
prettyprintname += file
4850
if isdir:
4951
prettyprintname += "/"
50-
print("{0:<40} Size: {1:>10}".format(prettyprintname, sizestr))
52+
print(f"{prettyprintname:<40} Size: {sizestr:>10}")
5153

5254
# recursively print directory contents
5355
if isdir:

‎ruff.toml

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
"PLR1702", # too-many-nested-blocks
103+
]
104+
105+
[format]
106+
line-ending = "lf"

0 commit comments

Comments
 (0)
Please sign in to comment.