diff --git a/craft_application/errors.py b/craft_application/errors.py index ea2c6db8..e4ea0c0d 100644 --- a/craft_application/errors.py +++ b/craft_application/errors.py @@ -19,7 +19,7 @@ """ from __future__ import annotations -from typing import TYPE_CHECKING, TypeVar +from typing import TYPE_CHECKING, Self, TypeVar from craft_cli import CraftError @@ -76,7 +76,7 @@ def from_pydantic( *, file_name: str = "yaml file", **kwargs: str | bool | int, - ) -> _ErrType: + ) -> Self: """Convert this error from a pydantic ValidationError. :param error: The pydantic error to convert @@ -91,12 +91,12 @@ class PartsLifecycleError(CraftError): """Error during parts processing.""" @classmethod - def from_parts_error(cls: type[_ErrType], err: craft_parts.PartsError) -> _ErrType: + def from_parts_error(cls: type[_ErrType], err: craft_parts.PartsError) -> Self: """Shortcut to create a PartsLifecycleError from a PartsError.""" return cls(message=err.brief, details=err.details, resolution=err.resolution) @classmethod - def from_os_error(cls: type[_ErrType], err: OSError) -> _ErrType: + def from_os_error(cls: type[_ErrType], err: OSError) -> Self: """Create a PartsLifecycleError from an OSError.""" message = f"{err.filename}: {err.strerror}" if err.filename else err.strerror details = err.__class__.__name__ diff --git a/craft_application/models/base.py b/craft_application/models/base.py index f8dbe230..1711cb7f 100644 --- a/craft_application/models/base.py +++ b/craft_application/models/base.py @@ -16,7 +16,7 @@ """Base pydantic model for *craft applications.""" from __future__ import annotations -from typing import TYPE_CHECKING, Any, TypeVar +from typing import TYPE_CHECKING, Any, Self, TypeVar import pydantic import yaml @@ -54,7 +54,7 @@ def marshal(self) -> dict[str, str | list[str] | dict[str, Any]]: return self.dict(by_alias=True, exclude_unset=True) @classmethod - def unmarshal(cls: type[_ModelType], data: dict[str, Any] | Any) -> _ModelType: + def unmarshal(cls: type[_ModelType], data: dict[str, Any]) -> Self: """Create and populate a new model object from dictionary data. The unmarshal method validates entries in the input dictionary, populating @@ -69,7 +69,7 @@ def unmarshal(cls: type[_ModelType], data: dict[str, Any] | Any) -> _ModelType: return cls(**data) @classmethod - def from_yaml_file(cls: type[_ModelType], path: pathlib.Path) -> _ModelType: + def from_yaml_file(cls: type[_ModelType], path: pathlib.Path) -> Self: """Instantiate this model from a YAML file.""" with path.open() as file: data = safe_yaml_load(file) diff --git a/craft_application/util/error_formatting.py b/craft_application/util/error_formatting.py index 6af6fa48..49866823 100644 --- a/craft_application/util/error_formatting.py +++ b/craft_application/util/error_formatting.py @@ -101,8 +101,7 @@ def _format_pydantic_error_location(loc: Iterable[Union[str, int]]) -> str: loc = ".".join(loc_parts) # Filter out internal __root__ detail. - loc = loc.replace(".__root__", "") - return loc + return loc.replace(".__root__", "") def _format_pydantic_error_message(msg: str) -> str: