Comments (5)
Thanks for reporting this Neal!
from freezegun.
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.
Oops, silly mistake. Forgot datetime needed to be a subclass of date. Fixed and pushed a new version to PyPI.
from freezegun.
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.
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)
- 1.3.0: `freezegun/__init__.py` is missing new version number HOT 1
- [Discussion] about freezing asyncio HOT 7
- ``PermissionError: [Errno 1] Operation not permitted`` appears when upgrading to ``1.3.0`` HOT 7
- Freezegun doesn't mock pointers to date functions HOT 1
- Tag for 1.2.2 is missing HOT 1
- Python 3.12: Deprecation warnings related to datetime.datetime.utcfromtimestamp HOT 2
- Time is not freezed when using `uvloop` event loop
- unsupported operand type(s) for +: 'FakeDatetime' and 'FakeDatetime'
- Datetime and Time packages do not have the same behavior when being frozen HOT 4
- timezone free datetime.datetime.fromtimestamp returns original datetime.datetime HOT 1
- Adding a return value to tick()
- Could have some problem with Django with TIME_ZONE setting
- Test regressions on Python 3.13.0b1 HOT 2
- `FakeDatetime`'s `timestamp()` is not consistent with `datetime().timestamp()` HOT 1
- consider overriding == on FakeDatetimeMeta HOT 2
- Freeze time doesn't work with FastAPI + pydantic V2 HOT 2
- [Bug] `tz_offset` shifts `datetime` which is instantiated with `tz` argument
- Possible to patch user-defined functions?
- FG incorrectly causes monotonic time to go back
- Huge performance degradation on Py 3.12
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from freezegun.