-
Notifications
You must be signed in to change notification settings - Fork 270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: avoid circular import of/via rest_framework's APIView #430
Conversation
The typing hints added in 9819424 caused a regression with version 0.17.1, where importing `OpenApiAuthenticationExtension` from `drf_spectacular.extensions` might cause a circular import error: > ImportError: Could not import > 'project.app.authentication.MyAuthentication' for API setting > 'DEFAULT_AUTHENTICATION_CLASSES'. ImportError: cannot import name > 'OpenApiAuthenticationExtension' from partially initialized module > 'drf_spectacular.extensions' (most likely due to a circular import) > (…/.venv/lib/python3.9/site-packages/drf_spectacular/extensions.py).
This probably should have a regression test, but wanted to leave it here for now already. |
Codecov Report
@@ Coverage Diff @@
## master #430 +/- ##
==========================================
- Coverage 98.58% 98.56% -0.02%
==========================================
Files 55 55
Lines 5493 5493
==========================================
- Hits 5415 5414 -1
- Misses 78 79 +1
Continue to review full report at Codecov.
|
I've get same problem! |
@blueyed thanks for reporting! i didn't think this import could cause any issues. i have trouble understanding exactly why that is. sure, the |
@tfranzel I've get this error when try to load my custom auth class.
|
@jairhenrique very strange. these errors are the worst. is your custom auth class in the same python file as the AuthExtension? |
@tfranzel yes from typing import Any, Dict
from django.contrib.auth.models import User
from drf_spectacular.extensions import OpenApiAuthenticationExtension
from rest_framework_simplejwt.authentication import JWTAuthentication
from rest_framework_simplejwt.exceptions import (
AuthenticationFailed,
InvalidToken,
)
from rest_framework_simplejwt.settings import api_settings
class MyAuthentication(JWTAuthentication):
def get_user(self, validated_token: Dict[str, Any]) -> User:
"""
Attempts to find and return a user using the given validated token.
"""
try:
user_id = validated_token[api_settings.USER_ID_CLAIM]
except KeyError:
raise InvalidToken(
"Token contained no recognizable user identification"
)
try:
user = User.objects.get(
**{api_settings.USER_ID_FIELD: user_id},
clientprofile__user_id=user_id,
)
except User.DoesNotExist:
raise AuthenticationFailed("User not found", code="invalid_user")
if not user.is_active:
raise AuthenticationFailed("User is inactive", code="invalid_user")
return user
class OpenApiMyAuthentication( # type:ignore
OpenApiAuthenticationExtension
):
target_class = "app.helpers.api.auth.MyAuthentication"
name = "jwtAuth"
matches_subclass = True
def get_security_definition(self, auto_schema: Any) -> Dict[str, str]:
return {
"type": "http",
"scheme": "bearer",
"bearerFormat": "Bearer",
} I tried to move the
|
i dug into this further. i changed 2 imports in this release. the other one looked equally innocent: 1ec133a#diff-d727722caf6fe5f5507940c9efbcbe843fcca1dec9ac64402f074b08dd1c294fR29 @jairhenrique i was able to reproduce your exact error. however this PR was not enough. the other import failed afterwards. can you confirm this? @blueyed is your case completely fixed by this PR? |
@jairhenrique @blueyed i fixed the second import error (hopefully). could you test if the current master works for you both? if yes we can do a hotfix release. thanks a lot. |
@tfranzel looks good for me after changes! |
@tfranzel master works for me also. Thanks for fixing it so fast! |
awesome! fyi, i just released |
The typing hints added in 9819424 caused a regression with version
0.17.1, where importing
OpenApiAuthenticationExtension
fromdrf_spectacular.extensions
might cause a circular import error: