Skip to content
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

- Add comprehensive test coverage for warning context managers (`ignore()` and `strict()`) ([#832](https://github.com/stac-utils/pystac-client/pull/832))

### Changed

- Make `get_collection` raise if `collection_id` is empty ([#809](https://github.com/stac-utils/pystac-client/pull/809))
Expand Down
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ select = ["E", "F", "W", "I"]
[tool.pytest.ini_options]
markers = "vcr: records network activity"
addopts = "--benchmark-skip --block-network"
filterwarnings = [
"ignore::ResourceWarning",
"ignore::pytest.PytestUnraisableExceptionWarning",
]

[tool.mypy]
show_error_codes = true
Expand Down
16 changes: 10 additions & 6 deletions pystac_client/collection_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,17 +304,21 @@ def __init__(
if any([bbox, datetime, q, query, filter, sortby, fields]):
if not self._collection_search_extension_enabled:
warnings.warn(
str(DoesNotConformTo("COLLECTION_SEARCH"))
+ ". Filtering will be performed client-side where only bbox, "
"datetime, and q arguments are supported"
DoesNotConformTo(
"COLLECTION_SEARCH",
"Filtering will be performed client-side where only bbox, "
"datetime, and q arguments are supported",
)
)
self._validate_client_side_args()
else:
if not self._collection_search_free_text_enabled:
warnings.warn(
str(DoesNotConformTo("COLLECTION_SEARCH#FREE_TEXT"))
+ ". Free-text search is not enabled for collection search"
"Free-text filters will be applied client-side."
DoesNotConformTo(
"COLLECTION_SEARCH#FREE_TEXT",
"Free-text search is not enabled for collection search"
"Free-text filters will be applied client-side.",
)
)

else:
Expand Down
2 changes: 1 addition & 1 deletion pystac_client/warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def ignore() -> Iterator[None]:
>>> from pystac_client import Client
>>> from pystac_client.warnings import ignore
>>> with ignore():
... Client.open("https://perfect-api.test")
... Client.open("https://imperfect-api.test")

For finer-grained control:

Expand Down
39 changes: 39 additions & 0 deletions tests/test_warnings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import pytest

from pystac_client import Client
from pystac_client.warnings import DoesNotConformTo, ignore, strict

from .helpers import TEST_DATA


class TestWarningContextManagers:
@pytest.mark.filterwarnings("error")
def test_ignore_context_manager(self) -> None:
"""Test that ignore() context manager suppresses warnings."""
api = Client.from_file(str(TEST_DATA / "planetary-computer-root.json"))
api.remove_conforms_to("COLLECTION_SEARCH")

with ignore():
api.collection_search(limit=10, max_collections=10, q="test")

@pytest.mark.filterwarnings("error")
def test_strict_context_manager(self) -> None:
"""Test that strict() context manager converts warnings to exceptions."""
api = Client.from_file(str(TEST_DATA / "planetary-computer-root.json"))
api.remove_conforms_to("COLLECTION_SEARCH")

with strict():
with pytest.raises(DoesNotConformTo, match="COLLECTION_SEARCH"):
api.collection_search(limit=10, max_collections=10, q="test")

def test_ignore_context_manager_cleanup(self) -> None:
"""Test that ignore() properly restores warning filters after exit."""
api = Client.from_file(str(TEST_DATA / "planetary-computer-root.json"))
api.remove_conforms_to("COLLECTION_SEARCH")

with ignore():
api.collection_search(limit=10, max_collections=10, q="test")

with strict():
with pytest.raises(DoesNotConformTo, match="COLLECTION_SEARCH"):
api.collection_search(limit=10, max_collections=10, q="test")