topmark.core.enum_mixins¶
Generic Enum utilities for TopMark (typing-friendly, UI-agnostic).
This module provides helpers that add lightweight introspection to Enum types without binding callers to UI/CLI concerns (like color). Keep presentation-specific concepts in topmark.presentation and reuse these mixins/utilities anywhere else.
Provided
enum_from_name(enum_cls, name, *, case_insensitive=False): Typed lookup bynamefrom__members__. ReturnsNoneon miss.EnumIntrospectionMixin: Adds.value_length(cached) to any Enum subclass for formatting.
Design
- Keep the functions pure and side-effect free.
- Avoid bringing UI libraries (e.g. yachalk) into this module.
- Prefer functions over overly clever metaclass tricks for type-checker sanity.
Example
EnumIntrospectionMixin ¶
Small, UI-agnostic mixin that adds introspection conveniences to Enums.
When mixed into an Enum class, provides:
• ``value_length`` (cached_property): maximum length (in characters) of all
``.value`` strings for the enum class. Useful for printing aligned labels.
Implementation note
We rely on the Enum metaclass providing iterability over members and the
presence of .value on each. Static analyzers may not infer this on the
mixin type itself; local # type: ignore hints are used sparingly.
value_length
cached
property
¶
Maximum length of the enum's .value strings.
Returns:
| Type | Description |
|---|---|
int
|
The maximum length among all member |
int
|
member belongs to. |
KeyedStrEnum ¶
Bases: str, Enum
Enum where .value is a stable machine key; metadata lives on attributes.
Use this when you want
- a stable, serialization-friendly key (
.value/.key) - a human label (
.label) and optional aliases for parsing
Attributes:
| Name | Type | Description |
|---|---|---|
label |
str
|
Human-readable label for the member. |
aliases |
tuple[str, ...]
|
Alternative tokens accepted by |
Example
class OutputTarget(KeyedStrEnum): FILE = ("file", "Write to file") STDOUT = ("stdout", "Write to STDOUT")
parse
classmethod
¶
Parse a token into an enum member.
Matches against
- the stable key (
.value) - the member name (
.name) - any configured aliases
Matching is case-insensitive and normalizes '-', ' ' to '_' via _norm_token().
Source code in src/topmark/core/enum_mixins.py
enum_from_name ¶
Return the enum member for key_name from enum_cls.__members__.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
enum_cls
|
type[_E]
|
The Enum class to search. |
required |
key_name
|
str | None
|
The member name (e.g., |
required |
case_insensitive
|
bool
|
If True, lookup is performed with |
False
|
Returns:
| Type | Description |
|---|---|
_E | None
|
The matching enum member, or |
Notes
- This is a name lookup. If you want to match against values as well,
normalize the candidate first and query
__members__for both keys, or introduce a separate helper (kept out of this core to avoid policy).