diff --git a/conservancy_beancount/beancount_types.py b/conservancy_beancount/beancount_types.py index 56956ec..98db52f 100644 --- a/conservancy_beancount/beancount_types.py +++ b/conservancy_beancount/beancount_types.py @@ -20,17 +20,23 @@ import datetime import beancount.core.data as bc_data from typing import ( + TYPE_CHECKING, Any, FrozenSet, Iterable, List, + Mapping, NamedTuple, Optional, Set, Tuple, Type, + Union, ) +if TYPE_CHECKING: + from . import errors as errormod + Account = bc_data.Account MetaKey = str MetaValue = Any @@ -42,6 +48,12 @@ class Directive(NamedTuple): date: datetime.date +class Error(NamedTuple): + source: Optional[bc_data.Meta] + message: str + entry: Optional[Directive] + + class Transaction(Directive): flag: bc_data.Flag payee: Optional[str] @@ -54,3 +66,8 @@ class Transaction(Directive): ALL_DIRECTIVES: FrozenSet[Type[Directive]] = frozenset([ Transaction, ]) + +Entries = List[Directive] +Errors = List[Union[Error, 'errormod.Error']] +OptionsMap = Mapping[str, Any] +LoadResult = Tuple[Entries, Errors, OptionsMap] diff --git a/conservancy_beancount/plugin/__init__.py b/conservancy_beancount/plugin/__init__.py index 3d6f283..9757889 100644 --- a/conservancy_beancount/plugin/__init__.py +++ b/conservancy_beancount/plugin/__init__.py @@ -32,6 +32,9 @@ from typing import ( from ..beancount_types import ( ALL_DIRECTIVES, Directive, + Entries, + Errors, + OptionsMap, ) from .. import config as configmod from .core import ( @@ -117,15 +120,15 @@ class HookRegistry: def run( - entries: List[Directive], - options_map: Dict[str, Any], + entries: Entries, + options_map: OptionsMap, config: str='', hook_registry: Optional[HookRegistry]=None, -) -> Tuple[List[Directive], List[Error]]: +) -> Tuple[Entries, Errors]: if hook_registry is None: hook_registry = HookRegistry() hook_registry.load_included_hooks() - errors: List[Error] = [] + errors: Errors = [] hooks: Dict[HookName, List[Hook]] = { # mypy thinks NamedTuples don't have __name__ but they do at runtime. t.__name__: [] for t in bc_data.ALL_DIRECTIVES # type:ignore[attr-defined]