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

use python package-specific "user agent" string #665

Merged
merged 2 commits into from
Oct 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Python-packages/covidcast-py/covidcast/covidcast.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,22 @@
import pandas as pd
import numpy as np
from delphi_epidata import Epidata
from delphi_epidata.delphi_epidata import _HEADERS
from pkg_resources import get_distribution, DistributionNotFound
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find documentation for pkg_resources. My guess is it comes from setuptools which is being deprecated.

Maybe use importlib.metadata to future-proof? From docs:

importlib.metadata is a library that provides access to the metadata of an installed Distribution Package, such as its entry points or its top-level names (Import Packages, modules, if any). Built in part on Python’s import system, this library intends to replace similar functionality in the entry point API and metadata API of pkg_resources. Along with importlib.resources, this package can eliminate the need to use the older and less efficient pkg_resources package.

> import pkg_resources
> import importlib.metadata
> print(pkg_resources.get_distribution('wheel'))
> print(importlib.metadata.version('wheel'))
wheel 0.41.2
0.41.2

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I shamelessly stole the version determination code from the delphi-epidata python client (that this client depends on): https://github.com/cmu-delphi/delphi-epidata/blob/d42517e714d6d1deebb6cffcdf3fb5adadc54481/src/client/delphi_epidata.py#L17-L25

I dont see where setuptools (as a whole) is being deprecated, but pkg_resources did get marked as such about half a year ago... But before we rush into yanking it out: importlib.metadata requires python3.8 or a fancy requirements directive to pull in its backport, so its not such a simple drop-in replacement. There are also two other files in this repo that already use pkg_resources (

and ), plus one in covidcast-indicators (https://github.com/cmu-delphi/covidcast-indicators/blob/39c02fa228fcb4de2b3f3d19469e862861351a18/_delphi_utils_python/delphi_utils/geomap.py#L15) as well as the aforementioned delphi-epidata client.

Lets kick this down the road a bit -- ill make GH issues to remove pkg_resources in each of the 3 repos.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, thanks for the clarifications. Not super critical as Python packaging evolves super slowly, so makes sense to just stay consistent with our other code.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from epiweeks import Week

from .errors import NoDataWarning


# Point API requests to the default endpoint
Epidata.BASE_URL = "https://api.covidcast.cmu.edu/epidata"
# Prepend to Epidata client's user agent to specify this package and version
try:
_ver = get_distribution("covidcast").version
except DistributionNotFound:
_ver = "0.0.0"
_HEADERS['user-agent'] = f"covidcast/{_ver} " + _HEADERS['user-agent']


VALID_GEO_TYPES = {"county", "hrr", "msa", "dma", "state", "hhs", "nation"}

Expand Down