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

refactor: Avoid implicit re-exports in main module #543

Closed
Closed
Show file tree
Hide file tree
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
21 changes: 10 additions & 11 deletions pystac/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
import dateutil.parser
from dateutil import tz

import pystac
from pystac import STACObjectType, CatalogType
from pystac.rel_type import RelType
from pystac.stac_io import StacIO
from pystac.stac_object import STACObjectType
from pystac.asset import Asset
from pystac.catalog import Catalog
from pystac.catalog import Catalog, CatalogType
from pystac.layout import HrefLayoutStrategy
from pystac.link import Link
from pystac.utils import datetime_to_str
Expand Down Expand Up @@ -653,7 +654,7 @@ def clone(self) -> "Collection":
clone._resolved_objects.cache(clone)

for link in self.links:
if link.rel == pystac.RelType.ROOT:
if link.rel == RelType.ROOT:
# Collection __init__ sets correct root to clone; don't reset
# if the root link points to self
root_is_self = link.is_resolved() and link.target is self
Expand Down Expand Up @@ -721,11 +722,11 @@ def from_dict(
)

for link in links:
if link["rel"] == pystac.RelType.ROOT:
if link["rel"] == RelType.ROOT:
# Remove the link that's generated in Catalog's constructor.
collection.remove_links(pystac.RelType.ROOT)
collection.remove_links(RelType.ROOT)

if link["rel"] != pystac.RelType.SELF or href is None:
if link["rel"] != RelType.SELF or href is None:
collection.add_link(Link.from_dict(link))

if assets is not None:
Expand Down Expand Up @@ -764,12 +765,10 @@ def full_copy(
return cast(Collection, super().full_copy(root, parent))

@classmethod
def from_file(
cls, href: str, stac_io: Optional[pystac.StacIO] = None
) -> "Collection":
def from_file(cls, href: str, stac_io: Optional[StacIO] = None) -> "Collection":
result = super().from_file(href, stac_io)
if not isinstance(result, Collection):
raise pystac.STACTypeError(f"{result} is not a {Collection}.")
raise STACTypeError(f"{result} is not a {Collection}.")
return result

@classmethod
Expand Down
34 changes: 17 additions & 17 deletions pystac/item_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
from pystac.errors import STACTypeError
from typing import Any, Dict, Iterator, List, Optional, Collection, Iterable, Union

import pystac
from pystac.item import Item
from pystac.stac_io import StacIO
from pystac.stac_object import STACObjectType
from pystac.utils import make_absolute_href, is_absolute_href
from pystac.serialization.identify import identify_stac_object_type


ItemLike = Union[pystac.Item, Dict[str, Any]]
ItemLike = Union[Item, Dict[str, Any]]


class ItemCollection(Collection[pystac.Item]):
class ItemCollection(Collection[Item]):
"""Implementation of a GeoJSON FeatureCollection whose features are all STAC
Items.

Expand Down Expand Up @@ -70,7 +72,7 @@ class ItemCollection(Collection[pystac.Item]):
# If an item is present in both ItemCollections it will only be added once
"""

items: List[pystac.Item]
items: List[Item]
"""List of :class:`pystac.Item` instances contained in this ``ItemCollection``."""

extra_fields: Dict[str, Any]
Expand All @@ -83,20 +85,20 @@ def __init__(
extra_fields: Optional[Dict[str, Any]] = None,
clone_items: bool = False,
):
def map_item(item_or_dict: ItemLike) -> pystac.Item:
def map_item(item_or_dict: ItemLike) -> Item:
# Converts dicts to pystac.Items and clones if necessary
if isinstance(item_or_dict, pystac.Item):
if isinstance(item_or_dict, Item):
return item_or_dict.clone() if clone_items else item_or_dict
else:
return pystac.Item.from_dict(item_or_dict)
return Item.from_dict(item_or_dict)

self.items = list(map(map_item, items))
self.extra_fields = extra_fields or {}

def __getitem__(self, idx: int) -> pystac.Item:
def __getitem__(self, idx: int) -> Item:
return self.items[idx]

def __iter__(self) -> Iterator[pystac.Item]:
def __iter__(self) -> Iterator[Item]:
return iter(self.items)

def __len__(self) -> int:
Expand Down Expand Up @@ -151,25 +153,23 @@ def from_dict(
raise STACTypeError("Dict is not a valid ItemCollection")

items = [
pystac.Item.from_dict(item, preserve_dict=preserve_dict)
Item.from_dict(item, preserve_dict=preserve_dict)
for item in d.get("features", [])
]
extra_fields = {k: v for k, v in d.items() if k not in ("features", "type")}

return cls(items=items, extra_fields=extra_fields)

@classmethod
def from_file(
cls, href: str, stac_io: Optional[pystac.StacIO] = None
) -> "ItemCollection":
def from_file(cls, href: str, stac_io: Optional[StacIO] = None) -> "ItemCollection":
"""Reads a :class:`ItemCollection` from a JSON file.

Arguments:
href : Path to the file.
stac_io : A :class:`~pystac.StacIO` instance to use for file I/O
"""
if stac_io is None:
stac_io = pystac.StacIO.default()
stac_io = StacIO.default()

if not is_absolute_href(href):
href = make_absolute_href(href)
Expand All @@ -181,7 +181,7 @@ def from_file(
def save_object(
self,
dest_href: str,
stac_io: Optional[pystac.StacIO] = None,
stac_io: Optional[StacIO] = None,
) -> None:
"""Saves this instance to the ``dest_href`` location.

Expand All @@ -191,7 +191,7 @@ def save_object(
will use the default instance.
"""
if stac_io is None:
stac_io = pystac.StacIO.default()
stac_io = StacIO.default()

stac_io.save_json(dest_href, self.to_dict())

Expand All @@ -217,6 +217,6 @@ def is_item_collection(d: Dict[str, Any]) -> bool:
# Prior to STAC 0.9 ItemCollections did not have a stac_version field and could
# only be identified by the fact that all of their 'features' are STAC Items.
return all(
identify_stac_object_type(feature) == pystac.STACObjectType.ITEM
identify_stac_object_type(feature) == STACObjectType.ITEM
for feature in d.get("features", [])
)