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

New pint version (0.24) breaks units #1771

Closed
Zeitsperre opened this issue Jun 7, 2024 · 4 comments
Closed

New pint version (0.24) breaks units #1771

Zeitsperre opened this issue Jun 7, 2024 · 4 comments
Labels
wontfix This will not be worked on

Comments

@Zeitsperre
Copy link
Collaborator

  • xclim version: 0.49.0
  • Python version: All
  • Operating System: Any

Description

The latest pint is causing xclim to error-out on import. We need to pin the library and perform a patch release, then adapt to the new API.

@Zeitsperre Zeitsperre mentioned this issue Jun 7, 2024
5 tasks
@aulemahal
Copy link
Collaborator

Is there a log we can see somewhere ?
On my machine, I installed a mamba env from xclim's environment.yml and then did pip install "pint >=0.24". In this setup, importing xclim works and a basic convert_units_to call does too.

@aulemahal
Copy link
Collaborator

Running the tests I see the following:

__________________________________________________________________________________________ ERROR at setup of TestPWMFit.test_pwm_fit[True-pearson3] __________________________________________________________________________________________
[gw0] linux -- Python 3.12.3 /home/pbourg/mambaforge/envs/xclim/bin/python3.12

threadsafe_data_dir = PosixPath('/tmp/pytest-of-pbourg/pytest-0/popen-gw0/data'), worker_id = 'gw0'
xdoctest_namespace = {'np': <module 'numpy' from '/home/pbourg/mambaforge/envs/xclim/lib/python3.12/site-packages/numpy/__init__.py'>, 'ope.../xclim/xclim/__init__.py'>, 'xr': <module 'xclim.testing' from '/home/pbourg/Projets/xclim/xclim/testing/__init__.py'>}

    @pytest.fixture(scope="session", autouse=True)
    def gather_session_data(threadsafe_data_dir, worker_id, xdoctest_namespace):
        """Gather testing data on pytest run.
    
        When running pytest with multiple workers, one worker will copy data remotely to _default_cache_dir while
        other workers wait using lockfile. Once the lock is released, all workers will then copy data to their local
        threadsafe_data_dir.As this fixture is scoped to the session, it will only run once per pytest run.
    
        Additionally, this fixture is also used to generate the `atmosds` synthetic testing dataset as well as add the
        example file paths to the xdoctest_namespace, used when running doctests.
        """
        if (
            not _default_cache_dir.joinpath(helpers.TESTDATA_BRANCH).exists()
            or helpers.PREFETCH_TESTING_DATA
        ):
            if helpers.PREFETCH_TESTING_DATA:
                print("`XCLIM_PREFETCH_TESTING_DATA` set. Prefetching testing data...")
            if sys.platform == "win32":
                raise OSError(
                    "UNIX-style file-locking is not supported on Windows. "
                    "Consider running `$ xclim prefetch_testing_data` to download testing data."
                )
            elif worker_id in ["master"]:
                helpers.populate_testing_data(branch=helpers.TESTDATA_BRANCH)
            else:
                _default_cache_dir.mkdir(exist_ok=True, parents=True)
                lockfile = _default_cache_dir.joinpath(".lock")
                test_data_being_written = FileLock(lockfile)
                with test_data_being_written:
                    # This flag prevents multiple calls from re-attempting to download testing data in the same pytest run
                    helpers.populate_testing_data(branch=helpers.TESTDATA_BRANCH)
                    _default_cache_dir.joinpath(".data_written").touch()
                with test_data_being_written.acquire():
                    if lockfile.exists():
                        lockfile.unlink()
        shutil.copytree(_default_cache_dir, threadsafe_data_dir)
>       helpers.generate_atmos(threadsafe_data_dir)

tests/conftest.py:500: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
xclim/testing/helpers.py:90: in generate_atmos
    rsus = shortwave_upwelling_radiation_from_net_downwelling(ds.rss, ds.rsds)
xclim/core/units.py:1291: in wrapper
    out = func(*args, **kwargs)
xclim/indices/_conversion.py:1140: in shortwave_upwelling_radiation_from_net_downwelling
    rss = convert_units_to(rss, rsds)
xclim/core/units.py:328: in convert_units_to
    target_cf_unit = pint2cfunits(target_unit)
xclim/core/units.py:190: in pint2cfunits
    return f"{value:cf}".replace("dimensionless", "")
../../mambaforge/envs/xclim/lib/python3.12/site-packages/pint/facets/plain/unit.py:67: in __format__
    return self._REGISTRY.formatter.format_unit(self, spec)
../../mambaforge/envs/xclim/lib/python3.12/site-packages/pint/delegates/formatter/full.py:129: in format_unit
    return self.get_formatter(uspec).format_unit(
../../mambaforge/envs/xclim/lib/python3.12/site-packages/pint/delegates/formatter/_to_register.py:102: in format_unit
    return func(units, registry=self._registry)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

unit = <UnitsContainer({'meter': -2, 'watt': 1})>, registry = None, options = {}, re = <module 're' from '/home/pbourg/mambaforge/envs/xclim/lib/python3.12/re/__init__.py'>

    @pint.register_unit_format("cf")
    def short_formatter(unit, registry, **options):
        """Return a CF-compliant unit string from a `pint` unit.
    
        Parameters
        ----------
        unit : pint.UnitContainer
            Input unit.
        registry : pint.UnitRegistry
            the associated registry
        **options
            Additional options (may be ignored)
    
        Returns
        -------
        out : str
            Units following CF-Convention, using symbols.
        """
        import re
    
        # convert UnitContainer back to Unit
>       unit = registry.Unit(unit)
E       AttributeError: 'NoneType' object has no attribute 'Unit'

../../mambaforge/envs/xclim/lib/python3.12/site-packages/cf_xarray/units.py:38: AttributeError

which looks like indeed a pint error, but stemming from cf-xarray rather than xclim ?

The nightly CI of cf-xarray has failed indeed xarray-contrib/cf-xarray#507.

@aulemahal
Copy link
Collaborator

I have opened an issue in pint, asking questions about the change hgrecco/pint#2009.

Zeitsperre added a commit that referenced this issue Jun 10, 2024
<!--Please ensure the PR fulfills the following requirements! -->
<!-- If this is your first PR, make sure to add your details to the
AUTHORS.rst! -->
### Pull Request Checklist:
- [x] This PR addresses an already opened issue (for bug fixes /
features)
    - This PR references #1771
- [ ] Tests for the changes have been added (for bug fixes / features)
- [ ] (If applicable) Documentation has been added / updated (for bug
fixes / features)
- [x] CHANGES.rst has been updated (with summary of main changes)
- [x] Link to issue (:issue:`number`) and pull request (:pull:`number`)
has been added

### What kind of change does this PR introduce?

* Pinned `pint` below v0.24 until we have time to adapt to their new
API.
* Updated the `dependency-review` action

### Does this PR introduce a breaking change?

Yes, `pint` is now pinned and `astroid` has been removed from the
`environment.yml` (was unused).

### Other information:

https://pint.readthedocs.io/en/stable/changes.html
@Zeitsperre Zeitsperre added the wontfix This will not be worked on label Jun 11, 2024
Zeitsperre added a commit to CSHS-CWRA/RavenPy that referenced this issue Jun 11, 2024
dsroberts pushed a commit to coecms/cms-conda-singularity that referenced this issue Jun 13, 2024
@Zeitsperre
Copy link
Collaborator Author

Closed with #1814

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants