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

Django Filters Extension is unable to handle filter fields with a function method kwarg #290

Closed
rvinzent opened this issue Feb 4, 2021 · 5 comments
Labels
bug Something isn't working fix confirmation pending issue has been fixed and confirmation from issue reporter is pending

Comments

@rvinzent
Copy link

rvinzent commented Feb 4, 2021

Describe the bug
Spectacular errors when on the second example from these django-filter docs

The method argument is allowed to be a function and drf-spectacular assumes it is a string, raising this error

    def resolve_filter_field(self, auto_schema, model, filterset_class, field_name, filter_field):
        from django_filters.rest_framework import filters
    
        if isinstance(filter_field, filters.OrderingFilter):
            # only here filter_field.field_name is not the model field name/path
            schema = build_basic_type(OpenApiTypes.STR)
        elif filter_field.method:
>           filter_method = getattr(filterset_class, filter_field.method)
E           TypeError: getattr(): attribute name must be string

To Reproduce
Follow example in docs above

Expected behavior
I expect it to not error here, and be able to handle this valid filter

tfranzel added a commit that referenced this issue Feb 4, 2021
@tfranzel tfranzel added bug Something isn't working fix confirmation pending issue has been fixed and confirmation from issue reporter is pending labels Feb 4, 2021
@rvinzent
Copy link
Author

rvinzent commented Feb 4, 2021

The docs fail to build when using functools.partial due to this error:

.venv/lib/python3.9/site-packages/drf_spectacular/contrib/django_filters.py:57: in resolve_filter_field
    filter_method_hints = typing.get_type_hints(filter_method)
TypeError: functools.partial(<function filter_by_unique_id at 0x10bf3e280>, prefix='SA') is not a module, class, method, or function.

The filters still work correctly.

@tfranzel
Copy link
Owner

tfranzel commented Feb 4, 2021

sounds weird, those are the batteries! a functools.partial is a callable thus could be considered a function, and typing.get_type_hints is python stdlib.

with doc you mean schema generation, right?

@rvinzent
Copy link
Author

rvinzent commented Feb 5, 2021

According to this post partial objects are not functions.

Maybe it could try to get the type hints and fall back to something else if that fails?

@tfranzel
Copy link
Owner

tfranzel commented Feb 7, 2021

that should do it. i hope that was the last line that could produce uncaught exceptions in django-filters.

@tfranzel
Copy link
Owner

closing this issue for now. feel free to comment if anything is missing or not working and we will follow-up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fix confirmation pending issue has been fixed and confirmation from issue reporter is pending
Projects
None yet
Development

No branches or pull requests

2 participants