#30024: The test client request methods should raise an error when passed None
as a
data value
---------------------------------------------+------------------------
Reporter: Jon Dufresne | Owner: nobody
Type: New feature | Status: new
Component: Testing framework | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
---------------------------------------------+------------------------
Both GET and POST encoded data do not have a concept of `None` or `NULL`.
The closest approximation is an empty string value or omitting the key.
For example, in a GET request this could be either `/my-url/?my_field=` or
simply `/my-url/` but not `/my-url/?my_field=None`)
When onboarding new developers to projects, this can cause confusion to
those less familiar with these details. For example, a new developer may
try the following:
{{{
def test_setting_value_to_none(self):
self.client.post('/my-url/', {'my_field': None})
self.assertIsNone(...)
}}}
In current versions of Django, behind the scenes, this `None` gets coerced
to the string `'None'` by the test client. The Django form field classes
don't recognize the string `'None'` as an empty value (good) and so this
test doesn't pass. Where the new developer thought a field would be
assigned `None` they instead get a form error. Depending on the
developers' knowledge of these details, this could take much debugging or
consulting a colleague.
I think we can recognize this pattern as a programming mistake and raise
an informative error to guide the developer. I propose something like the
following, but am open to suggestions:
{{{
TypeError: Cannot encode None as POST data. Did you mean to pass an empty
string or omit the value?
}}}
For GET requests, the query string data is processed by
`django.utils.http.urlencode()`. So perhaps this same check can be done
there as encoding `None` in a URL query string as `'None'` is rarely the
intended behavior. For those that really want the string `'None'` in the
query string, they can pass the string `'None'`.
--
Ticket URL: <https://code.djangoproject.com/ticket/30024>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/052.2b9f5333e227c086cb9ea301f0738cc6%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.