Skip to content

Commit 94969a7

Browse files
committed
refactor: adapt from feedback
1 parent 47a7468 commit 94969a7

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

eodag/api/plugin.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1616
# See the License for the specific language governing permissions and
1717
# limitations under the License.
18-
from typing import Annotated, Any, Literal, Optional, Self, TypedDict, Union
18+
from __future__ import annotations
19+
20+
from typing import TYPE_CHECKING, Annotated, Any, Literal, Optional, TypedDict, Union
1921

2022
import yaml
2123
from annotated_types import Gt
@@ -24,6 +26,9 @@
2426
from eodag.utils import merge_mappings, sort_dict
2527
from eodag.utils.exceptions import ValidationError
2628

29+
if TYPE_CHECKING:
30+
from typing_extensions import Self
31+
2732

2833
class Pagination(TypedDict):
2934
"""Search pagination configuration"""

eodag/api/provider.py

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from collections import UserDict
2525
from dataclasses import dataclass, field
2626
from inspect import isclass
27-
from typing import Any, Iterator, Optional, Self, Union, get_type_hints
27+
from typing import TYPE_CHECKING, Any, Iterator, Optional, Union, get_type_hints
2828

2929
import yaml
3030

@@ -43,9 +43,16 @@
4343
slugify,
4444
update_nested_dict,
4545
)
46-
from eodag.utils.exceptions import ValidationError
46+
from eodag.utils.exceptions import (
47+
UnsupportedProductType,
48+
UnsupportedProvider,
49+
ValidationError,
50+
)
4751
from eodag.utils.repr import dict_to_html_table
4852

53+
if TYPE_CHECKING:
54+
from typing_extensions import Self
55+
4956
logger = logging.getLogger("eodag.provider")
5057

5158
AUTH_TOPIC_KEYS = ("auth", "search_auth", "download_auth")
@@ -237,15 +244,18 @@ def __post_init__(self):
237244
self._config = ProviderConfig.from_mapping(self._config)
238245

239246
elif not isinstance(self._config, ProviderConfig):
240-
raise TypeError(
247+
msg = (
241248
f"Unsupported config type: {type(self._config)}. "
242249
"Expected ProviderConfig or dict."
243250
)
251+
raise ValidationError(msg)
244252

245253
self.name = self.name or self._config.name
246254

247255
if self.name is None:
248-
raise ValueError("Provider name could not be determined from the config.")
256+
raise ValidationError(
257+
"Provider name could not be determined from the config."
258+
)
249259

250260
def __str__(self) -> str:
251261
"""Return the provider's name as string."""
@@ -385,14 +395,13 @@ def delete_product_type(self, name: str) -> None:
385395
386396
:param name: The product type name.
387397
388-
:raises KeyError: If the product type is not found.
398+
:raises UnsupportedProductType: If the product type is not found.
389399
"""
390400
try:
391401
del self.product_types[name]
392402
except KeyError:
393-
raise KeyError(
394-
f"Product type '{name}' not found in provider '{self.name}'."
395-
)
403+
msg = f"Product type '{name}' not found in provider '{self.name}'."
404+
raise UnsupportedProductType(msg)
396405

397406
def sync_product_types(
398407
self,
@@ -511,18 +520,20 @@ def __setitem__(self, key: str, value: Provider) -> None:
511520
:raises ValueError: If the provider key already exists.
512521
"""
513522
if key in self.data:
514-
raise ValueError(f"Provider '{key}' already exists.")
523+
msg = f"Provider '{key}' already exists."
524+
raise ValueError(msg)
515525
super().__setitem__(key, value)
516526

517527
def __delitem__(self, key: str) -> None:
518528
"""
519529
Delete a provider by name.
520530
521531
:param key: The name of the provider to delete.
522-
:raises KeyError: If the provider key is not found.
532+
:raises UnsupportedProvider: If the provider key is not found.
523533
"""
524534
if key not in self.data:
525-
raise KeyError(f"Provider '{key}' not found.")
535+
msg = f"Provider '{key}' not found."
536+
raise UnsupportedProvider(msg)
526537
super().__delitem__(key)
527538

528539
def __or__(self, other: Self) -> Self:
@@ -642,23 +653,23 @@ def filter_by_name(self, name: Optional[str] = None) -> Self:
642653
{n: p for n, p in self.data.items() if name in [p.name, p.group]}
643654
)
644655

645-
def delete_product_type(self, provider: str, product_ID: str) -> None:
656+
def delete_product_type(self, provider: str, product_type: str) -> None:
646657
"""
647658
Delete a product type from a provider.
648659
649660
:param provider: The provider's name.
650661
:param product_ID: The product type to delete.
651-
:raises KeyError: If the provider or product is not found.
662+
:raises UnsupportedProvider: If the provider or product is not found.
652663
"""
653664
if provider_obj := self.get(provider):
654-
if product_ID in provider_obj.products:
655-
provider_obj.delete_product_type(product_ID)
665+
if product_type in provider_obj.product_types:
666+
provider_obj.delete_product_type(product_type)
656667
else:
657-
raise KeyError(
658-
f"Product '{product_ID}' not found for Provider '{provider}'."
659-
)
668+
msg = f"Product type '{product_type}' not found for provider '{provider}'."
669+
raise UnsupportedProductType(msg)
660670
else:
661-
raise KeyError(f"Provider '{provider}' not found.")
671+
msg = f"Provider '{provider}' not found."
672+
raise UnsupportedProvider(msg)
662673

663674
def _share_credentials(self) -> None:
664675
"""

0 commit comments

Comments
 (0)