-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
on_error
config option to allow raising EE exceptions (#49)
- Loading branch information
Showing
7 changed files
with
82 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,18 @@ | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
from typing import Literal | ||
|
||
|
||
@dataclass | ||
class Config: | ||
max_cache_size: int | None = None | ||
max_repr_mbs: int = 100 | ||
on_error: Literal["warn", "raise"] = "warn" | ||
|
||
def update(self, **kwargs) -> Config: | ||
if "on_error" in kwargs and kwargs["on_error"] not in ["warn", "raise"]: | ||
raise ValueError("on_error must be 'warn' or 'raise'") | ||
|
||
self.__dict__.update(**kwargs) | ||
return self |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from functools import _lru_cache_wrapper | ||
|
||
import ee | ||
import pytest | ||
|
||
import eerepr | ||
|
||
|
||
@pytest.mark.parametrize("max_cache_size", [0, None, 1, 10]) | ||
def test_cache_params(max_cache_size): | ||
""" | ||
Test that the cache size is correctly set, or disabled when max_cache_size=0. | ||
""" | ||
eerepr.initialize(max_cache_size=max_cache_size) | ||
|
||
if max_cache_size == 0: | ||
assert not isinstance(eerepr.repr._repr_html_, _lru_cache_wrapper) | ||
else: | ||
assert eerepr.repr._repr_html_.cache_info().maxsize == max_cache_size | ||
|
||
|
||
def test_max_repr_mbs(): | ||
""" | ||
Test that exceeding max_repr_mbs triggers a warning and falls back to string repr. | ||
""" | ||
eerepr.initialize(max_repr_mbs=0) | ||
|
||
with pytest.warns(UserWarning, match="HTML repr size"): | ||
rep = ee.Image.constant(0).set("system:id", "foo")._repr_html_() | ||
assert "<pre>" in rep | ||
|
||
|
||
def test_on_error(): | ||
"""Test that errors are correctly warned or raised based on on_error.""" | ||
invalid_obj = ee.Projection("not a real epsg") | ||
|
||
eerepr.initialize(on_error="warn") | ||
with pytest.warns(UserWarning, match="Getting info failed"): | ||
rep = invalid_obj._repr_html_() | ||
assert "Projection object" in rep | ||
|
||
eerepr.initialize(on_error="raise") | ||
with pytest.raises(ee.EEException): | ||
invalid_obj._repr_html_() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters