[Django] #30548: Improve exception message when BaseExpression raises "'Expression contains mixed types. You must set output_field.'"

8 views
Skip to first unread message

Django

unread,
Jun 6, 2019, 9:19:52 AM6/6/19
to django-...@googlegroups.com
#30548: Improve exception message when BaseExpression raises "'Expression contains
mixed types. You must set output_field.'"
-------------------------------------+-------------------------------------
Reporter: Keryn | Owner: nobody
Knight |
Type: | Status: new
Cleanup/optimization |
Component: Database | Version: master
layer (models, ORM) |
Severity: Normal | Keywords: expressions orm
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
The
[https://github.com/django/django/blob/661e6cc2c97d9bcb45198be787409488e1825c90/django/db/models/expressions.py#L290
source which raises the exception] has enough information to say both what
types were found, and which of those were unexpected, **and** probably
have a useful `repr()`

In the test suite, the unexpected output types encountered seem to be
`DurationField` and `IntegerField`, so a more thorough message might be
something like:
`Expression repr(self) contained mixed types: DateField, DurationField.
DurationField was unexpected; you must set the output_field= for this
Expression to either DurationField(), DateField() or ...` (''??? I dunno,
some concrete explanation of what the output_field has to be/implement if
you're not going to use any of the builtins'')

The merit of including the repr is arguable, as the `Expression` may not
what the user put in (eg: in the test suite it always seems to be a
`CombinedExpression(lhs, connector, rhs)`) but it gives more of a hint in
a query which contains multiple expressions (either nested or separate) as
to which one is actually causing the problem vs just being told "something
was wrong. Put an output_field= everywhere until it ceases, your guess is
as good as mine"; at the very least the word Expression could be replaced
with the class name which is actually raising it.

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

Django

unread,
Jun 6, 2019, 9:37:51 AM6/6/19
to django-...@googlegroups.com
#30548: Improve exceptions about mixed types in Expressions.
-------------------------------------+-------------------------------------
Reporter: Keryn Knight | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: expressions orm | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* stage: Unreviewed => Accepted
* easy: 0 => 1


Comment:

Agreed, we can add types to this exception but I would like to keep it
simple, e.g. "''Expression contains mixed types: FloatField, IntegerField.
You must set output_field to IntegerField.''"

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

Django

unread,
Jun 9, 2019, 3:58:46 AM6/9/19
to django-...@googlegroups.com
#30548: Improve exceptions about mixed types in Expressions.
-------------------------------------+-------------------------------------
Reporter: Keryn Knight | Owner: Shubham
Type: | Bhagat
Cleanup/optimization | Status: assigned

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: expressions orm | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Shubham Bhagat):

* owner: nobody => Shubham Bhagat
* status: new => assigned


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

Django

unread,
Jun 10, 2019, 2:55:11 AM6/10/19
to django-...@googlegroups.com
#30548: Improve exceptions about mixed types in Expressions.
-------------------------------------+-------------------------------------
Reporter: Keryn Knight | Owner: Shubham
Type: | Bhagat
Cleanup/optimization | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: expressions orm | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/11457 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/30548#comment:3>

Django

unread,
Jun 10, 2019, 2:14:57 PM6/10/19
to django-...@googlegroups.com
#30548: Improve exceptions about mixed types in Expressions.
-------------------------------------+-------------------------------------
Reporter: Keryn Knight | Owner: Shubham
Type: | Bhagat
Cleanup/optimization | Status: closed

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: expressions orm | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

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


Comment:

In [changeset:"34f8eeea4aaed37e1d02915b76c18892c8c1718e" 34f8eeea]:
{{{
#!CommitTicketReference repository=""
revision="34f8eeea4aaed37e1d02915b76c18892c8c1718e"
Fixed #30548 -- Improved exception when expression contains mixed types.
}}}

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

Reply all
Reply to author
Forward
0 new messages