22from dataclasses import dataclass , field
33from datetime import datetime , timedelta
44from operator import add , sub
5- from typing import (
6- TYPE_CHECKING , Any , Callable , Dict , Optional , Sequence , TypeVar , Union , overload ,
7- )
5+ from types import EllipsisType
6+ from typing import Any , Callable , Dict , Optional , Sequence , TypeVar , overload
87
98from jwt import PyJWT
109
11- from .types import AlgorithmType , RSAPrivateKey , RSAPublicKey
12-
13-
14- if TYPE_CHECKING :
15- # pylama:ignore=E0602
16- DateType = Union [timedelta , datetime , float , int , ellipsis ]
17- else :
18- DateType = Union [timedelta , datetime , float , int , type (Ellipsis )]
19-
10+ from .types import AlgorithmType , RSAPrivateKey , RSAPublicKey , DateType
2011
2112R = TypeVar ("R" )
2213DAY = 86400
2314DEFAULT_EXPIRATION = timedelta (days = 31 ).total_seconds ()
2415NBF_DELTA = 20
25- ALGORITHMS = tuple ( AlgorithmType . __args__ )
16+ ALGORITHMS : Sequence [ AlgorithmType ] = ( "RS256" , "RS384" , "RS512" )
2617
2718
2819def date_to_timestamp (
29- value : DateType ,
20+ value : DateType | EllipsisType ,
3021 default : Callable [[], R ],
3122 timedelta_func : Callable [[float , float ], int ] = add ,
32- ) -> Union [ int , float , R ] :
23+ ) -> int | float | R :
3324 if isinstance (value , timedelta ):
3425 return timedelta_func (time .time (), value .total_seconds ())
3526 elif isinstance (value , datetime ):
@@ -46,8 +37,8 @@ def date_to_timestamp(
4637class JWTDecoder :
4738 jwt : PyJWT = field (repr = False , compare = False )
4839 public_key : RSAPublicKey = field (repr = False , compare = True )
49- expires : Union [ int , float ]
50- nbf_delta : Union [ int , float ]
40+ expires : int | float
41+ nbf_delta : int | float
5142 algorithm : AlgorithmType
5243 algorithms : Sequence [AlgorithmType ]
5344
@@ -79,7 +70,7 @@ def __init__(self, key: RSAPrivateKey, *, options: Optional[Dict[str, Any]] = No
7970 super (JWTDecoder , self ).__setattr__ ('private_key' , key )
8071 super ().__init__ (key .public_key (), options = options , ** kwargs )
8172
82- def encode (self , expired : DateType = ..., nbf : DateType = ..., ** claims : Any ) -> str :
73+ def encode (self , expired : DateType | EllipsisType = ..., nbf : DateType | EllipsisType = ..., ** claims : Any ) -> str :
8374 claims .setdefault ('exp' , int (date_to_timestamp (expired , lambda : time .time () + self .expires )))
8475 claims .setdefault ('nbf' , int (date_to_timestamp (nbf , lambda : time .time () - self .nbf_delta , timedelta_func = sub )))
8576 return self .jwt .encode (claims , self .private_key , algorithm = self .algorithm )
@@ -97,7 +88,7 @@ def JWT(
9788
9889
9990@overload
100- def JWT ( # type: ignore[overload-cannot-match]
91+ def JWT (
10192 key : RSAPublicKey , * ,
10293 options : dict [str , Any ] | None = None ,
10394 expires : int | float = DEFAULT_EXPIRATION ,
@@ -108,15 +99,15 @@ def JWT( # type: ignore[overload-cannot-match]
10899
109100
110101def JWT (
111- key : Union [ RSAPrivateKey , RSAPublicKey ] ,
102+ key : RSAPrivateKey | RSAPublicKey ,
112103 * ,
113104 options : dict [str , Any ] | None = None ,
114105 expires : int | float = DEFAULT_EXPIRATION ,
115106 nbf_delta : int | float = NBF_DELTA ,
116107 algorithm : AlgorithmType = "RS512" ,
117108 algorithms : Sequence [AlgorithmType ] = ALGORITHMS ,
118- ) -> Union [ JWTSigner , JWTDecoder ] :
119- kwargs = dict (
109+ ) -> JWTSigner | JWTDecoder :
110+ kwargs : dict [ str , Any ] = dict (
120111 expires = expires ,
121112 nbf_delta = nbf_delta ,
122113 algorithm = algorithm ,
0 commit comments