Skip to content

Commit

Permalink
fix: merge locals() with globals() to support Python 3.13
Browse files Browse the repository at this point in the history
  • Loading branch information
CNSeniorious000 authored Oct 9, 2024
1 parent e7230bb commit 4fed982
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 2 deletions.
4 changes: 3 additions & 1 deletion python/promplate/prompt/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from functools import cached_property, partial
from pathlib import Path
from sys import path as sys_path
from sys import version_info
from textwrap import dedent
from typing import TYPE_CHECKING, Any, Literal, Protocol

Expand Down Expand Up @@ -87,7 +88,8 @@ def _on_special_token(self, token, sync: bool):
@staticmethod
def _make_context(text: str):
"""generate context parameter if specified otherwise use locals() by default"""

if version_info >= (3, 13):
return f"globals() | locals() | dict({text[text.index(' ') + 1:]})" if " " in text else "globals() | locals()"
return f"locals() | dict({text[text.index(' ') + 1:]})" if " " in text else "locals()"

def compile(self, sync=True, indent_str="\t"):
Expand Down
2 changes: 1 addition & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "promplate"
version = "0.3.4.9"
version = "0.3.4.10"
description = "Prompt engineering framework for humans"
homepage = "https://promplate.dev/"
documentation = "https://docs.py.promplate.dev/"
Expand Down
4 changes: 4 additions & 0 deletions python/tests/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,10 @@ def test_components_context():
assert Template("{% a b=1, c=2 %}").render({"a": Template("{{ b }}{{ c }}{{ d }}"), "d": 3}) == "123"


def test_components_in_for_loop():
assert Template("{% for i in '123' %}{% a %}{% endfor %}").render({"a": Template("{{ i }}")}) == "123"


def test_else_tag_in_for_loop():
render_assert("{% for i in '123' %}{{ i }}{% else %}4{% endfor %}", None, "1234")

Expand Down
1 change: 1 addition & 0 deletions python/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ async def test_accumulate():
assert await anext(it) == "a"
assert await anext(it) == "ab"
assert await anext(it) == "abc"
await it.aclose()


async def test_accumulate_empty():
Expand Down

0 comments on commit 4fed982

Please sign in to comment.