Skip to content

Instantly share code, notes, and snippets.

@samuelcolvin
Created February 21, 2026 16:44
Show Gist options
  • Select an option

  • Save samuelcolvin/141d84fb5a304e4f104af2204cbb0f4a to your computer and use it in GitHub Desktop.

Select an option

Save samuelcolvin/141d84fb5a304e4f104af2204cbb0f4a to your computer and use it in GitHub Desktop.
# /// script
# dependencies = [
# "pydantic-monty>=0.0.7",
# ]
# ///
import timeit
import pydantic_monty
code = "f'{get_greeting(tone='friendly')} {place}'"
type_stubs = """
def get_greeting(tone: str) -> str:
...
place: str
"""
def get_greeting(tone: str) -> str:
return "Hello" if tone == "friendly" else "Greetings"
def main(type_check: bool):
m = pydantic_monty.Monty(
code,
inputs=["place"],
external_functions=["get_greeting"],
type_check=type_check,
type_check_stubs=type_stubs,
)
return m.run(
inputs={"place": "World"}, external_functions={"get_greeting": get_greeting}
)
print(f"pydantic-monty version {pydantic_monty.__version__}")
#> pydantic-monty version 0.0.7
print(main(True))
#> Hello World
timer = timeit.Timer(lambda: main(type_check=True))
number, total = timer.autorange()
print(
f"{total / number * 1_000:.2f}ms per call with type checking ({number} calls in {total:.2f}s)"
)
#> 4.82ms per call with type checking (50 calls in 0.24s)
timer = timeit.Timer(lambda: main(type_check=False))
number, total = timer.autorange()
print(
f"{total / number * 1_000_000:.2f}us per call without type checking ({number} calls in {total:.2f}s)"
)
#> 4.50us per call without type checking (50000 calls in 0.23s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment