Give _FracTrim a __repr__ so format errors raise ValueError - #386
Merged
ariebovenberg merged 1 commit intoJul 6, 2026
Merged
Conversation
In the pure-Python implementation, _FracTrim inherited _Field.__repr__, which reads self.pattern. _FracTrim never sets that instance attribute (pattern is a class-only annotation), so rendering such a field inside an error message - duplicate-field detection or unsupported-field rejection - raised AttributeError: '_FracTrim' object has no attribute 'pattern' instead of the intended ValueError. The Rust extension already renders these correctly. Add a __repr__ returning the field's letter repeated by its width (e.g. 'FFF'), mirroring the sibling _FracExact, so the error paths render and raise ValueError.
Owner
|
@gaoflow thanks for taking the time to post this issue and fix it. It makes me wonder if there are other patterns where this pops up 🤔 |
Owner
|
Looks like only |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
In the pure-Python implementation, a format-pattern error that references a
trimmed-fraction (
F) field crashes withAttributeErrorinstead of theintended
ValueError:_FracTriminherits_Field.__repr__, which readsself.pattern._FracTrimnever sets that instance attribute (
patternis a class-only annotation), sorendering such a field inside an error message — duplicate-field detection or
unsupported-field rejection — raises
AttributeErrorrather than the documentedValueError. The Rust extension already renders these correctly, so this is apure-Python/Rust divergence.
Fix
Give
_FracTrima__repr__returning its letter repeated by its width (e.g."FFF"), mirroring the sibling_FracExact. The error paths then render andraise
ValueErroras intended.Tests
TestFracTrimErrorRenderingcovers the__repr__output and asserts aValueError(notAttributeError) for duplicate-nanos and unsupported-Fcases across
compile_pattern,format, andparse.Pure-Python suite
tests/test_format_parse.py→ 225 passed.ruff checkclean.