FJNR-inc / dry-rest-permissions

Rules based permissions for the Django Rest Framework
ISC License
78 stars 11 forks source link

Circular import error for 1.2.0 #25

Closed yuekui closed 2 years ago

yuekui commented 2 years ago

If we set DRY Permissions and Filters as default rest_framework settings in conftest.py:

REST_FRAMEWORK={
    "DEFAULT_PERMISSION_CLASSES": [
        "dry_rest_permissions.generics.DRYPermissions",
    ],
    "DEFAULT_FILTER_BACKENDS": [
        "dry_rest_permissions.generics.DRYPermissionFiltersBase",
    ],
}

There would be an error like:

_________________________________________ ERROR collecting tests/tests.py ___________________________________________
tests/tests.py:6: in <module>
    from rest_framework import viewsets
.tox/py38-django3-drf3.11/lib/python3.8/site-packages/rest_framework/viewsets.py:27: in <module>
    from rest_framework import generics, mixins, views
.tox/py38-django3-drf3.11/lib/python3.8/site-packages/rest_framework/generics.py:9: in <module>
    from rest_framework import mixins, views
.tox/py38-django3-drf3.11/lib/python3.8/site-packages/rest_framework/views.py:17: in <module>
    from rest_framework.schemas import DefaultSchema
.tox/py38-django3-drf3.11/lib/python3.8/site-packages/rest_framework/schemas/__init__.py:34: in <module>
    permission_classes=api_settings.DEFAULT_PERMISSION_CLASSES,
.tox/py38-django3-drf3.11/lib/python3.8/site-packages/rest_framework/settings.py:220: in __getattr__
    val = perform_import(val, attr)
.tox/py38-django3-drf3.11/lib/python3.8/site-packages/rest_framework/settings.py:168: in perform_import
    return [import_from_string(item, setting_name) for item in val]
.tox/py38-django3-drf3.11/lib/python3.8/site-packages/rest_framework/settings.py:168: in <listcomp>
    return [import_from_string(item, setting_name) for item in val]
.tox/py38-django3-drf3.11/lib/python3.8/site-packages/rest_framework/settings.py:177: in import_from_string
    return import_string(val)
.tox/py38-django3-drf3.11/lib/python3.8/site-packages/django/utils/module_loading.py:17: in import_string
    module = import_module(module_path)
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
dry_rest_permissions/generics.py:32: in <module>
    class DRYPermissionFiltersBase(filters.BaseFilterBackend):
dry_rest_permissions/generics.py:51: in DRYPermissionFiltersBase
    def filter_queryset(self, request: request.Request, queryset: QuerySet, view: views.APIView):
E   AttributeError: partially initialized module 'rest_framework.views' has no attribute 'APIView' (most likely due to a circular import)

Related change: https://github.com/FJNR-inc/dry-rest-permissions/pull/21 https://github.com/FJNR-inc/dry-rest-permissions/issues/18

palfrey commented 2 years ago

What did you do to make this occur? I've tried locally editing tests/settings.py with the REST_FRAMEWORK block you mentioned and I'm not getting that error.

yuekui commented 2 years ago

What did you do to make this occur? I've tried locally editing tests/settings.py with the REST_FRAMEWORK block you mentioned and I'm not getting that error.

Try conftest.py?

palfrey commented 2 years ago

https://github.com/FJNR-inc/dry-rest-permissions/pull/26 should fix this

RomainFayolle commented 2 years ago

Hi @palfrey , @yuekui

From what I read here https://github.com/FJNR-inc/dry-rest-permissions/blob/8f2abbe20bd176fed09c2d8eab47ddcacc8fc3c4/dry_rest_permissions/generics.py#L32-L38

You should not use the class as is but instead inherit it like here:https://github.com/FJNR-inc/dry-rest-permissions/blob/8f2abbe20bd176fed09c2d8eab47ddcacc8fc3c4/README.md?plain=1#L319-L335

This way you should avoid getting the error. I will close this ticket but feel free to re-open it if needed.