Skip to content
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

Error after update to 0.9.9 #104

Closed
jairhenrique opened this issue Jun 22, 2020 · 7 comments
Closed

Error after update to 0.9.9 #104

jairhenrique opened this issue Jun 22, 2020 · 7 comments

Comments

@jairhenrique
Copy link
Contributor

I have on my api a custom authentication class.

...
from rest_framework_simplejwt.authentication import JWTAuthentication

class MadmaxApiAuthentication(JWTAuthentication):
    def get_user(self, validated_token):
        """
        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

And on the same file, I create an authentication extension for drf-spectacular:

class OpenApiMadmaxAuthentication(OpenApiAuthenticationExtension):
    target_class = "madmax.helpers.api.auth.MadmaxApiAuthentication"
    name = "jwtAuth"
    matches_subclass = True

    def get_security_definition(self, auto_schema):
        return {
            "type": "http",
            "scheme": "bearer",
            "bearerFormat": "Bearer",
        }

After updating to 0.9.9 version I get many errors of loading auth class.

AttributeError: partially initialized module 'madmax.helpers.api.auth' has no attribute 'MadmaxApiAuthentication' (most likely due to a circular import)

AttributeError: partially initialized module 'rest_framework.views' has no attribute 'APIView' (most likely due to a circular import)
_ ERROR collecting src/madmax/helpers/api/tests/test_madmax_api_authentication.py _
ImportError while importing test module '/home/vsts/work/1/s/src/madmax/helpers/api/tests/test_madmax_api_authentication.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
python3.8/site-packages/django/utils/module_loading.py:20: in import_string
    return getattr(module, class_name)
E   AttributeError: partially initialized module 'madmax.helpers.api.auth' has no attribute 'MadmaxApiAuthentication' (most likely due to a circular import)

The above exception was the direct cause of the following exception:
python3.8/site-packages/rest_framework/settings.py:177: in import_from_string
    return import_string(val)
python3.8/site-packages/django/utils/module_loading.py:22: in import_string
    raise ImportError('Module "%s" does not define a "%s" attribute/class' % (
E   ImportError: Module "madmax.helpers.api.auth" does not define a "MadmaxApiAuthentication" attribute/class

During handling of the above exception, another exception occurred:
src/madmax/helpers/api/tests/test_madmax_api_authentication.py:9: in <module>
    from madmax.helpers.api.auth import MadmaxApiAuthentication
src/madmax/helpers/api/auth.py:2: in <module>
    from drf_spectacular.authentication import OpenApiAuthenticationExtension
python3.8/site-packages/drf_spectacular/authentication.py:1: in <module>
    from drf_spectacular.extensions import OpenApiAuthenticationExtension
python3.8/site-packages/drf_spectacular/extensions.py:4: in <module>
    from drf_spectacular.plumbing import OpenApiGeneratorExtension
python3.8/site-packages/drf_spectacular/plumbing.py:17: in <module>
    from rest_framework import (
python3.8/site-packages/rest_framework/generics.py:9: in <module>
    from rest_framework import mixins, views
python3.8/site-packages/rest_framework/views.py:17: in <module>
    from rest_framework.schemas import DefaultSchema
python3.8/site-packages/rest_framework/schemas/__init__.py:33: in <module>
    authentication_classes=api_settings.DEFAULT_AUTHENTICATION_CLASSES,
python3.8/site-packages/rest_framework/settings.py:220: in __getattr__
    val = perform_import(val, attr)
  File "python3.8/site-packages/rest_framework/generics.py", line 24, in <module>
    class GenericAPIView(views.APIView):
AttributeError: partially initialized module 'rest_framework.views' has no attribute 'APIView' (most likely due to a circular import)

I try to move extension auth class to other modules, without sucess.

@tfranzel
Copy link
Owner

tfranzel commented Jun 22, 2020

that looks weird @jairhenrique. can you confirm it worked on 0.9.8?

git diff 0.9.8..0.9.9 drf_spectacular/ shows me a lot of thinks but most should be totally unrelated. unfortunantely the import reordering was in that release too.

what i can see directly is a bugfix regarding matches_subclass. that was a typo bug and was never used. it should be match_subclasses, but that can hardly be the problem here, since your statement has no effect.

i need a bit of help to narrow it down.

@jairhenrique
Copy link
Contributor Author

@tfranzel yes, the version 0.9.8 is running in production! 😄 I fix the property to match_subclasses.

I got this error from my CI who updated the dependency with dependabot. I tried many things locally on my codebase, but I don't know what are happen.

@tfranzel
Copy link
Owner

i bisected the versions. i get an error first at 57ecb4a, but i don't yet completely understand what goes wrong. i get a different exception than you but i'm assuming its the same underlying problem. i'll dig deeper

tfranzel added a commit that referenced this issue Jun 22, 2020
@tfranzel
Copy link
Owner

although the error looks different for you, i encountered the same loop on rest_framework.views. @jairhenrique can you confirm this also solves your issue? if so i'll do a quick follow-up release.

@jairhenrique
Copy link
Contributor Author

@tfranzel thats works! 🚀

I get the new plumbing.py and changed on my env! Looks OK!

@tfranzel
Copy link
Owner

awesome. i would have liked to write a test for this scenario but the that kind of problem is tricky to test as it is not in the code itself but rather in the project setup.

0.9.10 is live on pypi.

@jairhenrique
Copy link
Contributor Author

Fixed on 0.9.10

Thanks @tfranzel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants