From 43a2e1bec828fd1969312cd22b8cb1219ce8d9ab 2020-04-21 15:57:54 From: Brett Smith Date: 2020-04-21 15:57:54 Subject: [PATCH] beancount_types: Add types related to loading the books. These will help support loading methods in the books module. --- diff --git a/conservancy_beancount/beancount_types.py b/conservancy_beancount/beancount_types.py index 56956ec200721dfd57356aeacc2c8a420d564162..98db52fe7213ae6109938b73bb90c51471877c48 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 3d6f283bb37ee18c7aa12009b79cebd927f0dbca..97578895ced3ab930b633df1b1ccec707a567616 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]