HEPData / hepdata_lib

Library for getting your data into HEPData
https://hepdata-lib.readthedocs.io
MIT License
15 stars 37 forks source link

Add `try`/`except` around ROOT import #259

Closed GraemeWatt closed 3 months ago

GraemeWatt commented 4 months ago

📚 Documentation preview 📚: https://hepdata-lib--259.org.readthedocs.build/en/259/

codecov[bot] commented 4 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 89.58%. Comparing base (3ed9148) to head (d51570d). Report is 12 commits behind head on main.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #259 +/- ## ========================================== + Coverage 87.71% 89.58% +1.86% ========================================== Files 5 5 Lines 1083 1085 +2 Branches 231 217 -14 ========================================== + Hits 950 972 +22 + Misses 93 81 -12 + Partials 40 32 -8 ``` | [Flag](https://app.codecov.io/gh/HEPData/hepdata_lib/pull/259/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=HEPData) | Coverage Δ | | |---|---|---| | [unittests-3.10](https://app.codecov.io/gh/HEPData/hepdata_lib/pull/259/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=HEPData) | `?` | | | [unittests-3.6](https://app.codecov.io/gh/HEPData/hepdata_lib/pull/259/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=HEPData) | `?` | | | [unittests-3.7](https://app.codecov.io/gh/HEPData/hepdata_lib/pull/259/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=HEPData) | `?` | | | [unittests-3.8](https://app.codecov.io/gh/HEPData/hepdata_lib/pull/259/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=HEPData) | `89.58% <100.00%> (+2.05%)` | :arrow_up: | | [unittests-3.9](https://app.codecov.io/gh/HEPData/hepdata_lib/pull/259/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=HEPData) | `?` | | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=HEPData#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

GraemeWatt commented 4 months ago

@clelange @alexander-held : I made a crude but simple attempt in this PR to address #99 by only importing ROOT functions in the hepdata_lib functions where they are actually needed. This means that import hepdata_lib now works without a ROOT installation as does the basic functionality (e.g. demonstrated in the Getting_started.ipynb notebook).

alexander-held commented 4 months ago

This is very nice to see, thanks for taking care of this!

clelange commented 3 months ago

Hi @GraemeWatt -- thanks for this! I'm wondering, however, if for hepdata_lib/root_utils.py (and probably also hepdata_lib/c_file_reader.py) it's not OK to actually keep the top-level ROOT import in a try-catch statement and rather print out a warning if the ROOT import failed.

My main issue with these changes is that while they move some of the ROOT import failures to later and therefore import hepdata_lib will not fail immediately, we currently don't have visibility how this would fail. It would be nice to actually have tests without a ROOT installation and then label in the docs which features work without a ROOT installation.

What do you think?

GraemeWatt commented 3 months ago

@clelange : Yes, I agree that keeping the top-level ROOT import and adding a try/except condition is a better (and simpler) solution than my first attempt at only importing ROOT functions in the hepdata_lib functions where they are actually needed. I've added a new commit on top of the upgrade-pylint branch and force-pushed to the change-root-import branch, so you should merge PR #260 before this one. I've added an explanation at the end of the README.md file. I don't see an easy way to modify the CI to work without a ROOT installation, but I tested locally (in a Python 3.11 virtual environment with an incompatible ROOT built with Python 3.12) that the code behaves as expected. Feel free to expand the tests run by the CI if you think it is important and you can work out how to do it.

clelange commented 3 months ago

Feel free to expand the tests run by the CI if you think it is important and you can work out how to do it.

I think we can just run some of the tests without installing ROOT, i.e. a separate set of tests. However, we can do that in a later PR.

There's a conflict for hepdata_lib/c_file_reader.py. Could you please check if you can fix it? You could also rebase on top of the current main branch now that #260 is merged, whichever is easier for you.

GraemeWatt commented 3 months ago

@clelange : I used a Pytest custom marker to add a marker needs_root to the tests that require a ROOT installation. I then introduced an additional element to the CI matrix without a ROOT installation root-version = "" and ran the tests using pytest -m 'not needs_root'. I also resolved the conflict with the main branch.