Skip to content

Commit

Permalink
Fix tests after Python 3.12 changes for comprehensions
Browse files Browse the repository at this point in the history
  • Loading branch information
nsoranzo committed Jan 21, 2024
1 parent 6860c9a commit 9978004
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions test/unit/util/test_fill_template.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import sys

import pytest
from Cheetah.NameMapper import NotFound

from galaxy.util.template import fill_template

# In Python 3.12 calling `locals()`` inside a comprehension now includes
# variables from outside the comprehension, see
# https://docs.python.org/dev/whatsnew/3.12.html#pep-709-comprehension-inlining
PY312_OR_LATER = sys.version_info[:2] >= (3, 12)

SIMPLE_TEMPLATE = """#for item in $a_list:
echo $item
#end for
Expand Down Expand Up @@ -48,8 +55,12 @@ def test_fill_simple_template():


def test_fill_list_comprehension_template():
with pytest.raises(NotFound):
fill_template(LIST_COMPREHENSION_TEMPLATE, retry=0)
if PY312_OR_LATER:
template_str = fill_template(LIST_COMPREHENSION_TEMPLATE, retry=0)
assert template_str == "echo 1\n"
else:
with pytest.raises(NotFound):
fill_template(LIST_COMPREHENSION_TEMPLATE, retry=0)


def test_fill_list_comprehension_template_2():
Expand All @@ -58,13 +69,21 @@ def test_fill_list_comprehension_template_2():


def test_fill_dict_comprehension():
with pytest.raises(NotFound):
fill_template(DICT_COMPREHENSION_TEMPLATE, retry=1)
if PY312_OR_LATER:
template_str = fill_template(DICT_COMPREHENSION_TEMPLATE, retry=1)
assert template_str == "echo 1\n"
else:
with pytest.raises(NotFound):
fill_template(DICT_COMPREHENSION_TEMPLATE, retry=1)


def test_set_comprehension():
with pytest.raises(NotFound):
fill_template(SET_COMPR_TEMPLATE, retry=1)
if PY312_OR_LATER:
template_str = fill_template(SET_COMPR_TEMPLATE, retry=1)
assert template_str == "echo 1\n"
else:
with pytest.raises(NotFound):
fill_template(SET_COMPR_TEMPLATE, retry=1)


def test_gen_expr():
Expand Down

0 comments on commit 9978004

Please sign in to comment.