Skip to content

Commit d50800d

Browse files
authored
Merge pull request #11 from ktkinsey37/typing
added typing on six different funcs: validate, generate, urlsafe_b64e…
2 parents 4204bae + 37e4041 commit d50800d

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

adafruit_jwt.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727
https://github.com/adafruit/Adafruit_CircuitPython_RSA
2828
2929
"""
30+
try:
31+
from typing import Tuple, Union, Optional
32+
from circuitpython_typing import ReadableBuffer
33+
except ImportError:
34+
pass
35+
3036
import io
3137
import json
3238
from adafruit_rsa import PrivateKey, sign
@@ -47,7 +53,7 @@ class JWT:
4753
"""
4854

4955
@staticmethod
50-
def validate(jwt):
56+
def validate(jwt: str) -> Tuple[str, dict]:
5157
"""Validates a provided JWT. Does not support validating
5258
nested signing. Returns JOSE Header and claim set.
5359
:param str jwt: JSON Web Token.
@@ -77,7 +83,12 @@ def validate(jwt):
7783
return (jose_header, claims)
7884

7985
@staticmethod
80-
def generate(claims, private_key_data=None, algo=None, headers=None):
86+
def generate(
87+
claims: dict,
88+
private_key_data: Optional[str] = None,
89+
algo: Optional[str] = None,
90+
headers: Optional[dict] = None,
91+
) -> str:
8192
"""Generates and returns a new JSON Web Token.
8293
:param dict claims: JWT claims set
8394
:param str private_key_data: Decoded RSA private key data.
@@ -140,7 +151,7 @@ class STRING_TOOLS:
140151
printable = digits + ascii_letters + punctuation + whitespace
141152

142153
@staticmethod
143-
def urlsafe_b64encode(payload):
154+
def urlsafe_b64encode(payload: ReadableBuffer) -> str:
144155
"""Encode bytes-like object using the URL- and filesystem-safe alphabet,
145156
which substitutes - instead of + and _ instead of / in
146157
the standard Base64 alphabet, and return the encoded bytes.
@@ -151,15 +162,15 @@ def urlsafe_b64encode(payload):
151162
)
152163

153164
@staticmethod
154-
def urlsafe_b64decode(payload):
165+
def urlsafe_b64decode(payload: Union[ReadableBuffer, str]) -> str:
155166
"""Decode bytes-like object or ASCII string using the URL
156167
and filesystem-safe alphabet
157168
:param bytes payload: bytes-like object or ASCII string
158169
"""
159170
return a2b_base64(STRING_TOOLS._bytes_from_decode_data(payload)).decode("utf-8")
160171

161172
@staticmethod
162-
def _bytes_from_decode_data(str_data):
173+
def _bytes_from_decode_data(str_data: Union[ReadableBuffer, str]) -> bytes:
163174
# Types acceptable as binary data
164175
bit_types = (bytes, bytearray)
165176
if isinstance(str_data, str):
@@ -180,7 +191,7 @@ def _bytes_from_decode_data(str_data):
180191
# Port of CPython str.translate to Pure-Python by Johan Brichau, 2019
181192
# https://github.com/jbrichau/TrackingPrototype/blob/master/Device/lib/string.py
182193
@staticmethod
183-
def translate(s, table):
194+
def translate(s: str, table: dict) -> str:
184195
"""Return a copy of the string in which each character
185196
has been mapped through the given translation table.
186197
:param string s: String to-be-character-table.

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
# SPDX-License-Identifier: Unlicense
44

55
Adafruit-Blinka
6+
adafruit-circuitpython-typing

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
# Author details
3434
author="Adafruit Industries",
3535
author_email="circuitpython@adafruit.com",
36-
install_requires=["Adafruit-Blinka"],
36+
install_requires=["Adafruit-Blinka", "adafruit-circuitpython-typing"],
3737
# Choose your license
3838
license="MIT",
3939
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers

0 commit comments

Comments
 (0)