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 af99fb8

Browse files
authoredMay 14, 2025··
Merge pull request #40 from adafruit/use_ruff
change to ruff
2 parents ce04ca1 + 9d63b07 commit af99fb8

12 files changed

+156
-458
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
@@ -15,9 +15,9 @@ Introduction
1515
:target: https://github.com/adafruit/Adafruit_CircuitPython_MCP9808/actions/
1616
:alt: Build Status
1717

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

2222
The MCP9808 is an awesome, high accuracy temperature sensor that communicates
2323
over I2C. Its available on `Adafruit as a breakout <https://www.adafruit.com/products/1782>`_.

‎adafruit_mcp9808.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,16 @@
3636
3737
"""
3838

39-
from micropython import const
4039
from adafruit_bus_device.i2c_device import I2CDevice
41-
from adafruit_register.i2c_bits import RWBits
4240
from adafruit_register.i2c_bit import ROBit
41+
from adafruit_register.i2c_bits import RWBits
42+
from micropython import const
4343

4444
try:
45-
import typing # pylint: disable=unused-import
46-
from typing_extensions import Literal
45+
import typing
46+
4747
from busio import I2C
48+
from typing_extensions import Literal
4849
except ImportError:
4950
pass
5051

@@ -145,9 +146,7 @@ def __init__(self, i2c_bus: I2C, address: int = _MCP9808_DEFAULT_ADDRESS) -> Non
145146
i2c.write_then_readinto(self.buf, self.buf, out_end=1, in_start=1)
146147

147148
if not ok or self.buf[1] != 0x04:
148-
raise ValueError(
149-
"Unable to find MCP9808 at i2c address " + str(hex(address))
150-
)
149+
raise ValueError("Unable to find MCP9808 at i2c address " + str(hex(address)))
151150

152151
@property
153152
def temperature(self) -> float:
@@ -167,9 +166,7 @@ def _temp_conv(self) -> float:
167166
return (self.buf[1] * 16 + self.buf[2] / 16.0) - 256
168167
return self.buf[1] * 16 + self.buf[2] / 16.0
169168

170-
def _limit_temperatures(
171-
self, temp: int, t_address: Literal[0x02, 0x03, 0x04] = 0x02
172-
) -> None:
169+
def _limit_temperatures(self, temp: int, t_address: Literal[0x02, 0x03, 0x04] = 0x02) -> None:
173170
"""Internal function to setup limit temperature
174171
175172
:param int temp: temperature limit
@@ -263,4 +260,4 @@ def resolution(self) -> Literal[0, 1, 2, 3]:
263260
def resolution(self, resol_value: Literal[0, 1, 2, 3] = 3) -> None:
264261
"""Setup Critical temperature"""
265262

266-
self._MCP9808_REG_RESOLUTION_SET = resol_value # pylint: disable=invalid-name
263+
self._MCP9808_REG_RESOLUTION_SET = resol_value

‎docs/api.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
API Reference
2+
#############
13

24
.. automodule:: adafruit_mcp9808
35
: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

@@ -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/mcp9808_average_temp_mqtt.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
Using discovery topic to create entity in Home Assistant.
66
"""
77

8-
import time
98
import json
9+
import time
1010
from array import array
11+
1112
import board
12-
import paho.mqtt.client as mqtt
1313
import numpy as np
14-
import adafruit_mcp9808
14+
import paho.mqtt.client as mqtt
1515

16+
import adafruit_mcp9808
1617

1718
i2c = board.I2C() # uses board.SCL and board.SDA
1819
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
@@ -64,9 +65,9 @@
6465
print(len(temp1m))
6566
for count in range(0, 9):
6667
temp1m[count] = mcp.temperature
67-
print("Temperature: {} C ".format(mcp.temperature))
68+
print(f"Temperature: {mcp.temperature} C ")
6869
avgtemp = round(np.average(temp1m), 1)
69-
print("avgtemp {} C".format(avgtemp))
70+
print(f"avgtemp {avgtemp} C")
7071
time.sleep(10)
7172
send_msg = {"temperature": avgtemp}
7273
client.publish(

‎examples/mcp9808_displayio_simpletest.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
# SPDX-License-Identifier: MIT
55

66
import time
7+
78
import board
89
from adafruit_display_text.bitmap_label import Label
9-
from terminalio import FONT
1010
from displayio import Group
11+
from terminalio import FONT
12+
1113
import adafruit_mcp9808
1214

1315
# Simple demo of using the built-in display.

‎examples/mcp9808_simpletest.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
# SPDX-License-Identifier: MIT
33

44
import time
5+
56
import board
7+
68
import adafruit_mcp9808
79

810
i2c = board.I2C() # uses board.SCL and board.SDA
@@ -18,5 +20,5 @@
1820
while True:
1921
tempC = mcp.temperature
2022
tempF = tempC * 9 / 5 + 32
21-
print("Temperature: {} C {} F ".format(tempC, tempF))
23+
print(f"Temperature: {tempC} C {tempF} F ")
2224
time.sleep(2)

‎examples/mcp9808_temperature_limits.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
"""
77

88
import time
9+
910
import board
11+
1012
import adafruit_mcp9808
1113

1214
i2c = board.I2C() # uses board.SCL and board.SDA

‎ruff.toml

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

0 commit comments

Comments
 (0)
Please sign in to comment.