Skip to content

Commit

Permalink
Merge pull request #15 from tekktrik/dod/typing-correction-just-blinka
Browse files Browse the repository at this point in the history
Type annotation corrections
  • Loading branch information
FoamyGuy committed May 2, 2022
2 parents 21161ae + 12fe3db commit 2a2225a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
21 changes: 12 additions & 9 deletions adafruit_binascii.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
"""
try:
from typing import Union
from circuitpython_typing import ReadableBuffer
from binascii import hexlify, unhexlify
except ImportError:
pass
Expand Down Expand Up @@ -63,11 +65,12 @@ class Error(Exception):

if not "unhexlify" in globals():
# pylint: disable=function-redefined
def unhexlify(hexstr: str) -> bytes:
def unhexlify(hexstr: Union[str, ReadableBuffer]) -> bytes:
"""Return the binary data represented by hexstr.
:param str hexstr: Hexadecimal string.
:param str|ReadableBuffer hexstr: Hexadecimal string.
"""

if len(hexstr) % 2 != 0:
raise Error("Odd-length string")

Expand All @@ -76,18 +79,18 @@ def unhexlify(hexstr: str) -> bytes:

if not "hexlify" in globals():
# pylint: disable=function-redefined
def hexlify(data: bytes) -> bytes:
def hexlify(data: ReadableBuffer) -> bytes:
"""Return the hexadecimal representation of the
binary data. Every byte of data is converted into
the corresponding 2-digit hex representation.
The returned bytes object is therefore twice
as long as the length of data.
:param bytes data: Binary data, as bytes.
"""
if not data:
raise TypeError("Data provided is zero-length")

data = "".join("%02x" % i for i in data)
return bytes(data, "utf-8")

Expand All @@ -106,12 +109,12 @@ def _transform(n: int) -> str:
assert len(TABLE_A2B_B64) == 256


def a2b_base64(b64_data: bytes) -> bytes:
def a2b_base64(b64_data: ReadableBuffer) -> bytes:
"""Convert a block of base64 data back to binary and return the binary data.
:param bytes b64_data: Base64 data.
"""

res = []
quad_pos = 0
leftchar = 0
Expand Down Expand Up @@ -148,12 +151,12 @@ def a2b_base64(b64_data: bytes) -> bytes:
return b"".join(res)


def b2a_base64(bin_data: bytes) -> bytes:
def b2a_base64(bin_data: ReadableBuffer) -> bytes:
"""Convert binary data to a line of ASCII characters in base64 coding.
:param bytes bin_data: Binary data string, as bytes
:param ReadableBuffer bin_data: Binary data string, as bytes
"""

newlength = (len(bin_data) + 2) // 3
newlength = newlength * 4 + 1
res = []
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
#
# SPDX-License-Identifier: Unlicense

Adafruit-Blinka
Adafruit-Blinka>=7.2.3
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
# Author details
author="Adafruit Industries",
author_email="circuitpython@adafruit.com",
install_requires=["Adafruit-Blinka"],
install_requires=["Adafruit-Blinka>=7.2.3"],
# Choose your license
license="MIT",
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers
Expand Down

0 comments on commit 2a2225a

Please sign in to comment.