[Django] #22549: DateTimeField raises timezone awareness related RuntimeWarnings when it should'nt

3 views
Skip to first unread message

Django

unread,
Apr 30, 2014, 4:01:02 PM4/30/14
to django-...@googlegroups.com
#22549: DateTimeField raises timezone awareness related RuntimeWarnings when it
should'nt
----------------------------------------------+--------------------
Reporter: matias@… | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version:
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 1 | UI/UX: 0
----------------------------------------------+--------------------
I've experienced some issues with wrong RuntimeWarnings about naive
datetimes being raised when the
datetime I was using was perfectly timezone aware and I think I found the
reason why:

In django/db/models/fields/init.py file, inside the definition of
DateTimeField.to_python method,
the timezone awareness of a datetime is checked calling the isinstance
function.


{{{
if isinstance(value, datetime.date):
value = datetime.datetime(value.year, value.month, value.day)
if settings.USE_TZ:
# For backwards compatibility, interpret naive datetimes in
# local time. This won't work during DST change, but we can't
# do much about it, so we let the exceptions percolate up the
# call stack.
warnings.warn("DateTimeField %s.%s received a naive datetime "
"(%s) while time zone support is active." %
(self.model.__name__, self.name, value),
RuntimeWarning)
}}}


Is true that, as said here
https://docs.python.org/2.7/library/datetime.html#available-types,
"objects of the date type are always naive", but is wrong to use
isinstance since all datetime.datetime
objects are also instances of datetime.date because datetime.datetime is a
subclass of datetime.date
(which I think is confusing since breakes the OOP intuition that says that
class inheritance should
describe "is a" relationships).

A posible solution is to compare using type(value) == datetime.date or,
more clearly, check using django.utils.is_naive

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

Django

unread,
Apr 30, 2014, 4:17:32 PM4/30/14
to django-...@googlegroups.com
#22549: DateTimeField raises timezone awareness related RuntimeWarnings when it
should'nt
-------------------------------------+-------------------------------------

Reporter: matias@… | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version:
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by ramiro):

* needs_better_patch: => 0
* stage: Unreviewed => Accepted
* easy: 1 => 0
* needs_tests: => 0
* needs_docs: => 0


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

Django

unread,
May 1, 2014, 12:11:00 PM5/1/14
to django-...@googlegroups.com
#22549: DateTimeField raises timezone awareness related RuntimeWarnings when it
should'nt
-------------------------------------+-------------------------------------
Reporter: matias@… | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version:
(models, ORM) | Resolution:
Severity: Normal | worksforme
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by aaugustin):

* status: new => closed
* resolution: => worksforme


Comment:

Look two lines above -- the complete code is:

{{{
if isinstance(value, datetime.datetime):
return value
if isinstance(value, datetime.date):
# ... stuff you complain about
}}}

If value is a `datetime.datetime` the fist branch returns, you don't hit
the second branch.

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

Reply all
Reply to author
Forward
0 new messages