Skip to content

Commit e38d695

Browse files
authored
✨ Add type checking for encoder/decoder parameters (#508)
1 parent c1f6726 commit e38d695

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

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)