Model registry derives wrong algorithm names via str.lstrip("m_")
Summary
MotherModelRegistry._load_models() derives an algorithm name from each
m_*.py model module filename using str.lstrip("m_"). str.lstrip removes a
set of leading characters, not a prefix, so any model file whose name starts
with a character that is also present in "m_" gets silently mangled.
The most visible case: a module named m_mlp.py produces the algorithm name
"lp" instead of "mlp", because lstrip strips the leading m, then _,
then the second m as well, stopping only at l.
Location
src/mother/ml/__init__.py, in MotherModelRegistry._load_models():
algo: str = model_file.lower().lstrip("m_")
Why it is wrong
str.lstrip(chars) treats its argument as a set of characters to strip from the
left, repeatedly, until a character not in the set is found. It is not a
prefix removal.
>>> "m_mlp".lstrip("m_")
'lp' # expected 'mlp'
>>> "m_catboost".lstrip("m_")
'catboost' # happens to be correct (next char 'c' is not in {'m','_'})
>>> "m_flow".lstrip("m_")
'flow' # correct by luck
Any module name of the form m_m... (a model family whose name starts with m,
e.g. mlp, mars, mlr) is corrupted. Names that don't start with m after
the prefix are corrupted only if their leading characters fall in {"m", "_"},
which is why the bug went unnoticed for the existing m_catboost, m_lasso,
m_node, m_randomforest, m_tabpfn modules.
Impact
get_available_algorithms() returns incorrect algorithm tokens (e.g. lp).
get_model_class_by_algorithm_and_type(algorithm, model_type) cannot resolve
the mangled name and raises
ValueError: Unsupported algorithm '<name>' or model type '<type>'.
- Any config-driven or test code that sweeps over
get_available_algorithms()
breaks for the affected model. This surfaced as failures in
test/unit/test_ml.py once an m_mlp.py module was added.
Reproduction
from mother import ml
# Add any model module named m_mlp.py under src/mother/ml/models/
# containing an AbstractMotherPipeline subclass, then:
print(ml.get_available_algorithms()) # -> [..., 'lp'] (should be 'mlp')
ml.get_model_class_by_algorithm_and_type("mlp", "regression") # KeyError/ValueError
Fix
Use str.removeprefix (Python 3.9+), which removes the literal prefix once:
algo: str = model_file.lower().removeprefix("m_")
>>> "m_mlp".removeprefix("m_")
'mlp'
>>> "m_catboost".removeprefix("m_")
'catboost'
Suggested follow-ups
- Add a regression test asserting that a module named
m_mlp maps to the
algorithm "mlp" (and generally that algo == model_file with the m_
prefix removed).
- Audit for other misuses of
str.lstrip/str.rstrip where a prefix/suffix
removal was intended.
Model registry derives wrong algorithm names via
str.lstrip("m_")Summary
MotherModelRegistry._load_models()derives an algorithm name from eachm_*.pymodel module filename usingstr.lstrip("m_").str.lstripremoves aset of leading characters, not a prefix, so any model file whose name starts
with a character that is also present in
"m_"gets silently mangled.The most visible case: a module named
m_mlp.pyproduces the algorithm name"lp"instead of"mlp", becauselstripstrips the leadingm, then_,then the second
mas well, stopping only atl.Location
src/mother/ml/__init__.py, inMotherModelRegistry._load_models():Why it is wrong
str.lstrip(chars)treats its argument as a set of characters to strip from theleft, repeatedly, until a character not in the set is found. It is not a
prefix removal.
Any module name of the form
m_m...(a model family whose name starts withm,e.g.
mlp,mars,mlr) is corrupted. Names that don't start withmafterthe prefix are corrupted only if their leading characters fall in
{"m", "_"},which is why the bug went unnoticed for the existing
m_catboost,m_lasso,m_node,m_randomforest,m_tabpfnmodules.Impact
get_available_algorithms()returns incorrect algorithm tokens (e.g.lp).get_model_class_by_algorithm_and_type(algorithm, model_type)cannot resolvethe mangled name and raises
ValueError: Unsupported algorithm '<name>' or model type '<type>'.get_available_algorithms()breaks for the affected model. This surfaced as failures in
test/unit/test_ml.pyonce anm_mlp.pymodule was added.Reproduction
Fix
Use
str.removeprefix(Python 3.9+), which removes the literal prefix once:Suggested follow-ups
m_mlpmaps to thealgorithm
"mlp"(and generally thatalgo == model_filewith them_prefix removed).
str.lstrip/str.rstripwhere a prefix/suffixremoval was intended.