Skip to content

Commit

Permalink
replace .removesuffix with more verbose approach for python3.8 suppor…
Browse files Browse the repository at this point in the history
…t, fix typos
  • Loading branch information
patricebechard authored and rlouf committed Jul 12, 2024
1 parent fb4aa81 commit 2fd189b
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions outlines/fsm/yaml_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def to_regex(
for i, (name, value) in enumerate(properties.items()):
subregex = f"{whitespace_pattern}{re.escape(name)}:"
if value.get("type") == "object":
subregex += r"( \{{\}}|\n"
subregex += r"( \{\}|\n"
elif value.get("$ref") is not None:
# exception, we might refer to an object or something else
pass
Expand Down Expand Up @@ -319,9 +319,12 @@ def to_regex(
elif isinstance(choice, type(None)) and choice is None:
choices.append(NULL)
elif type(choice) in [int, float, str]:
choices.append(
re.escape(yaml.dump(choice).strip().removesuffix("...").strip())
)
# HACK: `.removesuffix` not available in python3.8, so we have a more verbose solution
c = yaml.dump(choice).strip()
suffix = "..."
c = c[: -len(suffix)].strip() if c.endswith(suffix) else c
c = re.escape(c)
choices.append(c)
else:
raise TypeError(f"Unsupported data type in enum: {type(choice)}")
return f"({'|'.join(choices)})"
Expand All @@ -336,7 +339,12 @@ def to_regex(
elif isinstance(const, type(None)):
return NULL
elif type(const) in [int, float, str]:
const = re.escape(yaml.dump(const).strip().removesuffix("...").strip())
# HACK: `.removesuffix` not available in python3.8, so we have a more verbose solution
c = yaml.dump(const).strip()
suffix = "..."
c = c[: -len(suffix)].strip() if c.endswith(suffix) else c
c = re.escape(c)
const = c
else:
raise TypeError(f"Unsupported data type in const: {type(const)}")
return const
Expand Down

0 comments on commit 2fd189b

Please sign in to comment.