Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TimedeltaField to_python fix #1

Closed
wants to merge 3 commits into from
Closed

Conversation

lucashowell
Copy link
Member

Current TimedeltaField implementation of to_python breaks on Document.__init__(). Examples below showing the behavior that occurs without this fix:

from datetime import datetime, timedelta
from extras_mongoengine.fields import TimedeltaField
from dataportal.mongoengine import Document

class A(Document):
    tdf = TimedeltaField()

# this works
a = A()
a.tdf = timedelta(days=1)

# this breaks
a = A(tdf=timedelta(days=1))

Example stacktrace (without this fix):

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-ea1a829cb996> in <module>
      1 # this does not
----> 2 a = A(tdf=timedelta(days=1))

~/venv/lib/python3.8/site-packages/mongoengine/base/document.py in __init__(self, *args, **values)
    134                         field = self._fields.get(key)
    135                         if field and not isinstance(field, FileField):
--> 136                             value = field.to_python(value)
    137                     setattr(self, key, value)
    138                 else:

~/venv/src/extras-mongoengine/extras_mongoengine/fields.py in to_python(self, value)
     27 
     28     def to_python(self, value):
---> 29         return timedelta(seconds=value)
     30 
     31     def prepare_query_value(self, op, value):

TypeError: unsupported type for timedelta seconds component: datetime.timedelta

@terencehonles
Copy link
Member

Thanks for fixing this @lucashowell looks like it was already fixed roughly the same way in https://github.com/MongoEngine/extras-mongoengine/pull/17/files do you mind if I just pull in that PR into our branch? It would be nice if this package wasn't dead. The only reason I found that PR was because I was wondering if anyone had commented on that package since I last looked at it and also to potentially say here what I said here MongoEngine#27 (comment)

@lucashowell
Copy link
Member Author

@terencehonles ahhh, nice! Thanks for finding that other branch - nice that they've added tests! Yes let's pull it in, thanks! I'll go ahead and remove this one since that one supersedes it.

Your suggestion about trying to roll it into MongoEngine as a whole is a good one. It would be nice if repo wasn't seemingly dead (or would at least be willing to hand it off to a new maintainer).

@terencehonles terencehonles deleted the tdf_to_python_fix branch July 28, 2020 17:26
@lucashowell
Copy link
Member Author

@terencehonles thanks for closing - guess i neglected to do so!

@terencehonles
Copy link
Member

terencehonles commented Jul 28, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants