From 2b8bbb9ce34cbdba83d415d4fcea6dfce947dece Mon Sep 17 00:00:00 2001 From: BiffoBear Date: Tue, 25 Oct 2022 14:06:17 +0300 Subject: [PATCH 1/5] First commit after adding typing to functions. --- adafruit_character_lcd/character_lcd.py | 145 ++++++++++-------- adafruit_character_lcd/character_lcd_i2c.py | 18 ++- .../character_lcd_rgb_i2c.py | 19 ++- adafruit_character_lcd/character_lcd_spi.py | 15 +- 4 files changed, 123 insertions(+), 74 deletions(-) diff --git a/adafruit_character_lcd/character_lcd.py b/adafruit_character_lcd/character_lcd.py index b384524..f0deb84 100644 --- a/adafruit_character_lcd/character_lcd.py +++ b/adafruit_character_lcd/character_lcd.py @@ -30,6 +30,12 @@ """ +try: + from typing import Union, Optional, List, Sequence + from circuitpython_typing import pwmio +except ImportError: + pass + import time import digitalio from micropython import const @@ -73,7 +79,7 @@ _LCD_ROW_OFFSETS = (0x00, 0x40, 0x14, 0x54) -def _set_bit(byte_value, position, val): +def _set_bit(byte_value: int, position: int, val: bool) -> int: # Given the specified byte_value set the bit at position to the provided # boolean value val and return the modified byte. ret = None @@ -84,7 +90,7 @@ def _set_bit(byte_value, position, val): return ret -def _map(xval, in_min, in_max, out_min, out_max): +def _map(xval: int, in_min: int, in_max: int, out_min: int, out_max: int) -> float: # Affine transfer/map with constrained output. outrange = float(out_max - out_min) inrange = float(in_max - in_min) @@ -106,8 +112,8 @@ class Character_LCD: :param ~digitalio.DigitalInOut d5: The data line 5 :param ~digitalio.DigitalInOut d6: The data line 6 :param ~digitalio.DigitalInOut d7: The data line 7 - :param columns: The columns on the charLCD - :param lines: The lines on the charLCD + :param int columns: The columns on the charLCD + :param int lines: The lines on the charLCD """ @@ -115,7 +121,17 @@ class Character_LCD: RIGHT_TO_LEFT = const(1) # pylint: disable-msg=too-many-arguments - def __init__(self, rs, en, d4, d5, d6, d7, columns, lines): + def __init__( + self, + rs: digitalio.DigitalInOut, + en: digitalio.DigitalInOut, + d4: digitalio.DigitalInOut, + d5: digitalio.DigitalInOut, + d6: digitalio.DigitalInOut, + d7: digitalio.DigitalInOut, + columns: int, + lines: int, + ) -> None: self.columns = columns self.lines = lines @@ -147,7 +163,6 @@ def __init__(self, rs, en, d4, d5, d6, d7, columns, lines): # Set entry mode self._write8(_LCD_ENTRYMODESET | self.displaymode) self.clear() - self._message = None self._enable = None self._direction = None @@ -159,12 +174,12 @@ def __init__(self, rs, en, d4, d5, d6, d7, columns, lines): # pylint: enable-msg=too-many-arguments - def home(self): + def home(self) -> None: """Moves the cursor "home" to position (0, 0).""" self._write8(_LCD_RETURNHOME) time.sleep(0.003) - def clear(self): + def clear(self) -> None: """Clears everything displayed on the LCD. The following example displays, "Hello, world!", then clears the LCD. @@ -186,21 +201,21 @@ def clear(self): time.sleep(0.003) @property - def column_align(self): + def column_align(self) -> bool: """If True, message text after '\\n' starts directly below start of first character in message. If False, text after '\\n' starts at column zero. """ return self._column_align @column_align.setter - def column_align(self, enable): + def column_align(self, enable: bool): if isinstance(enable, bool): self._column_align = enable else: raise ValueError("The column_align value must be either True or False") @property - def cursor(self): + def cursor(self) -> bool: """True if cursor is visible. False to stop displaying the cursor. The following example shows the cursor after a displayed message: @@ -222,19 +237,19 @@ def cursor(self): return self.displaycontrol & _LCD_CURSORON == _LCD_CURSORON @cursor.setter - def cursor(self, show): + def cursor(self, show: bool) -> None: if show: self.displaycontrol |= _LCD_CURSORON else: self.displaycontrol &= ~_LCD_CURSORON self._write8(_LCD_DISPLAYCONTROL | self.displaycontrol) - def cursor_position(self, column, row): + def cursor_position(self, column: int, row: int) -> None: """Move the cursor to position ``column``, ``row`` for the next message only. Displaying a message resets the cursor position to (0, 0). - :param column: column location - :param row: row location + :param int column: column location + :param int row: row location """ # Clamp row to the last row of the display if row >= self.lines: @@ -249,7 +264,7 @@ def cursor_position(self, column, row): self.column = column @property - def blink(self): + def blink(self) -> bool: """ Blink the cursor. True to blink the cursor. False to stop blinking. @@ -272,7 +287,7 @@ def blink(self): return self.displaycontrol & _LCD_BLINKON == _LCD_BLINKON @blink.setter - def blink(self, blink): + def blink(self, blink: bool) -> None: if blink: self.displaycontrol |= _LCD_BLINKON else: @@ -280,7 +295,7 @@ def blink(self, blink): self._write8(_LCD_DISPLAYCONTROL | self.displaycontrol) @property - def display(self): + def display(self) -> bool: """ Enable or disable the display. True to enable the display. False to disable the display. @@ -302,7 +317,7 @@ def display(self): return self.displaycontrol & _LCD_DISPLAYON == _LCD_DISPLAYON @display.setter - def display(self, enable): + def display(self, enable: bool) -> None: if enable: self.displaycontrol |= _LCD_DISPLAYON else: @@ -310,7 +325,7 @@ def display(self, enable): self._write8(_LCD_DISPLAYCONTROL | self.displaycontrol) @property - def message(self): + def message(self) -> Optional[str]: """Display a string of text on the character LCD. Start position is (0,0) if cursor_position is not set. If cursor_position is set, message starts at the set @@ -335,7 +350,7 @@ def message(self): return self._message @message.setter - def message(self, message): + def message(self, message: str): self._message = message # Set line to match self.row from cursor_position() line = self.row @@ -377,7 +392,7 @@ def message(self, message): # reset column and row to (0,0) after message is displayed self.column, self.row = 0, 0 - def move_left(self): + def move_left(self) -> None: """Moves displayed text left one column. The following example scrolls a message to the left off the screen. @@ -400,7 +415,7 @@ def move_left(self): """ self._write8(_LCD_CURSORSHIFT | _LCD_DISPLAYMOVE | _LCD_MOVELEFT) - def move_right(self): + def move_right(self) -> None: """Moves displayed text right one column. The following example scrolls a message to the right off the screen. @@ -424,7 +439,7 @@ def move_right(self): self._write8(_LCD_CURSORSHIFT | _LCD_DISPLAYMOVE | _LCD_MOVERIGHT) @property - def text_direction(self): + def text_direction(self) -> Optional[int]: """The direction the text is displayed. To display the text left to right beginning on the left side of the LCD, set ``text_direction = LEFT_TO_RIGHT``. To display the text right to left beginning on the right size of the LCD, set ``text_direction = RIGHT_TO_LEFT``. @@ -448,24 +463,26 @@ def text_direction(self): return self._direction @text_direction.setter - def text_direction(self, direction): + def text_direction(self, direction: int) -> None: self._direction = direction if direction == self.LEFT_TO_RIGHT: self._left_to_right() elif direction == self.RIGHT_TO_LEFT: self._right_to_left() - def _left_to_right(self): + def _left_to_right(self) -> None: # Displays text from left to right on the LCD. self.displaymode |= _LCD_ENTRYLEFT self._write8(_LCD_ENTRYMODESET | self.displaymode) - def _right_to_left(self): + def _right_to_left(self) -> None: # Displays text from right to left on the LCD. self.displaymode &= ~_LCD_ENTRYLEFT self._write8(_LCD_ENTRYMODESET | self.displaymode) - def create_char(self, location, pattern): + def create_char( + self, location: int, pattern: Sequence[int, int, int, int, int, int, int, int] + ) -> None: """ Fill one of the first 8 CGRAM locations with custom characters. The location parameter should be between 0 and 7 and pattern should @@ -473,8 +490,8 @@ def create_char(self, location, pattern): design your custom character at http://www.quinapalus.com/hd44780udg.html To show your custom character use, for example, ``lcd.message = "\x01"`` - :param location: integer in range(8) to store the created character - :param ~bytes pattern: len(8) describes created character + :param int location: Integer in range(8) to store the created character. + :param Sequence pattern: len(8) describes created character. """ # only position 0..7 are allowed @@ -483,7 +500,7 @@ def create_char(self, location, pattern): for i in range(8): self._write8(pattern[i], char_mode=True) - def _write8(self, value, char_mode=False): + def _write8(self, value: [int], char_mode: bool = False) -> None: # Sends 8b ``value`` in ``char_mode``. # :param value: bytes # :param char_mode: character/data mode selector. False (default) for @@ -506,7 +523,7 @@ def _write8(self, value, char_mode=False): self.dl7.value = ((value >> 3) & 1) > 0 self._pulse_enable() - def _pulse_enable(self): + def _pulse_enable(self) -> None: # Pulses (lo->hi->lo) to send commands. self.enable.value = False # 1microsec pause @@ -530,8 +547,8 @@ class Character_LCD_Mono(Character_LCD): :param ~digitalio.DigitalInOut d5: The data line 5 :param ~digitalio.DigitalInOut d6: The data line 6 :param ~digitalio.DigitalInOut d7: The data line 7 - :param columns: The columns on the charLCD - :param lines: The lines on the charLCD + :param int columns: The columns on the charLCD + :param int lines: The lines on the charLCD :param ~digitalio.DigitalInOut backlight_pin: The backlight pin :param bool backlight_inverted: ``False`` if LCD is not inverted, i.e. backlight pin is connected to common anode. ``True`` if LCD is inverted i.e. backlight pin is connected @@ -542,16 +559,16 @@ class Character_LCD_Mono(Character_LCD): # pylint: disable-msg=too-many-arguments def __init__( self, - rs, - en, - db4, - db5, - db6, - db7, - columns, - lines, - backlight_pin=None, - backlight_inverted=False, + rs: digitalio.DigitalInOut, + en: digitalio.DigitalInOut, + db4: digitalio.DigitalInOut, + db5: digitalio.DigitalInOut, + db6: digitalio.DigitalInOut, + db7: digitalio.DigitalInOut, + columns: int, + lines: int, + backlight_pin: Optional[digitalio.DigitalInOut] = None, + backlight_inverted: bool = False, ): # Backlight pin and inversion @@ -567,7 +584,7 @@ def __init__( # pylint: enable-msg=too-many-arguments @property - def backlight(self): + def backlight(self) -> Optional[bool]: """Enable or disable backlight. True if backlight is on. False if backlight is off. The following example turns the backlight off, then displays, "Hello, world?", then turns @@ -594,7 +611,7 @@ def backlight(self): return self._enable @backlight.setter - def backlight(self, enable): + def backlight(self, enable: bool) -> None: self._enable = enable if enable: self.backlight_pin.value = not self.backlight_inverted @@ -611,8 +628,8 @@ class Character_LCD_RGB(Character_LCD): :param ~digitalio.DigitalInOut db5: The data line 5 :param ~digitalio.DigitalInOut db6: The data line 6 :param ~digitalio.DigitalInOut db7: The data line 7 - :param columns: The columns on the charLCD - :param lines: The lines on the charLCD + :param int columns: The columns on the charLCD + :param int lines: The lines on the charLCD :param ~pwmio.PWMOut,~digitalio.DigitalInOut red: Red RGB Anode :param ~pwmio.PWMOut,~digitalio.DigitalInOut green: Green RGB Anode :param ~pwmio.PWMOut,~digitalio.DigitalInOut blue: Blue RGB Anode @@ -624,19 +641,19 @@ class Character_LCD_RGB(Character_LCD): # pylint: disable-msg=too-many-arguments def __init__( self, - rs, - en, - db4, - db5, - db6, - db7, - columns, - lines, - red, - green, - blue, - read_write=None, - ): + rs: digitalio.DigitalInOut, + en: digitalio.DigitalInOut, + db4: digitalio.DigitalInOut, + db5: digitalio.DigitalInOut, + db6: digitalio.DigitalInOut, + db7: digitalio.DigitalInOut, + columns: int, + lines: int, + red: Union[pwmio.PWMOut, digitalio.DigitalInOut], + green: Union[pwmio.PWMOut, digitalio.DigitalInOut], + blue: Union[pwmio.PWMOut, digitalio.DigitalInOut], + read_write: Optional[digitalio.DigitalInOut] = None, + ) -> None: # Define read_write (rw) pin self.read_write = read_write @@ -662,7 +679,7 @@ def __init__( super().__init__(rs, en, db4, db5, db6, db7, columns, lines) @property - def color(self): + def color(self) -> List[int, int, int]: """ The color of the display. Provide a list of three integers ranging 0 - 100, ``[R, G, B]``. ``0`` is no color, or "off". ``100`` is maximum color. For example, the brightest red would @@ -693,7 +710,7 @@ def color(self): return self._color @color.setter - def color(self, color): + def color(self, color: Union[List[int, int, int], int]) -> None: if isinstance(color, int): if color >> 24: raise ValueError("Integer color value must be positive and 24 bits max") @@ -701,7 +718,7 @@ def color(self, color): r = (color >> 16) / 2.55 g = ((color >> 8) & 0xFF) / 2.55 b = (color & 0xFF) / 2.55 - color = [r, g, b] + color = [r, g, b] # This is List[float], should be List[int] self._color = color for number, pin in enumerate(self.rgb_led): if hasattr(pin, "duty_cycle"): diff --git a/adafruit_character_lcd/character_lcd_i2c.py b/adafruit_character_lcd/character_lcd_i2c.py index 4aff938..04e42d7 100644 --- a/adafruit_character_lcd/character_lcd_i2c.py +++ b/adafruit_character_lcd/character_lcd_i2c.py @@ -27,6 +27,11 @@ https://github.com/adafruit/Adafruit_CircuitPython_BusDevice """ +try: + from typing import Optional + import busio +except ImportError: + pass from adafruit_mcp230xx.mcp23008 import MCP23008 from adafruit_character_lcd.character_lcd import Character_LCD_Mono @@ -38,8 +43,8 @@ class Character_LCD_I2C(Character_LCD_Mono): # pylint: disable=too-few-public-methods, too-many-arguments """Character LCD connected to I2C/SPI backpack using its I2C connection. - This is a subclass of `Character_LCD_Mono` and implements all of the - same functions and functionality. + This is a subclass of `Character_LCD_Mono` and implements all the same + functions and functionality. To use, import and initialise as follows: @@ -52,7 +57,14 @@ class Character_LCD_I2C(Character_LCD_Mono): lcd = Character_LCD_I2C(i2c, 16, 2) """ - def __init__(self, i2c, columns, lines, address=None, backlight_inverted=False): + def __init__( + self, + i2c: busio.I2C, + columns: int, + lines: int, + address: Optional[int] = None, + backlight_inverted: bool = False, + ) -> None: """Initialize character LCD connected to backpack using I2C connection on the specified I2C bus with the specified number of columns and lines on the display. Optionally specify if backlight is inverted. diff --git a/adafruit_character_lcd/character_lcd_rgb_i2c.py b/adafruit_character_lcd/character_lcd_rgb_i2c.py index a44d341..d46f4c0 100644 --- a/adafruit_character_lcd/character_lcd_rgb_i2c.py +++ b/adafruit_character_lcd/character_lcd_rgb_i2c.py @@ -36,6 +36,11 @@ https://github.com/adafruit/Adafruit_CircuitPython_BusDevice """ +try: + from typing import Optional + import busio +except ImportError: + pass import digitalio from adafruit_mcp230xx.mcp23017 import MCP23017 @@ -62,7 +67,9 @@ class Character_LCD_RGB_I2C(Character_LCD_RGB): """ - def __init__(self, i2c, columns, lines, address=None): + def __init__( + self, i2c: busio.I2C, columns: int, lines: int, address: Optional[int] = None + ): # pylint: disable=too-many-locals """Initialize RGB character LCD connected to shield using I2C connection on the specified I2C bus with the specified number of columns and lines @@ -107,7 +114,7 @@ def __init__(self, i2c, columns, lines, address=None): ) @property - def left_button(self): + def left_button(self) -> bool: """The left button on the RGB Character LCD I2C Shield or Pi plate. The following example prints "Left!" to the LCD when the left button is pressed: @@ -128,7 +135,7 @@ def left_button(self): return not self._left_button.value @property - def up_button(self): + def up_button(self) -> bool: """The up button on the RGB Character LCD I2C Shield or Pi plate. The following example prints "Up!" to the LCD when the up button is pressed: @@ -149,7 +156,7 @@ def up_button(self): return not self._up_button.value @property - def down_button(self): + def down_button(self) -> bool: """The down button on the RGB Character LCD I2C Shield or Pi plate. The following example prints "Down!" to the LCD when the down button is pressed: @@ -170,7 +177,7 @@ def down_button(self): return not self._down_button.value @property - def right_button(self): + def right_button(self) -> bool: """The right button on the RGB Character LCD I2C Shield or Pi plate. The following example prints "Right!" to the LCD when the right button is pressed: @@ -191,7 +198,7 @@ def right_button(self): return not self._right_button.value @property - def select_button(self): + def select_button(self) -> bool: """The select button on the RGB Character LCD I2C Shield or Pi plate. The following example prints "Select!" to the LCD when the select button is pressed: diff --git a/adafruit_character_lcd/character_lcd_spi.py b/adafruit_character_lcd/character_lcd_spi.py index 457c23f..58a8f3e 100644 --- a/adafruit_character_lcd/character_lcd_spi.py +++ b/adafruit_character_lcd/character_lcd_spi.py @@ -27,6 +27,12 @@ https://github.com/adafruit/Adafruit_CircuitPython_BusDevice """ +try: + import typing # pylint: disable=unused-import + import busio + import digitalio +except ImportError: + pass import adafruit_74hc595 from adafruit_character_lcd.character_lcd import Character_LCD_Mono @@ -53,7 +59,14 @@ class Character_LCD_SPI(Character_LCD_Mono): # pylint: disable=too-few-public-m lcd = character_lcd.Character_LCD_SPI(spi, latch, 16, 2) """ - def __init__(self, spi, latch, columns, lines, backlight_inverted=False): + def __init__( + self, + spi: busio.SPI, + latch: digitalio.DigitalInOut, + columns: int, + lines: int, + backlight_inverted: bool = False, + ): # pylint: disable=too-many-arguments """Initialize character LCD connected to backpack using SPI connection on the specified SPI bus and latch line with the specified number of From b5b17d7abb10cb11cbbb204fae4b56cccf3ff71d Mon Sep 17 00:00:00 2001 From: BiffoBear Date: Tue, 25 Oct 2022 14:10:37 +0300 Subject: [PATCH 2/5] Set r, g and b to int to remove typing issue and respect the intent of the doc-string. --- adafruit_character_lcd/character_lcd.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/adafruit_character_lcd/character_lcd.py b/adafruit_character_lcd/character_lcd.py index f0deb84..a18e220 100644 --- a/adafruit_character_lcd/character_lcd.py +++ b/adafruit_character_lcd/character_lcd.py @@ -715,9 +715,9 @@ def color(self, color: Union[List[int, int, int], int]) -> None: if color >> 24: raise ValueError("Integer color value must be positive and 24 bits max") # NOTE: convert to 0-100 - r = (color >> 16) / 2.55 - g = ((color >> 8) & 0xFF) / 2.55 - b = (color & 0xFF) / 2.55 + r = int((color >> 16) / 2.55) + g = int(((color >> 8) & 0xFF) / 2.55) + b = int((color & 0xFF) / 2.55) color = [r, g, b] # This is List[float], should be List[int] self._color = color for number, pin in enumerate(self.rgb_led): From 1a13b17f28177f47f9f76165870c1e2430a251a8 Mon Sep 17 00:00:00 2001 From: BiffoBear Date: Tue, 25 Oct 2022 14:42:13 +0300 Subject: [PATCH 3/5] Fixed some List[int] issues. --- adafruit_character_lcd/character_lcd.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/adafruit_character_lcd/character_lcd.py b/adafruit_character_lcd/character_lcd.py index a18e220..429ac47 100644 --- a/adafruit_character_lcd/character_lcd.py +++ b/adafruit_character_lcd/character_lcd.py @@ -480,9 +480,7 @@ def _right_to_left(self) -> None: self.displaymode &= ~_LCD_ENTRYLEFT self._write8(_LCD_ENTRYMODESET | self.displaymode) - def create_char( - self, location: int, pattern: Sequence[int, int, int, int, int, int, int, int] - ) -> None: + def create_char(self, location: int, pattern: Sequence[int]) -> None: """ Fill one of the first 8 CGRAM locations with custom characters. The location parameter should be between 0 and 7 and pattern should @@ -679,7 +677,7 @@ def __init__( super().__init__(rs, en, db4, db5, db6, db7, columns, lines) @property - def color(self) -> List[int, int, int]: + def color(self) -> List[int]: """ The color of the display. Provide a list of three integers ranging 0 - 100, ``[R, G, B]``. ``0`` is no color, or "off". ``100`` is maximum color. For example, the brightest red would @@ -710,7 +708,7 @@ def color(self) -> List[int, int, int]: return self._color @color.setter - def color(self, color: Union[List[int, int, int], int]) -> None: + def color(self, color: Union[List[int], int]) -> None: if isinstance(color, int): if color >> 24: raise ValueError("Integer color value must be positive and 24 bits max") From 8e7c04b2b95b3cf4442fc2a7cab8672540b22071 Mon Sep 17 00:00:00 2001 From: BiffoBear Date: Tue, 1 Nov 2022 19:59:23 +0700 Subject: [PATCH 4/5] Requested changes made. --- .pre-commit-config.yaml | 46 +++++-------------------- adafruit_character_lcd/character_lcd.py | 19 +++++----- docs/requirements.txt | 2 ++ 3 files changed, 21 insertions(+), 46 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3343606..7c25d7b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -3,40 +3,12 @@ # SPDX-License-Identifier: Unlicense repos: - - repo: https://github.com/python/black - rev: 22.3.0 - hooks: - - id: black - - repo: https://github.com/fsfe/reuse-tool - rev: v0.14.0 - hooks: - - id: reuse - - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.2.0 - hooks: - - id: check-yaml - - id: end-of-file-fixer - - id: trailing-whitespace - - repo: https://github.com/pycqa/pylint - rev: v2.11.1 - 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 +- repo: https://github.com/fsfe/reuse-tool + rev: v0.14.0 + hooks: + - id: reuse +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.2.0 + hooks: + - id: end-of-file-fixer + - id: trailing-whitespace diff --git a/adafruit_character_lcd/character_lcd.py b/adafruit_character_lcd/character_lcd.py index 429ac47..02bd35c 100644 --- a/adafruit_character_lcd/character_lcd.py +++ b/adafruit_character_lcd/character_lcd.py @@ -29,6 +29,7 @@ https://github.com/adafruit/Adafruit_CircuitPython_BusDevice """ +import circuitpython_typing try: from typing import Union, Optional, List, Sequence @@ -90,7 +91,7 @@ def _set_bit(byte_value: int, position: int, val: bool) -> int: return ret -def _map(xval: int, in_min: int, in_max: int, out_min: int, out_max: int) -> float: +def _map(xval: float, in_min: float, in_max: float, out_min: float, out_max: float) -> float: # Affine transfer/map with constrained output. outrange = float(out_max - out_min) inrange = float(in_max - in_min) @@ -489,7 +490,7 @@ def create_char(self, location: int, pattern: Sequence[int]) -> None: To show your custom character use, for example, ``lcd.message = "\x01"`` :param int location: Integer in range(8) to store the created character. - :param Sequence pattern: len(8) describes created character. + :param Sequence[int] pattern: len(8) describes created character. """ # only position 0..7 are allowed @@ -498,9 +499,9 @@ def create_char(self, location: int, pattern: Sequence[int]) -> None: for i in range(8): self._write8(pattern[i], char_mode=True) - def _write8(self, value: [int], char_mode: bool = False) -> None: + def _write8(self, value: int, char_mode: bool = False) -> None: # Sends 8b ``value`` in ``char_mode``. - # :param value: bytes + # :param value: int # :param char_mode: character/data mode selector. False (default) for # data only, True for character bits. # one ms delay to prevent writing too quickly. @@ -708,15 +709,15 @@ def color(self) -> List[int]: return self._color @color.setter - def color(self, color: Union[List[int], int]) -> None: + def color(self, color: Union[List[float], int]) -> None: if isinstance(color, int): if color >> 24: raise ValueError("Integer color value must be positive and 24 bits max") # NOTE: convert to 0-100 - r = int((color >> 16) / 2.55) - g = int(((color >> 8) & 0xFF) / 2.55) - b = int((color & 0xFF) / 2.55) - color = [r, g, b] # This is List[float], should be List[int] + r = (color >> 16) / 2.55 + g = ((color >> 8) & 0xFF) / 2.55 + b = (color & 0xFF) / 2.55 + color = [r, g, b] self._color = color for number, pin in enumerate(self.rgb_led): if hasattr(pin, "duty_cycle"): diff --git a/docs/requirements.txt b/docs/requirements.txt index 88e6733..8cb81ef 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -3,3 +3,5 @@ # SPDX-License-Identifier: Unlicense sphinx>=4.0.0 +adafruit_circuitpython_typing>=1.8.2 +typing_extensions>=4.4.0 From 2d4683e66dad3582e5b1aedba222f8a768a3d273 Mon Sep 17 00:00:00 2001 From: BiffoBear Date: Tue, 1 Nov 2022 23:43:16 +0700 Subject: [PATCH 5/5] Fixed pre-commit yaml and made requested changes to requirements.txt. --- .pre-commit-config.yaml | 46 ++++++++++++++++++++----- adafruit_character_lcd/character_lcd.py | 6 ++-- docs/requirements.txt | 2 -- requirements.txt | 1 + 4 files changed, 41 insertions(+), 14 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 7c25d7b..3343606 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -3,12 +3,40 @@ # SPDX-License-Identifier: Unlicense repos: -- repo: https://github.com/fsfe/reuse-tool - rev: v0.14.0 - hooks: - - id: reuse -- repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.2.0 - hooks: - - id: end-of-file-fixer - - id: trailing-whitespace + - repo: https://github.com/python/black + rev: 22.3.0 + hooks: + - id: black + - repo: https://github.com/fsfe/reuse-tool + rev: v0.14.0 + hooks: + - id: reuse + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.2.0 + hooks: + - id: check-yaml + - id: end-of-file-fixer + - id: trailing-whitespace + - repo: https://github.com/pycqa/pylint + rev: v2.11.1 + 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 diff --git a/adafruit_character_lcd/character_lcd.py b/adafruit_character_lcd/character_lcd.py index 02bd35c..b4064ca 100644 --- a/adafruit_character_lcd/character_lcd.py +++ b/adafruit_character_lcd/character_lcd.py @@ -29,8 +29,6 @@ https://github.com/adafruit/Adafruit_CircuitPython_BusDevice """ -import circuitpython_typing - try: from typing import Union, Optional, List, Sequence from circuitpython_typing import pwmio @@ -91,7 +89,9 @@ def _set_bit(byte_value: int, position: int, val: bool) -> int: return ret -def _map(xval: float, in_min: float, in_max: float, out_min: float, out_max: float) -> float: +def _map( + xval: float, in_min: float, in_max: float, out_min: float, out_max: float +) -> float: # Affine transfer/map with constrained output. outrange = float(out_max - out_min) inrange = float(in_max - in_min) diff --git a/docs/requirements.txt b/docs/requirements.txt index 8cb81ef..88e6733 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -3,5 +3,3 @@ # SPDX-License-Identifier: Unlicense sphinx>=4.0.0 -adafruit_circuitpython_typing>=1.8.2 -typing_extensions>=4.4.0 diff --git a/requirements.txt b/requirements.txt index fcc4b07..c933bcb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,3 +6,4 @@ Adafruit-Blinka adafruit-circuitpython-mcp230xx adafruit-circuitpython-busdevice adafruit-circuitpython-74hc595 +adafruit-circuitpython-typing~=1.5