ESMValGroup / ESMValTool

ESMValTool: A community diagnostic and performance metrics tool for routine evaluation of Earth system models in CMIP
https://www.esmvaltool.org
Apache License 2.0
210 stars 121 forks source link

Fix failing tests after CMIP6 climate patterns merge #3670

Closed ehogan closed 1 week ago

ehogan commented 1 week ago

I noticed that the tests are failing since the CMIP6 climate patterns PR (#2785) was merged:

 _ ERROR collecting esmvaltool/diag_scripts/climate_patterns/climate_patterns.py _
ImportError while importing test module '/home/runner/work/ESMValTool/ESMValTool/esmvaltool/diag_scripts/climate_patterns/climate_patterns.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
../../../miniconda3/envs/esmvaltool-fromlock/lib/python3.11/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
esmvaltool/diag_scripts/climate_patterns/climate_patterns.py:41: in <module>
    from plotting import (
E   ImportError: cannot import name 'plot_timeseries' from 'plotting' (/home/runner/work/ESMValTool/ESMValTool/esmvaltool/diag_scripts/arctic_ocean/plotting.py)
____ ERROR collecting esmvaltool/diag_scripts/climate_patterns/plotting.py _____
import file mismatch:
imported module 'plotting' has this __file__ attribute:
  /home/runner/work/ESMValTool/ESMValTool/esmvaltool/diag_scripts/arctic_ocean/plotting.py
which is not the same as the test file we want to collect:
  /home/runner/work/ESMValTool/ESMValTool/esmvaltool/diag_scripts/climate_patterns/plotting.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules

All the checks were passing on the PR. Is it possible to update the PR checks to catch issues like this?

I will open a PR now with a fix 😊

ehogan commented 1 week ago

I have been looking into this issue and have discovered the following:

diff --git a/setup.cfg b/setup.cfg
index c738c5d71..e28f8079a 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -1,5 +1,6 @@
 [tool:pytest]
 addopts =
+    --import-mode=importlib
     --doctest-modules
     --ignore=doc/sphinx/source/conf.py
     --cov=esmvaltool

This causes different import errors, e.g.:

__________________ ERROR collecting esmvaltool/diag_scripts/climate_patterns/climate_patterns.py ___________________
ImportError while importing test module '/ESMValTool/esmvaltool/diag_scripts/climate_patterns/climate_patterns.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
esmvaltool/diag_scripts/climate_patterns/climate_patterns.py:40: in <module>
    import sub_functions as sf
E   ModuleNotFoundError: No module named 'sub_functions'

which will need updating. I would be happy to do this, but I would like some input from the @ESMValGroup/technical-lead-development-team whether you would prefer absolute or relative imports. For example:

What are your preferences? 🤔

bouweandela commented 1 week ago

Pep8 recommends absolute imports, so those should be preferred: https://peps.python.org/pep-0008/#imports

Maybe you could use some of the changes from #3646

ehogan commented 1 week ago

Pep8 recommends absolute imports, so those should be preferred: https://peps.python.org/pep-0008/#imports

Great!

Maybe you could use some of the changes from #3646

Yes, I see about 15-20 import changes within the diag_scripts directory that appear to align with changes from #3646; would you be happy for me to capture those in a new PR against this issue so we can solve the failing tests on main, please? 😊

bouweandela commented 1 week ago

Yes, that would be great!