33from calendar import timegm
44from collections import Iterable , Mapping
55from datetime import datetime , timedelta
6+ try :
7+ # import required by mypy to perform type checking, not used for normal execution
8+ from typing import Callable , Dict , List , Optional , Union # NOQA
9+ except ImportError :
10+ pass
611
712from .api_jws import PyJWS
813from .algorithms import Algorithm , get_default_algorithms # NOQA
@@ -20,6 +25,7 @@ class PyJWT(PyJWS):
2025
2126 @staticmethod
2227 def _get_default_options ():
28+ # type: () -> Dict[str, bool]
2329 return {
2430 'verify_signature' : True ,
2531 'verify_exp' : True ,
@@ -32,8 +38,13 @@ def _get_default_options():
3238 'require_nbf' : False
3339 }
3440
35- def encode (self , payload , key , algorithm = 'HS256' , headers = None ,
36- json_encoder = None ):
41+ def encode (self ,
42+ payload , # type: Union[Dict, bytes]
43+ key , # type: str
44+ algorithm = 'HS256' , # type: str
45+ headers = None , # type: Optional[Dict]
46+ json_encoder = None # type: Optional[Callable]
47+ ):
3748 # Check that we get a mapping
3849 if not isinstance (payload , Mapping ):
3950 raise TypeError ('Expecting a mapping object, as JWT only supports '
@@ -43,7 +54,7 @@ def encode(self, payload, key, algorithm='HS256', headers=None,
4354 for time_claim in ['exp' , 'iat' , 'nbf' ]:
4455 # Convert datetime to a intDate value in known time-format claims
4556 if isinstance (payload .get (time_claim ), datetime ):
46- payload [time_claim ] = timegm (payload [time_claim ].utctimetuple ())
57+ payload [time_claim ] = timegm (payload [time_claim ].utctimetuple ()) # type: ignore
4758
4859 json_payload = json .dumps (
4960 payload ,
@@ -55,7 +66,12 @@ def encode(self, payload, key, algorithm='HS256', headers=None,
5566 json_payload , key , algorithm , headers , json_encoder
5667 )
5768
58- def decode (self , jwt , key = '' , verify = True , algorithms = None , options = None ,
69+ def decode (self ,
70+ token , # type: str
71+ key = '' , # type: str
72+ verify = True , # type: bool
73+ algorithms = None , # type: List[str]
74+ options = None , # type: Dict
5975 ** kwargs ):
6076
6177 if verify and not algorithms :
@@ -66,15 +82,15 @@ def decode(self, jwt, key='', verify=True, algorithms=None, options=None,
6682 DeprecationWarning
6783 )
6884
69- payload , signing_input , header , signature = self ._load (jwt )
85+ payload , _ , _ , _ = self ._load (token )
7086
7187 if options is None :
7288 options = {'verify_signature' : verify }
7389 else :
7490 options .setdefault ('verify_signature' , verify )
7591
7692 decoded = super (PyJWT , self ).decode (
77- jwt , key = key , algorithms = algorithms , options = options , ** kwargs
93+ token , key = key , algorithms = algorithms , options = options , ** kwargs
7894 )
7995
8096 try :
0 commit comments