Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pump Version python to 3.12, 3.13. Allow pip install for python 3.12, 3.13 #210

Merged
merged 16 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ corresponding $\LaTeX$ expression:

1. *Which Python versions are supported?*

Syntaxes on **Pythons 3.7 to 3.11** are officially supported, or will be supported.
Syntaxes on **Pythons 3.9 to 3.13** are officially supported, or will be supported.

2. *Which technique is used?*

Expand Down
16 changes: 8 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ build-backend = "hatchling.build"
name = "latexify-py"
description = "Generates LaTeX math description from Python functions."
readme = "README.md"
requires-python = ">=3.7, <3.12"
requires-python = ">=3.9, <3.14"
license = {text = "Apache Software License 2.0"}
authors = [
{name = "Yusuke Oda", email = "[email protected]"}
Expand All @@ -24,11 +24,11 @@ classifiers = [
"Framework :: IPython",
"Framework :: Jupyter",
"License :: OSI Approved :: Apache Software License",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Topic :: Scientific/Engineering :: Mathematics",
"Topic :: Software Development :: Code Generators",
"Topic :: Text Processing :: Markup :: LaTeX",
Expand All @@ -43,17 +43,17 @@ dynamic = [
[project.optional-dependencies]
dev = [
"build>=0.8",
"black>=22.10",
"flake8>=5.0",
"black>=24.3",
"flake8>=6.0",
"isort>=5.10",
"mypy>=0.991",
"mypy>=1.9",
"notebook>=6.5.1",
"pyproject-flake8>=5.0",
"pyproject-flake8>=6.0",
"pytest>=7.1",
"twine>=4.0",
]
mypy = [
"mypy>=0.991",
"mypy>=1.9",
"pytest>=7.1",
]

Expand Down
14 changes: 4 additions & 10 deletions src/latexify/ast_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,10 @@ def make_constant(value: Any) -> ast.expr:
ValueError: Unsupported value type.
"""
if sys.version_info.minor < 8:
if value is None or value is False or value is True:
return ast.NameConstant(value=value)
if value is ...:
return ast.Ellipsis()
if isinstance(value, (int, float, complex)):
return ast.Num(n=value)
if isinstance(value, str):
return ast.Str(s=value)
if isinstance(value, bytes):
return ast.Bytes(s=value)
if value in {None, False, True, ...} or isinstance(
value, (int, float, complex, str, bytes)
):
return ast.Constant(value=value)
else:
maycuatroi marked this conversation as resolved.
Show resolved Hide resolved
if (
value is None
Expand Down
53 changes: 29 additions & 24 deletions src/latexify/ast_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ def test_make_attribute() -> None:
@pytest.mark.parametrize(
maycuatroi marked this conversation as resolved.
Show resolved Hide resolved
"value,expected",
[
(None, ast.NameConstant(value=None)),
(False, ast.NameConstant(value=False)),
(True, ast.NameConstant(value=True)),
(..., ast.Ellipsis()),
(123, ast.Num(n=123)),
(4.5, ast.Num(n=4.5)),
(6 + 7j, ast.Num(n=6 + 7j)),
("foo", ast.Str(s="foo")),
(b"bar", ast.Bytes(s=b"bar")),
(None, ast.Constant(value=None)),
(False, ast.Constant(value=False)),
(True, ast.Constant(value=True)),
(..., ast.Constant(value=Ellipsis)),
(123, ast.Constant(value=123)),
(4.5, ast.Constant(value=4.5)),
(6 + 7j, ast.Constant(value=6 + 7j)),
("foo", ast.Constant(value="foo")),
(b"bar", ast.Constant(value=b"bar")),
],
)
def test_make_constant_legacy(value: Any, expected: ast.Constant) -> None:
Expand Down Expand Up @@ -87,13 +87,13 @@ def test_make_constant_invalid() -> None:
@pytest.mark.parametrize(
"value,expected",
[
(ast.Bytes(s=b"foo"), True),
(ast.Constant("bar"), True),
(ast.Ellipsis(), True),
(ast.NameConstant(value=None), True),
(ast.Num(n=123), True),
(ast.Str(s="baz"), True),
(ast.Expr(value=ast.Num(456)), False),
(ast.Constant(value=b"foo"), True),
(ast.Constant(value="bar"), True),
(ast.Constant(value=...), True),
(ast.Constant(value=None), True),
(ast.Constant(value=123), True),
(ast.Constant(value="baz"), True),
(ast.Expr(value=ast.Constant(value=456)), False),
(ast.Global(names=["qux"]), False),
],
)
Expand All @@ -118,13 +118,13 @@ def test_is_constant(value: ast.AST, expected: bool) -> None:
@pytest.mark.parametrize(
"value,expected",
[
(ast.Bytes(s=b"foo"), False),
(ast.Constant("bar"), True),
(ast.Ellipsis(), False),
(ast.NameConstant(value=None), False),
(ast.Num(n=123), False),
(ast.Str(s="baz"), True),
(ast.Expr(value=ast.Num(456)), False),
(ast.Constant(value=b"foo"), False),
(ast.Constant(value="bar"), True),
(ast.Constant(value=...), False),
(ast.Constant(value=None), False),
(ast.Constant(value=123), False),
(ast.Constant(value="baz"), True),
(ast.Expr(value=ast.Constant(value=456)), False),
(ast.Global(names=["qux"]), False),
],
)
Expand Down Expand Up @@ -194,6 +194,7 @@ def test_extract_int_invalid() -> None:
ast.Call(
func=ast.Name(id="hypot", ctx=ast.Load()),
args=[],
keywords=[],
),
"hypot",
),
Expand All @@ -205,13 +206,17 @@ def test_extract_int_invalid() -> None:
ctx=ast.Load(),
),
args=[],
keywords=[],
),
"hypot",
),
(
ast.Call(
func=ast.Call(func=ast.Name(id="foo", ctx=ast.Load()), args=[]),
func=ast.Call(
func=ast.Name(id="foo", ctx=ast.Load()), args=[], keywords=[]
),
args=[],
keywords=[],
),
None,
),
Expand Down
42 changes: 35 additions & 7 deletions src/latexify/codegen/expression_rules_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,41 @@
@pytest.mark.parametrize(
"node,precedence",
[
(ast.Call(), expression_rules._CALL_PRECEDENCE),
(ast.BinOp(op=ast.Add()), expression_rules._PRECEDENCES[ast.Add]),
(ast.UnaryOp(op=ast.UAdd()), expression_rules._PRECEDENCES[ast.UAdd]),
(ast.BoolOp(op=ast.And()), expression_rules._PRECEDENCES[ast.And]),
(ast.Compare(ops=[ast.Eq()]), expression_rules._PRECEDENCES[ast.Eq]),
(ast.Name(), expression_rules._INF_PRECEDENCE),
(ast.Attribute(), expression_rules._INF_PRECEDENCE),
(
ast.Call(func=ast.Name(id="func", ctx=ast.Load()), args=[], keywords=[]),
expression_rules._CALL_PRECEDENCE,
),
(
ast.BinOp(
left=ast.Name(id="left", ctx=ast.Load()),
op=ast.Add(),
right=ast.Name(id="right", ctx=ast.Load()),
),
expression_rules._PRECEDENCES[ast.Add],
),
(
ast.UnaryOp(op=ast.UAdd(), operand=ast.Name(id="operand", ctx=ast.Load())),
expression_rules._PRECEDENCES[ast.UAdd],
),
(
ast.BoolOp(op=ast.And(), values=[ast.Name(id="value", ctx=ast.Load())]),
expression_rules._PRECEDENCES[ast.And],
),
(
ast.Compare(
left=ast.Name(id="left", ctx=ast.Load()),
ops=[ast.Eq()],
comparators=[ast.Name(id="right", ctx=ast.Load())],
),
expression_rules._PRECEDENCES[ast.Eq],
),
(ast.Name(id="name", ctx=ast.Load()), expression_rules._INF_PRECEDENCE),
(
ast.Attribute(
value=ast.Name(id="value", ctx=ast.Load()), attr="attr", ctx=ast.Load()
),
expression_rules._INF_PRECEDENCE,
),
],
)
def test_get_precedence(node: ast.AST, precedence: int) -> None:
Expand Down
15 changes: 15 additions & 0 deletions src/latexify/parser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,26 @@ def f(x):
ast.FunctionDef(
name="f",
args=ast.arguments(
posonlyargs=[],
args=[ast.arg(arg="x")],
vararg=None,
kwonlyargs=[],
kw_defaults=[],
kwarg=None,
defaults=[],
),
body=[ast.Return(value=ast.Name(id="x", ctx=ast.Load()))],
decorator_list=[],
returns=None,
type_comment=None,
type_params=[],
lineno=1,
col_offset=0,
end_lineno=2,
end_col_offset=0,
)
],
type_ignores=[],
)

obtained = parser.parse_function(f)
Expand Down
30 changes: 11 additions & 19 deletions src/latexify/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,15 @@ def ast_equal(observed: ast.AST, expected: ast.AST) -> bool:
Returns:
True if observed and expected represent the same AST, False otherwise.
"""
ignore_keys = {"lineno", "col_offset", "end_lineno", "end_col_offset", "kind"}
if sys.version_info.minor <= 12:
ignore_keys.add("type_params")

try:
assert type(observed) is type(expected)

for k, ve in vars(expected).items():
if k in {"col_offset", "end_col_offset", "end_lineno", "kind", "lineno"}:
if k in ignore_keys:
continue

vo = getattr(observed, k) # May cause AttributeError.
Expand All @@ -93,8 +97,8 @@ def ast_equal(observed: ast.AST, expected: ast.AST) -> bool:
assert type(vo) is type(ve)
assert vo == ve

except (AssertionError, AttributeError):
return False
except (AssertionError, AttributeError) as e:
raise e # raise to debug easier.
maycuatroi marked this conversation as resolved.
Show resolved Hide resolved

return True

Expand All @@ -109,19 +113,7 @@ def assert_ast_equal(observed: ast.AST, expected: ast.AST) -> None:
Raises:
AssertionError: observed and expected represent different ASTs.
"""
if sys.version_info.minor >= 9:
assert ast_equal(
observed, expected
), f"""\
AST does not match.
observed={ast.dump(observed, indent=4)}
expected={ast.dump(expected, indent=4)}
"""
else:
assert ast_equal(
observed, expected
), f"""\
AST does not match.
observed={ast.dump(observed)}
expected={ast.dump(expected)}
"""
indent = 4 if sys.version_info.minor >= 9 else None
assert ast_equal(
observed, expected
), f"AST does not match. observed = {observed}\nexpected = {expected}"
maycuatroi marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 3 additions & 1 deletion src/latexify/transformers/assignment_reducer.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,14 @@ def visit_FunctionDef(self, node: ast.FunctionDef) -> Any:

# Pop stack
self._assignments = parent_assignments

type_params = getattr(node, "type_params", [])
return ast.FunctionDef(
name=node.name,
args=node.args,
body=[return_transformed],
decorator_list=node.decorator_list,
returns=node.returns,
type_params=type_params,
)

def visit_Name(self, node: ast.Name) -> Any:
Expand Down
3 changes: 3 additions & 0 deletions src/latexify/transformers/assignment_reducer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ def _make_ast(body: list[ast.stmt]) -> ast.Module:
kwonlyargs=[],
kw_defaults=[],
defaults=[],
posonlyargs=[],
),
body=body,
decorator_list=[],
type_params=[],
)
],
type_ignores=[],
)


Expand Down
17 changes: 16 additions & 1 deletion src/latexify/transformers/docstring_remover_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,24 @@ def f():
targets=[ast.Name(id="x", ctx=ast.Store())],
value=ast_utils.make_constant(42),
),
ast.Expr(value=ast.Call(func=ast.Name(id="f", ctx=ast.Load()))),
ast.Expr(
value=ast.Call(
func=ast.Name(id="f", ctx=ast.Load()), args=[], keywords=[]
)
),
ast.Return(value=ast.Name(id="x", ctx=ast.Load())),
],
args=ast.arguments(
posonlyargs=[],
args=[],
vararg=None,
kwonlyargs=[],
kw_defaults=[],
kwarg=None,
defaults=[],
),
decorator_list=[],
type_params=[],
)
transformed = DocstringRemover().visit(tree)
test_utils.assert_ast_equal(transformed, expected)
4 changes: 4 additions & 0 deletions src/latexify/transformers/function_expander.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def _atan2_expander(function_expander: FunctionExpander, node: ast.Call) -> ast.
right=function_expander.visit(node.args[1]),
)
],
keywords=[],
)


Expand Down Expand Up @@ -88,6 +89,7 @@ def _expm1_expander(function_expander: FunctionExpander, node: ast.Call) -> ast.
ast.Call(
func=ast.Name(id="exp", ctx=ast.Load()),
args=[node.args[0]],
keywords=[],
)
),
op=ast.Sub(),
Expand All @@ -114,6 +116,7 @@ def _hypot_expander(function_expander: FunctionExpander, node: ast.Call) -> ast.
return ast.Call(
func=ast.Name(id="sqrt", ctx=ast.Load()),
args=[args_reduced],
keywords=[],
)


Expand All @@ -128,6 +131,7 @@ def _log1p_expander(function_expander: FunctionExpander, node: ast.Call) -> ast.
right=function_expander.visit(node.args[0]),
)
],
keywords=[],
)


Expand Down
Loading