Skip to content

Commit 2fd189b

Browse files
patricebechardrlouf
authored andcommitted
replace .removesuffix with more verbose approach for python3.8 support, fix typos
1 parent fb4aa81 commit 2fd189b

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

outlines/fsm/yaml_schema.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def to_regex(
220220
for i, (name, value) in enumerate(properties.items()):
221221
subregex = f"{whitespace_pattern}{re.escape(name)}:"
222222
if value.get("type") == "object":
223-
subregex += r"( \{{\}}|\n"
223+
subregex += r"( \{\}|\n"
224224
elif value.get("$ref") is not None:
225225
# exception, we might refer to an object or something else
226226
pass
@@ -319,9 +319,12 @@ def to_regex(
319319
elif isinstance(choice, type(None)) and choice is None:
320320
choices.append(NULL)
321321
elif type(choice) in [int, float, str]:
322-
choices.append(
323-
re.escape(yaml.dump(choice).strip().removesuffix("...").strip())
324-
)
322+
# HACK: `.removesuffix` not available in python3.8, so we have a more verbose solution
323+
c = yaml.dump(choice).strip()
324+
suffix = "..."
325+
c = c[: -len(suffix)].strip() if c.endswith(suffix) else c
326+
c = re.escape(c)
327+
choices.append(c)
325328
else:
326329
raise TypeError(f"Unsupported data type in enum: {type(choice)}")
327330
return f"({'|'.join(choices)})"
@@ -336,7 +339,12 @@ def to_regex(
336339
elif isinstance(const, type(None)):
337340
return NULL
338341
elif type(const) in [int, float, str]:
339-
const = re.escape(yaml.dump(const).strip().removesuffix("...").strip())
342+
# HACK: `.removesuffix` not available in python3.8, so we have a more verbose solution
343+
c = yaml.dump(const).strip()
344+
suffix = "..."
345+
c = c[: -len(suffix)].strip() if c.endswith(suffix) else c
346+
c = re.escape(c)
347+
const = c
340348
else:
341349
raise TypeError(f"Unsupported data type in const: {type(const)}")
342350
return const

0 commit comments

Comments
 (0)