[Django] #27081: DateField with current date intitialisation pypy migration issue

7 views
Skip to first unread message

Django

unread,
Aug 17, 2016, 10:58:39 PM8/17/16
to django-...@googlegroups.com
#27081: DateField with current date intitialisation pypy migration issue
-------------------------------+--------------------
Reporter: Kurdakov | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 1.9
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------
if model contains DateField and it is initialized with current date

example:

from datetime import date

class TestClass(models.Model):

start_date = models.DateField(
verbose_name=u'start date',
default=date.today,
)

pypy migrations will fail

django/db/migrations/writer.py", line 540, in serialize
"topics/migrations/#migration-serializing" % (value,
get_docs_version())
ValueError: Cannot serialize: <bound method type.today of <class
'datetime.date'>>

reason:
code for serialising methods in Django checks
`if isinstance(value, (types.FunctionType, types.BuiltinFunctionType))`
which succeeds on cpython because datetime.date.today is a
BuiltinFunctionType, wheras it's a types.MethodType on pypy and this check
is missing in django
( link
https://github.com/django/django/blob/3b383085fb89a48e756383e7cd5d3bd867353ba1/django/db/migrations/serializer.py#L379
)

a solution for client code is to declare local function

def today():
return date.today()

but would be better to add types.MethodType check for pypy compatibility
class TestClass(models.Model):

start_date = models.DateField(
verbose_name=u'start date',
default=today,
)

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

Django

unread,
Aug 17, 2016, 11:01:12 PM8/17/16
to django-...@googlegroups.com
#27081: DateField with current date intitialisation pypy migration issue
-------------------------------+--------------------------------------

Reporter: Kurdakov | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 1.9
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Kurdakov):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Old description:

New description:

if model contains DateField and it is initialized with current date

example:

from datetime import date
{{{
#!python
class TestClass(models.Model):

start_date = models.DateField(
verbose_name=u'start date',
default=date.today,
)
}}}
pypy migrations will fail

django/db/migrations/writer.py", line 540, in serialize
"topics/migrations/#migration-serializing" % (value, get_docs_version())
ValueError: Cannot serialize: <bound method type.today of <class
'datetime.date'>>

reason:
code for serialising methods in Django checks
`if isinstance(value, (types.FunctionType, types.BuiltinFunctionType))`
which succeeds on cpython because datetime.date.today is a
BuiltinFunctionType, wheras it's a types.MethodType on pypy and this check
is missing in django
( link
https://github.com/django/django/blob/3b383085fb89a48e756383e7cd5d3bd867353ba1/django/db/migrations/serializer.py#L379
)

a solution for client code is to declare local function

{{{
#!python

def today():
return date.today()


class TestClass(models.Model):

start_date = models.DateField(
verbose_name=u'start date',
default=today,
)
}}}

but would be better to add types.MethodType check for pypy compatibility

--

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

Django

unread,
Aug 18, 2016, 9:49:18 AM8/18/16
to django-...@googlegroups.com
#27081: Allow migrations to serialize methods on pypy
----------------------------+------------------------------------
Reporter: Kurdakov | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.9
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+------------------------------------
Changes (by timgraham):

* type: Uncategorized => Bug
* has_patch: 0 => 1
* stage: Unreviewed => Accepted
* component: Uncategorized => Migrations


Comment:

Seems okay. That proposed change fixes these existing test failures on
pypy, so I guess no new tests are needed:
{{{
======================================================================
ERROR: test_serialize_datetime (migrations.test_writer.WriterTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/tim/code/django/tests/migrations/test_writer.py", line 330,
in test_serialize_datetime
self.assertSerializedEqual(datetime.datetime.utcnow)
File "/home/tim/code/django/tests/migrations/test_writer.py", line 194,
in assertSerializedEqual
self.assertEqual(self.serialize_round_trip(value), value)
File "/home/tim/code/django/tests/migrations/test_writer.py", line 190,
in serialize_round_trip
string, imports = MigrationWriter.serialize(value)
File "/home/tim/code/django/django/db/migrations/writer.py", line 293,
in serialize
return serializer_factory(value).serialize()
File "/home/tim/code/django/django/db/migrations/serializer.py", line
388, in serializer_factory
"topics/migrations/#migration-serializing" % (value,
get_docs_version())
ValueError: Cannot serialize: <bound method type.utcnow of <class
'datetime.datetime'>>
There are some values Django cannot serialize into migration files.
For more, see https://docs.djangoproject.com/en/dev/topics/migrations
/#migration-serializing

======================================================================
ERROR: test_simple_migration (migrations.test_writer.WriterTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/tim/code/django/tests/migrations/test_writer.py", line 542,
in test_simple_migration
output = writer.as_string()
File "/home/tim/code/django/django/db/migrations/writer.py", line 163,
in as_string
operation_string, operation_imports =
OperationWriter(operation).serialize()
File "/home/tim/code/django/django/db/migrations/writer.py", line 120,
in serialize
_write(arg_name, arg_value)
File "/home/tim/code/django/django/db/migrations/writer.py", line 72, in
_write
arg_string, arg_imports = MigrationWriter.serialize(item)
File "/home/tim/code/django/django/db/migrations/writer.py", line 293,
in serialize
return serializer_factory(value).serialize()
File "/home/tim/code/django/django/db/migrations/serializer.py", line
43, in serialize
item_string, item_imports = serializer_factory(item).serialize()
File "/home/tim/code/django/django/db/migrations/serializer.py", line
228, in serialize
return self.serialize_deconstructed(path, args, kwargs)
File "/home/tim/code/django/django/db/migrations/serializer.py", line
100, in serialize_deconstructed
arg_string, arg_imports = serializer_factory(arg).serialize()
File "/home/tim/code/django/django/db/migrations/serializer.py", line
388, in serializer_factory
"topics/migrations/#migration-serializing" % (value,
get_docs_version())
ValueError: Cannot serialize: <bound method type.utcnow of <class
'datetime.datetime'>>
There are some values Django cannot serialize into migration files.
For more, see https://docs.djangoproject.com/en/dev/topics/migrations
/#migration-serializing
}}}
[https://github.com/django/django/pull/7113 PR]

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

Django

unread,
Aug 30, 2016, 9:47:44 PM8/30/16
to django-...@googlegroups.com
#27081: Allow migrations to serialize methods on pypy
----------------------------+------------------------------------
Reporter: Kurdakov | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: 1.9
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+------------------------------------
Changes (by GitHub <noreply@…>):

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


Comment:

In [changeset:"96ee486ea415a532dbded3209114b98736031118" 96ee486e]:
{{{
#!CommitTicketReference repository=""
revision="96ee486ea415a532dbded3209114b98736031118"
Fixed #27081 -- Allowed migrations to serialize methods on pypy.
}}}

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

Reply all
Reply to author
Forward
0 new messages