feat: MetaflowTaggingError formatting and add test coverage for tagging_util
The validate_tag function was passing arguments incorrectly to MetaflowTaggingError, causing str() to fail when the exception was rendered. This also adds comprehensive test coverage for is_utf8_encodable, is_utf8_decodable, validate_tag, and validate_tags including edge cases for type validation, length limits, and tag set size constraints.
Fixes error message formatting for non-string tag types.
PR Type
- [ ] Bug fix
- [x] New feature
- [ ] Core Runtime change (higher bar -- see CONTRIBUTING.md)
- [ ] Docs / tooling
- [ ] Refactoring
Summary
Fix error message formatting bug in validate_tag and add comprehensive unit test coverage for tagging_util.py (30 tests).
Issue
Fixes #3162
Context / Motivation
The validate_tag function passed two positional arguments to MetaflowTaggingError, but the exception class expects msg, lineno, source_file as positional args. This caused TypeError: %d format: a real number is required, not str when rendering the exception for non-string tag types.
Additionally, tagging_util.py had zero test coverage despite being
critical infrastructure for run/step tagging validation.
Reproduction
Runtime:
Commands to run:
# paste exact commands
Where evidence shows up:
Before (error / log snippet)
paste here
After (evidence that fix works)
paste here
Root Cause
Changes Made
- Fixed
MetaflowTaggingErrorcall to use string formatting instead of multiple positional arguments - Added 30 tests covering:
is_utf8_encodable/is_utf8_decodablefunctionsvalidate_tagwith valid inputs and all error conditionsvalidate_tagsincluding tag set size limits and remediation logic
Why This Fix Is Correct
- All 30 new tests pass
- Tests cover edge cases: empty tags, oversized tags, non-string types, UTF-8 encoding/decoding, tag deduplication, and size limits
Failure Modes Considered
1. 2.
Tests
- [x] Unit tests added/updated
- [ ] Reproduction script provided (required for Core Runtime)
- [ ] CI passes
- [ ] If tests are impractical: explain why below and provide manual evidence above
Non-Goals
AI Tool Usage
- [ ] No AI tools were used in this contribution
- [x] AI tools were used (describe below)
For formatting
Greptile Summary
This PR fixes a bug in validate_tag where two positional arguments were passed to MetaflowTaggingError, causing TypeError when the exception was rendered for non-string tag types. It also adds 30 unit tests for tagging_util.py.
- Bug fix:
MetaflowTaggingError(msg, str(type(tag)))is replaced withMetaflowTaggingError(msg % str(type(tag))), correctly formatting the string before constructing the exception (the second positional arg was being treated aslineno, triggering%dformatting failure on__str__). - New tests: Covers
is_utf8_encodable,is_utf8_decodable,validate_tag(type checks, length limits, empty inputs), andvalidate_tags(deduplication, size limits, remediation logic); one error path — non-UTF-8-decodable bytes invalidate_tag— is not yet tested.
Confidence Score: 5/5
Safe to merge — the production change is a minimal, correct one-line formatting fix with no behavioral side effects.
The fix is a single-line change that applies % formatting before constructing the exception rather than passing a raw string as the lineno positional argument. The root cause and the correction are both clear, and the new test suite validates all the surrounding logic. The only gap is a missing test for the non-UTF-8-decodable bytes path, which does not affect correctness of the shipped code.
No files require special attention.
Important Files Changed
| Filename | Overview |
|---|---|
| metaflow/tagging_util.py | Bug fix: string formatting applied before passing to MetaflowTaggingError constructor, preventing TypeError on str() of the exception for non-string tag types. |
| test/unit/test_tagging_util.py | New test file with 30 tests covering is_utf8_encodable, is_utf8_decodable, validate_tag, and validate_tags; one error path (non-UTF-8-decodable bytes in validate_tag) is not exercised. |
Reviews (5): Last reviewed commit: "Merge branch 'master' into feat/add-tagg..." | Re-trigger Greptile
@saikonen