Skip to content

Type annotation corrections #15

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 6 commits into from
May 2, 2022
Merged
Show file tree
Hide file tree
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
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="[email protected]",
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