Skip to content

Commit f1b9548

Browse files
authoredMay 16, 2025··
Merge pull request #26 from adafruit/use_ruff
change to ruff
2 parents e888bcb + e3736d5 commit f1b9548

File tree

12 files changed

+164
-488
lines changed

12 files changed

+164
-488
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
@@ -13,9 +13,9 @@ Introduction
1313
:target: https://github.com/adafruit/Adafruit_CircuitPython_IterTools/actions
1414
:alt: Build Status
1515

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

2020
Python's itertools for CircuitPython
2121

‎adafruit_itertools/__init__.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
* Adafruit CircuitPython firmware for the supported boards:
2323
https://github.com/adafruit/circuitpython/releases
2424
"""
25-
# pylint:disable=invalid-name,redefined-builtin,attribute-defined-outside-init
26-
# pylint:disable=stop-iteration-return,anomalous-backslash-in-string
2725

2826
__version__ = "0.0.0+auto.0"
2927
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Itertools.git"
@@ -41,6 +39,7 @@
4139
TypeVar,
4240
Union,
4341
)
42+
4443
from typing_extensions import TypeAlias
4544

4645
_T = TypeVar("_T")
@@ -140,9 +139,7 @@ def combinations(iterable: Iterable[_T], r: int) -> Iterator[Tuple[_T, ...]]:
140139
yield tuple(pool[i] for i in indices)
141140

142141

143-
def combinations_with_replacement(
144-
iterable: Iterable[_T], r: int
145-
) -> Iterator[Tuple[_T, ...]]:
142+
def combinations_with_replacement(iterable: Iterable[_T], r: int) -> Iterator[Tuple[_T, ...]]:
146143
"""Return r length subsequences of elements from the input iterable allowing
147144
individual elements to be repeated more than once.
148145
@@ -245,9 +242,7 @@ def dropwhile(predicate: _Predicate[_T], iterable: Iterable[_T]) -> Iterator[_T]
245242
yield x
246243

247244

248-
def filterfalse(
249-
predicate: Optional[_Predicate[_T]], iterable: Iterable[_T]
250-
) -> Iterator[_T]:
245+
def filterfalse(predicate: Optional[_Predicate[_T]], iterable: Iterable[_T]) -> Iterator[_T]:
251246
"""Make an iterator that filters elements from iterable returning only those
252247
for which the predicate is False. If predicate is None, return the items
253248
that are false.
@@ -391,9 +386,7 @@ def islice(
391386
return
392387

393388

394-
def permutations(
395-
iterable: Iterable[_T], r: Optional[int] = None
396-
) -> Iterator[Tuple[_T, ...]]:
389+
def permutations(iterable: Iterable[_T], r: Optional[int] = None) -> Iterator[Tuple[_T, ...]]:
397390
"""Return successive r length permutations of elements in the iterable.
398391
399392
If r is not specified or is None, then r defaults to the length of the
@@ -486,9 +479,7 @@ def repeat(el: _T, n: Optional[int] = None) -> Iterator[_T]:
486479
yield el
487480

488481

489-
def starmap(
490-
function: Callable[..., _T], iterable: Iterable[Iterable[Any]]
491-
) -> Iterator[_T]:
482+
def starmap(function: Callable[..., _T], iterable: Iterable[Iterable[Any]]) -> Iterator[_T]:
492483
"""Make an iterator that computes the function using arguments obtained from
493484
the iterable. Used instead of map() when argument parameters are already
494485
grouped in tuples from a single iterable (the data has been “pre-zipped”).
@@ -529,9 +520,7 @@ def tee(iterable: Iterable[_T], n: int = 2) -> Sequence[Iterator[_T]]:
529520
return [iter(iterable) for _ in range(n)]
530521

531522

532-
def zip_longest(
533-
*args: Iterable[Any], fillvalue: _OptionalFill = None
534-
) -> Iterator[Tuple[Any, ...]]:
523+
def zip_longest(*args: Iterable[Any], fillvalue: _OptionalFill = None) -> Iterator[Tuple[Any, ...]]:
535524
"""Make an iterator that aggregates elements from each of the
536525
iterables. If the iterables are of uneven length, missing values are
537526
filled-in with fillvalue. Iteration continues until the longest

‎adafruit_itertools/adafruit_itertools_extras.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@
3737
https://github.com/adafruit/circuitpython/releases
3838
"""
3939

40-
# pylint:disable=invalid-name,keyword-arg-before-vararg,relative-beyond-top-level
41-
4240
import adafruit_itertools as it
4341

4442
try:
@@ -54,6 +52,7 @@
5452
TypeVar,
5553
Union,
5654
)
55+
5756
from typing_extensions import TypeAlias
5857

5958
_T = TypeVar("_T")
@@ -230,9 +229,7 @@ def pairwise(iterable: Iterable[_T]) -> Iterator[Tuple[_T, _T]]:
230229
return zip(a, b)
231230

232231

233-
def partition(
234-
pred: _Predicate[_T], iterable: Iterable[_T]
235-
) -> Tuple[Iterator[_T], Iterator[_T]]:
232+
def partition(pred: _Predicate[_T], iterable: Iterable[_T]) -> Tuple[Iterator[_T], Iterator[_T]]:
236233
"""Use a predicate to partition entries into false entries and true entries.
237234
238235
:param pred: the predicate that divides the values
@@ -267,9 +264,7 @@ def quantify(iterable: Iterable[_T], pred: _Predicate[_T] = bool) -> int:
267264
return sum(map(pred, iterable))
268265

269266

270-
def repeatfunc(
271-
func: Callable[..., _T], times: Optional[int] = None, *args: Any
272-
) -> Iterator[_T]:
267+
def repeatfunc(func: Callable[..., _T], times: Optional[int] = None, *args: Any) -> Iterator[_T]:
273268
"""Repeat calls to func with specified arguments.
274269
275270
Example: repeatfunc(random.random)

‎docs/api.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
.. If your library file(s) are nested in a directory (e.g. /adafruit_foo/foo.py)
55
.. use this format as the module name: "adafruit_foo.foo"
66
7+
API Reference
8+
#############
9+
710
.. automodule:: adafruit_itertools
811
:members:
912

‎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

@@ -41,9 +39,7 @@
4139
creation_year = "2019"
4240
current_year = str(datetime.datetime.now().year)
4341
year_duration = (
44-
current_year
45-
if current_year == creation_year
46-
else creation_year + " - " + current_year
42+
current_year if current_year == creation_year else creation_year + " - " + current_year
4743
)
4844
copyright = year_duration + " Dave Astels"
4945
author = "Dave Astels"

‎examples/itertools_simpletest.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@
2424
# THE SOFTWARE.
2525

2626
import time
27+
28+
import adafruit_si7021
2729
import board
2830
import busio
29-
import adafruit_si7021
31+
3032
from adafruit_itertools import count
3133
from adafruit_itertools.adafruit_itertools_extras import repeatfunc
3234

33-
3435
i2c = busio.I2C(board.SCL, board.SDA)
3536
sensor = adafruit_si7021.SI7021(i2c)
3637

‎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+
"UP028", # for loop yield
106+
]
107+
108+
[format]
109+
line-ending = "lf"

‎tests/test_itertools.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# SPDX-FileCopyrightText: KB Sriram
22
# SPDX-License-Identifier: MIT
33

4-
from typing import Any, Callable, Iterator, Optional, Sequence, Tuple, TypeVar, Union
54
import itertools as it
5+
from typing import Any, Callable, Iterator, Optional, Sequence, Tuple, TypeVar, Union
6+
67
import pytest
8+
79
import adafruit_itertools as ait
810

911
_K = TypeVar("_K")
@@ -41,7 +43,8 @@ def test_accumulate_types() -> None:
4143
assert list(x_str_f) == list(it.accumulate("abc", lambda a, x: a + x))
4244

4345
x_bad_arg_f: Iterator[int] = ait.accumulate(
44-
[1, 2], lambda a, x: a + ord(x) # type: ignore[arg-type]
46+
[1, 2],
47+
lambda a, x: a + ord(x), # type: ignore[arg-type]
4548
)
4649
with pytest.raises(TypeError):
4750
list(x_bad_arg_f)
@@ -206,9 +209,7 @@ def test_dropwhile(predicate: Callable[[_T], object], seq: Sequence[_T]) -> None
206209
(lambda x: x % 2, range(10)),
207210
],
208211
)
209-
def test_filterfalse(
210-
predicate: Optional[Callable[[_T], object]], seq: Sequence[_T]
211-
) -> None:
212+
def test_filterfalse(predicate: Optional[Callable[[_T], object]], seq: Sequence[_T]) -> None:
212213
x: Iterator[_T] = ait.filterfalse(predicate, seq)
213214
y: Iterator[_T] = it.filterfalse(predicate, seq)
214215
assert list(x) == list(y)
@@ -228,9 +229,7 @@ def test_filterfalse(
228229
],
229230
)
230231
def test_groupby(data: Sequence[_T], key: Callable[[_T], _K]) -> None:
231-
def _listify(
232-
iterable: Iterator[Tuple[_K, Iterator[_T]]]
233-
) -> Sequence[Tuple[_K, Sequence[_T]]]:
232+
def _listify(iterable: Iterator[Tuple[_K, Iterator[_T]]]) -> Sequence[Tuple[_K, Sequence[_T]]]:
234233
return [(k, list(group)) for k, group in iterable]
235234

236235
it_l = _listify(it.groupby(data, key))

‎tests/test_itertools_extras.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
Sequence,
99
TypeVar,
1010
)
11-
from typing_extensions import TypeAlias
1211

1312
import more_itertools as itextras
1413
import pytest
14+
from typing_extensions import TypeAlias
15+
1516
from adafruit_itertools import adafruit_itertools_extras as aextras
1617

1718
_K = TypeVar("_K")
@@ -66,9 +67,7 @@ def test_dotproduct(vec1: Sequence[int], vec2: Sequence[int]) -> None:
6667
([], -1, lambda _: True),
6768
],
6869
)
69-
def test_first_true(
70-
seq: Sequence[_T], dflt: _T, pred: Optional[_Predicate[_T]]
71-
) -> None:
70+
def test_first_true(seq: Sequence[_T], dflt: _T, pred: Optional[_Predicate[_T]]) -> None:
7271
assert itextras.first_true(seq, dflt, pred) == aextras.first_true(seq, dflt, pred)
7372

7473

@@ -84,12 +83,8 @@ def test_first_true(
8483
def test_flatten(seq1: str, seq2: str) -> None:
8584
assert list(itextras.flatten(seq1 + seq2)) == list(aextras.flatten(seq1 + seq2))
8685
for repeat in range(3):
87-
assert list(itextras.flatten([seq1] * repeat)) == list(
88-
aextras.flatten([seq1] * repeat)
89-
)
90-
assert list(itextras.flatten([seq2] * repeat)) == list(
91-
aextras.flatten([seq2] * repeat)
92-
)
86+
assert list(itextras.flatten([seq1] * repeat)) == list(aextras.flatten([seq1] * repeat))
87+
assert list(itextras.flatten([seq2] * repeat)) == list(aextras.flatten([seq2] * repeat))
9388

9489

9590
@pytest.mark.parametrize(
@@ -254,9 +249,7 @@ def test_roundrobin(seq1: str, seq2: str) -> None:
254249
)
255250
def test_tabulate(func: Callable[[int], int], start: int) -> None:
256251
assert _take(5, itextras.tabulate(func)) == _take(5, aextras.tabulate(func))
257-
assert _take(5, itextras.tabulate(func, start)) == _take(
258-
5, aextras.tabulate(func, start)
259-
)
252+
assert _take(5, itextras.tabulate(func, start)) == _take(5, aextras.tabulate(func, start))
260253

261254

262255
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)
Please sign in to comment.