Git Product home page Git Product logo

Comments (5)

spulec avatar spulec commented on September 26, 2024

Thanks for reporting this Neal!

from freezegun.

nealtodd avatar nealtodd commented on September 26, 2024

Hi Steve,

No problem. Thanks for looking at the issues so quickly. This'll be a very useful library for my unit tests.

Slight issue still with the dateutil library because isinstance(freezegun.api.FakeDatetime, datetime.date) is False, whereas isinstance(datetime.datetime.now(), datetime.date) is True:

In [1]: import datetime

In [2]: import dateutil

In [3]: d = datetime.date.today()

In [4]: type(d)
Out[4]: <type 'datetime.date'>

In [5]: isinstance(d, datetime.date)
Out[5]: True

In [6]: d + dateutil.relativedelta.relativedelta(days=1)
Out[6]: datetime.date(2012, 12, 13)

In [7]: d = datetime.datetime.now()

In [8]: type(d)
Out[8]: <type 'datetime.datetime'>

In [9]: isinstance(d, datetime.datetime)
Out[9]: True

In [10]: isinstance(d, datetime.date)
Out[10]: True

In [11]: d + dateutil.relativedelta.relativedelta(days=1)
Out[11]: datetime.datetime(2012, 12, 13, 17, 14, 30, 291827)

In [12]: from freezegun import freeze_time

In [13]: d = datetime.date.today()

In [14]: type(d)
Out[14]: <class 'freezegun.api.FakeDate'>

In [15]: isinstance(d, datetime.date)
Out[15]: True

In [16]: d + dateutil.relativedelta.relativedelta(days=1)
Out[16]: datetime.date(2012, 12, 13)

In [17]: d = datetime.datetime.now()

In [18]: type(d)
Out[18]: <class 'freezegun.api.FakeDatetime'>

In [19]: isinstance(d, datetime.datetime)
Out[19]: True

In [20]: isinstance(d, datetime.date)
Out[20]: False

In [21]: d + dateutil.relativedelta.relativedelta(days=1)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/vagrant/<ipython console> in <module>()

/home/vagrant/.virtualenvs/upshot/local/lib/python2.7/site-packages/dateutil/relativedelta.pyc in __radd__(self, other)
    245     def __radd__(self, other):
    246         if not isinstance(other, datetime.date):
--> 247             raise TypeError, "unsupported type for add operation"
    248         elif self._has_time and not isinstance(other, datetime.datetime):
    249             other = datetime.datetime.fromordinal(other.toordinal())

TypeError: unsupported type for add operation

/home/vagrant/.virtualenvs/upshot/local/lib/python2.7/site-packages/dateutil/relativedelta.pyc in __radd__(self, other)
    245     def __radd__(self, other):
    246         if not isinstance(other, datetime.date):
--> 247             raise TypeError, "unsupported type for add operation"
    248         elif self._has_time and not isinstance(other, datetime.datetime):
    249             other = datetime.datetime.fromordinal(other.toordinal())

TypeError: unsupported type for add operation

from freezegun.

spulec avatar spulec commented on September 26, 2024

Oops, silly mistake. Forgot datetime needed to be a subclass of date. Fixed and pushed a new version to PyPI.

from freezegun.

nealtodd avatar nealtodd commented on September 26, 2024

Hi again Steve,

Think there might still be a problem with how the instances are resolved to classes. E.g. I think isinstance(myFakeDatetime, datetime.datetime) is True because datetime.datetime gets assigned to FakeDatetime.

When used in conjunction with something like dateutils there's an inconsistency as to what datetime.datetime is. Easier seen with a test:

import datetime
from dateutil.relativedelta import relativedelta
from freezegun import freeze_time


def test_dateutil():
        d1 = datetime.datetime.now()
        #print type(d1)  # <class 'freezegun.api.FakeDatetime'>
        assert isinstance(d1, datetime.datetime)
        d2 = d1 + relativedelta(days=0)
        #print type(d2)  # <type 'datetime.datetime'>
        assert isinstance(d2, datetime.datetime)


======================================================================
FAIL: test_dateutil.test_dateutil
----------------------------------------------------------------------
Traceback (most recent call last):
  File "[...]test_dateutil.py", line 12, in test_dateutil
    assert isinstance(d2, datetime.datetime)
AssertionError:
    assert isinstance(FakeDatetime(2012, 12, 13, 16, 0, 38, 500929), <module 'datetime' from '/virtualenvs/freezegun/lib/python2.6/lib-dynload/datetime.so'>.<module 'datetime' from '/virtualenvs/freezegun/lib/python2.6/lib-dynload/datetime.so'>)
    datetime.datetime(2012, 12, 13, 16, 0, 38, 500929) = FakeDatetime(2012, 12, 13, 16, 0, 38, 500929) + relativedelta(days=0)
    #print type(d2)  # <type 'datetime.datetime'>
>>  assert isinstance(datetime.datetime(2012, 12, 13, 16, 0, 38, 500929), <module 'datetime' from '/virtualenvs/freezegun/lib/python2.6/lib-dynload/datetime.so'>.<module 'datetime' from '/virtualenvs/freezegun/lib/python2.6/lib-dynload/datetime.so'>)

Although type(d2) gives <type 'datetime.datetime'>, isinstance(d2, datetime.datetime) is False.

from freezegun.

spulec avatar spulec commented on September 26, 2024

I put in a quick fix that will make addition and subtraction work. I'll go back at some point and reconsider other operators. If you find any, please reopen this ticket or open another ticket.

from freezegun.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.