fix(client): guard against MetaflowObject subclasses with unrecognised _NAME#3262
Conversation
MetaflowObject._NAME defaults to "base". The pathspec component-count
validation uses a series of if/elif branches keyed on _NAME, so any
subclass with _NAME not in {"flow","run","step","task","artifact"} would
silently skip all count validation — accepting any-length pathspec.
Add a _KNOWN_NAMES class attribute and an explicit guard at the start of
the pathspec-validation block so misuse is caught with a clear
MetaflowInternalError instead of silently producing wrong behaviour.
Fixes Netflix#948 (partial)
Greptile SummaryThis PR adds a
Confidence Score: 4/5Safe to merge — the change is narrowly scoped to defensive error-raising and does not alter any existing execution path for well-formed objects. The guard is logically correct for the stated scenario, the frozenset is immutable and cannot be accidentally mutated, and the else branch already handled unknown names. The only gap is that the new error can be shadowed by the earlier attempt check when both a bad _NAME and a non-None attempt are present simultaneously, which is a minor diagnostic annoyance rather than a correctness problem. No tests were added to lock in the new behaviour. Only metaflow/client/core.py changed; the guard placement relative to the attempt check (lines 307–311) is worth a second look. Important Files Changed
|
| if self._NAME not in self._KNOWN_NAMES: | ||
| raise MetaflowInternalError( | ||
| "MetaflowObject subclass '%s' has _NAME='%s' which is not a " | ||
| "recognised object type. Subclasses must set _NAME to one of: %s." | ||
| % ( | ||
| type(self).__name__, | ||
| self._NAME, | ||
| ", ".join(sorted(self._KNOWN_NAMES)), | ||
| ) | ||
| ) |
There was a problem hiding this comment.
No test coverage for the new guard
The fix converts a silent failure into an explicit MetaflowInternalError, which is easy to verify, but no unit test is included. A minimal test that instantiates a dummy subclass with an unrecognised _NAME and asserts the new error is raised would lock in this behaviour and prevent future regressions.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #3262 +/- ##
=========================================
Coverage ? 29.11%
=========================================
Files ? 381
Lines ? 52516
Branches ? 9267
=========================================
Hits ? 15290
Misses ? 36187
Partials ? 1039 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Problem
MetaflowObject._NAMEdefaults to"base". The pathspec component-count validation is a chain ofif self._NAME == "flow" ... elif self._NAME == "run" ...branches. Any subclass that forgets to override_NAME(or sets it to an unrecognised value) silently skips all count validation and accepts any-length pathspec.This is a silent failure: no exception is raised, the wrong object is looked up, and the error surfaces far later with a confusing traceback.
Fix
_KNOWN_NAMESclass attribute (frozensetof the five valid names).This converts a silent, hard-to-debug failure into an immediate, actionable error.
Fixes #948 (partial — defensive correctness)