Opened 3 years ago

Closed 3 years ago

#21555 closed Bug (fixed)

ValidationError is not picklable

Reported by: zanuxzan Owned by: nobody
Component: Forms Version: master
Severity: Normal Keywords:
Cc: loic@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


I have a use case where I'd like to pickle ValidationError however it throws up as error:

For example, I'd expect the following to work:

import pickle
from django.core.exceptions import ValidationError
pickle.loads(pickle.dumps(ValidationError('eggs', 'sausage')))

Results in:

TypeError: __init__() takes at least 2 arguments (1 given)

The reason for this is because ValidationError does not call super.

Attachments (1)

ticket-21555-rev1.patch (1.1 KB) - added by zanuxzan 3 years ago.

Download all attachments as: .zip

Change History (4)

Changed 3 years ago by zanuxzan

comment:1 Changed 3 years ago by loic84

  • Cc loic@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

It's a good idea as PY2 can't pickle naive exceptions see: (fix was backported all the way to python 3.2).

Since we'd now be calling the constructor, I think it's good practice to pass all arguments so the args attribute holds meaningful information.

I've completed the test suite to ensure the more complex scenarios like nested ValidationError, or Validation with error_dict behave as expected.


comment:2 Changed 3 years ago by timo

  • Component changed from Uncategorized to Forms
  • Triage Stage changed from Accepted to Ready for checkin

comment:3 Changed 3 years ago by Loic Bistuer <loic.bistuer@…>

  • Resolution set to fixed
  • Status changed from new to closed

In a8f4553aaecc7bc6775e0fd54f8c615c792b3d97:

Fixed #21555 -- Made ValidationError pickable.

Thanks trac username zanuxzan for the report and original patch.

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