Skip to content

change to ruff #32

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 15, 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: v2.17.4
- 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_TSL2591/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://github.com/raw/astral-sh/ruff/main/assets/badge/v2.json
:target: https://github.com/astral-sh/ruff
:alt: Code Style: Ruff

CircuitPython module for the TSL2591 high precision light sensor.

22 changes: 8 additions & 14 deletions adafruit_tsl2591.py
Original file line number Diff line number Diff line change
@@ -26,12 +26,13 @@
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
"""
from micropython import const

from adafruit_bus_device import i2c_device
from micropython import const

try:
from typing import Tuple

from busio import I2C
except ImportError:
pass
@@ -166,9 +167,6 @@ def _read_u8(self, address: int) -> int:
i2c.write_then_readinto(self._BUFFER, self._BUFFER, out_end=1, in_end=1)
return self._BUFFER[0]

# Disable invalid name check since pylint isn't smart enough to know LE
# is an abbreviation for little-endian.
# pylint: disable=invalid-name
def _read_u16LE(self, address: int) -> int:
# Read a 16-bit little-endian unsigned value from the specified 8-bit
# address.
@@ -178,8 +176,6 @@ def _read_u16LE(self, address: int) -> int:
i2c.write_then_readinto(self._BUFFER, self._BUFFER, out_end=1, in_end=2)
return (self._BUFFER[1] << 8) | self._BUFFER[0]

# pylint: enable=invalid-name

def _write_u8(self, address: int, val: int) -> None:
# Write an 8-bit unsigned value to the specified 8-bit address.
with self._device as i2c:
@@ -206,11 +202,11 @@ def clear_interrupt(self, operation: int) -> None:
- ``CLEAR_ALL_INTERRUPTS``
- ``CLEAR_PERSIST_INTERRUPT``
"""
assert operation in (
assert operation in {
CLEAR_INTERRUPT,
CLEAR_ALL_INTERRUPTS,
CLEAR_PERSIST_INTERRUPT,
)
}
control = (_TSL2591_SPECIAL_BIT | operation) & 0xFF
with self._device as i2c:
self._BUFFER[0] = control
@@ -228,7 +224,7 @@ def enable_interrupt(self, interrupts: int) -> None:
- ``ENABLE_AIEN``
- ``ENABLE_NPAIEN``
"""
assert interrupts in (ENABLE_NPIEN, ENABLE_AIEN, (ENABLE_NPIEN | ENABLE_AIEN))
assert interrupts in {ENABLE_NPIEN, ENABLE_AIEN, (ENABLE_NPIEN | ENABLE_AIEN)}
functions = self._read_u8(_TSL2591_REGISTER_ENABLE)
functions = (functions | interrupts) & 0xFF
self._write_u8(_TSL2591_REGISTER_ENABLE, functions)
@@ -240,7 +236,7 @@ def disable_interrupt(self, interrupts: int) -> None:
- ``ENABLE_AIEN``
- ``ENABLE_NPAIEN``
"""
assert interrupts in (ENABLE_NPIEN, ENABLE_AIEN, (ENABLE_NPIEN | ENABLE_AIEN))
assert interrupts in {ENABLE_NPIEN, ENABLE_AIEN, (ENABLE_NPIEN | ENABLE_AIEN)}
functions = self._read_u8(_TSL2591_REGISTER_ENABLE)
functions = (functions & ~interrupts) & 0xFF
self._write_u8(_TSL2591_REGISTER_ENABLE, functions)
@@ -259,7 +255,7 @@ def gain(self) -> int:

@gain.setter
def gain(self, val: int) -> None:
assert val in (GAIN_LOW, GAIN_MED, GAIN_HIGH, GAIN_MAX)
assert val in {GAIN_LOW, GAIN_MED, GAIN_HIGH, GAIN_MAX}
# Set appropriate gain value.
control = self._read_u8(_TSL2591_REGISTER_CONTROL)
control &= 0b11001111
@@ -457,7 +453,5 @@ def lux(self) -> float:
again = 9876.0
cpl = (atime * again) / _TSL2591_LUX_DF
lux1 = (channel_0 - (_TSL2591_LUX_COEFB * channel_1)) / cpl
lux2 = (
(_TSL2591_LUX_COEFC * channel_0) - (_TSL2591_LUX_COEFD * channel_1)
) / cpl
lux2 = ((_TSL2591_LUX_COEFC * channel_0) - (_TSL2591_LUX_COEFD * channel_1)) / cpl
return max(lux1, lux2)
3 changes: 3 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@

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

.. automodule:: adafruit_tsl2591
: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: 2021 ladyada for Adafruit Industries
#
# SPDX-License-Identifier: MIT

import datetime
import os
import sys
import datetime

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

@@ -49,9 +47,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 + " Tony DiCola"
author = "Tony DiCola"
4 changes: 3 additions & 1 deletion examples/tsl2591_displayio_simpletest.py
Original file line number Diff line number Diff line change
@@ -4,10 +4,12 @@
# SPDX-License-Identifier: MIT

import time

import board
from adafruit_display_text.bitmap_label import Label
from terminalio import FONT
from displayio import Group
from terminalio import FONT

import adafruit_tsl2591

# Simple demo of using the built-in display.
10 changes: 6 additions & 4 deletions examples/tsl2591_simpletest.py
Original file line number Diff line number Diff line change
@@ -4,7 +4,9 @@
# Simple demo of the TSL2591 sensor. Will print the detected light value
# every second.
import time

import board

import adafruit_tsl2591

# Create sensor object, communicating over the board's default I2C bus
@@ -30,17 +32,17 @@
while True:
# Read and calculate the light level in lux.
lux = sensor.lux
print("Total light: {0}lux".format(lux))
print(f"Total light: {lux}lux")
# You can also read the raw infrared and visible light levels.
# These are unsigned, the higher the number the more light of that type.
# There are no units like lux.
# Infrared levels range from 0-65535 (16-bit)
infrared = sensor.infrared
print("Infrared light: {0}".format(infrared))
print(f"Infrared light: {infrared}")
# Visible-only levels range from 0-2147483647 (32-bit)
visible = sensor.visible
print("Visible light: {0}".format(visible))
print(f"Visible light: {visible}")
# Full spectrum (visible + IR) also range from 0-2147483647 (32-bit)
full_spectrum = sensor.full_spectrum
print("Full spectrum (IR + visible) light: {0}".format(full_spectrum))
print(f"Full spectrum (IR + visible) light: {full_spectrum}")
time.sleep(1.0)
106 changes: 106 additions & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# 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
]

[format]
line-ending = "lf"