Opened 7 years ago

Closed 7 years ago

#28192 closed Cleanup/optimization (fixed)

Prevent passing positional arguments to form fields

Reported by: Claude Paroz Owned by: nobody
Component: Forms Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The documentation already indicates (**kwargs) for most of form field signatures. But in practice, this isn't enforced and any positional *args are passed to the __init__ methods.

I encountered a strange error with some code of mine which was due to some label passed erroneously as positional parameter:
distance = DecimalField("SomeLabel", max_digits=6, decimal_places=2) ("SomeLabel" was passed as max_value, which only crashed when validating the form with a strange comparison error).

I think that preventing positional parameters (with a lonely *) should avoid this sort of unexpected errors. This might however be backwards incompatible for previously well-functioning code.

Change History (6)

comment:1 by Tim Graham, 7 years ago

Summary: Prevent positional arguments on form fieldsPrevent passing positional arguments to form fields
Triage Stage: UnreviewedAccepted

I think it's worth trying this -- we can always revert it and consider a deprecation path if prerelease testing indicates major pain. I don't recall encountering form fields using positional arguments during my time working with Django.

comment:2 by Claude Paroz, 7 years ago

Has patch: set

comment:3 by GitHub <noreply@…>, 7 years ago

In 8e752d84:

Refs #28192 -- Made MultiValueField/ComboField fields argument required as documented.

comment:4 by Claude Paroz <claude@…>, 7 years ago

In 54caca2:

Refs #28192 -- Fixed documentation of ChoiceField choices requirement

Thanks Tim Graham for noticing the issue.

comment:5 by Claude Paroz <claude@…>, 7 years ago

In 81c3967e:

[1.11.x] Refs #28192 -- Fixed documentation of ChoiceField choices requirement

Thanks Tim Graham for noticing the issue.
Backport of 54caca2d34c7cb6807da0a82bcec7b3a679ac104 from master.

comment:6 by Tim Graham <timograham@…>, 7 years ago

Resolution: fixed
Status: newclosed

In 43b57400:

Fixed #28192 -- Required passing optional form field args as keyword args.

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