Skip to content

Commit 8d94e4e

Browse files
MiWeissclaude
andcommitted
Add type checking for encoder/decoder parameters in latex_encoding.py
Add explicit type validation for the `encoder` and `decoder` parameters in LatexEncodingMiddleware and LatexDecodingMiddleware respectively. This provides a clear error message at initialization time rather than a confusing AttributeError later during usage. Also fixed missing spaces in error message strings. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 59a3c34 commit 8d94e4e

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

bibtexparser/middlewares/latex_encoding.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,15 @@ def __init__(
102102

103103
if encoder is not None and (keep_math is not None or enclose_urls is not None):
104104
raise ValueError(
105-
"Cannot specify both encoder and keep_math or enclose_urls."
105+
"Cannot specify both encoder and keep_math or enclose_urls. "
106106
"If you want to use a custom encoder, you have to specify it completely."
107107
)
108108

109+
if encoder is not None and not isinstance(encoder, UnicodeToLatexEncoder):
110+
raise TypeError(
111+
f"encoder must be a UnicodeToLatexEncoder instance, got {type(encoder).__name__}"
112+
)
113+
109114
# Defaults (not specified as defaults in args,
110115
# to make sure we can identify if they were specified)
111116
keep_math = keep_math if keep_math is not None else True
@@ -167,11 +172,16 @@ def __init__(
167172
if decoder is not None and (keep_braced_groups is not None or keep_math_mode is not None):
168173
raise ValueError(
169174
"Cannot specify both decoder and one of "
170-
"`keep_braced_groups` or `keep_math_mode`."
175+
"`keep_braced_groups` or `keep_math_mode`. "
171176
"If you want to use a custom decoder, "
172177
"you have to specify it completely."
173178
)
174179

180+
if decoder is not None and not isinstance(decoder, LatexNodes2Text):
181+
raise TypeError(
182+
f"decoder must be a LatexNodes2Text instance, got {type(decoder).__name__}"
183+
)
184+
175185
# Defaults (not specified as defaults in args,
176186
# to make sure we can identify if they were specified)
177187
keep_braced_groups = keep_braced_groups if keep_braced_groups is not None else False

0 commit comments

Comments
 (0)