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

Freeze Time doesn't freeze time #12

Closed
PuercoPop opened this issue Feb 6, 2013 · 9 comments
Closed

Freeze Time doesn't freeze time #12

PuercoPop opened this issue Feb 6, 2013 · 9 comments

Comments

@PuercoPop
Copy link
Contributor

Sorry for the lousy Issue title, I don't know how to frase it, but the test case is pretty clear.

Test Case: https://gist.github.com/PuercoPop/4724228

# -*- coding: utf-8 -*-

from datetime import datetime
from freezegun import freeze_time


class A(object):
    def __init__(self):
        self.date = datetime.now()


def test_():
    with freeze_time("1990-01-01 18:00:00"):
        a = A()
    assert a.date.year == 1990
@PuercoPop
Copy link
Contributor Author

I've just that If I swap the order of the import the testcase works fine, but from the pep8
Imports should be grouped in the following order:

  1. standard library imports
  2. related third party imports
  3. local application/library specific imports

@spulec
Copy link
Owner

spulec commented Feb 10, 2013

Yes, this is a known limitation of the library (there's a warning in the README). The other workaround involves just importing the datetime module and using that instead of importing classes from the module. Sorry that there's not currently a better answer.

@spulec spulec closed this as completed Feb 10, 2013
@jorgearanda
Copy link

The warning in the README is gone, but this is still failing for me.

@spulec
Copy link
Owner

spulec commented Nov 18, 2014

Are you running this exact code? It works on my local machine.

If you are using this exact code, can you let me know some more details about your setup? Python version, OS, freezegun version, etc.

If you are not using this exact code, can you show me the code you are using?

@jorgearanda
Copy link

That code works, but this, for instance, fails:

from freezegun import freeze_time
from datetime import datetime as dt

freezer = freeze_time('1980-01-01')
freezer.start()
assert dt.now() == dt(1980,1,1)

...whereas importing datetime.datetime after the call to start() succeeds:

from freezegun import freeze_time

freezer = freeze_time('1980-01-01')
freezer.start()
from datetime import datetime as dt
assert dt.now() == dt(1980,1,1)

I have similar issues using the @freeze_time decorator.

I'm on Mac OSX 10.10, python 2.7.1, freezegun 0.2.2.

@spulec
Copy link
Owner

spulec commented Nov 19, 2014

Okay. So I think the problem here is the aliasing of the datetime import as dt.

The following code works:

from freezegun import freeze_time
from datetime import datetime

freezer = freeze_time('1980-01-01')
freezer.start()
assert datetime.now() == datetime(1980,1,1)

This is definitely a bug, but I want to spend some time figuring out the best way to fix it. I've opened a new issue for this at #64.

Thanks for reporting it.

@jorgearanda
Copy link

Awesome, thanks!

@spulec
Copy link
Owner

spulec commented Nov 20, 2014

I have pushed a fix to master that I think will address this. Can you give it a test?

@jorgearanda
Copy link

That was extremely fast, and it works. Thanks a lot for fixing this!

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

No branches or pull requests

3 participants