Parsers⚓︎
parsers
⚓︎
Reusable field parsers and parser-composition helpers.
Every parser follows the same callable contract used by field(parser=...):
it accepts the current value and a :class:~talika.CellContext. Parser
factories return plain callable objects, so projects can use them directly,
compose them, or mix them with custom functions.
Info
Parser factories in this module do not know BDD business vocabulary. They provide small, predictable conversion primitives that project schemas can combine into their own table language.
_OptionalParser
dataclass
⚓︎
Parser wrapper that converts configured null-like tokens to None.
Attributes:
-
parser(Parser) –Parser used when the value is not null-like.
-
none_values(frozenset[str]) –Normalized tokens that should become
None. -
case_sensitive(bool) –Whether null-token matching preserves case.
Info
Field declarations decide whether explicit empty cells reach this
parser through empty="parse".
__call__
⚓︎
__call__(value: Any, context: CellContext) -> Any
Return None for null-like input or delegate to the wrapped parser.
Parameters:
-
value(Any) –Current logical cell value.
-
context(CellContext) –Source-aware parser context for the active field.
Returns:
Warning
Empty strings are always treated as None by this wrapper. Use a
custom parser when empty text is a meaningful domain value.
string
⚓︎
Return a parser that normalizes text.
Parameters:
-
strip(bool, default:False) –Remove leading and trailing whitespace before case conversion.
-
lower(bool, default:False) –Convert the resulting text to lowercase.
-
upper(bool, default:False) –Convert the resulting text to uppercase.
Returns:
-
Parser–A parser that always returns
str.
Raises:
-
ValueError–If both
lowerandupperare enabled.
Warning
This parser deliberately performs no semantic validation. Use
choice() or a custom parser when the table cell must be one of a
known set of values.
integer
⚓︎
integer(*, base: int = 10) -> Parser
Return a parser that converts values to int.
Parameters:
-
base(int, default:10) –Numeric base used when the incoming value is a string.
Returns:
-
Parser–A parser that returns an integer.
Warning
The base is passed to Python's int(value, base) only for strings.
Non-string values use int(value) so already-typed project values
still follow Python's normal conversion rules.
floating
⚓︎
Return a parser that converts values to float.
Returns:
-
Parser–A parser that returns a Python
float.
Warning
Use decimal() instead when tests need exact decimal arithmetic or
should avoid binary floating-point representation.
decimal
⚓︎
Return a parser that converts through text to Decimal.
Returns:
-
Parser–A parser that returns
decimal.Decimal.
Info
Conversion goes through str(value) so existing numeric objects and
raw table text follow the same exact decimal path.
boolean
⚓︎
boolean(
*,
true_values: Iterable[str] = ("true",),
false_values: Iterable[str] = ("false",),
case_sensitive: bool = False,
) -> Parser
Return a boolean parser with an explicit accepted vocabulary.
Parameters:
-
true_values(Iterable[str], default:('true',)) –Strings accepted as
True. -
false_values(Iterable[str], default:('false',)) –Strings accepted as
False. -
case_sensitive(bool, default:False) –Whether matching should preserve case.
Returns:
-
Parser–A parser that returns
bool.
Raises:
-
TypeError–If token collections or
case_sensitivehave invalid types. -
ValueError–If the true and false token sets overlap.
The default vocabulary is deliberately small
Without configuration, the parser accepts only "true" and
"false". Matching is case-insensitive by default, so "TRUE"
and "False" also work. Words such as "yes" and "on" and
numeric flags such as "1" must be declared by the schema.
Warning
Unknown values fail instead of falling back to Python truthiness. This
prevents cells such as "False" or "nope" from accidentally
becoming true.
Whitespace is not removed
" true " is different from "true". Compose string(strip=True)
before this parser when the authored format permits padding.
choice
⚓︎
Return a parser that validates one allowed string value.
Parameters:
-
*values(str, default:()) –Accepted display values.
-
case_sensitive(bool, default:True) –Whether input must match the case of an accepted value.
Returns:
-
Parser–A parser that returns the canonical value from
values.
Raises:
-
ValueError–If no values are supplied.
Info
With case_sensitive=False, the returned value is still the
canonical spelling passed to the factory.
split
⚓︎
Return a parser that splits one cell into a list of strings.
Parameters:
-
separator(str, default:',') –Text separator used between items.
-
strip_items(bool, default:True) –Strip whitespace around each split item.
-
keep_empty(bool, default:False) –Preserve empty segments instead of filtering them out.
Returns:
-
Parser–A parser that returns
list[str].
Raises:
-
ValueError–If
separatoris empty.
Info
This parser is often paired with each(...) when a table cell holds
a compact list of typed values.
map_value
⚓︎
Return a parser that maps cell strings to Python values.
Parameters:
-
values(Mapping[str, Any]) –Mapping from table text to the Python value to return.
-
case_sensitive(bool, default:True) –Whether lookup should preserve case.
Returns:
-
Parser–A parser that returns the mapped value.
Info
This is useful for compact BDD vocabulary such as "TBD" becoming
a sentinel object or "high" becoming a domain enum value.
compose
⚓︎
Run parsers left-to-right.
Parameters:
-
*parsers(Parser, default:()) –Parser callables following the talika parser contract.
Returns:
-
Parser–A parser that feeds each result into the next parser.
Raises:
-
ValueError–If no parsers are supplied.
Info
Every parser receives the same CellContext so later stages still
know the original field, item ID, and source value.
each
⚓︎
Apply one parser to every item in an iterable value.
Parameters:
-
parser(Parser) –Parser applied to each non-string item.
Returns:
-
Parser–A parser that returns
list[Any].
Info
each() is designed for composition after parsers that produce a
sequence, such as split().
optional
⚓︎
optional(
parser: Parser,
*,
none_values: Iterable[str] = ("none", "null"),
case_sensitive: bool = False,
) -> Parser
Return a parser that maps empty or null-like tokens to None.
Parameters:
-
parser(Parser) –Parser used for non-null values.
-
none_values(Iterable[str], default:('none', 'null')) –Text tokens that should parse as
None. -
case_sensitive(bool, default:False) –Whether matching
none_valuesshould preserve case.
Returns:
-
Parser–A parser object that converts empty or configured null-like values to
-
Parser–Nonewhen the field invokes it.
Info
Use field(..., empty="parse") when an explicit blank cell should
reach the returned parser. Without that field policy, blanks follow
the field's normal empty-cell behavior.