beancount_types: Add types related to loading the books.

These will help support loading methods in the books module.
This commit is contained in:
Brett Smith 2020-04-21 11:57:42 -04:00
parent 855c1c2bf0
commit 43a2e1bec8
2 changed files with 24 additions and 4 deletions

View file

@ -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]

View file

@ -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]