[Django] #20744: Docs imply that forms.Field.__init__ accepts any keyword arguments

18 views
Skip to first unread message

Django

unread,
Jul 12, 2013, 7:49:43 PM7/12/13
to django-...@googlegroups.com
#20744: Docs imply that forms.Field.__init__ accepts any keyword arguments
-------------------------------+--------------------
Reporter: gthb | Owner: nobody
Type: Bug | Status: new
Component: Documentation | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------
At https://docs.djangoproject.com/en/dev/ref/forms/fields/ there is this:

{{{
class Field(**kwargs)
}}}

with no stated constraints on `kwargs`, thus implying (incorrectly) that
any keyword argument can safely be passed to the `forms.Field`
constructor.

That leads to code which worked in previous Django versions but regresses
in the current version. The case I have in mind is
[https://github.com/jezdez/django-
authority/blob/11461f808160e09bb992f9c602c342718892bb6a/authority/admin.py#L28-L33
in django-authority's admin.py] where the `kwargs` passed to
`formfield_callback` (which can include `request`) are passed on to
`Field.formfield`, whose documentation says “All of the `kwargs`
dictionary is passed directly to the form field’s `__init__()` method” ...
and that `__init__` method ''can't'' include `request`. So upgrading
Django yields this somewhat tricky-to-troubleshoot error in a django-
authority admin action:

{{{
TypeError: __init__() got an unexpected keyword argument 'request'

...
File "django/forms/models.py", line 170, in fields_for_model
formfield = formfield_callback(f, **kwargs)
File "authority/admin.py", line 33, in formfield_for_dbfield
return db_field.formfield(**kwargs)
File "django/db/models/fields/__init__.py", line 646, in formfield
return super(CharField, self).formfield(**defaults)
File "django/db/models/fields/__init__.py", line 499, in formfield
return form_class(**defaults)
File "django/forms/fields.py", line 188, in __init__
super(CharField, self).__init__(*args, **kwargs)
}}}

At some point in this chain, the `request` keyword argument must be popped
out. Presumably directly before the call to `Field.formfield`. In any
case, this requirement should be documented.

(Or `forms.Field.__init__` could be changed to tolerate and ignore unknown
keyword arguments. But that's a riskier change.)

--
Ticket URL: <https://code.djangoproject.com/ticket/20744>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 25, 2013, 9:05:02 AM7/25/13
to django-...@googlegroups.com
#20744: Docs imply that forms.Field.__init__ accepts any keyword arguments
-------------------------------+------------------------------------

Reporter: gthb | Owner: nobody
Type: Bug | Status: new
Component: Documentation | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by timo):

* needs_better_patch: => 0
* stage: Unreviewed => Accepted
* needs_tests: => 0
* needs_docs: => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/20744#comment:1>

Django

unread,
Jun 8, 2014, 3:39:11 AM6/8/14
to django-...@googlegroups.com
#20744: Docs imply that forms.Field.__init__ accepts any keyword arguments
-------------------------------+------------------------------------

Reporter: gthb | Owner: nobody
Type: Bug | Status: new
Component: Documentation | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by Pashkin):

* cc: Pashkin (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/20744#comment:2>

Django

unread,
Apr 20, 2024, 12:10:16 PM4/20/24
to django-...@googlegroups.com
#20744: Docs imply that forms.Field.__init__ accepts any keyword arguments
-------------------------------------+-------------------------------------
Reporter: Gunnlaugur Þór | Owner: Adam
Briem | Zapletal
Type: Bug | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Adam Zapletal):

* cc: Adam Zapletal (added)
* has_patch: 0 => 1
* owner: nobody => Adam Zapletal
* status: new => assigned

Comment:

I opened a simple pull request changing


`.. class:: Field(**kwargs)`

to

`.. class:: Field`

in the docs. That's how the `Form` class is documented.
--
Ticket URL: <https://code.djangoproject.com/ticket/20744#comment:3>

Django

unread,
Apr 26, 2024, 10:23:02 AM4/26/24
to django-...@googlegroups.com
#20744: Docs imply that forms.Field.__init__ accepts any keyword arguments
-------------------------------------+-------------------------------------
Reporter: Gunnlaugur Þór | Owner: Adam
Briem | Zapletal
Type: Bug | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/20744#comment:4>

Django

unread,
Apr 26, 2024, 10:23:48 AM4/26/24
to django-...@googlegroups.com
#20744: Docs imply that forms.Field.__init__ accepts any keyword arguments
-------------------------------------+-------------------------------------
Reporter: Gunnlaugur Þór | Owner: Adam
Briem | Zapletal
Type: Bug | Status: closed
Component: Documentation | Version: dev
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by GitHub <noreply@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"828b94b178736f7882cc6e5cd86b5c8e84b62ece" 828b94b]:
{{{#!CommitTicketReference repository=""
revision="828b94b178736f7882cc6e5cd86b5c8e84b62ece"
Fixed #20744 -- Removed hint that arbitrary kwargs are allowed when
creating forms.Fields.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/20744#comment:5>

Django

unread,
Apr 26, 2024, 1:39:07 PM4/26/24
to django-...@googlegroups.com
#20744: Docs imply that forms.Field.__init__ accepts any keyword arguments
-------------------------------------+-------------------------------------
Reporter: Gunnlaugur Þór | Owner: Adam
Briem | Zapletal
Type: Bug | Status: closed
Component: Documentation | Version: dev
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Natalia <124304+nessita@…>):

In [changeset:"f29922b6ef10e913a12d569eec0a87d3ae208235" f29922b]:
{{{#!CommitTicketReference repository=""
revision="f29922b6ef10e913a12d569eec0a87d3ae208235"
[5.0.x] Fixed #20744 -- Removed hint that arbitrary kwargs are allowed
when creating forms.Fields.

Backport of 828b94b178736f7882cc6e5cd86b5c8e84b62ece from main
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/20744#comment:6>
Reply all
Reply to author
Forward
0 new messages