Skip to content

cointoss.instrument

instrument

Instrument identity: minting Instrument Ids and establishing identity by External Reference.

Implements ADR-0004. Pure: no network, no clock, no database. Observations carry their own observation date and their own FIGI Resolution outcome, so every rule here is testable without a resolver. The module cannot compel a FIGI attempt; ADR-0004's "resolution attempt is mandatory at creation" is an ingest-orchestration invariant enforced where Observations are constructed.

Chain vocabulary (permanent, baked into Instrument Ids): the scope of an on-chain token is the short symbolic form of its chain -- eth, not ethereum and not the numeric chain id 1. Other chains follow the same convention (sol, bsc, avax); native is reserved for a chain's own coin, so BTC is crypto.native.btc. The vocabulary is Chain below and is deliberately small: a new chain is a code change, because an Instrument Id minted under a name can never be reminted under another.

Instrument Id grammar: {type}.{scope}.{symbol}[_{qualifier}]. Symbols are normalised to alphanumerics separated by -, so the first _ in the third part always starts the Qualifier.

Source attribution (the ADR-0004 amendment): a symbol is one source's lease on a name, so every Ticker History record names the source that reported it and every symbol lookup takes one. An Instrument states the Identity Source its displayed name and symbol come from and the ordered Price Sources its Bars should come from, both defaulted from its type when it is minted. A stable provider slug such as a CoinGecko id is an External Reference instead -- it is identity and never changes, whereas a ticker is a lease.

Note for the next reader: stock.us.fb denotes Meta permanently, because the id is minted from the symbol first seen and never changed. This reads backwards and is the deliberate price of immutable readable ids -- see ADR-0004.

InstrumentError

Bases: Exception

Base class for identity errors.

Source code in src/cointoss/instrument.py
class InstrumentError(Exception):
    """Base class for identity errors."""

UnknownScope

Bases: InstrumentError

Scope is not a known chain or a plausible ISO country.

Source code in src/cointoss/instrument.py
class UnknownScope(InstrumentError):
    """Scope is not a known chain or a plausible ISO country."""

AmbiguousSymbol

Bases: InstrumentError

More than one Instrument traded under a symbol on the given date.

Source code in src/cointoss/instrument.py
class AmbiguousSymbol(InstrumentError):
    """More than one Instrument traded under a symbol on the given date."""

SupersessionCycle

Bases: InstrumentError

A Supersession chain does not terminate. Always a defect.

Source code in src/cointoss/instrument.py
class SupersessionCycle(InstrumentError):
    """A Supersession chain does not terminate. Always a defect."""

Chain

Bases: StrEnum

Controlled vocabulary for crypto Scope. Short symbolic chain names, plus native.

Source code in src/cointoss/instrument.py
class Chain(StrEnum):
    """Controlled vocabulary for crypto Scope. Short symbolic chain names, plus `native`."""

    NATIVE = "native"
    ETH = "eth"
    BTC = "btc"
    SOL = "sol"
    BSC = "bsc"
    AVAX = "avax"
    MATIC = "matic"
    TRX = "trx"
    ARB = "arb"
    OP = "op"
    BASE = "base"

Source

Bases: StrEnum

Controlled vocabulary for the sources cointoss ingests from.

Deliberately small and closed, like Chain: a source is named in stored Ticker History, in an Instrument's Identity Source and Price Sources, and later on every Bar, so adding one is a code change rather than a free-text value drifting into the data.

Source code in src/cointoss/instrument.py
class Source(StrEnum):
    """Controlled vocabulary for the sources cointoss ingests from.

    Deliberately small and closed, like `Chain`: a source is named in stored Ticker History,
    in an Instrument's Identity Source and Price Sources, and later on every Bar, so adding
    one is a code change rather than a free-text value drifting into the data.
    """

    YAHOO = "yahoo"
    COINGECKO = "coingecko"

    @property
    def session_offset(self) -> int:
        """Days by which the date this source stamps on a Bar leads the session it closes.

        >>> Source.YAHOO.session_offset, Source.COINGECKO.session_offset
        (0, 1)
        """
        return _SESSION_OFFSET[self]

    def session_of(self, stamped: date) -> date:
        """The session closed by this source's Bar stamped `stamped`.

        >>> Source.COINGECKO.session_of(date(2024, 3, 1))
        datetime.date(2024, 2, 29)
        """
        return stamped - timedelta(days=self.session_offset)

session_offset property

Days by which the date this source stamps on a Bar leads the session it closes.

>>> Source.YAHOO.session_offset, Source.COINGECKO.session_offset
(0, 1)

session_of(stamped)

The session closed by this source's Bar stamped stamped.

>>> Source.COINGECKO.session_of(date(2024, 3, 1))
datetime.date(2024, 2, 29)
Source code in src/cointoss/instrument.py
def session_of(self, stamped: date) -> date:
    """The session closed by this source's Bar stamped `stamped`.

    >>> Source.COINGECKO.session_of(date(2024, 3, 1))
    datetime.date(2024, 2, 29)
    """
    return stamped - timedelta(days=self.session_offset)

FigiResolution

Bases: StrEnum

Outcome of attempting to anchor an Instrument to a FIGI.

NOT_FOUND is a coverage gap; NOT_ATTEMPTED is a missing ingestion step. Both are retryable and both appear in the unresolved work queue.

Source code in src/cointoss/instrument.py
class FigiResolution(StrEnum):
    """Outcome of attempting to anchor an Instrument to a FIGI.

    `NOT_FOUND` is a coverage gap; `NOT_ATTEMPTED` is a missing ingestion step. Both are
    retryable and both appear in the unresolved work queue.
    """

    RESOLVED = "resolved"
    NOT_FOUND = "not_found"
    NOT_ATTEMPTED = "not_attempted"

ReferenceKind

Bases: StrEnum

Kinds of External Reference. SHARE_CLASS_FIGI is stored but never used for matching.

Source code in src/cointoss/instrument.py
class ReferenceKind(StrEnum):
    """Kinds of External Reference. `SHARE_CLASS_FIGI` is stored but never used for matching."""

    COMPOSITE_FIGI = "composite_figi"
    ASSET_FIGI = "asset_figi"
    SHARE_CLASS_FIGI = "share_class_figi"
    CONTRACT = "contract"
    PROVIDER_ID = "provider_id"

Outcome

Bases: StrEnum

What an Observation did to the collection.

Source code in src/cointoss/instrument.py
class Outcome(StrEnum):
    """What an Observation did to the collection."""

    MINTED = "minted"
    MATCHED = "matched"
    SUPERSEDED = "superseded"
    FLAGGED = "flagged"

ScopeKind

Bases: StrEnum

Which authority a Scope belongs to.

Source code in src/cointoss/instrument.py
class ScopeKind(StrEnum):
    """Which authority a Scope belongs to."""

    CHAIN = "chain"
    COUNTRY = "country"

Scope

Bases: str

A validated Scope: the authority under which a symbol is unique.

Construction is validation, so a Scope cannot hold an unchecked value and there is no way to reach one that was never checked. It is a str, so its stored and printed form is the scope itself.

>>> Scope(InstrumentType.STOCK, "US")
Scope('us')
>>> Scope(InstrumentType.CRYPTO, "eth").kind
<ScopeKind.CHAIN: 'chain'>
>>> Scope(InstrumentType.CRYPTO, "ethereum")
Traceback (most recent call last):
    ...
cointoss.instrument.UnknownScope: 'ethereum' is not in the chain vocabulary...
Source code in src/cointoss/instrument.py
class Scope(str):
    """A validated Scope: the authority under which a symbol is unique.

    Construction is validation, so a `Scope` cannot hold an unchecked value and there is no
    way to reach one that was never checked. It is a `str`, so its stored and printed form is
    the scope itself.

    >>> Scope(InstrumentType.STOCK, "US")
    Scope('us')
    >>> Scope(InstrumentType.CRYPTO, "eth").kind
    <ScopeKind.CHAIN: 'chain'>
    >>> Scope(InstrumentType.CRYPTO, "ethereum")
    Traceback (most recent call last):
        ...
    cointoss.instrument.UnknownScope: 'ethereum' is not in the chain vocabulary...
    """

    kind: ScopeKind

    def __new__(cls, instrument_type: InstrumentType, value: str) -> Scope:
        normalized = value.strip().lower()
        if instrument_type is InstrumentType.CRYPTO:
            if normalized not in set(Chain):
                raise UnknownScope(
                    f"{normalized!r} is not in the chain vocabulary: {sorted(set(Chain))}"
                )
            kind = ScopeKind.CHAIN
        else:
            if not re.fullmatch(r"[a-z]{2}", normalized):
                raise UnknownScope(f"{normalized!r} is not an ISO 3166-1 alpha-2 country")
            kind = ScopeKind.COUNTRY
        scope = super().__new__(cls, normalized)
        scope.kind = kind
        return scope

    def __reduce__(self) -> tuple[Any, tuple[str, ScopeKind]]:
        """Rebuild from the validated value and its kind.

        The default `str` reduction would call `__new__` with one argument, so a `Scope` would
        be uncopyable and unpicklable. Reconstruction skips revalidation because a `Scope` that
        exists was validated when it was made.
        """
        return (_rebuild_scope, (str.__str__(self), self.kind))

    def __repr__(self) -> str:
        return f"Scope({str.__str__(self)!r})"

__reduce__()

Rebuild from the validated value and its kind.

The default str reduction would call __new__ with one argument, so a Scope would be uncopyable and unpicklable. Reconstruction skips revalidation because a Scope that exists was validated when it was made.

Source code in src/cointoss/instrument.py
def __reduce__(self) -> tuple[Any, tuple[str, ScopeKind]]:
    """Rebuild from the validated value and its kind.

    The default `str` reduction would call `__new__` with one argument, so a `Scope` would
    be uncopyable and unpicklable. Reconstruction skips revalidation because a `Scope` that
    exists was validated when it was made.
    """
    return (_rebuild_scope, (str.__str__(self), self.kind))

InstrumentId

Bases: str

An Instrument Id: {type}.{scope}.{symbol}[_{qualifier}], minted once and immutable.

A str subclass, so the readable slug ADR-0004 chose is exactly what is stored, printed, and used as a key -- but a distinct type, so a symbol or a name cannot be passed where an id belongs. Construction parses and validates; there is no partial value.

>>> InstrumentId("stock.us.fb").symbol
'FB'
>>> InstrumentId("stock.us.fb_proshares").qualifier
'proshares'
>>> InstrumentId.mint(InstrumentType.CRYPTO, Scope(InstrumentType.CRYPTO, "eth"), "USDC")
InstrumentId('crypto.eth.usdc')
>>> InstrumentId("AAPL")
Traceback (most recent call last):
    ...
ValueError: 'AAPL' is not an Instrument Id...
Source code in src/cointoss/instrument.py
class InstrumentId(str):
    """An Instrument Id: `{type}.{scope}.{symbol}[_{qualifier}]`, minted once and immutable.

    A `str` subclass, so the readable slug ADR-0004 chose is exactly what is stored, printed,
    and used as a key -- but a distinct type, so a symbol or a name cannot be passed where an
    id belongs. Construction parses and validates; there is no partial value.

    >>> InstrumentId("stock.us.fb").symbol
    'FB'
    >>> InstrumentId("stock.us.fb_proshares").qualifier
    'proshares'
    >>> InstrumentId.mint(InstrumentType.CRYPTO, Scope(InstrumentType.CRYPTO, "eth"), "USDC")
    InstrumentId('crypto.eth.usdc')
    >>> InstrumentId("AAPL")
    Traceback (most recent call last):
        ...
    ValueError: 'AAPL' is not an Instrument Id...
    """

    type: InstrumentType
    scope: Scope
    symbol: str
    qualifier: str | None

    def __new__(cls, text: str) -> InstrumentId:
        parts = text.split(".")
        if len(parts) != 3:
            raise ValueError(f"{text!r} is not an Instrument Id: expected type.scope.symbol")
        raw_type, raw_scope, tail = parts
        try:
            instrument_type = InstrumentType(raw_type)
        except ValueError:
            raise ValueError(
                f"{text!r} is not an Instrument Id: unknown type {raw_type!r}"
            ) from None
        # A normalised symbol never contains "_", so the first one always starts the Qualifier.
        symbol, _, qualifier = tail.partition("_")
        if not symbol:
            raise ValueError(f"{text!r} is not an Instrument Id: empty symbol")
        instrument_id = super().__new__(cls, text)
        instrument_id.type = instrument_type
        instrument_id.scope = Scope(instrument_type, raw_scope)
        instrument_id.symbol = symbol.upper()
        instrument_id.qualifier = qualifier or None
        return instrument_id

    @classmethod
    def mint(
        cls,
        instrument_type: InstrumentType,
        scope: Scope,
        symbol: str,
        qualifier: str | None = None,
    ) -> InstrumentId:
        """Compose an id from its parts. The only way to build one that has never existed."""
        base = f"{instrument_type}.{scope}.{symbol.lower()}"
        return cls(f"{base}_{qualifier}" if qualifier else base)

    @classmethod
    def __get_pydantic_core_schema__(cls, source: Any, handler: Any) -> Any:
        """Let Pydantic carry an `InstrumentId` without it degrading to `str`.

        Without this a model field annotated `InstrumentId` fails at class construction,
        because the custom one-argument `__new__` is not a schema Pydantic can infer. Values
        are validated by construction on the way in and serialised as the slug on the way out.
        """
        from pydantic_core import core_schema

        return core_schema.no_info_plain_validator_function(
            _coerce_instrument_id,
            serialization=core_schema.plain_serializer_function_ser_schema(
                str.__str__, return_schema=core_schema.str_schema()
            ),
        )

    def __reduce__(self) -> tuple[Any, tuple[str]]:
        """Rebuild by reparsing the slug, which is the whole of the value."""
        return (InstrumentId, (str.__str__(self),))

    def __repr__(self) -> str:
        return f"InstrumentId({str.__str__(self)!r})"

mint(instrument_type, scope, symbol, qualifier=None) classmethod

Compose an id from its parts. The only way to build one that has never existed.

Source code in src/cointoss/instrument.py
@classmethod
def mint(
    cls,
    instrument_type: InstrumentType,
    scope: Scope,
    symbol: str,
    qualifier: str | None = None,
) -> InstrumentId:
    """Compose an id from its parts. The only way to build one that has never existed."""
    base = f"{instrument_type}.{scope}.{symbol.lower()}"
    return cls(f"{base}_{qualifier}" if qualifier else base)

__get_pydantic_core_schema__(source, handler) classmethod

Let Pydantic carry an InstrumentId without it degrading to str.

Without this a model field annotated InstrumentId fails at class construction, because the custom one-argument __new__ is not a schema Pydantic can infer. Values are validated by construction on the way in and serialised as the slug on the way out.

Source code in src/cointoss/instrument.py
@classmethod
def __get_pydantic_core_schema__(cls, source: Any, handler: Any) -> Any:
    """Let Pydantic carry an `InstrumentId` without it degrading to `str`.

    Without this a model field annotated `InstrumentId` fails at class construction,
    because the custom one-argument `__new__` is not a schema Pydantic can infer. Values
    are validated by construction on the way in and serialised as the slug on the way out.
    """
    from pydantic_core import core_schema

    return core_schema.no_info_plain_validator_function(
        _coerce_instrument_id,
        serialization=core_schema.plain_serializer_function_ser_schema(
            str.__str__, return_schema=core_schema.str_schema()
        ),
    )

__reduce__()

Rebuild by reparsing the slug, which is the whole of the value.

Source code in src/cointoss/instrument.py
def __reduce__(self) -> tuple[Any, tuple[str]]:
    """Rebuild by reparsing the slug, which is the whole of the value."""
    return (InstrumentId, (str.__str__(self),))

ExternalReference dataclass

An identifier issued by an outside authority.

qualifier namespaces the value: the provider for a provider id, the chain for a contract address, empty for a FIGI, which is globally unique.

Source code in src/cointoss/instrument.py
@dataclass(frozen=True, order=True)
class ExternalReference:
    """An identifier issued by an outside authority.

    `qualifier` namespaces the value: the provider for a provider id, the chain for a contract
    address, empty for a FIGI, which is globally unique.
    """

    kind: ReferenceKind
    value: str
    qualifier: str = ""

    @classmethod
    def composite_figi(cls, figi: str) -> ExternalReference:
        return cls(ReferenceKind.COMPOSITE_FIGI, figi.strip().upper())

    @classmethod
    def asset_figi(cls, figi: str) -> ExternalReference:
        return cls(ReferenceKind.ASSET_FIGI, figi.strip().upper())

    @classmethod
    def share_class_figi(cls, figi: str) -> ExternalReference:
        return cls(ReferenceKind.SHARE_CLASS_FIGI, figi.strip().upper())

    @classmethod
    def contract(cls, chain: str, address: str) -> ExternalReference:
        """Chain and contract address. Addresses are compared case-insensitively.

        >>> ExternalReference.contract("eth", "0xA0B8").value
        '0xa0b8'
        """
        return cls(ReferenceKind.CONTRACT, address.strip().lower(), chain.strip().lower())

    @classmethod
    def provider_id(cls, provider: str, value: str) -> ExternalReference:
        return cls(ReferenceKind.PROVIDER_ID, value.strip(), provider.strip().lower())

    @property
    def is_anchor_figi(self) -> bool:
        """Whether this reference is the FIGI an Instrument of its type anchors on."""
        return self.kind in (ReferenceKind.COMPOSITE_FIGI, ReferenceKind.ASSET_FIGI)

    def group(self) -> tuple[int, str, str] | None:
        """Priority tier and comparison key, or None when the reference never matches."""
        tier = _MATCH_GROUPS.get(self.kind)
        return None if tier is None else (tier[0], tier[1], self.qualifier)

is_anchor_figi property

Whether this reference is the FIGI an Instrument of its type anchors on.

contract(chain, address) classmethod

Chain and contract address. Addresses are compared case-insensitively.

>>> ExternalReference.contract("eth", "0xA0B8").value
'0xa0b8'
Source code in src/cointoss/instrument.py
@classmethod
def contract(cls, chain: str, address: str) -> ExternalReference:
    """Chain and contract address. Addresses are compared case-insensitively.

    >>> ExternalReference.contract("eth", "0xA0B8").value
    '0xa0b8'
    """
    return cls(ReferenceKind.CONTRACT, address.strip().lower(), chain.strip().lower())

group()

Priority tier and comparison key, or None when the reference never matches.

Source code in src/cointoss/instrument.py
def group(self) -> tuple[int, str, str] | None:
    """Priority tier and comparison key, or None when the reference never matches."""
    tier = _MATCH_GROUPS.get(self.kind)
    return None if tier is None else (tier[0], tier[1], self.qualifier)

TickerRecord dataclass

One time-bounded stretch of Ticker History, as reported by one source.

valid_to None means current. Records are not vendor-neutral: Yahoo's BRK-B and another source's BRK.B are two current records for one Instrument, which is why source is part of the record and of every lookup against it.

Source code in src/cointoss/instrument.py
@dataclass(frozen=True)
class TickerRecord:
    """One time-bounded stretch of Ticker History, as reported by one source.

    `valid_to` None means current. Records are not vendor-neutral: Yahoo's `BRK-B` and another
    source's `BRK.B` are two current records for one Instrument, which is why `source` is part
    of the record and of every lookup against it.
    """

    source: Source
    symbol: str
    scope: str
    valid_from: date
    valid_to: date | None = None

    def covers(self, as_of: date) -> bool:
        return self.valid_from <= as_of and (self.valid_to is None or as_of < self.valid_to)

Observation dataclass

A normalised sighting of an asset from some source.

source is required: a sighting with no stated source cannot be recorded as Ticker History, since the symbol it carries is only that source's name for the asset.

figi_resolution is the outcome the caller obtained, not something this module can compel. An anchor FIGI reference and a RESOLVED state must agree.

Source code in src/cointoss/instrument.py
@dataclass(frozen=True)
class Observation:
    """A normalised sighting of an asset from some source.

    `source` is required: a sighting with no stated source cannot be recorded as Ticker
    History, since the symbol it carries is only that source's name for the asset.

    `figi_resolution` is the outcome the caller obtained, not something this module can compel.
    An anchor FIGI reference and a `RESOLVED` state must agree.
    """

    type: InstrumentType
    symbol: str
    scope: str
    observed_at: date
    source: Source
    name: str | None = None
    issuer_name: str | None = None
    references: tuple[ExternalReference, ...] = ()
    figi_resolution: FigiResolution = FigiResolution.NOT_ATTEMPTED

    @property
    def is_resolved(self) -> bool:
        return self.figi_resolution is FigiResolution.RESOLVED

    def __post_init__(self) -> None:
        has_anchor = any(r.is_anchor_figi for r in self.references)
        if has_anchor != self.is_resolved:
            raise ValueError(
                "figi_resolution RESOLVED and an anchor FIGI reference must accompany each other"
            )
        if not normalize_symbol(self.symbol):
            raise ValueError("observation symbol is empty after normalisation")

Instrument dataclass

The canonical, stable identity for a tradable asset.

symbol and name are the denormalised display values read from identity_source; they are that source's account of the asset, not facts. price_sources is an ordered preference, first holder of a Bar for a date wins.

Source code in src/cointoss/instrument.py
@dataclass
class Instrument:
    """The canonical, stable identity for a tradable asset.

    `symbol` and `name` are the denormalised display values read from `identity_source`; they
    are that source's account of the asset, not facts. `price_sources` is an ordered
    preference, first holder of a Bar for a date wins.
    """

    id: InstrumentId
    type: InstrumentType
    scope: Scope
    symbol: str
    mint_seq: int
    minted_at: date
    identity_source: Source
    price_sources: tuple[Source, ...]
    name: str | None = None
    issuer_name: str | None = None
    references: list[ExternalReference] = field(default_factory=list)
    figi_resolution: FigiResolution = FigiResolution.NOT_ATTEMPTED
    superseded_by: InstrumentId | None = None
    ticker_history: list[TickerRecord] = field(default_factory=list)

    def __post_init__(self) -> None:
        if not self.price_sources:
            raise ValueError(f"{self.id} has no Price Sources: an empty preference names nothing")
        if len(set(self.price_sources)) != len(self.price_sources):
            raise ValueError(f"{self.id} repeats a Price Source: {self.price_sources}")

    @property
    def is_resolved(self) -> bool:
        return self.figi_resolution is FigiResolution.RESOLVED

    @property
    def is_unresolved(self) -> bool:
        return not self.is_resolved

    def ticker_history_from(self, source: Source) -> list[TickerRecord]:
        """This Instrument's Ticker History as reported by one source, oldest first."""
        return [r for r in self.ticker_history if r.source is source]

    def provider_ids(self, source: Source) -> list[str]:
        """This Instrument's provider ids from `source`, in the order they were recorded."""
        return [
            r.value
            for r in self.references
            if r.kind is ReferenceKind.PROVIDER_ID and r.qualifier == source
        ]

    def provider_id(self, source: Source) -> str | None:
        """The first of this Instrument's provider ids from `source`, if it has one."""
        return next(iter(self.provider_ids(source)), None)

    def absorb(self, obs: Observation) -> None:
        """Fold an Observation this Instrument has been identified with into itself."""
        known = set(self.references)
        self.references.extend(r for r in obs.references if r not in known)
        if obs.is_resolved:
            self.figi_resolution = obs.figi_resolution
        elif self.figi_resolution is FigiResolution.NOT_ATTEMPTED:
            self.figi_resolution = obs.figi_resolution
        self.name = self.name or obs.name
        self.issuer_name = self.issuer_name or obs.issuer_name
        self._record_symbol(obs)

    def _record_symbol(self, obs: Observation) -> None:
        """Record Ticker History under the source that reported the symbol.

        Each source has its own chain of records, so a symbol current at one source neither
        closes nor contradicts another source's. Only forward renames are recorded; a symbol
        observed before that source's current record began is an out-of-order sighting and is
        ignored rather than rewriting history.

        The display `symbol` follows the Identity Source alone, so a second source naming the
        asset differently does not rename the Instrument.
        """
        latest = [i for i, r in enumerate(self.ticker_history) if r.source is obs.source]
        if latest:
            index = latest[-1]
            current = self.ticker_history[index]
            if obs.symbol == current.symbol or obs.observed_at < current.valid_from:
                return
            self.ticker_history[index] = replace(current, valid_to=obs.observed_at)
        self.ticker_history.append(
            TickerRecord(obs.source, obs.symbol, self.scope, valid_from=obs.observed_at)
        )
        if obs.source is self.identity_source:
            self.symbol = obs.symbol

ticker_history_from(source)

This Instrument's Ticker History as reported by one source, oldest first.

Source code in src/cointoss/instrument.py
def ticker_history_from(self, source: Source) -> list[TickerRecord]:
    """This Instrument's Ticker History as reported by one source, oldest first."""
    return [r for r in self.ticker_history if r.source is source]

provider_ids(source)

This Instrument's provider ids from source, in the order they were recorded.

Source code in src/cointoss/instrument.py
def provider_ids(self, source: Source) -> list[str]:
    """This Instrument's provider ids from `source`, in the order they were recorded."""
    return [
        r.value
        for r in self.references
        if r.kind is ReferenceKind.PROVIDER_ID and r.qualifier == source
    ]

provider_id(source)

The first of this Instrument's provider ids from source, if it has one.

Source code in src/cointoss/instrument.py
def provider_id(self, source: Source) -> str | None:
    """The first of this Instrument's provider ids from `source`, if it has one."""
    return next(iter(self.provider_ids(source)), None)

absorb(obs)

Fold an Observation this Instrument has been identified with into itself.

Source code in src/cointoss/instrument.py
def absorb(self, obs: Observation) -> None:
    """Fold an Observation this Instrument has been identified with into itself."""
    known = set(self.references)
    self.references.extend(r for r in obs.references if r not in known)
    if obs.is_resolved:
        self.figi_resolution = obs.figi_resolution
    elif self.figi_resolution is FigiResolution.NOT_ATTEMPTED:
        self.figi_resolution = obs.figi_resolution
    self.name = self.name or obs.name
    self.issuer_name = self.issuer_name or obs.issuer_name
    self._record_symbol(obs)

IdentityResult dataclass

What an Observation meant. instrument is None exactly when the outcome is FLAGGED.

Source code in src/cointoss/instrument.py
@dataclass(frozen=True)
class IdentityResult:
    """What an Observation meant. `instrument` is None exactly when the outcome is FLAGGED."""

    outcome: Outcome
    instrument: Instrument | None = None
    superseded: tuple[InstrumentId, ...] = ()
    review: str | None = None

InstrumentRegistry

The identity seam over an in-memory collection of Instruments.

Write side: observe, and anchor for a retry. Read side: resolve_symbol, survivor, unresolved. Storage is out of scope; the collection is passed in and handed back, and no ordering beyond mint order -- which the registry tracks itself -- is assumed.

Source code in src/cointoss/instrument.py
class InstrumentRegistry:
    """The identity seam over an in-memory collection of Instruments.

    Write side: `observe`, and `anchor` for a retry. Read side: `resolve_symbol`, `survivor`,
    `unresolved`. Storage is out of scope; the collection is passed in and handed back, and no
    ordering beyond mint order -- which the registry tracks itself -- is assumed.
    """

    def __init__(self, instruments: list[Instrument] | None = None) -> None:
        self._instruments: list[Instrument] = instruments if instruments is not None else []
        self._by_id: dict[InstrumentId, Instrument] = {i.id: i for i in self._instruments}
        self._next_seq: int = max((i.mint_seq for i in self._instruments), default=0) + 1

    # -- Read side --

    def instruments(self) -> list[Instrument]:
        """Every Instrument, superseded ones included, in mint order."""
        return sorted(self._instruments, key=lambda i: i.mint_seq)

    def get(self, instrument_id: InstrumentId | str) -> Instrument:
        """Look up by Instrument Id exactly as stored, without chasing Supersession.

        A plain `str` is parsed first, so a value that is not an Instrument Id at all raises
        `ValueError` or `UnknownScope`, while an id that is well formed but unknown raises
        `KeyError`. Malformed input and a genuine miss are different failures.
        """
        return self._by_id[InstrumentId(instrument_id)]

    def survivor(self, instrument_id: InstrumentId | str) -> Instrument:
        """Chase a Supersession chain to its fixed point."""
        seen: set[InstrumentId] = set()
        current = self.get(instrument_id)
        while current.superseded_by is not None:
            if current.id in seen:
                raise SupersessionCycle(f"supersession cycle through {sorted(seen)}")
            seen.add(current.id)
            current = self._by_id[current.superseded_by]
        return current

    def unresolved(self) -> list[Instrument]:
        """Work queue: surviving Instruments whose FIGI Resolution is not resolved."""
        return [i for i in self.instruments() if i.is_unresolved and i.superseded_by is None]

    def resolve_symbol(
        self,
        source: Source,
        instrument_type: InstrumentType,
        symbol: str,
        scope: str,
        as_of: date,
    ) -> Instrument | None:
        """Resolve a symbol supplied by a person or a document, as reported by one source.

        The source is required and is not a hint: a symbol recorded under one source does not
        resolve under another, because vendors name the same asset differently and neither
        name is canonical. A broker CSV must be read against the source that wrote it.

        The date is required too: defaulting it would silently mis-resolve historical
        statements. Overlapping Ticker History across Instruments is expected -- that is the
        reused-ticker case -- and is disambiguated by the date, so an overlap within one source
        on one date is an error.
        """
        wanted = normalize_symbol(symbol)
        wanted_scope = Scope(instrument_type, scope)
        found: dict[str, Instrument] = {}
        for inst in self.instruments():
            if inst.type is not instrument_type:
                continue
            if any(
                r.symbol == wanted and r.scope == wanted_scope and r.covers(as_of)
                for r in inst.ticker_history_from(source)
            ):
                alive = self.survivor(inst.id)
                found[alive.id] = alive
        if not found:
            return None
        if len(found) > 1:
            raise AmbiguousSymbol(
                f"{wanted} in {wanted_scope} at {source} on {as_of}: {sorted(found)}"
            )
        return next(iter(found.values()))

    # -- Write side --

    def observe(self, obs: Observation) -> IdentityResult:
        """Turn a source Observation into the Instrument it belongs to.

        Identity is established by External Reference in a fixed order: FIGI, then contract
        address, then a provider's own stable id. Symbol and scope match only between two
        Instruments that are both still unresolved, and never merge two Instruments.
        """
        obs = replace(obs, symbol=normalize_symbol(obs.symbol))
        scope = Scope(obs.type, obs.scope)
        matched = self._match_by_reference(obs)
        if matched:
            return self._from_reference_match(obs, scope, matched)
        symbol_match = self._match_by_symbol(obs, scope)
        if isinstance(symbol_match, IdentityResult):
            return symbol_match
        if symbol_match is not None:
            symbol_match.absorb(obs)
            return IdentityResult(Outcome.MATCHED, symbol_match)
        return IdentityResult(Outcome.MINTED, self._mint(obs, scope))

    def anchor(self, instrument_id: InstrumentId | str, obs: Observation) -> IdentityResult:
        """Attach a FIGI answer to a named Instrument: the retry of ADR-0004's unresolved flow.

        `observe` cannot do this, because its symbol tier only joins two unresolved parties and a
        FIGI answer is resolved, so it would mint a second Instrument beside the one waiting for
        that answer. Here the caller names the target instead, and the evidence is checked
        against it: the target must be surviving, of the Observation's type and scope, not
        already anchored on another FIGI, and not disagreeing on any other External Reference;
        and no other Instrument may already hold one of the Observation's References, since
        that is a merge question for review rather than a retry. Contrary evidence is flagged and
        nothing is changed.

        On success the Observation is absorbed like any match: References are added, the FIGI
        Resolution becomes `RESOLVED`, and its symbol is recorded as Ticker History under the
        usual forward-only rule, so a retry under a later ticker records the rename.
        """
        if not obs.is_resolved:
            raise ValueError("anchoring needs a FIGI-resolved Observation")
        obs = replace(obs, symbol=normalize_symbol(obs.symbol))
        target = self.get(instrument_id)
        review = self._anchor_review(target, obs)
        if review is not None:
            return IdentityResult(Outcome.FLAGGED, review=review)
        target.absorb(obs)
        return IdentityResult(Outcome.MATCHED, target)

    def _anchor_review(self, target: Instrument, obs: Observation) -> str | None:
        """Why `obs` may not be anchored on `target`, or None when nothing contradicts it."""
        if target.superseded_by is not None:
            return f"{target.id} is superseded by {target.superseded_by}"
        if target.type is not obs.type or target.scope != Scope(obs.type, obs.scope):
            return f"{target.id} is not a {obs.type} in {obs.scope}"
        if _conflict(obs.references, target.references):
            return f"observation disagrees with {target.id} on an External Reference"
        others = sorted(
            {self.survivor(i.id).id for i in self._match_by_reference(obs)} - {target.id}
        )
        if others:
            return f"a reference of {obs.symbol} is already held by {others}, not {target.id}"
        return None

    # -- Matching --

    def _match_by_reference(self, obs: Observation) -> list[Instrument]:
        """Instruments sharing an External Reference with the Observation, in mint order."""
        obs_keys = _match_keys(obs.references)
        if not obs_keys:
            return []
        best: dict[int, list[Instrument]] = {}
        for inst in self.instruments():
            shared = obs_keys & _match_keys(inst.references)
            if shared:
                best.setdefault(min(tier for tier, _, _, _ in shared), []).append(inst)
        # Higher-priority evidence wins outright, but every tier that fired is reported, so a
        # FIGI and a provider id pointing at two Instruments is seen as the Supersession it is.
        return [inst for tier in sorted(best) for inst in best[tier]]

    def _match_by_symbol(
        self, obs: Observation, scope: Scope
    ) -> Instrument | IdentityResult | None:
        """Last-resort tier. Only two unresolved parties may be joined by a symbol."""
        if obs.is_resolved:
            return None
        eligible = [
            i
            for i in self.instruments()
            if i.superseded_by is None
            and i.type is obs.type
            and i.scope == scope
            and i.symbol == obs.symbol
            and i.is_unresolved
            and not _conflict(obs.references, i.references)
        ]
        if len(eligible) > 1:
            return IdentityResult(
                Outcome.FLAGGED,
                review=f"{obs.symbol} in {scope} matches several unresolved Instruments: "
                f"{sorted(i.id for i in eligible)}",
            )
        return eligible[0] if eligible else None

    def _from_reference_match(
        self, obs: Observation, scope: Scope, matched: list[Instrument]
    ) -> IdentityResult:
        survivors: dict[str, Instrument] = {}
        for inst in matched:
            alive = self.survivor(inst.id)
            survivors[alive.id] = alive
        alive_list = sorted(survivors.values(), key=lambda i: i.mint_seq)
        target = alive_list[0]
        mismatched = [i for i in alive_list if i.type is not obs.type or i.scope != scope]
        if mismatched:
            return IdentityResult(
                Outcome.FLAGGED,
                review=f"anchor matches {sorted(i.id for i in mismatched)} across a type or "
                f"scope boundary ({obs.type}.{scope})",
            )
        if len(alive_list) > 1:
            if any(i.superseded_by is not None for i in matched):
                return IdentityResult(
                    Outcome.FLAGGED,
                    review=f"merge claim over an Instrument already superseded elsewhere: "
                    f"{sorted(i.id for i in matched if i.superseded_by is not None)}",
                )
            for other in alive_list[1:]:
                if _conflict(target.references, other.references):
                    return IdentityResult(
                        Outcome.FLAGGED,
                        review=f"{target.id} and {other.id} match on one reference but "
                        f"disagree on another",
                    )
        if _conflict(obs.references, target.references):
            return IdentityResult(
                Outcome.FLAGGED,
                review=f"observation disagrees with {target.id} on an External Reference",
            )
        # Earliest-minted survives, so the outcome does not depend on processing order.
        for other in alive_list[1:]:
            other.superseded_by = target.id
        target.absorb(obs)
        if len(alive_list) > 1:
            return IdentityResult(Outcome.SUPERSEDED, target, tuple(i.id for i in alive_list[1:]))
        return IdentityResult(Outcome.MATCHED, target)

    # -- Mutation --

    def _mint(self, obs: Observation, scope: Scope) -> Instrument:
        """Mint a new Instrument. The id is fixed here for good -- see ADR-0004.

        Identity Source and Price Sources default from the instrument type. They are stored on
        the Instrument rather than recomputed, so a later change to the defaults cannot silently
        restate where an existing Instrument's name and Bars come from.
        """
        instrument_id = self._mint_id(obs, scope)
        inst = Instrument(
            id=instrument_id,
            type=obs.type,
            scope=scope,
            symbol=obs.symbol,
            mint_seq=self._next_seq,
            minted_at=obs.observed_at,
            name=obs.name,
            identity_source=default_identity_source(obs.type),
            price_sources=default_price_sources(obs.type),
            issuer_name=obs.issuer_name,
            references=list(obs.references),
            figi_resolution=obs.figi_resolution,
            ticker_history=[
                TickerRecord(obs.source, obs.symbol, scope, valid_from=obs.observed_at)
            ],
        )
        self._next_seq += 1
        self._instruments.append(inst)
        self._by_id[inst.id] = inst
        return inst

    def _mint_id(self, obs: Observation, scope: Scope) -> InstrumentId:
        """Find the first Instrument Id this Observation may claim.

        Ids are never reused, so a superseded Instrument keeps its id out of circulation.
        """
        bare = InstrumentId.mint(obs.type, scope, obs.symbol)
        if bare not in self._by_id:
            return bare
        # The symbol is taken by an unrelated Instrument: qualify by issuer, else by number.
        qualifier = issuer_qualifier(obs.issuer_name)
        if qualifier:
            by_issuer = InstrumentId.mint(obs.type, scope, obs.symbol, qualifier)
            if by_issuer not in self._by_id:
                return by_issuer
        n = 2
        while (numbered := InstrumentId.mint(obs.type, scope, obs.symbol, str(n))) in self._by_id:
            n += 1
        return numbered

instruments()

Every Instrument, superseded ones included, in mint order.

Source code in src/cointoss/instrument.py
def instruments(self) -> list[Instrument]:
    """Every Instrument, superseded ones included, in mint order."""
    return sorted(self._instruments, key=lambda i: i.mint_seq)

get(instrument_id)

Look up by Instrument Id exactly as stored, without chasing Supersession.

A plain str is parsed first, so a value that is not an Instrument Id at all raises ValueError or UnknownScope, while an id that is well formed but unknown raises KeyError. Malformed input and a genuine miss are different failures.

Source code in src/cointoss/instrument.py
def get(self, instrument_id: InstrumentId | str) -> Instrument:
    """Look up by Instrument Id exactly as stored, without chasing Supersession.

    A plain `str` is parsed first, so a value that is not an Instrument Id at all raises
    `ValueError` or `UnknownScope`, while an id that is well formed but unknown raises
    `KeyError`. Malformed input and a genuine miss are different failures.
    """
    return self._by_id[InstrumentId(instrument_id)]

survivor(instrument_id)

Chase a Supersession chain to its fixed point.

Source code in src/cointoss/instrument.py
def survivor(self, instrument_id: InstrumentId | str) -> Instrument:
    """Chase a Supersession chain to its fixed point."""
    seen: set[InstrumentId] = set()
    current = self.get(instrument_id)
    while current.superseded_by is not None:
        if current.id in seen:
            raise SupersessionCycle(f"supersession cycle through {sorted(seen)}")
        seen.add(current.id)
        current = self._by_id[current.superseded_by]
    return current

unresolved()

Work queue: surviving Instruments whose FIGI Resolution is not resolved.

Source code in src/cointoss/instrument.py
def unresolved(self) -> list[Instrument]:
    """Work queue: surviving Instruments whose FIGI Resolution is not resolved."""
    return [i for i in self.instruments() if i.is_unresolved and i.superseded_by is None]

resolve_symbol(source, instrument_type, symbol, scope, as_of)

Resolve a symbol supplied by a person or a document, as reported by one source.

The source is required and is not a hint: a symbol recorded under one source does not resolve under another, because vendors name the same asset differently and neither name is canonical. A broker CSV must be read against the source that wrote it.

The date is required too: defaulting it would silently mis-resolve historical statements. Overlapping Ticker History across Instruments is expected -- that is the reused-ticker case -- and is disambiguated by the date, so an overlap within one source on one date is an error.

Source code in src/cointoss/instrument.py
def resolve_symbol(
    self,
    source: Source,
    instrument_type: InstrumentType,
    symbol: str,
    scope: str,
    as_of: date,
) -> Instrument | None:
    """Resolve a symbol supplied by a person or a document, as reported by one source.

    The source is required and is not a hint: a symbol recorded under one source does not
    resolve under another, because vendors name the same asset differently and neither
    name is canonical. A broker CSV must be read against the source that wrote it.

    The date is required too: defaulting it would silently mis-resolve historical
    statements. Overlapping Ticker History across Instruments is expected -- that is the
    reused-ticker case -- and is disambiguated by the date, so an overlap within one source
    on one date is an error.
    """
    wanted = normalize_symbol(symbol)
    wanted_scope = Scope(instrument_type, scope)
    found: dict[str, Instrument] = {}
    for inst in self.instruments():
        if inst.type is not instrument_type:
            continue
        if any(
            r.symbol == wanted and r.scope == wanted_scope and r.covers(as_of)
            for r in inst.ticker_history_from(source)
        ):
            alive = self.survivor(inst.id)
            found[alive.id] = alive
    if not found:
        return None
    if len(found) > 1:
        raise AmbiguousSymbol(
            f"{wanted} in {wanted_scope} at {source} on {as_of}: {sorted(found)}"
        )
    return next(iter(found.values()))

observe(obs)

Turn a source Observation into the Instrument it belongs to.

Identity is established by External Reference in a fixed order: FIGI, then contract address, then a provider's own stable id. Symbol and scope match only between two Instruments that are both still unresolved, and never merge two Instruments.

Source code in src/cointoss/instrument.py
def observe(self, obs: Observation) -> IdentityResult:
    """Turn a source Observation into the Instrument it belongs to.

    Identity is established by External Reference in a fixed order: FIGI, then contract
    address, then a provider's own stable id. Symbol and scope match only between two
    Instruments that are both still unresolved, and never merge two Instruments.
    """
    obs = replace(obs, symbol=normalize_symbol(obs.symbol))
    scope = Scope(obs.type, obs.scope)
    matched = self._match_by_reference(obs)
    if matched:
        return self._from_reference_match(obs, scope, matched)
    symbol_match = self._match_by_symbol(obs, scope)
    if isinstance(symbol_match, IdentityResult):
        return symbol_match
    if symbol_match is not None:
        symbol_match.absorb(obs)
        return IdentityResult(Outcome.MATCHED, symbol_match)
    return IdentityResult(Outcome.MINTED, self._mint(obs, scope))

anchor(instrument_id, obs)

Attach a FIGI answer to a named Instrument: the retry of ADR-0004's unresolved flow.

observe cannot do this, because its symbol tier only joins two unresolved parties and a FIGI answer is resolved, so it would mint a second Instrument beside the one waiting for that answer. Here the caller names the target instead, and the evidence is checked against it: the target must be surviving, of the Observation's type and scope, not already anchored on another FIGI, and not disagreeing on any other External Reference; and no other Instrument may already hold one of the Observation's References, since that is a merge question for review rather than a retry. Contrary evidence is flagged and nothing is changed.

On success the Observation is absorbed like any match: References are added, the FIGI Resolution becomes RESOLVED, and its symbol is recorded as Ticker History under the usual forward-only rule, so a retry under a later ticker records the rename.

Source code in src/cointoss/instrument.py
def anchor(self, instrument_id: InstrumentId | str, obs: Observation) -> IdentityResult:
    """Attach a FIGI answer to a named Instrument: the retry of ADR-0004's unresolved flow.

    `observe` cannot do this, because its symbol tier only joins two unresolved parties and a
    FIGI answer is resolved, so it would mint a second Instrument beside the one waiting for
    that answer. Here the caller names the target instead, and the evidence is checked
    against it: the target must be surviving, of the Observation's type and scope, not
    already anchored on another FIGI, and not disagreeing on any other External Reference;
    and no other Instrument may already hold one of the Observation's References, since
    that is a merge question for review rather than a retry. Contrary evidence is flagged and
    nothing is changed.

    On success the Observation is absorbed like any match: References are added, the FIGI
    Resolution becomes `RESOLVED`, and its symbol is recorded as Ticker History under the
    usual forward-only rule, so a retry under a later ticker records the rename.
    """
    if not obs.is_resolved:
        raise ValueError("anchoring needs a FIGI-resolved Observation")
    obs = replace(obs, symbol=normalize_symbol(obs.symbol))
    target = self.get(instrument_id)
    review = self._anchor_review(target, obs)
    if review is not None:
        return IdentityResult(Outcome.FLAGGED, review=review)
    target.absorb(obs)
    return IdentityResult(Outcome.MATCHED, target)

default_identity_source(instrument_type)

The Identity Source an Instrument of this type is minted with.

>>> default_identity_source(InstrumentType.CRYPTO)
<Source.COINGECKO: 'coingecko'>
Source code in src/cointoss/instrument.py
def default_identity_source(instrument_type: InstrumentType) -> Source:
    """The Identity Source an Instrument of this type is minted with.

    >>> default_identity_source(InstrumentType.CRYPTO)
    <Source.COINGECKO: 'coingecko'>
    """
    return _DEFAULT_IDENTITY_SOURCE[instrument_type]

default_price_sources(instrument_type)

The ordered Price Sources an Instrument of this type is minted with.

>>> default_price_sources(InstrumentType.CRYPTO)
(<Source.YAHOO: 'yahoo'>, <Source.COINGECKO: 'coingecko'>)
Source code in src/cointoss/instrument.py
def default_price_sources(instrument_type: InstrumentType) -> tuple[Source, ...]:
    """The ordered Price Sources an Instrument of this type is minted with.

    >>> default_price_sources(InstrumentType.CRYPTO)
    (<Source.YAHOO: 'yahoo'>, <Source.COINGECKO: 'coingecko'>)
    """
    return _DEFAULT_PRICE_SOURCES[instrument_type]

normalize_symbol(symbol)

Normalise a symbol so casing and punctuation cannot mint two Instruments.

>>> normalize_symbol(" brk.b ")
'BRK-B'
>>> normalize_symbol("BRK-B") == normalize_symbol("brk b")
True
Source code in src/cointoss/instrument.py
def normalize_symbol(symbol: str) -> str:
    """Normalise a symbol so casing and punctuation cannot mint two Instruments.

    >>> normalize_symbol(" brk.b ")
    'BRK-B'
    >>> normalize_symbol("BRK-B") == normalize_symbol("brk b")
    True
    """
    return _NON_ALNUM.sub("-", symbol.strip()).strip("-").upper()

issuer_qualifier(issuer_name)

Slugify an issuer name into a Qualifier, dropping legal-form words.

>>> issuer_qualifier("ProShares Trust")
'proshares'
>>> issuer_qualifier("Meta Platforms, Inc.")
'meta-platforms'
>>> issuer_qualifier("Trust Inc") is None
True
Source code in src/cointoss/instrument.py
def issuer_qualifier(issuer_name: str | None) -> str | None:
    """Slugify an issuer name into a Qualifier, dropping legal-form words.

    >>> issuer_qualifier("ProShares Trust")
    'proshares'
    >>> issuer_qualifier("Meta Platforms, Inc.")
    'meta-platforms'
    >>> issuer_qualifier("Trust Inc") is None
    True
    """
    if not issuer_name:
        return None
    words = [w for w in _NON_ALNUM.sub(" ", issuer_name).lower().split() if w]
    kept = [w for w in words if w not in _LEGAL_SUFFIXES]
    return "-".join(kept) or None

test_instrument_id_round_trips_through_its_string_form()

The readable slug is the stored form; parsing it back must lose nothing.

Source code in src/cointoss/instrument.py
def test_instrument_id_round_trips_through_its_string_form() -> None:
    """The readable slug is the stored form; parsing it back must lose nothing."""
    for text in ("stock.us.fb", "stock.us.fb_proshares", "crypto.eth.usdc", "stock.us.brk-b_2"):
        parsed = InstrumentId(text)
        assert parsed == text
        assert str(parsed) == text
        assert InstrumentId.mint(parsed.type, parsed.scope, parsed.symbol, parsed.qualifier) == text

test_malformed_instrument_ids_are_rejected_whole()

A partial parse would be worse than no parse: every part is validated or nothing is.

Source code in src/cointoss/instrument.py
def test_malformed_instrument_ids_are_rejected_whole() -> None:
    """A partial parse would be worse than no parse: every part is validated or nothing is."""
    for bad in (
        "AAPL",
        "stock.us",
        "stock.us.fb.extra",
        "bond.us.x",
        "stock.usa.fb",
        "stock.us._q",
    ):
        try:
            InstrumentId(bad)
            raise AssertionError(f"{bad!r} should not parse as an Instrument Id")
        except (ValueError, UnknownScope):
            pass

test_a_qualifier_is_separated_from_the_symbol()

A normalised symbol never contains "_", so the first one always starts the Qualifier.

Source code in src/cointoss/instrument.py
def test_a_qualifier_is_separated_from_the_symbol() -> None:
    """A normalised symbol never contains "_", so the first one always starts the Qualifier."""
    plain = InstrumentId("stock.us.brk-b")
    assert (plain.symbol, plain.qualifier) == ("BRK-B", None)
    qualified = InstrumentId("stock.us.brk-b_proshares")
    assert (qualified.symbol, qualified.qualifier) == ("BRK-B", "proshares")
    # The id renders its symbol lowercase, but `symbol` means what it means on an Instrument.
    assert plain.symbol == normalize_symbol("brk.b")

test_ids_and_scopes_survive_copying()

A str subclass carrying attributes is uncopyable unless it says how to rebuild, and both types end up inside dataclasses that callers will copy and stores will serialize.

Source code in src/cointoss/instrument.py
def test_ids_and_scopes_survive_copying() -> None:
    """A str subclass carrying attributes is uncopyable unless it says how to rebuild, and
    both types end up inside dataclasses that callers will copy and stores will serialize."""
    for original in (InstrumentId("stock.us.fb_proshares"), Scope(InstrumentType.CRYPTO, "eth")):
        for restored in (copy.deepcopy(original), pickle.loads(pickle.dumps(original))):
            assert restored == original
            assert type(restored) is type(original)
    assert copy.deepcopy(InstrumentId("crypto.eth.usdc")).scope.kind is ScopeKind.CHAIN

test_source_defaults_follow_the_instrument_type()

Yahoo names and prices equities; CoinGecko names coins and backs Yahoo up on prices.

Source code in src/cointoss/instrument.py
def test_source_defaults_follow_the_instrument_type() -> None:
    """Yahoo names and prices equities; CoinGecko names coins and backs Yahoo up on prices."""
    assert default_identity_source(InstrumentType.STOCK) is Source.YAHOO
    assert default_price_sources(InstrumentType.STOCK) == (Source.YAHOO,)
    assert default_identity_source(InstrumentType.CRYPTO) is Source.COINGECKO
    assert default_price_sources(InstrumentType.CRYPTO) == (Source.YAHOO, Source.COINGECKO)

test_price_sources_must_be_a_usable_preference()

An empty or repeating preference names nothing, so it is refused at construction.

Source code in src/cointoss/instrument.py
def test_price_sources_must_be_a_usable_preference() -> None:
    """An empty or repeating preference names nothing, so it is refused at construction."""
    for price_sources in ((), (Source.YAHOO, Source.YAHOO)):
        try:
            Instrument(
                id=InstrumentId("stock.us.aapl"),
                type=InstrumentType.STOCK,
                scope=Scope(InstrumentType.STOCK, "us"),
                symbol="AAPL",
                mint_seq=1,
                minted_at=date(2020, 1, 1),
                identity_source=Source.YAHOO,
                price_sources=price_sources,
            )
            raise AssertionError(f"{price_sources} should not be accepted as Price Sources")
        except ValueError:
            pass