Skip to content

Commit b90b4dc

Browse files
committed
fix: Suggested changes and test with static analysis
1 parent 9cc0daf commit b90b4dc

File tree

8 files changed

+26
-16
lines changed

8 files changed

+26
-16
lines changed

supertokens_python/framework/django/django_middleware.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
from asgiref.sync import async_to_sync
2020

21-
from supertokens_python.utils import default_user_context
22-
2321

2422
def middleware(get_response: Any):
2523
from supertokens_python import Supertokens
@@ -30,6 +28,7 @@ def middleware(get_response: Any):
3028
from supertokens_python.supertokens import manage_session_post_response
3129

3230
from django.http import HttpRequest
31+
from supertokens_python.utils import default_user_context
3332

3433
if asyncio.iscoroutinefunction(get_response):
3534

supertokens_python/framework/fastapi/fastapi_middleware.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
from typing import TYPE_CHECKING, Union
1717

1818
from supertokens_python.framework import BaseResponse
19-
from supertokens_python.utils import default_user_context
2019

2120
if TYPE_CHECKING:
2221
from fastapi import FastAPI, Request
2322

2423

2524
def get_middleware():
2625
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
26+
from supertokens_python.utils import default_user_context
2727

2828
class Middleware(BaseHTTPMiddleware):
2929
def __init__(self, app: FastAPI):

supertokens_python/framework/flask/flask_middleware.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
from supertokens_python.async_to_sync_wrapper import sync
2020
from supertokens_python.framework import BaseResponse
21-
from supertokens_python.utils import default_user_context
2221

2322
if TYPE_CHECKING:
2423
from flask import Flask
@@ -35,6 +34,7 @@ def set_before_after_request(self):
3534
from supertokens_python.framework.flask.flask_request import FlaskRequest
3635
from supertokens_python.framework.flask.flask_response import FlaskResponse
3736
from supertokens_python.supertokens import manage_session_post_response
37+
from supertokens_python.utils import default_user_context
3838

3939
from flask.wrappers import Response
4040

supertokens_python/recipe/session/framework/django/asyncio/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from supertokens_python.framework.django.django_response import DjangoResponse
2121
from supertokens_python.recipe.session import SessionContainer, SessionRecipe
2222
from supertokens_python.recipe.session.interfaces import SessionClaimValidator
23-
from supertokens_python.utils import default_user_context
23+
from supertokens_python.utils import set_request_in_user_context_if_not_defined
2424
from supertokens_python.types import MaybeAwaitable
2525

2626
_T = TypeVar("_T", bound=Callable[..., Any])
@@ -50,8 +50,7 @@ async def wrapped_function(request: HttpRequest, *args: Any, **kwargs: Any):
5050

5151
try:
5252
baseRequest = DjangoRequest(request)
53-
if user_context is None:
54-
user_context = default_user_context(baseRequest)
53+
user_context = set_request_in_user_context_if_not_defined(user_context, baseRequest)
5554

5655
recipe = SessionRecipe.get_instance()
5756
session = await recipe.verify_session(

supertokens_python/recipe/session/framework/django/syncio/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from supertokens_python.framework.django.django_response import DjangoResponse
2222
from supertokens_python.recipe.session import SessionRecipe, SessionContainer
2323
from supertokens_python.recipe.session.interfaces import SessionClaimValidator
24-
from supertokens_python.utils import default_user_context
24+
from supertokens_python.utils import set_request_in_user_context_if_not_defined
2525
from supertokens_python.types import MaybeAwaitable
2626

2727
_T = TypeVar("_T", bound=Callable[..., Any])
@@ -51,8 +51,7 @@ def wrapped_function(request: HttpRequest, *args: Any, **kwargs: Any):
5151

5252
try:
5353
baseRequest = DjangoRequest(request)
54-
if user_context is None:
55-
user_context = default_user_context(baseRequest)
54+
user_context = set_request_in_user_context_if_not_defined(user_context, baseRequest)
5655

5756
recipe = SessionRecipe.get_instance()
5857
session = sync(

supertokens_python/recipe/session/framework/fastapi/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from supertokens_python.types import MaybeAwaitable
1919

2020
from ...interfaces import SessionContainer, SessionClaimValidator
21-
from supertokens_python.utils import default_user_context
21+
from supertokens_python.utils import set_request_in_user_context_if_not_defined
2222

2323

2424
def verify_session(
@@ -39,8 +39,7 @@ def verify_session(
3939
async def func(request: Request) -> Union[SessionContainer, None]:
4040
nonlocal user_context
4141
baseRequest = FastApiRequest(request)
42-
if user_context is None:
43-
user_context = default_user_context(baseRequest)
42+
user_context = set_request_in_user_context_if_not_defined(user_context, baseRequest)
4443

4544
recipe = SessionRecipe.get_instance()
4645
session = await recipe.verify_session(

supertokens_python/recipe/session/framework/flask/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from supertokens_python.framework.flask.flask_request import FlaskRequest
1919
from supertokens_python.recipe.session import SessionRecipe, SessionContainer
2020
from supertokens_python.recipe.session.interfaces import SessionClaimValidator
21-
from supertokens_python.utils import default_user_context
21+
from supertokens_python.utils import set_request_in_user_context_if_not_defined
2222
from supertokens_python.types import MaybeAwaitable
2323

2424
_T = TypeVar("_T", bound=Callable[..., Any])
@@ -45,8 +45,7 @@ def wrapped_function(*args: Any, **kwargs: Any):
4545
from flask import make_response, request
4646

4747
baseRequest = FlaskRequest(request)
48-
if user_context is None:
49-
user_context = default_user_context(baseRequest)
48+
user_context = set_request_in_user_context_if_not_defined(user_context, baseRequest)
5049

5150
recipe = SessionRecipe.get_instance()
5251
session = sync(

tests/test_user_context.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414
from typing import Any, Dict, List, Optional
15+
from pathlib import Path
1516

1617
from fastapi import FastAPI
1718
from fastapi.testclient import TestClient
@@ -400,3 +401,17 @@ async def create_new_session(
400401
create_new_session_context_works,
401402
]
402403
)
404+
405+
406+
407+
408+
async def test_default_user_context_func_calls():
409+
# Tests run in the root directory of the repo
410+
root_dir = Path("supertokens_python")
411+
file_occurences: List[str] = []
412+
for path in root_dir.rglob("*.py"):
413+
with open(path) as f:
414+
file_occurences.extend([str(path)] * f.read().count("user_context = set_request_in_user_context_if_not_defined("))
415+
file_occurences.extend([str(path)] * f.read().count("user_context = default_user_context("))
416+
417+
assert len(file_occurences) == 7

0 commit comments

Comments
 (0)