Git Product home page Git Product logo

Comments (9)

merwok avatar merwok commented on September 26, 2024

Same issue here: importing freezegun has uncool side effects. Given the usage as decorator or context manager, this was fully unexpected to me. Can I help fix this?

from freezegun.

spulec avatar spulec commented on September 26, 2024

The reason freezegun mocks on import is to accomodate those that do from datetime import datetime before freezegun is activated. If you can find a good workaround, I'll be happy to pull it in.

from freezegun.

merwok avatar merwok commented on September 26, 2024

Hm, if someone imports datetime from datetime before the first import of freezegun (e.g. a web app that imports models on startup and the models import datetime, but freezegun is only imported after, in app.tests), it won’t work anyway, will it? IOW, recommended form of import becomes “import datetime” everywhere, and then import-time monkey-patching is not necessary. My own code would need changes as I always import classes from datetime, but I wouldn’t mind doing that if it allowed me to use freezegun instead of tedious mock.patch + datetime.

from freezegun.

spulec avatar spulec commented on September 26, 2024

Sorry, my comment was unclear. What works now is if someone imports freezegun, then does from datetime import datetime and then uses the decorator.

When freezegun first started, it didn't do any of this patching on import, but then I got a bunch of complaints. The issue is that people can't always control how third-party software imports datetime.

Sorry that this all kinda sucks. It's my least favorite thing about this library. We could do something like

import freezegun
freezegun.dont_patch()

and have dont_patch revert the patching.

That seems pretty gross to me though. Another idea is to have a different branch that works the old way.

from freezegun.

akshayjshah avatar akshayjshah commented on September 26, 2024

It's only marginally less gross, but there could also be two different import paths:

from freezegun import freeze_time  # doesn't patch on import
from freezegun.clobbering import freeze_time  # patches on import

from freezegun.

merwok avatar merwok commented on September 26, 2024

I am not sure that we can find a satisfactory solution. The problem in my case is that I need to control the return value of datetime.now in one module but without overriding the datetime type, as another module is for example checking types to know how to convert objects to JSON. These constraints seem at odds with what freezegun users want (according to the complaints you described earlier). I think I’ll have to write a shortcut function like patch_datetime('module', 'wanted time') using the regular mock module.

from freezegun.

spulec avatar spulec commented on September 26, 2024

This should be fixed in the newest version.

from freezegun.

merwok avatar merwok commented on September 26, 2024

Great news! Thank you for considering our use cases.

from freezegun.

spulec avatar spulec commented on September 26, 2024

Just to be completely clear (in case people find this issue in the future), Freezegun will not work directly with Pandas, but Freezegun now only mocks datetime when it is activated allowing use of Freezegun in non-Panda tests.

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.