Opened 5 months ago

Closed 5 months ago

#35179 closed Bug (fixed)

Admindocs does not recognize methods containing positional-only arguments or keyword-only arguments as such

Reported by: David Sanders Owned by: Salvo Polizzi
Component: contrib.admindocs Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by David Sanders)

Given the model:

class Foo(Model):
    def arg_kwarg_method(self, arg, kwarg=None): ...
    def posarg_only_method(self, posarg, /): ...
    def kwarg_only_method(self, *, kwarg): ...
    def posarg_only_and_kwarg_only_method(self, posarg, /, *, kwarg): ...
    def posarg_only_and_arg_and_kwarg_only_method(self, posarg, /, arg, *, kwarg): ...

The following are documented as methods:

  • arg_kwarg_method()
  • posarg_only_method()
  • posarg_only_and_kwarg_only_method()

The following are documented as attributes:

  • kwarg_only_method()
  • posarg_only_and_arg_and_kwarg_only_method()

Attachments (1)

admindocs.png (109.9 KB ) - added by David Sanders 5 months ago.
Screenshot of admindocs treating some methods as attributes

Download all attachments as: .zip

Change History (10)

comment:1 by David Sanders, 5 months ago

Description: modified (diff)
Summary: Admindocs omits model methods containing only keyword-only argumentsAdmindocs treats model some model methods containing positional-only arguments or keyword-only arguments as attributes

by David Sanders, 5 months ago

Attachment: admindocs.png added

Screenshot of admindocs treating some methods as attributes

comment:2 by Natalia Bidart, 5 months ago

Triage Stage: UnreviewedAccepted
Version: 5.0dev

After some investigation, this definitely seems like a valid issue. I did some initial debugging, and both methods are being detected as fields because the guard (in django/contrib/admindocs/views.py:ModelDetailView):

                elif (
                    method_has_no_args(func)
                    and not func_accepts_kwargs(func)
                    and not func_accepts_var_args(func)
                ):
                    fields.append(
                        {
                            "name": func_name,
                            "data_type": get_return_data_type(func_name),
                            "verbose": verbose or "",
                        }
                    )

is being evaluated with these values:

method_has_no_args(func)=True func_accepts_kwargs(func)=False func_accepts_var_args(func)=False

The methods func_accepts_kwargs and func_accepts_var_args need to be updated to understand the * and / syntax (new since Python 3.8).
David, would you like to prepare a patch? :-)

comment:3 by Natalia Bidart, 5 months ago

Summary: Admindocs treats model some model methods containing positional-only arguments or keyword-only arguments as attributesAdmindocs does not recognize methods containing positional-only arguments or keyword-only arguments as such

comment:4 by Salvo Polizzi, 5 months ago

Has patch: set
Needs tests: set
Last edited 5 months ago by Salvo Polizzi (previous) (diff)

comment:5 by Salvo Polizzi, 5 months ago

Needs tests: unset

comment:6 by David Sanders, 5 months ago

Needs tests: set
Patch needs improvement: set

Hi Salvo,

Thanks for picking this up. You're in the right areaa but the patch provided appears to break the patched functions.

My advice is to start with creating a test for admindocs first then take a look at the function method_has_no_args(). The fix should be the same for ticket #35175 👍

comment:7 by Salvo Polizzi, 5 months ago

Needs tests: unset
Patch needs improvement: unset

comment:8 by Mariusz Felisiak, 5 months ago

Owner: changed from nobody to Salvo Polizzi
Status: newassigned
Triage Stage: AcceptedReady for checkin

comment:9 by Mariusz Felisiak <felisiak.mariusz@…>, 5 months ago

Resolution: fixed
Status: assignedclosed

In e6fa74f:

Fixed #35179 -- Made admindocs detect positional/keyword-only arguments.

Note: See TracTickets for help on using tickets.
Back to Top