Skip to content

accounts

Generic T-account types and account classifications for double-entry bookkeeping.

Classes:

  • AccountBalances

    A dictionary-like class to hold account balances.

  • AccountNormalBalance

    The normal balance of an account or the preferred type of net balance that it should have.

  • AccountType

    Types of accounts in the accounting equation.

  • CompositeTAccount

    Composite T-account that can hold multiple sub T-accounts.

  • TAccount

    A T-account in double-entry bookkeeping.

AccountBalances

Bases: UserDict['TAccount', float]

A dictionary-like class to hold account balances.

Methods:

  • from_accounts

    Create an AccountBalances instance from a list of accounts.

from_accounts classmethod

from_accounts(accounts: list[TAccount]) -> AccountBalances

Create an AccountBalances instance from a list of accounts.

AccountNormalBalance

Bases: Enum

The normal balance of an account or the preferred type of net balance that it should have.

In double-entry bookkeeping, every account has a "normal" side:

  • DEBIT_NORMAL: Asset and Expense accounts normally carry debit balances. Debits increase these accounts; credits decrease them.
  • CREDIT_NORMAL: Liability, Equity, and Income accounts normally carry credit balances. Credits increase these accounts; debits decrease them.

AccountType

Bases: Enum

Types of accounts in the accounting equation.

The five fundamental account types:

  • Asset -- resources owned (debit-normal)
  • Liability -- obligations owed (credit-normal)
  • Equity -- residual interest (credit-normal)
  • Income -- revenue earned (credit-normal, temporary)
  • Expense -- costs incurred (debit-normal, temporary)

Accounting equation: Assets = Liabilities + Equity

Methods:

get_normal_balance staticmethod

get_normal_balance(
    account_type: AccountType,
    *,
    contra_account: bool = False,
) -> AccountNormalBalance

Return the normal balance for a given account type.

A contra account has the opposite normal balance of its parent account type. For example, a contra-asset account has a credit-normal balance.

CompositeTAccount

CompositeTAccount(
    name: str,
    account_type: AccountType,
    contra_accounts: list[TAccount] | None = None,
    parent: TAccount | None = None,
    debit: float = 0.0,
    credit: float = 0.0,
    *,
    is_contra_account: bool = False,
)

Bases: TAccount

Composite T-account that can hold multiple sub T-accounts.

A composite account aggregates the balances of its children. Debiting or crediting a child automatically recalculates the parent's totals.

Example -- Investment Securities (composite)::

Investment Securities  [composite, debit-normal]
  +-- Investment HTM       debit: 5,000
  +-- Investment FVOCI     debit: 3,000
  = total debit: 8,000

Methods:

  • add_sub_account

    Add a child account to this composite.

  • balance

    Return the net balance of this account.

  • credit

    Add credit amount to account.

  • debit

    Add debit amount to account.

  • has_contra_account

    Check if this account has any contra accounts attached.

  • posting_accounts

    Recursively yield the lowest-level accounts that accept direct postings.

  • remove_sub_account

    Remove a child account from this composite.

Attributes:

credit_value property writable

credit_value: float

Return the credit value of the account.

debit_value property writable

debit_value: float

Return the debit value of the account.

parent property writable

parent: TAccount | None

Get the parent account.

sub_accounts property

sub_accounts: Generator[TAccount, None, None]

Yield direct child accounts.

add_sub_account

add_sub_account(account: TAccount) -> None

Add a child account to this composite.

The child's parent is set to this account, and balances are recalculated.

balance

balance() -> float

Return the net balance of this account.

Debit-normal accounts: balance = debits - credits. Credit-normal accounts: balance = credits - debits.

credit

credit(amount: float) -> None

Add credit amount to account.

debit

debit(amount: float) -> None

Add debit amount to account.

has_contra_account

has_contra_account() -> bool

Check if this account has any contra accounts attached.

posting_accounts

posting_accounts() -> Generator[TAccount, None, None]

Recursively yield the lowest-level accounts that accept direct postings.

If this composite has no children, yields itself. Otherwise, recurses into each child's posting_accounts.

remove_sub_account

remove_sub_account(account: TAccount) -> None

Remove a child account from this composite.

TAccount

TAccount(
    name: str,
    account_type: AccountType,
    contra_accounts: list[TAccount] | None = None,
    parent: TAccount | None = None,
    debit: float = 0.0,
    credit: float = 0.0,
    description: str = "",
    *,
    is_contra_account: bool = False,
    is_temporary_account: bool = False,
)

A T-account in double-entry bookkeeping.

A T-account has two sides: debit (left) and credit (right). The normal balance determines which side increases the account value.

Asset and Expense accounts have DEBIT normal balance::

+----------------------------------+
|         Cash (Asset)             |
+-----------------+----------------+
|     Debit       |    Credit      |
|   (increase)    |   (decrease)   |
+-----------------+----------------+
|    1,000        |      200       |
|      500        |                |
+-----------------+----------------+
|   Balance: 1,300                 |
+----------------------------------+

Liability, Equity, and Income accounts have CREDIT normal balance::

+----------------------------------+
|       Deposits (Liability)       |
+-----------------+----------------+
|     Debit       |    Credit      |
|   (decrease)    |   (increase)   |
+-----------------+----------------+
|                 |    5,000       |
|      200        |    1,000       |
+-----------------+----------------+
|              Balance: 5,800      |
+----------------------------------+

Double-entry principle: every transaction debits one account and credits another for the same amount, keeping the books balanced.

Example: Receiving a $10,000 deposit::

Cash.debit(10_000)      -- Cash increases
Deposits.credit(10_000) -- Deposits increases

Methods:

  • balance

    Return the net balance of this account.

  • credit

    Add credit amount to account.

  • debit

    Add debit amount to account.

  • has_contra_account

    Check if this account has any contra accounts attached.

  • posting_accounts

    Yield all accounts that can directly receive debit/credit postings.

Attributes:

  • credit_value (float) –

    Return the credit value of the account.

  • debit_value (float) –

    Return the debit value of the account.

  • parent (TAccount | None) –

    Get the parent account.

  • sub_accounts (Generator[TAccount, None, None]) –

    Yield direct child accounts. Empty for simple accounts.

credit_value property writable

credit_value: float

Return the credit value of the account.

debit_value property writable

debit_value: float

Return the debit value of the account.

parent property writable

parent: TAccount | None

Get the parent account.

sub_accounts property

sub_accounts: Generator[TAccount, None, None]

Yield direct child accounts. Empty for simple accounts.

balance

balance() -> float

Return the net balance of this account.

Debit-normal accounts: balance = debits - credits. Credit-normal accounts: balance = credits - debits.

credit

credit(amount: float) -> None

Add credit amount to account.

debit

debit(amount: float) -> None

Add debit amount to account.

has_contra_account

has_contra_account() -> bool

Check if this account has any contra accounts attached.

posting_accounts

posting_accounts() -> Generator[TAccount, None, None]

Yield all accounts that can directly receive debit/credit postings.

For a simple account, yields itself. For a composite account, recursively yields the lowest-level accounts in the hierarchy that are not themselves composites with children.

The ledger uses this when building closing entries to ensure postings go to accounts that accept direct value changes.