Skip to content
Open
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## [Unreleased]

- Speed up importing pystac ([#1584](https://github.com/stac-utils/pystac/pull/1584))

## [v1.14.1] - 2025-09-18

### Fixed
Expand Down
3 changes: 2 additions & 1 deletion pystac/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"set_stac_version",
]

import os
import warnings
from typing import Any

Expand Down Expand Up @@ -199,6 +198,8 @@ def write_file(
"""
if stac_io is None:
stac_io = StacIO.default()
import os

dest_href = None if dest_href is None else str(os.fspath(dest_href))
obj.save_object(
include_self_link=include_self_link, dest_href=dest_href, stac_io=stac_io
Expand Down
9 changes: 6 additions & 3 deletions pystac/asset.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
from __future__ import annotations

import os
import shutil
from copy import copy, deepcopy
from html import escape
from typing import TYPE_CHECKING, Any, Protocol, TypeVar

from pystac import MediaType, STACError, common_metadata, utils
from pystac.html.jinja_env import get_jinja_env
from pystac.utils import is_absolute_href, make_absolute_href, make_relative_href

if TYPE_CHECKING:
Expand Down Expand Up @@ -182,6 +179,10 @@ def __repr__(self) -> str:
return f"<Asset href={self.href}>"

def _repr_html_(self) -> str:
from html import escape

from pystac.html.jinja_env import get_jinja_env

jinja_env = get_jinja_env()
if jinja_env:
template = jinja_env.get_template("JSON.jinja2")
Expand Down Expand Up @@ -259,6 +260,8 @@ def delete(self) -> None:

Does not modify the asset.
"""
import os

href = _absolute_href(self.href, self.owner, "delete")
os.remove(href)

Expand Down
9 changes: 7 additions & 2 deletions pystac/catalog.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from __future__ import annotations

import os
import warnings
from collections.abc import Callable, Iterable, Iterator
from copy import deepcopy
from itertools import chain
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -521,6 +519,8 @@ def get_item(self, id: str, recursive: bool = False) -> Item | None:
Return:
Item or None: The item with the given ID, or None if not found.
"""
import warnings

warnings.warn(
"get_item is deprecated and will be removed in v2. "
"Use next(self.get_items(id), None) instead",
Expand Down Expand Up @@ -549,6 +549,8 @@ def get_items(self, *ids: str, recursive: bool = False) -> Iterator[Item]:
(if recursive) all catalogs or collections connected to this catalog
through child links.
"""
from itertools import chain

items: Iterator[Item]
if not recursive:
items = map(
Expand Down Expand Up @@ -615,6 +617,9 @@ def get_all_items(self) -> Iterator[Item]:
catalogs or collections connected to this catalog through
child links.
"""
import warnings
from itertools import chain

warnings.warn(
"get_all_items is deprecated and will be removed in v2",
DeprecationWarning,
Expand Down
9 changes: 6 additions & 3 deletions pystac/collection.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import warnings
from collections.abc import Iterable, Sequence
from copy import deepcopy
from datetime import datetime, timezone
Expand All @@ -12,8 +11,6 @@
cast,
)

from dateutil import tz

import pystac
from pystac import CatalogType, STACObjectType
from pystac.asset import Asset, Assets
Expand Down Expand Up @@ -257,6 +254,8 @@ def from_dict(d: dict[str, Any]) -> TemporalExtent:
parsed_intervals: list[list[datetime | None]] = []
for i in d["interval"]:
if isinstance(i, str):
import warnings

# d["interval"] is a list of strings, so we correct the list and
# try again
# https://github.com/stac-utils/pystac/issues/1221
Expand Down Expand Up @@ -384,6 +383,8 @@ def from_items(
Extent: An Extent that spatially and temporally covers all of the
given items.
"""
from dateutil import tz

bounds_values: list[list[float]] = [
[float("inf")],
[float("inf")],
Expand Down Expand Up @@ -635,6 +636,8 @@ def from_dict(
migrate: bool = True,
preserve_dict: bool = True,
) -> C:
import warnings

from pystac.extensions.version import CollectionVersionExtension

if migrate:
Expand Down
3 changes: 2 additions & 1 deletion pystac/extensions/eo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from __future__ import annotations

import warnings
from collections.abc import Iterable
from typing import (
Any,
Expand Down Expand Up @@ -386,6 +385,8 @@ def get_schema_uri(cls) -> str:

@classmethod
def get_schema_uris(cls) -> list[str]:
import warnings

warnings.warn(
"get_schema_uris is deprecated and will be removed in v2",
DeprecationWarning,
Expand Down
3 changes: 2 additions & 1 deletion pystac/extensions/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from __future__ import annotations

import warnings
from collections.abc import Iterable
from typing import Any, Generic, Literal, TypeVar, cast

Expand Down Expand Up @@ -370,6 +369,8 @@ def migrate(
found_fields[asset_key] = values

if found_fields:
import warnings

warnings.warn(
f"Assets {list(found_fields.keys())} contain fields: "
f"{list(set.union(*found_fields.values()))} which "
Expand Down
3 changes: 2 additions & 1 deletion pystac/extensions/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from __future__ import annotations

import re
import warnings
from re import Pattern
from typing import Any, Literal

Expand Down Expand Up @@ -92,6 +91,8 @@ def get_schema_uri(cls) -> str:

@classmethod
def get_schema_uris(cls) -> list[str]:
import warnings

warnings.warn(
"get_schema_uris is deprecated and will be removed in v2",
DeprecationWarning,
Expand Down
5 changes: 4 additions & 1 deletion pystac/extensions/item_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from __future__ import annotations

import warnings
from typing import Any, Literal

import pystac
Expand All @@ -27,6 +26,8 @@ class AssetDefinition(ItemAssetDefinition):
"""

def __init__(cls, *args: Any, **kwargs: Any) -> None:
import warnings

warnings.warn(
(
"``AssetDefinition`` is deprecated. "
Expand All @@ -49,6 +50,8 @@ class ItemAssetsExtension(ExtensionManagementMixin[pystac.Collection]):
collection: pystac.Collection

def __init__(self, collection: pystac.Collection) -> None:
import warnings

warnings.warn(
(
"The ``item_assets`` extension is deprecated. "
Expand Down
6 changes: 4 additions & 2 deletions pystac/extensions/projection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

from __future__ import annotations

import json
import warnings
from collections.abc import Iterable
from typing import (
Any,
Expand Down Expand Up @@ -223,6 +221,8 @@ def crs_string(self) -> str | None:
elif self.wkt2:
return self.wkt2
elif self.projjson:
import json

return json.dumps(self.projjson)
else:
return None
Expand Down Expand Up @@ -321,6 +321,8 @@ def get_schema_uri(cls) -> str:

@classmethod
def get_schema_uris(cls) -> list[str]:
import warnings

warnings.warn(
"get_schema_uris is deprecated and will be removed in v2",
DeprecationWarning,
Expand Down
3 changes: 2 additions & 1 deletion pystac/extensions/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from __future__ import annotations

import warnings
from collections.abc import Iterable
from typing import (
Any,
Expand Down Expand Up @@ -716,6 +715,8 @@ def get_schema_uri(cls) -> str:

@classmethod
def get_schema_uris(cls) -> list[str]:
import warnings

warnings.warn(
"get_schema_uris is deprecated and will be removed in v2",
DeprecationWarning,
Expand Down
6 changes: 4 additions & 2 deletions pystac/extensions/scientific.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@

from __future__ import annotations

import copy
from typing import Any, Generic, Literal, TypeVar, cast
from urllib import parse

import pystac
from pystac.extensions.base import (
Expand Down Expand Up @@ -49,6 +47,8 @@ class ScientificRelType(StringEnum):

def doi_to_url(doi: str) -> str:
"""Converts a DOI to the corresponding URL."""
from urllib import parse

return DOI_URL_BASE + parse.quote(doi)


Expand All @@ -72,6 +72,8 @@ def __repr__(self) -> str:
return f"<Publication doi={self.doi} target={self.citation}>"

def to_dict(self) -> dict[str, str | None]:
import copy

return copy.deepcopy({"doi": self.doi, "citation": self.citation})

@staticmethod
Expand Down
3 changes: 2 additions & 1 deletion pystac/extensions/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from __future__ import annotations

import warnings
from collections.abc import Generator
from contextlib import contextmanager
from typing import (
Expand Down Expand Up @@ -437,6 +436,8 @@ def ignore_deprecated() -> Generator[None]:
"""Context manager for suppressing the :class:`pystac.DeprecatedWarning`
when creating a deprecated :class:`~pystac.Item` or :class:`~pystac.Collection`
from a dictionary."""
import warnings

with warnings.catch_warnings():
warnings.simplefilter("ignore", category=DeprecatedWarning)
yield
3 changes: 2 additions & 1 deletion pystac/item.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import warnings
from copy import copy, deepcopy
from typing import TYPE_CHECKING, Any, TypeVar, cast

Expand Down Expand Up @@ -419,6 +418,8 @@ def from_dict(
migrate: bool = True,
preserve_dict: bool = True,
) -> T:
import warnings

from pystac.extensions.version import ItemVersionExtension

if preserve_dict:
Expand Down
3 changes: 2 additions & 1 deletion pystac/item_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from __future__ import annotations

from copy import deepcopy
from typing import TYPE_CHECKING, Any

import pystac
Expand Down Expand Up @@ -179,6 +178,8 @@ def roles(self, v: list[str] | None) -> None:

def to_dict(self) -> dict[str, Any]:
"""Returns a dictionary representing this ``ItemAssetDefinition``."""
from copy import deepcopy

return deepcopy(self.properties)

def create_asset(self, href: str) -> pystac.Asset:
Expand Down
9 changes: 6 additions & 3 deletions pystac/item_collection.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from __future__ import annotations

from collections.abc import Collection, Iterable, Iterator
from copy import deepcopy
from html import escape
from typing import (
Any,
TypeAlias,
Expand All @@ -11,7 +9,6 @@

import pystac
from pystac.errors import STACTypeError
from pystac.html.jinja_env import get_jinja_env
from pystac.serialization.identify import identify_stac_object_type
from pystac.utils import HREF, is_absolute_href, make_absolute_href, make_posix_style

Expand Down Expand Up @@ -147,6 +144,10 @@ def to_dict(self, transform_hrefs: bool = False) -> dict[str, Any]:
}

def _repr_html_(self) -> str:
from html import escape

from pystac.html.jinja_env import get_jinja_env

jinja_env = get_jinja_env()
if jinja_env:
template = jinja_env.get_template("JSON.jinja2")
Expand All @@ -158,6 +159,8 @@ def clone(self) -> ItemCollection:
"""Creates a clone of this instance. This clone is a deep copy; all
:class:`~pystac.Item` instances are cloned and all additional top-level fields
are deep copied."""
from copy import deepcopy

return self.__class__(
items=[item.clone() for item in self.items],
extra_fields=deepcopy(self.extra_fields),
Expand Down
Loading