Skip to content

fix: wrap all type-hint resolution errors in CodecError#75

Open
zza-830 wants to merge 1 commit into
dexpace:mainfrom
zza-830:fix/codec-exception-wrapping
Open

fix: wrap all type-hint resolution errors in CodecError#75
zza-830 wants to merge 1 commit into
dexpace:mainfrom
zza-830:fix/codec-exception-wrapping

Conversation

@zza-830

@zza-830 zza-830 commented Jun 17, 2026

Copy link
Copy Markdown

Problem

Codec.decode documents that it raises CodecError on any structural mismatch or conversion failure, but get_type_hints can also raise AttributeError (stale qualified references like "os.ThisDoesNotExist"), TypeError (malformed expressions like "1 + 'x'"), and SyntaxError (invalid annotations like "def f("). These escaped the CodecError contract.

Closes #38

Fix

Broaden the except clause from NameError to (NameError, AttributeError, TypeError, SyntaxError):

# Before
except NameError as err:

# After
except (NameError, AttributeError, TypeError, SyntaxError) as err:

Tests

Added two new test cases:

  • test_decode_stale_qualified_ref_raises_codec_error — validates AttributeError wrapping
  • test_decode_malformed_expression_raises_codec_error — validates SyntaxError wrapping
tests/serde/test_codec.py::test_decode_unresolvable_forward_ref_raises_codec_error PASSED
tests/serde/test_codec.py::test_decode_stale_qualified_ref_raises_codec_error PASSED
tests/serde/test_codec.py::test_decode_malformed_expression_raises_codec_error PASSED

Codec.decode documented that it raises CodecError on any structural
mismatch or conversion failure, but get_type_hints can also raise
AttributeError (stale qualified references), TypeError (malformed
expressions), and SyntaxError (invalid annotations). These escaped
the CodecError contract.

Fix: broaden the except clause to catch all four exception types.

Closes dexpace#38
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.

Codec only wraps NameError when resolving type hints; AttributeError/TypeError/SyntaxError escape the CodecError contract

1 participant