Skip to content

Commit

Permalink
Updated typing of expires_delta and fresh (#510)
Browse files Browse the repository at this point in the history
* Update typing.py

cleaned up ExpiresDelta type definition and added Fresh type

* Update utils.py

updated typing for `expires_delta` and `fresh`

* Update jwt_manager.py

updated typing of `fresh`

* Update tokens.py

updated typing for `fresh`

* Update tokens.py

added missing import

* Update typing.py

float type is required while building the token_data dict in _encode_jwt
eMaerthin authored May 26, 2023
1 parent af8646e commit 0803dc5
Showing 4 changed files with 12 additions and 8 deletions.
3 changes: 2 additions & 1 deletion flask_jwt_extended/jwt_manager.py
Original file line number Diff line number Diff line change
@@ -42,6 +42,7 @@
from flask_jwt_extended.tokens import _decode_jwt
from flask_jwt_extended.tokens import _encode_jwt
from flask_jwt_extended.typing import ExpiresDelta
from flask_jwt_extended.typing import Fresh
from flask_jwt_extended.utils import current_user_context_processor


@@ -493,7 +494,7 @@ def _encode_jwt_from_config(
identity: Any,
token_type: str,
claims=None,
fresh: bool = False,
fresh: Fresh = False,
expires_delta: Optional[ExpiresDelta] = None,
headers=None,
) -> str:
3 changes: 2 additions & 1 deletion flask_jwt_extended/tokens.py
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@
from flask_jwt_extended.exceptions import CSRFError
from flask_jwt_extended.exceptions import JWTDecodeError
from flask_jwt_extended.typing import ExpiresDelta
from flask_jwt_extended.typing import Fresh


def _encode_jwt(
@@ -23,7 +24,7 @@ def _encode_jwt(
claim_overrides: dict,
csrf: bool,
expires_delta: ExpiresDelta,
fresh: bool,
fresh: Fresh,
header_overrides: dict,
identity: Any,
identity_claim_key: str,
5 changes: 3 additions & 2 deletions flask_jwt_extended/typing.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import sys
from typing import Any
from datetime import timedelta
from typing import Union

if sys.version_info >= (3, 8):
from typing import Literal # pragma: no cover
else:
from typing_extensions import Literal # pragma: no cover

ExpiresDelta = Union[Literal[False], Any]
ExpiresDelta = Union[Literal[False], timedelta]
Fresh = Union[bool, float, timedelta]
9 changes: 5 additions & 4 deletions flask_jwt_extended/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import datetime
from typing import Any
from typing import Optional

@@ -9,6 +8,8 @@

from flask_jwt_extended.config import config
from flask_jwt_extended.internal_utils import get_jwt_manager
from flask_jwt_extended.typing import ExpiresDelta
from flask_jwt_extended.typing import Fresh

# Proxy to access the current user
current_user: Any = LocalProxy(lambda: get_current_user())
@@ -129,8 +130,8 @@ def decode_token(

def create_access_token(
identity: Any,
fresh: bool = False,
expires_delta: Optional[datetime.timedelta] = None,
fresh: Fresh = False,
expires_delta: Optional[ExpiresDelta] = None,
additional_claims=None,
additional_headers=None,
):
@@ -183,7 +184,7 @@ def create_access_token(

def create_refresh_token(
identity: Any,
expires_delta: Optional[datetime.timedelta] = None,
expires_delta: Optional[ExpiresDelta] = None,
additional_claims=None,
additional_headers=None,
):

0 comments on commit 0803dc5

Please sign in to comment.