Closed joetsoi closed 2 months ago
Thanks for the detailed report! I appreciate the minimal repo to help illustrate the situation.
pytest_collection
was chosen arbitrarily, as a early enough place to monkey patch before application code imported lru_cache
. I have no reservations using pytest_load_initial_conftest
, to get ahead of pytest-django
.
I'd very much appreciate a PR, as I have not used django and verifying correctness would take time.
Is there a better way to solve this?
I don't think there's much more we can do. The bootstrap hooks are super early and without the ability to specify ordering, will be tricky. I think a doc section for gotchas will be all we can do, for pytest-django and other plugins that import early. The open pytest issue doesn't seem to provide much of a solution.
I think I am encountering this with https://github.com/tophat/syrupy as well. https://github.com/ipwnponies/pytest-antilru/pull/35 fixes it.
pytest-django uses pytest_load_initial_conftests and runs
_setup_django()
this occurs very early to parse command line optionspytest-antilru uses pytest_collection( which runs at the beginning of each session and importantly runs after
pytest_load_initial_conftests
This means if you have an AppConfig that import code that uses
lru_cache
, then that function will be imported beforepython-antilru
runs preventing the cache from being busted.Minimal example
exampleapp/models.py
exampleapp/tests.py
in our app config we import a function that uses
lru_cache
this is called super early by pytest-django in _setup_django preventing python-antilru from monkey patching lru_cachein our project settings
example/settings.py
we add ourexampleapp
Now we run with
python-antilru
installed, but withoutpytest-django
settings configured followed by with settings configured.I think a solution to this might be to make
pytest-antilru
usepytest_load_initial_conftest
instead ofpytest_collection
, but thought I'd ask here first. I'm also aware that it would dependent on the order that plugins would be loaded, which I think (currently) can't be specifed. Is there a better way to solve this? I'm happy to open a pr