Opened 3 years ago
Closed 3 years ago
#32978 closed Cleanup/optimization (wontfix)
Improve an error message on loaddata/dumpdata when PyYAML is not installed.
Reported by: | Brad | Owned by: | Brad |
---|---|---|---|
Component: | Core (Serialization) | Version: | 3.2 |
Severity: | Normal | Keywords: | serializers, yaml, dumpdata, loaddata |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description (last modified by )
While the docs at https://docs.djangoproject.com/en/3.2/topics/serialization/#serialization-formats do mention that PyYAML is required for the yaml serializer, ./manage.py dumpdata|loaddata --format yaml
, provides quite a sparse error message, "Unable to serialize database: module yaml not found" (from here: https://github.com/django/django/blob/fbb1984046ae00bdf0b894a6b63294395da1cce8/django/core/management/commands/dumpdata.py#L245) if PyYAML is not installed.
This could be especially confusing for new Django developers who are not aware that yaml
is pointing to the PyYAML project on PyPI and is not a part of Django. Since yaml is the only one of the four serializers requiring an add-on third party package, it may be helpful to provide a more verbose error in this case.
For instance, something like. "Serializing to and from YAML requires the PyYAML package. Install PyYAML to use the YAML format."
Change History (12)
comment:1 by , 3 years ago
Description: | modified (diff) |
---|
comment:2 by , 3 years ago
Type: | Uncategorized → Cleanup/optimization |
---|
comment:3 by , 3 years ago
Component: | Core (Management commands) → Core (Serialization) |
---|---|
Easy pickings: | set |
Triage Stage: | Unreviewed → Accepted |
comment:4 by , 3 years ago
Summary: | Provide more informative error message on loaddata/dumpdata --format yaml when PyYAML not installed → Improve an error message on loaddata/dumpdata when PyYAML is not installed. |
---|
comment:5 by , 3 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:6 by , 3 years ago
Interestingly, I found a comment in django/core/serializers/pyyaml.py
:
# Requires PyYaml (https://pyyaml.org/), but that's checked for in __init__."
However, I'm not seeing any such check in django/core/serializers/__init__.py
. (I expected a HAS_PYYAML or something like that.)
There are also some tests in tests/serializers/test_yaml.py
that test specifically for
YAML_IMPORT_ERROR_MESSAGE = r'No module named yaml'
So those will need to be revised.
comment:8 by , 3 years ago
Having read the background from Ticket 12756, the comment from core/serializers/pyyaml.py about the checking being done in __init__.py
makes more sense now. The check that's referring to is here:
https://github.com/django/django/blob/main/django/core/serializers/__init__.py#L70
So again, the import error is caught correctly, but in this ticket I'm proposing adding a friendlier message in the special case of yaml/PyYAML.
comment:9 by , 3 years ago
Has patch: | set |
---|
PR: https://github.com/django/django/pull/14729
Old:
$ ./manage.py dumpdata --format yaml --pks 1 users.User
CommandError: Unable to serialize database: No module named 'yaml'
New:
$ ./manage.py dumpdata --format yaml --pks 1 users.User
CommandError: Unable to serialize database: Error loading yaml module. Did you install PyYAML?
follow-up: 12 comment:10 by , 3 years ago
I do realize this is arguably an addition of kludgy code, especially the test_yaml.py changes, in exchange for a small improvement in the error message, so my feelings won't be hurt either way on this one.
comment:11 by , 3 years ago
Owner: | changed from | to
---|
comment:12 by , 3 years ago
Resolution: | → wontfix |
---|---|
Status: | assigned → closed |
Replying to Brad:
I do realize this is arguably an addition of kludgy code, especially the test_yaml.py changes, in exchange for a small improvement in the error message, so my feelings won't be hurt either way on this one.
Thanks for all your efforts! I agree that it's not worth changing.
Agreed, we can raise an
ImproperlyConfigured
error, e.g.