Skip to content

Degrade to identity on a malformed transform substring instead of raising ValueError - #247

Closed
eeshsaxena wants to merge 1 commit into
mathandy:masterfrom
eeshsaxena:fix/parse-transform-malformed
Closed

Degrade to identity on a malformed transform substring instead of raising ValueError#247
eeshsaxena wants to merge 1 commit into
mathandy:masterfrom
eeshsaxena:fix/parse-transform-malformed

Conversation

@eeshsaxena

Copy link
Copy Markdown

parse_transform already tolerates malformed input in most places: it warns and returns the identity matrix for an unknown transform type or a wrong number of arguments. But two cases still raise a bare ValueError:

  • non-numeric values, e.g. parse_transform('matrix(1 x 3 4 5 6)') -> float('x') raises;
  • a substring without exactly one (, e.g. a stray 'matrix', so the type, values = substr.split('(') unpack fails.

I guarded both so they warn and fall back to the identity matrix, matching the existing _check_num_parsed_values / unknown-type behavior. Valid transforms are unchanged.

Added a test with the malformed variants; it raises ValueError on master and passes with the change, and the rest of the parsing tests still pass.

_parse_transform_substr raised a bare ValueError when a transform substring had
non-numeric values (float('x')) or the wrong number of parentheses (the
type(...) split). parse_transform already warns and returns the identity matrix
for an unknown transform type or a wrong argument count, so handle these the
same way instead of raising.
@eeshsaxena

Copy link
Copy Markdown
Author

Hi! Gentle nudge on this one whenever you have some bandwidth. It's a small, self-contained fix (Degrade to identity on a malformed transform substring instead of raising ValueError), and it's currently mergeable with no conflicts. No urgency at all, and I'm happy to make any changes you'd like. Thanks for maintaining svgpathtools!

@mathandy

Copy link
Copy Markdown
Owner

I have one suggestion, otherwise LGTM. What do you think @eeshsaxena?

@eeshsaxena

Copy link
Copy Markdown
Author

Thanks! I think the suggestion didn't actually come through on my end though, I'm not seeing an inline comment or a review body on the PR, just the "one suggestion" note. Mind re-posting it? Happy to make the change once I can see what you had in mind.

Comment thread svgpathtools/parser.py

transform = np.identity(3)
try:
values = list(map(float, filter(None, value_str.split(' '))))

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
values = list(map(float, filter(None, value_str.split(' '))))
values = [float(s) for s in value_str.split()]

A bit easier to read (IMO) and robust against other/multiple whitespace chars.

@mathandy

mathandy commented Sep 5, 2026

Copy link
Copy Markdown
Owner

Thanks! I think the suggestion didn't actually come through on my end though, I'm not seeing an inline comment or a review body on the PR, just the "one suggestion" note. Mind re-posting it? Happy to make the change once I can see what you had in mind.

My bad, I never pushed submit 🙃

@mathandy

mathandy commented Sep 5, 2026

Copy link
Copy Markdown
Owner

I'm realizing I was a bit confused about the direction of this MR -- why do we want to just warn users in these cases instead of raising an error? Unless there's an application here my thinking is we should be moving towards more errors when invalid syntax is encountered and is not unambigiously repairable.

@mathandy

mathandy commented Sep 5, 2026

Copy link
Copy Markdown
Owner

why do we want to just warn users in these cases

I see, this is really the design here. Your change makes total sense.

mathandy added a commit that referenced this pull request Sep 5, 2026
parse_transform's error policy for invalid syntax was mixed, by
accident rather than design: unknown transform types and wrong argument
counts warned and degraded to identity, while non-numeric values and
malformed parentheses raised bare errors from float() and tuple
unpacking, and anything after the last ')' was silently discarded.

By default all invalid substrings now warn and contribute an identity
matrix, with valid substrings still applied -- the behavior proposed in
PR #247, and no change for input that already parsed. For callers who
prefer errors, parse_transform(s, strict=True) raises a ValueError
whose message identifies the offending substring.

Also split values on any whitespace (tabs, newlines) rather than only
spaces, as the SVG spec allows.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
mathandy added a commit that referenced this pull request Sep 5, 2026
parse_transform's error policy for invalid syntax was mixed, by
accident rather than design: unknown transform types and wrong argument
counts warned and degraded to identity, while non-numeric values and
malformed parentheses raised bare errors from float() and tuple
unpacking, and anything after the last ')' was silently discarded.

By default all invalid substrings now warn and contribute an identity
matrix, with valid substrings still applied -- the behavior proposed in
PR #247, and no change for input that already parsed. For callers who
prefer errors, parse_transform(s, strict=True) raises a ValueError
whose message identifies the offending substring.

Also split values on any whitespace (tabs, newlines) rather than only
spaces, as the SVG spec allows.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@mathandy

mathandy commented Sep 5, 2026

Copy link
Copy Markdown
Owner

I've merged this in with #250 . Thanks for the fixes @eeshsaxena !

@mathandy mathandy closed this Sep 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants