stac-utils / pystac

Python library for working with any SpatioTemporal Asset Catalog (STAC)
https://pystac.readthedocs.io
Other
325 stars 115 forks source link

Namespace the client so we do `from pystac.client import Client` #1334

Open alexgleith opened 2 months ago

alexgleith commented 2 months ago

This is a pedantic nice-to-have!

I would love to be able to get the client like from pystac.client import Client rather than from pystac_client import Client.

It's a little thing, but it would make me happy!

gadomski commented 2 months ago

🤔 I'm not sure how this would work in practice — from my understanding (and experience with) namespace packages, they don't allow top-level imports, e.g. if we had from pystac.client import Client we couldn't have from pystac import Item. I could be wrong about this, though...

In the past, we've considered merging pystac-client into pystac itself, which would get you what you wanted. We haven't done this because we didn't want to increase pystac's dependencies, and we felt that using optional dependencies was a bit awkward. The awkwardness of having a separate API client library is part of the reason why we haven't pushed pystac-client to v1, because we haven't been totally sure that it should be its own thing.

jsignell commented 2 months ago

Dask certainly has both top-level imports from dask import delayed and namespaced imports from dask.distributed import Client. It implements that just by having a distributed.py file within dask proper.

from __future__ import annotations

_import_error_message = (
    "dask.distributed is not installed.\n\n"
    "Please either conda or pip install distributed:\n\n"
    "  conda install dask distributed             # either conda install\n"
    '  python -m pip install "dask[distributed]" --upgrade    # or pip install'
)

try:
    from distributed import *
except ImportError as e:
    if e.msg == "No module named 'distributed'":
        raise ImportError(_import_error_message) from e
    else:
        raise

def __getattr__(value):
    try:
        import distributed
    except ImportError as e:
        raise ImportError(_import_error_message) from e
    return getattr(distributed, value)

https://github.com/dask/dask/blob/dafb6ac0a647d751f662d0e30f616c62cf62b799/dask/distributed.py

gadomski commented 2 months ago

Nice, thanks @jsignell . Moving this issue to pystac as we'd need to implement here.

alexgleith commented 2 months ago

Thanks for being open to this!