ecmwf / cdsapi

Python API to access the Copernicus Climate Data Store (CDS)
Apache License 2.0
233 stars 56 forks source link

Use importlib instead of pkg_resources when possible #74

Open grigaut opened 1 year ago

grigaut commented 1 year ago

Use of pkg_resources triggers DeprecationWarning and according to https://setuptools.pypa.io/en/latest/pkg_resources.html, pkg_resources should be replaced by importlib.

Minimal steps to reproduce warning:

Pytest should raise following warning: warnings.warn("pkg_resources is deprecated as an API", DeprecationWarning)

FussyDuck commented 1 year ago

CLA assistant check
All committers have signed the CLA.

veenstrajelmer commented 4 months ago

@grigaut thanks for this PR, it is a step towards fixing https://github.com/ecmwf/cdsapi/issues/89.

Hierover, I would suggest to replace the current suggestion by something more robust without the need for a try-except statement. The builtin importlib.metadata is available since python 3.8. should work like this:

from importlib.metadata import version

cdsapi_version = version("cdsapi")
self.session.headers = {
        "User-Agent": f"cdsapi/{cdsapi_version}",
        }

I believe this should result in the desired behaviour with less code, but I have not tested it so I might be mistaken.